annotate tools/regVariation/delete_overlapping_indels.pl @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/bin/perl -w
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # This program detects overlapping indels in a chromosome and keeps all non-overlapping indels. As for overlapping indels,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 # the first encountered one is kept and all others are removed. It requires three inputs:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 # The first input is a TABULAR format file containing coordinates of indels in blocks extracted from multi-alignment.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 # The second input is an integer number representing the number of the column where indel start coordinates are stored in the input file.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 # The third input is an integer number representing the number of the column where indel end coordinates are stored in the input file.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 # The output is a TABULAR format file containing all non-overlapping indels in the input file, and the first encountered indel of overlapping ones.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 # Note: The number of the first column is 1.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 use strict;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 use warnings;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 #varaibles to handle information related to indels
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 my $indel1 = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 my $indel2 = "";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 my @indelArray1 = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 my @indelArray2 = ();
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19 my $lineCounter1 = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 my $lineCounter2 = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 my $totalNumberofNonOverlappingIndels = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 # check to make sure having correct files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 my $usage = "usage: delete_overlapping_indels.pl [TABULAR.in] [indelStartColumn] [indelEndColumn] [TABULAR.out]\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 die $usage unless @ARGV == 4;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 my $inputFile = $ARGV[0];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 my $indelStartColumn = $ARGV[1] - 1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 my $indelEndColumn = $ARGV[2] - 1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30 my $outputFile = $ARGV[3];
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 #verifie column numbers
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 if ($indelStartColumn < 0 ){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 die ("The indel start column number is invalid \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 if ($indelEndColumn < 0 ){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 die ("The indel end column number is invalid \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 #open the input and output files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 open (INPUT, "<", $inputFile) || die ("Could not open file $inputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 open (OUTPUT, ">", $outputFile) || die ("Could not open file $outputFile \n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 #store the input file in the array @rawData
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 my @indelsRawData = <INPUT>;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 #iterated through the indels of the input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 INDEL1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 foreach $indel1 (@indelsRawData){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 chomp ($indel1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 $lineCounter1++;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 #get the first indel
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 @indelArray1 = split(/\t/, $indel1);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 #our purpose is to detect overlapping indels and to store one copy of them only in the output file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 #all other non-overlapping indels will stored in the output file also
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 $lineCounter2 = 0;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 #iterated through the indels of the input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 INDEL2:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 foreach $indel2 (@indelsRawData){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 chomp ($indel2);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 $lineCounter2++;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 if ($lineCounter2 > $lineCounter1){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 #get the second indel
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 @indelArray2 = split(/\t/, $indel2);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 #check if the two indels are overlapping
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 if (($indelArray2[$indelEndColumn] >= $indelArray1[$indelStartColumn] && $indelArray2[$indelEndColumn] <= $indelArray1[$indelEndColumn]) || ($indelArray2[$indelStartColumn] >= $indelArray1[$indelStartColumn] && $indelArray2[$indelStartColumn] <= $indelArray1[$indelEndColumn])){
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 #print ("There is an overlap between" . "\n" . $indel1 . "\n" . $indel2 . "\n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 #print("The two overlapping indels are located at the lines: " . $lineCounter1 . " " . $lineCounter2 . "\n\n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 #break out of the loop and go back to the outerloop
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 next INDEL1;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 else{
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 #print("The two non-overlaapping indels are located at the lines: " . $lineCounter1 . " " . $lineCounter2 . "\n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 print OUTPUT $indel1 . "\n";
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 $totalNumberofNonOverlappingIndels++;
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 }
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 #print("The total number of indels is: " . $lineCounter1 . "\n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 #print("The total number of non-overlapping indels is: " . $totalNumberofNonOverlappingIndels . "\n");
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 #close the input and output files
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 close(OUTPUT);
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 close(INPUT);