« blib/lib/Software/GenoScan/HairpinExtractor.pm
↑ Summary
blib/lib/Software/GenoScan/HSS_Annotator.pm »
| 1 | package Software::GenoScan::InputProcessor; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use Software::GenoScan::Segmentor qw(segmentize); | ||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our $VERSION = "v1.0.4"; | ||
| 10 | our @ISA = qw(Exporter); | ||
| 11 | our %EXPORT_TAGS = ("all" => [ qw( | ||
| 12 | processInput | ||
| 13 | ) ] ); | ||
| 14 | our @EXPORT_OK = (@{ $EXPORT_TAGS{"all"} }); | ||
| 15 | |||
| 16 | #RNAfold command | ||
| 17 | my $RNAfold; | ||
| 18 | |||
| 19 | #Description: Wrapper function for processing input sequences | ||
| 20 | #Parameters: (1) input directory, (2) output directory, (3) verbose flag, (4) log array | ||
| 21 | #Return value: None | ||
| 22 | sub processInput($ $ $ $){ | ||
| 23 | my ($INPUT_DIR, $OUTPUT_DIR, $VERBOSE, $logArray) = @_; | ||
| 24 | if(! -e $OUTPUT_DIR){ | ||
| 25 | system("mkdir $OUTPUT_DIR"); | ||
| 26 | } | ||
| 27 | if(! -e "$OUTPUT_DIR/step_2"){ | ||
| 28 | system("mkdir $OUTPUT_DIR/step_2"); | ||
| 29 | } | ||
| 30 | if(! -e "$OUTPUT_DIR/step_2/chunks"){ | ||
| 31 | system("mkdir $OUTPUT_DIR/step_2/chunks"); | ||
| 32 | } | ||
| 33 | if(! -e "$OUTPUT_DIR/step_2/folded"){ | ||
| 34 | system("mkdir $OUTPUT_DIR/step_2/folded"); | ||
| 35 | } | ||
| 36 | if(! -e "$OUTPUT_DIR/step_2/substring"){ | ||
| 37 | system("mkdir $OUTPUT_DIR/step_2/substring"); | ||
| 38 | } | ||
| 39 | push(@{$logArray}, "Step 2: segmentize and fold input sequences\n"); | ||
| 40 | |||
| 41 | #Detect input sequence files | ||
| 42 | if(! -e $INPUT_DIR){ | ||
| 43 | die "GenoScan error: Unable to find input sequence directory '$INPUT_DIR'\n"; | ||
| 44 | } | ||
| 45 | $INPUT_DIR =~ s/\/+$//; | ||
| 46 | my @files = split(/\n+/, qx(ls $INPUT_DIR)); | ||
| 47 | my @seqFiles = grep(/[^~]$/, @files); | ||
| 48 | my $numSeqFiles = scalar(@seqFiles); | ||
| 49 | if($VERBOSE){ | ||
| 50 | print(" $numSeqFiles input sequence file(s) detected\n"); | ||
| 51 | } | ||
| 52 | if($numSeqFiles == 0){ | ||
| 53 | print("GenoScan: no input files detected, terminaing\n"); | ||
| 54 | exit; | ||
| 55 | } | ||
| 56 | push(@{$logArray}, "\t$numSeqFiles input sequence file(s) detected\n"); | ||
| 57 | |||
| 58 | #Read annotation | ||
| 59 | my %excFilters; | ||
| 60 | my %excStartPos; | ||
| 61 | my %incFilters; | ||
| 62 | my %incStartPos; | ||
| 63 | if(-e "$OUTPUT_DIR/step_1/exclusion_filters"){ | ||
| 64 | if($VERBOSE){ | ||
| 65 | print(" Exclusion filters found\n"); | ||
| 66 | } | ||
| 67 | open(EXC, "$OUTPUT_DIR/step_1/exclusion_filters") or die "GenoScan error: Unable to read exclusion_filters file\n"; | ||
| 68 | my @rows = <EXC>; | ||
| 69 | close(EXC); | ||
| 70 | foreach my $row (@rows){ | ||
| 71 | if($row =~ m/^\s*$/){ | ||
| 72 | next; | ||
| 73 | } | ||
| 74 | $row =~ s/\s+//g; | ||
| 75 | my ($chr, $start, $stop) = split(/:|-/, $row); | ||
| 76 | if(!$excFilters{$chr}){ | ||
| 77 | $excFilters{$chr} = [[$start, $stop]]; | ||
| 78 | $excStartPos{$chr} = [$start]; | ||
| 79 | } | ||
| 80 | else{ | ||
| 81 | push(@{$excFilters{$chr}}, [$start, $stop]); | ||
| 82 | push(@{$excStartPos{$chr}}, $start); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } | ||
| 86 | if(-e "$OUTPUT_DIR/step_1/inclusion_filters"){ | ||
| 87 | if($VERBOSE){ | ||
| 88 | print(" Inclusion filters found\n"); | ||
| 89 | } | ||
| 90 | open(EXC, "$OUTPUT_DIR/step_1/inclusion_filters") or die "GenoScan error: Unable to read inclusion_filters file\n"; | ||
| 91 | my @rows = <EXC>; | ||
| 92 | close(EXC); | ||
| 93 | foreach my $row (@rows){ | ||
| 94 | if($row =~ m/^\s*$/){ | ||
| 95 | next; | ||
| 96 | } | ||
| 97 | $row =~ s/\s+//g; | ||
| 98 | my ($chr, $start, $stop) = split(/:|-/, $row); | ||
| 99 | if(!$incFilters{$chr}){ | ||
| 100 | $incFilters{$chr} = [[$start, $stop]]; | ||
| 101 | $incStartPos{$chr} = [$start]; | ||
| 102 | } | ||
| 103 | else{ | ||
| 104 | push(@{$incFilters{$chr}}, [$start, $stop]); | ||
| 105 | push(@{$incStartPos{$chr}}, $start); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | #Ensure that, for each chromosome, exc/inc filters are ordered w.r.t. nucleotide position | ||
| 111 | foreach my $chr (keys(%excFilters)){ | ||
| 112 | my @startPos = @{$excStartPos{$chr}}; | ||
| 113 | my @excIndices = sort {$startPos[$a] <=> $startPos[$b]} (0 .. $#startPos); | ||
| 114 | @{$excFilters{$chr}} = @{$excFilters{$chr}}[@excIndices]; | ||
| 115 | } | ||
| 116 | foreach my $chr (keys(%incFilters)){ | ||
| 117 | my @startPos = @{$incStartPos{$chr}}; | ||
| 118 | my @incIndices = sort {$startPos[$a] <=> $startPos[$b]} (0 .. $#startPos); | ||
| 119 | @{$incFilters{$chr}} = @{$incFilters{$chr}}[@incIndices]; | ||
| 120 | } | ||
| 121 | |||
| 122 | #Extract input sequence substring (1) not excluded and (2) included | ||
| 123 | my @manifestFiles; | ||
| 124 | my @filterFlags; | ||
| 125 | foreach my $file (@seqFiles){ | ||
| 126 | |||
| 127 | #Read chromosome sequence | ||
| 128 | open(CHR, "$INPUT_DIR/$file") or die "GenoScan error: Unable to read input sequence file '$INPUT_DIR/$file'\n"; | ||
| 129 | my $seq = join("", <CHR>); | ||
| 130 | $seq =~ s/\r|\r\n|\n\r/\n/g; | ||
| 131 | close(CHR); | ||
| 132 | |||
| 133 | #Extract and remove header | ||
| 134 | $seq =~ m/^([^\n]+)/; | ||
| 135 | my $header = $1; | ||
| 136 | my $headerPattern = $header; | ||
| 137 | $headerPattern =~ s/\|/\\|/g; | ||
| 138 | $seq =~ s/^$headerPattern\n//; | ||
| 139 | |||
| 140 | #Remove newlines and determine seq length | ||
| 141 | $seq =~ s/\s+//g; | ||
| 142 | my $seqLen = length($seq); | ||
| 143 | |||
| 144 | #Determine chromosome name | ||
| 145 | my $chr = "chr"; | ||
| 146 | if($header =~ m/chromosome ([0-9XYxy]+)/){ | ||
| 147 | $chr = "chr$1"; | ||
| 148 | } | ||
| 149 | elsif($header =~ m/^>([0-9XYxy]+)/){ | ||
| 150 | $chr = "chr$1"; | ||
| 151 | } | ||
| 152 | |||
| 153 | #If no filters | ||
| 154 | if(!$excFilters{$chr} && !$incFilters{$chr}){ | ||
| 155 | push(@manifestFiles, "$INPUT_DIR/$file\n$file"); | ||
| 156 | push(@filterFlags, 0); | ||
| 157 | next; | ||
| 158 | } | ||
| 159 | push(@filterFlags, 1); | ||
| 160 | |||
| 161 | #Write substrings file | ||
| 162 | open(SUB, ">$OUTPUT_DIR/step_2/substring/$file") or die "GenoScan error: Unable to write '$OUTPUT_DIR/step_2/substring/$file'\n"; | ||
| 163 | push(@manifestFiles, "$OUTPUT_DIR/step_2/substring/$file\n$file"); | ||
| 164 | |||
| 165 | #If exlusion filters, substring outside them | ||
| 166 | my @substrings; | ||
| 167 | if($excFilters{$chr}){ | ||
| 168 | my @excRegions = @{$excFilters{$chr}}; | ||
| 169 | my $startNuc = 0; #start position for substr | ||
| 170 | my $endNuc; #end position for substr | ||
| 171 | my $subHeader; #substring header | ||
| 172 | my $substr; #substring of chromosome sequence | ||
| 173 | my $subStart; #start position for header | ||
| 174 | my $subEnd; #end position for header | ||
| 175 | foreach my $region (@excRegions){ | ||
| 176 | my ($regStart, $regEnd) = @{$region}; | ||
| 177 | $endNuc = $regStart - 2; | ||
| 178 | $subStart = $startNuc + 1; | ||
| 179 | $subEnd = $endNuc + 1; | ||
| 180 | $subHeader = $header; | ||
| 181 | $substr = substr($seq, $startNuc, $endNuc - $startNuc + 1); | ||
| 182 | if(length($substr) < 200){ | ||
| 183 | $startNuc = $regEnd; | ||
| 184 | next; | ||
| 185 | } | ||
| 186 | push(@substrings, "$subHeader\n$subStart\n$subEnd\n$substr"); | ||
| 187 | $startNuc = $regEnd; | ||
| 188 | } | ||
| 189 | |||
| 190 | #After last region to end of chromosome | ||
| 191 | $subStart = $startNuc + 1; | ||
| 192 | $subEnd = $seqLen; | ||
| 193 | $subHeader = $header; | ||
| 194 | $substr = substr($seq, $startNuc, $subEnd - $startNuc); | ||
| 195 | if(length($substr) >= 200){ | ||
| 196 | push(@substrings, "$subHeader\n$subStart\n$subEnd\n$substr"); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | |||
| 200 | #If only exlusion filters, write substrings to file | ||
| 201 | if($excFilters{$chr} && !$incFilters{$chr}){ | ||
| 202 | print(SUB join("\n", @substrings)); | ||
| 203 | } | ||
| 204 | |||
| 205 | #If exlusion and inclusion filters, discard/trunkate substrings that do not overlap with inclusion filters before writing to substring file | ||
| 206 | if($excFilters{$chr} && $incFilters{$chr}){ | ||
| 207 | my @incRegions = @{$incFilters{$chr}}; | ||
| 208 | my @substrPass; | ||
| 209 | foreach my $sub (@substrings){ | ||
| 210 | my ($header, $subStart, $subEnd, $substr) = split(/\n/, $sub); | ||
| 211 | foreach my $reg (@incRegions){ | ||
| 212 | my ($regStart, $regEnd) = @{$reg}; | ||
| 213 | my $overlap = 0; | ||
| 214 | |||
| 215 | #Complete within inclusion region | ||
| 216 | if($regStart <= $subStart && $subEnd <= $regEnd){ | ||
| 217 | push(@substrPass, $sub); | ||
| 218 | last; | ||
| 219 | } | ||
| 220 | |||
| 221 | #Left part of substring outside inclusion region, trunkate | ||
| 222 | if($subStart < $regStart && $regStart < $subEnd){ | ||
| 223 | my $diff = $regStart - $subStart; | ||
| 224 | $subStart = $regStart; | ||
| 225 | $substr = substr($substr, $diff); | ||
| 226 | $overlap = 1; | ||
| 227 | } | ||
| 228 | |||
| 229 | #Right part of substring outside inclusion region, trunkate | ||
| 230 | if($subStart < $regEnd && $regEnd < $subEnd){ | ||
| 231 | my $diff = $subEnd - $regEnd; | ||
| 232 | $subEnd = $regEnd; | ||
| 233 | $substr = substr($substr, 0, length($substr) - $diff); | ||
| 234 | $overlap = 1; | ||
| 235 | } | ||
| 236 | |||
| 237 | #If partial overlap | ||
| 238 | if($overlap && length($substr) >= 200){ | ||
| 239 | push(@substrPass, "$header\n$subStart\n$subEnd\n$substr"); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | } | ||
| 243 | print(SUB join("\n", @substrPass)); | ||
| 244 | } | ||
| 245 | |||
| 246 | #If only inclusion filters | ||
| 247 | if(!$excFilters{$chr} && $incFilters{$chr}){ | ||
| 248 | my @incRegions = @{$incFilters{$chr}}; | ||
| 249 | foreach my $reg (@incRegions){ | ||
| 250 | my ($regStart, $regEnd) = @{$reg}; | ||
| 251 | my $subHeader = "$header substring $regStart-$regEnd"; | ||
| 252 | my $substr = substr($seq, $regStart - 1, $regEnd - $regStart + 1); | ||
| 253 | if(length($substr) >= 200){ | ||
| 254 | push(@substrings, "$subHeader\n$regStart\n$regEnd\n$substr"); | ||
| 255 | } | ||
| 256 | } | ||
| 257 | print(SUB join("\n", @substrings)); | ||
| 258 | } | ||
| 259 | close(SUB); | ||
| 260 | } | ||
| 261 | |||
| 262 | #Write chromosome/substring file paths to manifest | ||
| 263 | open(MAN, ">$OUTPUT_DIR/step_2/manifest") or die "GenoScan error: Unable to write input manifest"; | ||
| 264 | print(MAN join("\n", @manifestFiles)); | ||
| 265 | close(MAN); | ||
| 266 | |||
| 267 | #Segmentize input sequence files | ||
| 268 | segmentize(\@filterFlags, "$OUTPUT_DIR/step_2/manifest", "$OUTPUT_DIR/step_2/chunks/", $VERBOSE); | ||
| 269 | my @chunks = split(/\s+/, qx(ls $OUTPUT_DIR/step_2/chunks)); | ||
| 270 | my $numChunks = scalar(@chunks); | ||
| 271 | if($VERBOSE){ | ||
| 272 | print(" $numChunks chunk(s) generated\n"); | ||
| 273 | } | ||
| 274 | if($numChunks == 0){ | ||
| 275 | die "GenoScan: No chunks generated, terminating\n"; | ||
| 276 | } | ||
| 277 | push(@{$logArray}, "\t$numChunks chunk(s) generated\n\n"); | ||
| 278 | |||
| 279 | #Fold chunks | ||
| 280 | my $chunkCounter = 0; | ||
| 281 | foreach my $chunk (@chunks){ | ||
| 282 | $chunkCounter++; | ||
| 283 | if($VERBOSE){ | ||
| 284 | $| = 1; | ||
| 285 | print(" Folding chunk $chunkCounter/$numChunks\r"); | ||
| 286 | } | ||
| 287 | |||
| 288 | #Read chunk | ||
| 289 | open(CHUNK, "$OUTPUT_DIR/step_2/chunks/$chunk") or die "GenoScan error: Unable to read chunk file\n"; | ||
| 290 | my $chunkContents = join("", <CHUNK>); | ||
| 291 | close(CHUNK); | ||
| 292 | my @chunkContents = split(/\n\n/, $chunkContents); | ||
| 293 | my @passChunks; | ||
| 294 | foreach my $win (@chunkContents){ | ||
| 295 | my ($header, $seq) = split(/\n/, $win); | ||
| 296 | if($seq =~ m/N/i){ | ||
| 297 | next; | ||
| 298 | } | ||
| 299 | $seq =~ tr/ATGCatgc/UACGuacg/; | ||
| 300 | push(@passChunks, "$header\n$seq"); | ||
| 301 | } | ||
| 302 | |||
| 303 | #Rewrite chunk as RNA transcript of DNA | ||
| 304 | open(CHUNK, ">$OUTPUT_DIR/step_2/chunks/$chunk") or die "GenoScan error: Unable to write RNA chunk file\n"; | ||
| 305 | print(CHUNK join("\n\n", @passChunks)); | ||
| 306 | close(CHUNK); | ||
| 307 | |||
| 308 | #Run RNAfold, check if run is successful | ||
| 309 | my $result = qx($RNAfold < $OUTPUT_DIR/step_2/chunks/$chunk); | ||
| 310 | my @folds = split(/>/, $result); | ||
| 311 | shift(@folds); | ||
| 312 | |||
| 313 | #Process folded sequences | ||
| 314 | my @passFolded; | ||
| 315 | for(my $winIndex = 0; $winIndex < scalar(@passChunks); $winIndex++){ | ||
| 316 | my ($header, $seq) = split(/\n/, $passChunks[$winIndex]); | ||
| 317 | my ($h, $s, $secstr) = split(/\n/, $folds[$winIndex]); | ||
| 318 | push(@passFolded, "$header\n$seq\n$secstr"); | ||
| 319 | } | ||
| 320 | my $name = $chunk; | ||
| 321 | $name =~ s/txt$/folded/; | ||
| 322 | open(FOLDED, ">$OUTPUT_DIR/step_2/folded/$name") or die "GenoScan error: Unable to write RNA chunk file\n"; | ||
| 323 | print(FOLDED join("\n\n", @passFolded)); | ||
| 324 | close(FOLDED); | ||
| 325 | } | ||
| 326 | if($VERBOSE){ | ||
| 327 | print("\n"); | ||
| 328 | } | ||
| 329 | } | ||
| 330 | |||
| 331 | #Description: Sets the RNAfold running command | ||
| 332 | #Parameters: (1) RNAfold command | ||
| 333 | #Return value: None | ||
| 334 | sub commandRNAfold($){ | ||
| 335 | $RNAfold = shift(@_); | ||
| 336 | } | ||
| 337 | |||
| 338 | return 1; |