« blib/lib/Software/GenoScan/Regression.pm
↑ Summary
blib/lib/Software/GenoScan/OutputProcessor.pm »
| 1 | package Software::GenoScan::SeqAnnotator; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | require Exporter; | ||
| 7 | |||
| 8 | our $VERSION = "v1.0.4"; | ||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | our %EXPORT_TAGS = ("all" => [ qw( | ||
| 11 | readAnnotation | ||
| 12 | ) ] ); | ||
| 13 | our @EXPORT_OK = (@{ $EXPORT_TAGS{"all"} }); | ||
| 14 | |||
| 15 | #Description: Wrapper function for processing exclusion and inclusion annotation | ||
| 16 | #Parameters: (1) directory containing GBS annotation files, (2) file containing inclusion filters, (3) output directory, (4) verbose flag, (5) log array | ||
| 17 | #Return value: None | ||
| 18 | sub readAnnotation($ $ $ $ $){ | ||
| 19 | my ($exclusionDir, $inclusionFile, $OUTPUT_DIR, $VERBOSE, $logArray) = @_; | ||
| 20 | if(! -e $OUTPUT_DIR){ | ||
| 21 | system("mkdir $OUTPUT_DIR"); | ||
| 22 | } | ||
| 23 | if(! -e "$OUTPUT_DIR/step_1"){ | ||
| 24 | system("mkdir $OUTPUT_DIR/step_1"); | ||
| 25 | } | ||
| 26 | push(@{$logArray}, "Step 1: read annotation filters\n"); | ||
| 27 | my $annotationDir = "$OUTPUT_DIR/step_1"; | ||
| 28 | my $exc = 0; | ||
| 29 | my $inc = 0; | ||
| 30 | if($exclusionDir){ | ||
| 31 | $exc = readGBS($exclusionDir, $annotationDir, $VERBOSE); | ||
| 32 | push(@{$logArray}, "\tExclusion filters used\n"); | ||
| 33 | } | ||
| 34 | if($inclusionFile){ | ||
| 35 | $inc = readIncFile($inclusionFile, $annotationDir, $VERBOSE); | ||
| 36 | push(@{$logArray}, "\tInclusions filters used\n"); | ||
| 37 | } | ||
| 38 | if($VERBOSE && !$exc && !$inc){ | ||
| 39 | print(" No filters supplied\n"); | ||
| 40 | } | ||
| 41 | if(!$exc && !$inc){ | ||
| 42 | push(@{$logArray}, "\tNo filters supplied\n"); | ||
| 43 | } | ||
| 44 | push(@{$logArray}, "\n"); | ||
| 45 | } | ||
| 46 | |||
| 47 | #Description: Reads GBS files in directory and extracts exclusion filters | ||
| 48 | #Parameters: (1) directory containing GBS annotation files, (2) annotation output folder, (3) verbose flag | ||
| 49 | #Return value: 1 if annotation is extracted, 0 otherwise | ||
| 50 | sub readGBS($ $ $){ | ||
| 51 | my ($exclusionDir, $annotationDir, $VERBOSE) = @_; | ||
| 52 | |||
| 53 | #Detect GBS files | ||
| 54 | if(! -e $exclusionDir){ | ||
| 55 | die "GenoScan error: Unable to find GBS annotation directory '$exclusionDir'\n"; | ||
| 56 | } | ||
| 57 | my @files = split(/\s+/, qx(ls $exclusionDir)); | ||
| 58 | my @gbs = grep(/gbs$/i, @files); | ||
| 59 | my $numFiles = scalar(@gbs); | ||
| 60 | if($VERBOSE){ | ||
| 61 | print(" $numFiles GBS file(s) found\n"); | ||
| 62 | } | ||
| 63 | my $fileCounter = 0; | ||
| 64 | |||
| 65 | #Process GBS files | ||
| 66 | my @annotArray; | ||
| 67 | foreach my $gbs (@gbs){ | ||
| 68 | my @gbsArray; | ||
| 69 | $fileCounter++; | ||
| 70 | if($VERBOSE){ | ||
| 71 | print(" Extracting exclusion filters ($fileCounter/$numFiles)\r"); | ||
| 72 | } | ||
| 73 | open(GBS, "$exclusionDir/$gbs") or die "GenoScan error: Unable to read GBS file\n"; | ||
| 74 | $gbs =~ m/(chr[0-9XY]+)/; | ||
| 75 | my $chr = ""; | ||
| 76 | $chr = $1; | ||
| 77 | my $annotation = join("", <GBS>); | ||
| 78 | close(GBS); | ||
| 79 | |||
| 80 | #Extract annotation | ||
| 81 | while($annotation =~ m/CDS([^\/]+)/g){ | ||
| 82 | my $cds = $1; | ||
| 83 | $cds =~ s/\s+|<|>//g; | ||
| 84 | while($cds =~ m/([0-9]+)\.\.([0-9]+)/g){ | ||
| 85 | push(@gbsArray, "$chr:$1-$2"); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | while($annotation =~ m/ncRNA([^\/]+)/g){ | ||
| 89 | my $region = $1; | ||
| 90 | while($region =~ m/([0-9]+)\.\.([0-9]+)/g){ | ||
| 91 | push(@gbsArray, "$chr:$1-$2"); | ||
| 92 | } | ||
| 93 | } | ||
| 94 | while($annotation =~ m/(rRNA|tRNA)([^\/]+)/g){ | ||
| 95 | my $region = $2; | ||
| 96 | while($region =~ m/([0-9]+)\.\.([0-9]+)/g){ | ||
| 97 | push(@gbsArray, "$chr:$1-$2"); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | #Merge overlapping annotations | ||
| 102 | #Strategy: extend outer to completely cover inner and eliminate inner | ||
| 103 | my %indToRemove; | ||
| 104 | for(my $outer = 0; $outer < scalar(@gbsArray)-1; $outer++){ | ||
| 105 | for(my $inner = $outer+1; $inner < scalar(@gbsArray); $inner++){ | ||
| 106 | my ($outerChr, $outerStart, $outerEnd) = split(/:|-/, $gbsArray[$outer]); | ||
| 107 | my ($innerChr, $innerStart, $innerEnd) = split(/:|-/, $gbsArray[$inner]); | ||
| 108 | |||
| 109 | #outer start inside inner | ||
| 110 | if($innerStart <= $outerStart && $outerStart <= $innerEnd){ | ||
| 111 | $outerStart = $innerStart; | ||
| 112 | |||
| 113 | #outer end also inside inner | ||
| 114 | if($outerEnd < $innerEnd){ | ||
| 115 | $outerEnd = $innerEnd; | ||
| 116 | } | ||
| 117 | $indToRemove{$inner} = 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | #outer end inside inner | ||
| 121 | elsif($innerStart <= $outerEnd && $outerEnd <= $innerEnd){ | ||
| 122 | $outerEnd = $innerEnd; | ||
| 123 | $indToRemove{$inner} = 1; | ||
| 124 | } | ||
| 125 | |||
| 126 | #outer contains inner | ||
| 127 | elsif($outerStart <= $innerStart && $innerEnd <= $outerEnd){ | ||
| 128 | $indToRemove{$inner} = 1; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | #Remove inners | ||
| 134 | for(my $index = scalar(@gbsArray)-1; $index >= 0; $index--){ | ||
| 135 | if($indToRemove{$index}){ | ||
| 136 | splice(@gbsArray, $index, 1); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | push(@annotArray, @gbsArray); | ||
| 141 | } | ||
| 142 | if($VERBOSE){ | ||
| 143 | print("\n"); | ||
| 144 | } | ||
| 145 | |||
| 146 | #Write annotation array to annotation folder | ||
| 147 | if(@gbs){ | ||
| 148 | open(EXC, ">$annotationDir/exclusion_filters") or die "Unable write exclusion_filters file\n"; | ||
| 149 | print(EXC join("\n", @annotArray)); | ||
| 150 | return 1; | ||
| 151 | } | ||
| 152 | return 0; | ||
| 153 | } | ||
| 154 | |||
| 155 | #Description: Reads file and extracts inclusion filters | ||
| 156 | #Parameters: (1) file containing inclusion filters, (2) annotation output folder, (3) verbose flag | ||
| 157 | #Return value: 1 if inclusion file is read, 0 otherwise | ||
| 158 | sub readIncFile($ $ $){ | ||
| 159 | my ($inclusionFile, $annotationDir, $VERBOSE) = @_; | ||
| 160 | |||
| 161 | #Check inclusion file format | ||
| 162 | if($VERBOSE){ | ||
| 163 | print(" Reading inclusion filters\n"); | ||
| 164 | } | ||
| 165 | open(INC, $inclusionFile) or die "GenoScan error: Unable to read inclusion file\n"; | ||
| 166 | my @inclusion = <INC>; | ||
| 167 | close(INC); | ||
| 168 | my $lineCount = 0; | ||
| 169 | foreach my $line (@inclusion){ | ||
| 170 | $lineCount++; | ||
| 171 | if($line =~ m/^\s*$/){ | ||
| 172 | next; | ||
| 173 | } | ||
| 174 | if($line !~ m/chr[0-9XY]+\s*:\s*[0-9]+\s*-\s*[0-9]+/){ | ||
| 175 | die "GenoScan error: Invalid format in inclusion file line $lineCount\n"; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | #Copy inclusion file to annotation folder | ||
| 180 | if(@inclusion){ | ||
| 181 | system("cp $inclusionFile $annotationDir/inclusion_filters"); | ||
| 182 | return 1; | ||
| 183 | } | ||
| 184 | return 0; | ||
| 185 | } | ||
| 186 | |||
| 187 | return 1; | ||
| 188 |