| 1 | package Software::GenoScan::FoldVisualizer; | ||
| 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 | visualizeFold | ||
| 12 | ) ] ); | ||
| 13 | our @EXPORT_OK = (@{ $EXPORT_TAGS{"all"} }); | ||
| 14 | |||
| 15 | #Description: Wrapper function for hairpin visualization | ||
| 16 | #Parameters: (1) Extracted hairpins file, (2) output file name | ||
| 17 | #Return value: None | ||
| 18 | sub visualizeFold($ $){ | ||
| 19 | my ($inputFile, $outputFile) = @_; | ||
| 20 | |||
| 21 | open(MIRNA, $inputFile) or die "GenoScan error: Unable to read '$inputFile'\n"; | ||
| 22 | my @mirnas = split(/>/, join("", <MIRNA>)); | ||
| 23 | shift(@mirnas); | ||
| 24 | close(MIRNA); | ||
| 25 | open(OUTPUT, ">$outputFile") or die "GenoScan error: Unable to write '$outputFile'\n"; | ||
| 26 | |||
| 27 | #annotations (FP = 5', TP = 3') | ||
| 28 | my $FP_STEM_BASEPAIR = "fp_stem_basepair"; | ||
| 29 | my $FP_STEM_UNPAIRED = "fp_stem_unpaired"; | ||
| 30 | my $FP_STEM_GAP = "fp_stem_gap"; | ||
| 31 | my $HAIRPIN_LOOP_TOP = "hairpin_loop_top"; | ||
| 32 | my $HAIRPIN_LOOP_SIDE1 = "hairpin_loop_side1"; | ||
| 33 | my $HAIRPIN_LOOP_SIDE2 = "hairpin_loop_side2"; | ||
| 34 | my $HAIRPIN_LOOP_SIDE3 = "hairpin_loop_side3"; | ||
| 35 | my $HAIRPIN_LOOP_BOTTOM = "hairpin_loop_bottom"; | ||
| 36 | my $TP_STEM_BASEPAIR = "tp_stem_basepair"; | ||
| 37 | my $TP_STEM_UNPAIRED = "tp_stem_unpaired"; | ||
| 38 | my $TP_STEM_GAP = "tp_stem_gap"; | ||
| 39 | |||
| 40 | my $FP_ARM_LEFT_BASEPAIR = "fp_arm_left_basepair"; | ||
| 41 | my $FP_ARM_RIGHT_BASEPAIR = "fp_arm_right_basepair"; | ||
| 42 | my $FP_ARM_LEFT_UNPAIRED = "fp_arm_left_unpaired"; | ||
| 43 | my $FP_ARM_RIGHT_UNPAIRED = "fp_arm_right_unpaired"; | ||
| 44 | my $FP_ARM_LEFT_GAP = "fp_arm_left_gap"; | ||
| 45 | my $FP_ARM_RIGHT_GAP = "fp_arm_right_gap"; | ||
| 46 | my $FP_ARM_LEFT_LOOP = "fp_arm_left_loop"; | ||
| 47 | my $FP_ARM_RIGHT_LOOP = "fp_arm_right_loop"; | ||
| 48 | my $FP_ARM_TOP1_LOOP = "fp_arm_top1_loop"; | ||
| 49 | my $FP_ARM_TOP2_LOOP = "fp_arm_top2_loop"; | ||
| 50 | my $FP_ARM_TOP3_LOOP = "fp_arm_top3_loop"; | ||
| 51 | |||
| 52 | my $TP_ARM_LEFT_BASEPAIR = "tp_arm_left_basepair"; | ||
| 53 | my $TP_ARM_RIGHT_BASEPAIR = "tp_arm_right_basepair"; | ||
| 54 | my $TP_ARM_LEFT_UNPAIRED = "tp_arm_left_unpaired"; | ||
| 55 | my $TP_ARM_RIGHT_UNPAIRED = "tp_arm_right_unpaired"; | ||
| 56 | my $TP_ARM_LEFT_GAP = "tp_arm_left_gap"; | ||
| 57 | my $TP_ARM_RIGHT_GAP = "tp_arm_right_gap"; | ||
| 58 | my $TP_ARM_LEFT_LOOP = "tp_arm_left_loop"; | ||
| 59 | my $TP_ARM_RIGHT_LOOP = "tp_arm_right_loop"; | ||
| 60 | my $TP_ARM_BOTTOM1_LOOP = "tp_arm_bottom1_loop"; | ||
| 61 | my $TP_ARM_BOTTOM2_LOOP = "tp_arm_bottom2_loop"; | ||
| 62 | my $TP_ARM_BOTTOM3_LOOP = "tp_arm_bottom3_loop"; | ||
| 63 | |||
| 64 | foreach my $mir (@mirnas){ | ||
| 65 | $mir = ">$mir"; | ||
| 66 | my ($name, $seq, $strRow) = split("\n", $mir); | ||
| 67 | my $str = $strRow; | ||
| 68 | $str =~ s/\s*\(\s*-?[0-9]+(\.[0-9]+)?\s*\)//; | ||
| 69 | |||
| 70 | #find hairpin loop | ||
| 71 | my $numLoops = () = $str =~ m/(\(\.+\))/g; | ||
| 72 | print(OUTPUT "$mir"); | ||
| 73 | my $hairpinLoopLength = length($1) - 2; | ||
| 74 | my $hairpinLoopStart = index($str, $1) + 1; | ||
| 75 | my $hairpinLoopEnd = $hairpinLoopStart + $hairpinLoopLength - 1; | ||
| 76 | |||
| 77 | #derive nucleotide annotation | ||
| 78 | my @structure = split("", $str); #secondary structure in "( . )" notation | ||
| 79 | my @sequence = split("", $seq); #sequence in nucleotides | ||
| 80 | my @annotation; #annotation for each nucleotide | ||
| 81 | |||
| 82 | #annotate hairpin loop | ||
| 83 | if($hairpinLoopLength == 1){ | ||
| 84 | push(@annotation, $HAIRPIN_LOOP_SIDE2); | ||
| 85 | } | ||
| 86 | elsif($hairpinLoopLength == 2){ | ||
| 87 | push(@annotation, $HAIRPIN_LOOP_SIDE1); | ||
| 88 | push(@annotation, $HAIRPIN_LOOP_SIDE3); | ||
| 89 | } | ||
| 90 | elsif($hairpinLoopLength == 3){ | ||
| 91 | push(@annotation, $HAIRPIN_LOOP_TOP); | ||
| 92 | push(@annotation, $HAIRPIN_LOOP_SIDE2); | ||
| 93 | push(@annotation, $HAIRPIN_LOOP_BOTTOM); | ||
| 94 | } | ||
| 95 | elsif($hairpinLoopLength == 4){ | ||
| 96 | push(@annotation, $HAIRPIN_LOOP_TOP); | ||
| 97 | push(@annotation, $HAIRPIN_LOOP_SIDE1); | ||
| 98 | push(@annotation, $HAIRPIN_LOOP_SIDE3); | ||
| 99 | push(@annotation, $HAIRPIN_LOOP_BOTTOM); | ||
| 100 | } | ||
| 101 | elsif($hairpinLoopLength >= 5){ | ||
| 102 | if($hairpinLoopLength % 2 == 0){ | ||
| 103 | my $extend = ($hairpinLoopLength - 2) / 2; | ||
| 104 | foreach my $pos (0 .. $extend-1){ | ||
| 105 | push(@annotation, $HAIRPIN_LOOP_TOP); | ||
| 106 | } | ||
| 107 | push(@annotation, $HAIRPIN_LOOP_SIDE1); | ||
| 108 | push(@annotation, $HAIRPIN_LOOP_SIDE3); | ||
| 109 | foreach my $pos (0 .. $extend-1){ | ||
| 110 | push(@annotation, $HAIRPIN_LOOP_BOTTOM); | ||
| 111 | } | ||
| 112 | } | ||
| 113 | else{ | ||
| 114 | my $extend = ($hairpinLoopLength - 3) / 2; | ||
| 115 | foreach my $pos (0 .. $extend-1){ | ||
| 116 | push(@annotation, $HAIRPIN_LOOP_TOP); | ||
| 117 | } | ||
| 118 | push(@annotation, $HAIRPIN_LOOP_SIDE1); | ||
| 119 | push(@annotation, $HAIRPIN_LOOP_SIDE2); | ||
| 120 | push(@annotation, $HAIRPIN_LOOP_SIDE3); | ||
| 121 | foreach my $pos (0 .. $extend-1){ | ||
| 122 | push(@annotation, $HAIRPIN_LOOP_BOTTOM); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | #annotate left and right | ||
| 128 | my $leftAdjust = 0; | ||
| 129 | my $rightAdjust = 0; | ||
| 130 | my $leftArm = 0; | ||
| 131 | my $rightArm = 0; | ||
| 132 | my $fpArmFill = 0; | ||
| 133 | my $tpArmFill = 0; | ||
| 134 | for(my $distance = 1; 1 == 1; $distance++){ | ||
| 135 | my $leftSymbol; | ||
| 136 | my $rightSymbol; | ||
| 137 | my $leftDistance = $hairpinLoopStart - ($distance - $leftAdjust + $leftArm); | ||
| 138 | my $rightDistance = $hairpinLoopEnd + $distance - $rightAdjust + $rightArm; | ||
| 139 | if($leftDistance < 0){ | ||
| 140 | $leftSymbol = ""; | ||
| 141 | } | ||
| 142 | else{ | ||
| 143 | $leftSymbol = $structure[$leftDistance]; | ||
| 144 | } | ||
| 145 | if($rightDistance > scalar(@structure) - 1){ | ||
| 146 | $rightSymbol = ""; | ||
| 147 | } | ||
| 148 | else{ | ||
| 149 | $rightSymbol = $structure[$rightDistance]; | ||
| 150 | } | ||
| 151 | if(!$leftSymbol && !$rightSymbol){ | ||
| 152 | last; | ||
| 153 | } | ||
| 154 | if($fpArmFill > 0 && $tpArmFill > 0){ | ||
| 155 | $fpArmFill--; | ||
| 156 | $tpArmFill--; | ||
| 157 | } | ||
| 158 | elsif($fpArmFill > 0){ | ||
| 159 | if($leftSymbol eq "."){ | ||
| 160 | unshift(@annotation, $FP_STEM_UNPAIRED); | ||
| 161 | $rightAdjust++; | ||
| 162 | $fpArmFill--; | ||
| 163 | next; | ||
| 164 | } | ||
| 165 | elsif($leftSymbol eq "("){ | ||
| 166 | unshift(@annotation, $FP_STEM_GAP); | ||
| 167 | $rightAdjust++; | ||
| 168 | $leftAdjust++; | ||
| 169 | $fpArmFill--; | ||
| 170 | next; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | elsif($tpArmFill > 0){ | ||
| 174 | if($rightSymbol eq "."){ | ||
| 175 | push(@annotation, $TP_STEM_UNPAIRED); | ||
| 176 | $leftAdjust++; | ||
| 177 | $tpArmFill--; | ||
| 178 | next; | ||
| 179 | } | ||
| 180 | elsif($rightSymbol eq ")"){ | ||
| 181 | push(@annotation, $TP_STEM_GAP); | ||
| 182 | $rightAdjust++; | ||
| 183 | $leftAdjust++; | ||
| 184 | $tpArmFill--; | ||
| 185 | next; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | #( with ) | ||
| 190 | if($leftSymbol eq "(" && $rightSymbol eq ")"){ | ||
| 191 | unshift(@annotation, $FP_STEM_BASEPAIR); | ||
| 192 | push(@annotation, $TP_STEM_BASEPAIR); | ||
| 193 | } | ||
| 194 | |||
| 195 | #. with . | ||
| 196 | elsif($leftSymbol eq "." && $rightSymbol eq "."){ | ||
| 197 | unshift(@annotation, $FP_STEM_UNPAIRED); | ||
| 198 | push(@annotation, $TP_STEM_UNPAIRED); | ||
| 199 | } | ||
| 200 | |||
| 201 | #( with . | ||
| 202 | elsif($leftSymbol eq "(" && $rightSymbol eq "."){ | ||
| 203 | unshift(@annotation, $FP_STEM_GAP); | ||
| 204 | push(@annotation, $TP_STEM_UNPAIRED); | ||
| 205 | $leftAdjust++; | ||
| 206 | } | ||
| 207 | |||
| 208 | #. with ) | ||
| 209 | elsif($leftSymbol eq "." && $rightSymbol eq ")"){ | ||
| 210 | unshift(@annotation, $FP_STEM_UNPAIRED); | ||
| 211 | push(@annotation, $TP_STEM_GAP); | ||
| 212 | $rightAdjust++; | ||
| 213 | } | ||
| 214 | |||
| 215 | #tail: $left = "" | ||
| 216 | elsif(!$leftSymbol && $rightSymbol){ | ||
| 217 | unshift(@annotation, $FP_STEM_GAP); | ||
| 218 | push(@annotation, $TP_STEM_UNPAIRED); | ||
| 219 | } | ||
| 220 | |||
| 221 | #tail: $right = "" | ||
| 222 | elsif($leftSymbol && !$rightSymbol){ | ||
| 223 | unshift(@annotation, $FP_STEM_UNPAIRED); | ||
| 224 | push(@annotation, $TP_STEM_GAP); | ||
| 225 | } | ||
| 226 | |||
| 227 | #arm: $left = ) | ||
| 228 | elsif($leftSymbol eq ")"){ | ||
| 229 | my $armStart = $leftDistance; #position of first ) in 5' arm | ||
| 230 | my $armEnd; #position of last ( in 5' arm | ||
| 231 | my $armOpen = 0; #number of ) | ||
| 232 | my $armClose = 0; #number of ( | ||
| 233 | my $closeReached = 0; #end of arm loop has been seen | ||
| 234 | my $loopLeft; #position of first ( left of arm loop | ||
| 235 | my $loopRight; #position of first ) right of arm loop | ||
| 236 | my @armAnnot; | ||
| 237 | |||
| 238 | #walk left across the arm to determine its length | ||
| 239 | for(my $index = $armStart; 1 == 1; $index--){ | ||
| 240 | if(!$closeReached){ | ||
| 241 | if($structure[$index] eq ")"){ | ||
| 242 | $armOpen++; | ||
| 243 | } | ||
| 244 | elsif($structure[$index] eq "("){ | ||
| 245 | $closeReached = 1; | ||
| 246 | $armClose++; | ||
| 247 | $loopLeft = $index; | ||
| 248 | my $loop = 1; | ||
| 249 | while($structure[$index+$loop] eq "."){ | ||
| 250 | $loop++; | ||
| 251 | } | ||
| 252 | $loopRight = $index + $loop; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | else{ | ||
| 256 | if($armOpen == $armClose){ | ||
| 257 | $armEnd = $index + 1; | ||
| 258 | last; | ||
| 259 | } | ||
| 260 | elsif($structure[$index] eq "("){ | ||
| 261 | $armClose++; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | #annotate arm loop | ||
| 267 | my $loopLength = $loopRight - $loopLeft - 1; | ||
| 268 | if($loopLength == 1){ | ||
| 269 | push(@armAnnot, $FP_ARM_TOP2_LOOP); | ||
| 270 | } | ||
| 271 | elsif($loopLength == 2){ | ||
| 272 | push(@armAnnot, $FP_ARM_TOP1_LOOP); | ||
| 273 | push(@armAnnot, $FP_ARM_TOP3_LOOP); | ||
| 274 | } | ||
| 275 | elsif($loopLength == 3){ | ||
| 276 | push(@armAnnot, $FP_ARM_LEFT_LOOP); | ||
| 277 | push(@armAnnot, $FP_ARM_TOP2_LOOP); | ||
| 278 | push(@armAnnot, $FP_ARM_RIGHT_LOOP); | ||
| 279 | } | ||
| 280 | elsif($loopLength == 4){ | ||
| 281 | push(@armAnnot, $FP_ARM_LEFT_LOOP); | ||
| 282 | push(@armAnnot, $FP_ARM_TOP1_LOOP); | ||
| 283 | push(@armAnnot, $FP_ARM_TOP3_LOOP); | ||
| 284 | push(@armAnnot, $FP_ARM_RIGHT_LOOP); | ||
| 285 | } | ||
| 286 | elsif($loopLength >= 5){ | ||
| 287 | if($loopLength % 2 == 0){ | ||
| 288 | my $extendLoop = ($loopLength - 2) / 2; | ||
| 289 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 290 | push(@armAnnot, $FP_ARM_LEFT_LOOP); | ||
| 291 | } | ||
| 292 | push(@armAnnot, $FP_ARM_TOP1_LOOP); | ||
| 293 | push(@armAnnot, $FP_ARM_TOP3_LOOP); | ||
| 294 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 295 | push(@armAnnot, $FP_ARM_RIGHT_LOOP); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | else{ | ||
| 299 | my $extendLoop = ($loopLength - 3) / 2; | ||
| 300 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 301 | push(@armAnnot, $FP_ARM_LEFT_LOOP); | ||
| 302 | } | ||
| 303 | push(@armAnnot, $FP_ARM_TOP1_LOOP); | ||
| 304 | push(@armAnnot, $FP_ARM_TOP2_LOOP); | ||
| 305 | push(@armAnnot, $FP_ARM_TOP3_LOOP); | ||
| 306 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 307 | push(@armAnnot, $FP_ARM_RIGHT_LOOP); | ||
| 308 | } | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | #simultaneously walk left and right from the arm loop | ||
| 313 | my $leftAdj = 0; | ||
| 314 | my $rightAdj = 0; | ||
| 315 | for(my $armloopDistance = 1; 1 == 1; $armloopDistance++){ | ||
| 316 | my $leftStr; | ||
| 317 | my $rightStr; | ||
| 318 | my $leftDist = $loopLeft + 1 - ($armloopDistance - $leftAdj); | ||
| 319 | my $rightDist = $loopRight - 1 + $armloopDistance - $rightAdj; | ||
| 320 | if($leftDist < $armEnd){ | ||
| 321 | $leftStr = ""; | ||
| 322 | } | ||
| 323 | else{ | ||
| 324 | $leftStr = $structure[$leftDist]; | ||
| 325 | } | ||
| 326 | if($rightDist > $armStart){ | ||
| 327 | $rightStr = ""; | ||
| 328 | } | ||
| 329 | else{ | ||
| 330 | $rightStr = $structure[$rightDist]; | ||
| 331 | } | ||
| 332 | |||
| 333 | if(!$leftStr && !$rightStr){ | ||
| 334 | last; | ||
| 335 | } | ||
| 336 | |||
| 337 | #( with ) | ||
| 338 | if($leftStr eq "(" && $rightStr eq ")"){ | ||
| 339 | unshift(@armAnnot, $FP_ARM_LEFT_BASEPAIR); | ||
| 340 | push(@armAnnot, $FP_ARM_RIGHT_BASEPAIR); | ||
| 341 | } | ||
| 342 | |||
| 343 | #. with . | ||
| 344 | elsif($leftStr eq "." && $rightStr eq "."){ | ||
| 345 | unshift(@armAnnot, $FP_ARM_LEFT_UNPAIRED); | ||
| 346 | push(@armAnnot, $FP_ARM_RIGHT_UNPAIRED); | ||
| 347 | } | ||
| 348 | |||
| 349 | #( with . | ||
| 350 | elsif($leftStr eq "(" && $rightStr eq "."){ | ||
| 351 | unshift(@armAnnot, $FP_ARM_LEFT_GAP); | ||
| 352 | push(@armAnnot, $FP_ARM_RIGHT_UNPAIRED); | ||
| 353 | $leftAdj++; | ||
| 354 | } | ||
| 355 | |||
| 356 | #. with ) | ||
| 357 | elsif($leftStr eq "." && $rightStr eq ")"){ | ||
| 358 | unshift(@armAnnot, $FP_ARM_LEFT_UNPAIRED); | ||
| 359 | push(@armAnnot, $FP_ARM_RIGHT_GAP); | ||
| 360 | $rightAdj++; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | foreach my $a (@armAnnot){ | ||
| 364 | unshift(@annotation, $a); | ||
| 365 | } | ||
| 366 | $leftArm += scalar(@armAnnot); | ||
| 367 | $rightAdjust++; | ||
| 368 | $tpArmFill += 3; | ||
| 369 | } | ||
| 370 | |||
| 371 | #arm: $right = ( | ||
| 372 | elsif($rightSymbol eq "("){ | ||
| 373 | my $armStart = $rightDistance; #position of first ( in 3' arm | ||
| 374 | my $armEnd; #position of last ) in 3' arm | ||
| 375 | my $armOpen = 0; #number of ) | ||
| 376 | my $armClose = 0; #number of ( | ||
| 377 | my $closeReached = 0; #end of arm loop has been seen | ||
| 378 | my $loopLeft; #position of first ( left of arm loop | ||
| 379 | my $loopRight; #position of first ) right of arm loop | ||
| 380 | my @armAnnot; | ||
| 381 | |||
| 382 | #walk right across the arm to determine its length | ||
| 383 | for(my $index = $armStart; 1 == 1; $index++){ | ||
| 384 | if(!$closeReached){ | ||
| 385 | if($structure[$index] eq "("){ | ||
| 386 | $armOpen++; | ||
| 387 | } | ||
| 388 | elsif($structure[$index] eq ")"){ | ||
| 389 | $closeReached = 1; | ||
| 390 | $armClose++; | ||
| 391 | $loopRight = $index; | ||
| 392 | my $loop = 1; | ||
| 393 | my $loopSize = 0; | ||
| 394 | while($structure[$index-$loop] eq "."){ | ||
| 395 | $loop++; | ||
| 396 | $loopSize++; | ||
| 397 | } | ||
| 398 | $loopLeft = $index - $loopSize - 1; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | else{ | ||
| 402 | if($armOpen == $armClose){ | ||
| 403 | $armEnd = $index - 1; | ||
| 404 | last; | ||
| 405 | } | ||
| 406 | elsif($structure[$index] eq ")"){ | ||
| 407 | $armClose++; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | #annotate arm loop | ||
| 413 | my $loopLength = $loopRight - $loopLeft - 1; | ||
| 414 | if($loopLength == 1){ | ||
| 415 | push(@armAnnot, $TP_ARM_BOTTOM2_LOOP); | ||
| 416 | } | ||
| 417 | elsif($loopLength == 2){ | ||
| 418 | push(@armAnnot, $TP_ARM_BOTTOM1_LOOP); | ||
| 419 | push(@armAnnot, $TP_ARM_BOTTOM3_LOOP); | ||
| 420 | } | ||
| 421 | elsif($loopLength == 3){ | ||
| 422 | push(@armAnnot, $TP_ARM_RIGHT_LOOP); | ||
| 423 | push(@armAnnot, $TP_ARM_BOTTOM2_LOOP); | ||
| 424 | push(@armAnnot, $TP_ARM_LEFT_LOOP); | ||
| 425 | } | ||
| 426 | elsif($loopLength == 4){ | ||
| 427 | push(@armAnnot, $TP_ARM_RIGHT_LOOP); | ||
| 428 | push(@armAnnot, $TP_ARM_BOTTOM1_LOOP); | ||
| 429 | push(@armAnnot, $TP_ARM_BOTTOM3_LOOP); | ||
| 430 | push(@armAnnot, $TP_ARM_LEFT_LOOP); | ||
| 431 | } | ||
| 432 | elsif($loopLength >= 5){ | ||
| 433 | if($loopLength % 2 == 0){ | ||
| 434 | my $extendLoop = ($loopLength - 2) / 2; | ||
| 435 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 436 | push(@armAnnot, $TP_ARM_RIGHT_LOOP); | ||
| 437 | } | ||
| 438 | push(@armAnnot, $TP_ARM_BOTTOM1_LOOP); | ||
| 439 | push(@armAnnot, $TP_ARM_BOTTOM3_LOOP); | ||
| 440 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 441 | push(@armAnnot, $TP_ARM_LEFT_LOOP); | ||
| 442 | } | ||
| 443 | } | ||
| 444 | else{ | ||
| 445 | my $extendLoop = ($loopLength - 3) / 2; | ||
| 446 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 447 | push(@armAnnot, $TP_ARM_RIGHT_LOOP); | ||
| 448 | } | ||
| 449 | push(@armAnnot, $TP_ARM_BOTTOM1_LOOP); | ||
| 450 | push(@armAnnot, $TP_ARM_BOTTOM2_LOOP); | ||
| 451 | push(@armAnnot, $TP_ARM_BOTTOM3_LOOP); | ||
| 452 | foreach my $pos (0 .. $extendLoop-1){ | ||
| 453 | push(@armAnnot, $TP_ARM_LEFT_LOOP); | ||
| 454 | } | ||
| 455 | } | ||
| 456 | } | ||
| 457 | |||
| 458 | #simultaneously walk left and right from the arm loop | ||
| 459 | my $leftAdj = 0; | ||
| 460 | my $rightAdj = 0; | ||
| 461 | for(my $armloopDistance = 1; 1 == 1; $armloopDistance++){ | ||
| 462 | my $leftStr; | ||
| 463 | my $rightStr; | ||
| 464 | my $leftDist = $loopLeft + 1 - ($armloopDistance - $leftAdj); | ||
| 465 | my $rightDist = $loopRight - 1 + $armloopDistance - $rightAdj; | ||
| 466 | if($leftDist < $armStart){ | ||
| 467 | $leftStr = ""; | ||
| 468 | } | ||
| 469 | else{ | ||
| 470 | $leftStr = $structure[$leftDist]; | ||
| 471 | } | ||
| 472 | if($rightDist > $armEnd){ | ||
| 473 | $rightStr = ""; | ||
| 474 | } | ||
| 475 | else{ | ||
| 476 | $rightStr = $structure[$rightDist]; | ||
| 477 | } | ||
| 478 | if(!$leftStr && !$rightStr){ | ||
| 479 | last; | ||
| 480 | } | ||
| 481 | |||
| 482 | #( with ) | ||
| 483 | if($leftStr eq "(" && $rightStr eq ")"){ | ||
| 484 | unshift(@armAnnot, $TP_ARM_RIGHT_BASEPAIR); | ||
| 485 | push(@armAnnot, $TP_ARM_LEFT_BASEPAIR); | ||
| 486 | } | ||
| 487 | |||
| 488 | #. with . | ||
| 489 | elsif($leftStr eq "." && $rightStr eq "."){ | ||
| 490 | unshift(@armAnnot, $TP_ARM_RIGHT_UNPAIRED); | ||
| 491 | push(@armAnnot, $TP_ARM_LEFT_UNPAIRED); | ||
| 492 | } | ||
| 493 | |||
| 494 | #( with . | ||
| 495 | elsif($leftStr eq "(" && $rightStr eq "."){ | ||
| 496 | unshift(@armAnnot, $TP_ARM_RIGHT_GAP); | ||
| 497 | push(@armAnnot, $TP_ARM_LEFT_UNPAIRED); | ||
| 498 | $leftAdj++; | ||
| 499 | } | ||
| 500 | |||
| 501 | #. with ) | ||
| 502 | elsif($leftStr eq "." && $rightStr eq ")"){ | ||
| 503 | unshift(@armAnnot, $TP_ARM_RIGHT_UNPAIRED); | ||
| 504 | push(@armAnnot, $TP_ARM_LEFT_GAP); | ||
| 505 | $rightAdj++; | ||
| 506 | } | ||
| 507 | } | ||
| 508 | foreach my $a (@armAnnot){ | ||
| 509 | push(@annotation, $a); | ||
| 510 | } | ||
| 511 | $rightArm += scalar(@armAnnot); | ||
| 512 | $leftAdjust++; | ||
| 513 | $fpArmFill += 3; | ||
| 514 | } | ||
| 515 | } | ||
| 516 | |||
| 517 | #use annotation to draw 2D character plot of RNA structure | ||
| 518 | my $row = 50; | ||
| 519 | my $col = 5; | ||
| 520 | my @structPlot; | ||
| 521 | my $state = "stem"; | ||
| 522 | my $prime = "five"; | ||
| 523 | foreach my $annot (@annotation){ | ||
| 524 | |||
| 525 | #Change state | ||
| 526 | if($state eq "stem" && $annot =~ m/fp_arm_left/){ | ||
| 527 | $state = "fp_arm_left"; | ||
| 528 | $row--; | ||
| 529 | } | ||
| 530 | elsif($state eq "fp_arm_left" && $annot =~ m/fp_arm_right/){ | ||
| 531 | $state = "fp_arm_right"; | ||
| 532 | $col += 2; | ||
| 533 | $row++; | ||
| 534 | } | ||
| 535 | elsif($state eq "fp_arm_right" && $annot =~ m/fp_stem|hairpin/){ | ||
| 536 | $state = "stem"; | ||
| 537 | $col++; | ||
| 538 | } | ||
| 539 | elsif($state eq "stem" && $annot =~ m/tp_arm_right/){ | ||
| 540 | $state = "tp_arm_right"; | ||
| 541 | $row++; | ||
| 542 | } | ||
| 543 | elsif($state eq "tp_arm_right" && $annot =~ m/tp_arm_left/){ | ||
| 544 | $state = "tp_arm_left"; | ||
| 545 | $col -= 2; | ||
| 546 | $row--; | ||
| 547 | } | ||
| 548 | elsif($state eq "tp_arm_left" && $annot =~ m/tp_stem/){ | ||
| 549 | $state = "stem"; | ||
| 550 | $col--; | ||
| 551 | } | ||
| 552 | if($prime eq "five" && $annot =~ m/tp_stem/){ | ||
| 553 | $row += 2; | ||
| 554 | $col--; | ||
| 555 | $prime = "three"; | ||
| 556 | } | ||
| 557 | |||
| 558 | # | ||
| 559 | # FP STEM | ||
| 560 | # | ||
| 561 | |||
| 562 | #move right | ||
| 563 | if($annot eq $FP_STEM_BASEPAIR){ | ||
| 564 | my $nuc = shift(@sequence); | ||
| 565 | $structPlot[$row][$col] = $nuc; | ||
| 566 | $structPlot[$row+1][$col] = "|"; | ||
| 567 | $col++; | ||
| 568 | } | ||
| 569 | |||
| 570 | #move right | ||
| 571 | elsif($annot eq $FP_STEM_UNPAIRED){ | ||
| 572 | my $nuc = shift(@sequence); | ||
| 573 | $structPlot[$row-1][$col] = $nuc; | ||
| 574 | $col++; | ||
| 575 | } | ||
| 576 | |||
| 577 | #move right | ||
| 578 | elsif($annot eq $FP_STEM_GAP){ | ||
| 579 | $structPlot[$row-1][$col] = "-"; | ||
| 580 | $col++; | ||
| 581 | } | ||
| 582 | |||
| 583 | # | ||
| 584 | # FP ARM | ||
| 585 | # | ||
| 586 | |||
| 587 | #move up | ||
| 588 | elsif($annot eq $FP_ARM_LEFT_BASEPAIR){ | ||
| 589 | my $nuc = shift(@sequence); | ||
| 590 | $structPlot[$row][$col] = $nuc; | ||
| 591 | $structPlot[$row][$col+1] = "-"; | ||
| 592 | $row--; | ||
| 593 | } | ||
| 594 | |||
| 595 | #move up | ||
| 596 | elsif($annot eq $FP_ARM_LEFT_UNPAIRED){ | ||
| 597 | my $nuc = shift(@sequence); | ||
| 598 | $structPlot[$row][$col-1] = $nuc; | ||
| 599 | $row--; | ||
| 600 | } | ||
| 601 | |||
| 602 | #move up | ||
| 603 | elsif($annot eq $FP_ARM_LEFT_GAP){ | ||
| 604 | $structPlot[$row][$col-1] = "|"; | ||
| 605 | $row--; | ||
| 606 | } | ||
| 607 | |||
| 608 | #move up | ||
| 609 | elsif($annot eq $FP_ARM_LEFT_LOOP){ | ||
| 610 | my $nuc = shift(@sequence); | ||
| 611 | $structPlot[$row][$col-1] = $nuc; | ||
| 612 | $row--; | ||
| 613 | } | ||
| 614 | |||
| 615 | #do not move | ||
| 616 | elsif($annot eq $FP_ARM_TOP1_LOOP){ | ||
| 617 | my $nuc = shift(@sequence); | ||
| 618 | $structPlot[$row][$col] = $nuc; | ||
| 619 | } | ||
| 620 | |||
| 621 | #do not move | ||
| 622 | elsif($annot eq $FP_ARM_TOP2_LOOP){ | ||
| 623 | my $nuc = shift(@sequence); | ||
| 624 | $structPlot[$row][$col+1] = $nuc; | ||
| 625 | } | ||
| 626 | |||
| 627 | #do not move | ||
| 628 | elsif($annot eq $FP_ARM_TOP3_LOOP){ | ||
| 629 | my $nuc = shift(@sequence); | ||
| 630 | $structPlot[$row][$col+2] = $nuc; | ||
| 631 | } | ||
| 632 | |||
| 633 | #move down | ||
| 634 | elsif($annot eq $FP_ARM_RIGHT_LOOP){ | ||
| 635 | my $nuc = shift(@sequence); | ||
| 636 | $structPlot[$row][$col+1] = $nuc; | ||
| 637 | $row++; | ||
| 638 | } | ||
| 639 | |||
| 640 | #move down | ||
| 641 | elsif($annot eq $FP_ARM_RIGHT_BASEPAIR){ | ||
| 642 | my $nuc = shift(@sequence); | ||
| 643 | $structPlot[$row][$col] = $nuc; | ||
| 644 | $row++; | ||
| 645 | } | ||
| 646 | |||
| 647 | #move down | ||
| 648 | elsif($annot eq $FP_ARM_RIGHT_UNPAIRED){ | ||
| 649 | my $nuc = shift(@sequence); | ||
| 650 | $structPlot[$row][$col+1] = $nuc; | ||
| 651 | $row++; | ||
| 652 | } | ||
| 653 | |||
| 654 | #move down | ||
| 655 | elsif($annot eq $FP_ARM_RIGHT_GAP){ | ||
| 656 | $structPlot[$row][$col+1] = "-"; | ||
| 657 | $row++; | ||
| 658 | } | ||
| 659 | |||
| 660 | # | ||
| 661 | # HAIRPIN | ||
| 662 | # | ||
| 663 | |||
| 664 | #move right | ||
| 665 | elsif($annot eq $HAIRPIN_LOOP_TOP){ | ||
| 666 | my $nuc = shift(@sequence); | ||
| 667 | $structPlot[$row-1][$col] = $nuc; | ||
| 668 | $col++; | ||
| 669 | } | ||
| 670 | |||
| 671 | #do not move | ||
| 672 | elsif($annot eq $HAIRPIN_LOOP_SIDE1){ | ||
| 673 | my $nuc = shift(@sequence); | ||
| 674 | $structPlot[$row][$col] = $nuc; | ||
| 675 | } | ||
| 676 | |||
| 677 | #do not move | ||
| 678 | elsif($annot eq $HAIRPIN_LOOP_SIDE2){ | ||
| 679 | my $nuc = shift(@sequence); | ||
| 680 | $structPlot[$row+1][$col] = $nuc; | ||
| 681 | } | ||
| 682 | |||
| 683 | #do not move | ||
| 684 | elsif($annot eq $HAIRPIN_LOOP_SIDE3){ | ||
| 685 | my $nuc = shift(@sequence); | ||
| 686 | $structPlot[$row+2][$col] = $nuc; | ||
| 687 | } | ||
| 688 | |||
| 689 | #move left | ||
| 690 | elsif($annot eq $HAIRPIN_LOOP_BOTTOM){ | ||
| 691 | my $nuc = shift(@sequence); | ||
| 692 | $structPlot[$row+3][$col-1] = $nuc; | ||
| 693 | $col--; | ||
| 694 | } | ||
| 695 | |||
| 696 | # | ||
| 697 | # TP STEM | ||
| 698 | # | ||
| 699 | |||
| 700 | #move left | ||
| 701 | elsif($annot eq $TP_STEM_BASEPAIR){ | ||
| 702 | my $nuc = shift(@sequence); | ||
| 703 | $structPlot[$row][$col] = $nuc; | ||
| 704 | $col--; | ||
| 705 | } | ||
| 706 | |||
| 707 | #move left | ||
| 708 | elsif($annot eq $TP_STEM_UNPAIRED){ | ||
| 709 | my $nuc = shift(@sequence); | ||
| 710 | $structPlot[$row+1][$col] = $nuc; | ||
| 711 | $col--; | ||
| 712 | } | ||
| 713 | |||
| 714 | #move left | ||
| 715 | elsif($annot eq $TP_STEM_GAP){ | ||
| 716 | $structPlot[$row+1][$col] = "-"; | ||
| 717 | $col--; | ||
| 718 | } | ||
| 719 | |||
| 720 | # | ||
| 721 | # TP ARM | ||
| 722 | # | ||
| 723 | |||
| 724 | #move down | ||
| 725 | elsif($annot eq $TP_ARM_RIGHT_BASEPAIR){ | ||
| 726 | my $nuc = shift(@sequence); | ||
| 727 | $structPlot[$row][$col] = $nuc; | ||
| 728 | $structPlot[$row][$col-1] = "-"; | ||
| 729 | $row++; | ||
| 730 | } | ||
| 731 | |||
| 732 | #move down | ||
| 733 | elsif($annot eq $TP_ARM_RIGHT_UNPAIRED){ | ||
| 734 | my $nuc = shift(@sequence); | ||
| 735 | $structPlot[$row][$col+1] = $nuc; | ||
| 736 | $row++; | ||
| 737 | } | ||
| 738 | |||
| 739 | #move down | ||
| 740 | elsif($annot eq $TP_ARM_RIGHT_GAP){ | ||
| 741 | $structPlot[$row][$col+1] = "|"; | ||
| 742 | $row++; | ||
| 743 | } | ||
| 744 | |||
| 745 | #move down | ||
| 746 | elsif($annot eq $TP_ARM_RIGHT_LOOP){ | ||
| 747 | my $nuc = shift(@sequence); | ||
| 748 | $structPlot[$row][$col+1] = $nuc; | ||
| 749 | $row++; | ||
| 750 | } | ||
| 751 | |||
| 752 | #do not move | ||
| 753 | elsif($annot eq $TP_ARM_BOTTOM1_LOOP){ | ||
| 754 | my $nuc = shift(@sequence); | ||
| 755 | $structPlot[$row][$col] = $nuc; | ||
| 756 | } | ||
| 757 | |||
| 758 | #do not move | ||
| 759 | elsif($annot eq $TP_ARM_BOTTOM2_LOOP){ | ||
| 760 | my $nuc = shift(@sequence); | ||
| 761 | $structPlot[$row][$col-1] = $nuc; | ||
| 762 | } | ||
| 763 | |||
| 764 | #do not move | ||
| 765 | elsif($annot eq $TP_ARM_BOTTOM3_LOOP){ | ||
| 766 | my $nuc = shift(@sequence); | ||
| 767 | $structPlot[$row][$col-2] = $nuc; | ||
| 768 | } | ||
| 769 | |||
| 770 | #move down | ||
| 771 | elsif($annot eq $TP_ARM_LEFT_LOOP){ | ||
| 772 | my $nuc = shift(@sequence); | ||
| 773 | $structPlot[$row][$col-1] = $nuc; | ||
| 774 | $row--; | ||
| 775 | } | ||
| 776 | |||
| 777 | #move down | ||
| 778 | elsif($annot eq $TP_ARM_LEFT_BASEPAIR){ | ||
| 779 | my $nuc = shift(@sequence); | ||
| 780 | $structPlot[$row][$col] = $nuc; | ||
| 781 | $row--; | ||
| 782 | } | ||
| 783 | |||
| 784 | #move down | ||
| 785 | elsif($annot eq $TP_ARM_LEFT_UNPAIRED){ | ||
| 786 | my $nuc = shift(@sequence); | ||
| 787 | $structPlot[$row][$col-1] = $nuc; | ||
| 788 | $row--; | ||
| 789 | } | ||
| 790 | |||
| 791 | #move down | ||
| 792 | elsif($annot eq $TP_ARM_LEFT_GAP){ | ||
| 793 | $structPlot[$row][$col-1] = "|"; | ||
| 794 | $row--; | ||
| 795 | } | ||
| 796 | } | ||
| 797 | |||
| 798 | #Find bounding box around structure plot | ||
| 799 | my $leftSide; | ||
| 800 | my $topSide; | ||
| 801 | my $rightSide; | ||
| 802 | my $bottomSide; | ||
| 803 | my $found = 0; | ||
| 804 | |||
| 805 | #top side | ||
| 806 | for(my $row = 0; $row <= 100; $row++){ | ||
| 807 | for(my $col = 0; $col <= 100; $col++){ | ||
| 808 | if($structPlot[$row][$col]){ | ||
| 809 | $topSide = $row; | ||
| 810 | $found = 1; | ||
| 811 | last; | ||
| 812 | } | ||
| 813 | } | ||
| 814 | if($found){ | ||
| 815 | last; | ||
| 816 | } | ||
| 817 | } | ||
| 818 | |||
| 819 | #left side | ||
| 820 | $found = 0; | ||
| 821 | for(my $col = 0; $col <= 100; $col++){ | ||
| 822 | for(my $row = 0; $row <= 100; $row++){ | ||
| 823 | if($structPlot[$row][$col]){ | ||
| 824 | $leftSide = $col; | ||
| 825 | $found = 1; | ||
| 826 | last; | ||
| 827 | } | ||
| 828 | } | ||
| 829 | if($found){ | ||
| 830 | last; | ||
| 831 | } | ||
| 832 | } | ||
| 833 | |||
| 834 | #right side | ||
| 835 | $found = 0; | ||
| 836 | for(my $col = 200; $col > 0; $col--){ | ||
| 837 | for(my $row = 0; $row <= 100; $row++){ | ||
| 838 | if($structPlot[$row][$col]){ | ||
| 839 | $rightSide = $col; | ||
| 840 | $found = 1; | ||
| 841 | last; | ||
| 842 | } | ||
| 843 | } | ||
| 844 | if($found){ | ||
| 845 | last; | ||
| 846 | } | ||
| 847 | } | ||
| 848 | |||
| 849 | #bottom side | ||
| 850 | $found = 0; | ||
| 851 | for(my $row = 200; $row > 0; $row--){ | ||
| 852 | for(my $col = 0; $col <= 100; $col++){ | ||
| 853 | if($structPlot[$row][$col]){ | ||
| 854 | $bottomSide = $row; | ||
| 855 | $found = 1; | ||
| 856 | last; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | if($found){ | ||
| 860 | last; | ||
| 861 | } | ||
| 862 | } | ||
| 863 | |||
| 864 | #print plot to output file | ||
| 865 | for(my $row = $topSide; $row < $bottomSide + 1; $row++){ | ||
| 866 | for(my $col = $leftSide - 3; $col < $rightSide + 3; $col++){ | ||
| 867 | if($structPlot[$row][$col]){ | ||
| 868 | print(OUTPUT $structPlot[$row][$col]); | ||
| 869 | } | ||
| 870 | else{ | ||
| 871 | print(OUTPUT " "); | ||
| 872 | } | ||
| 873 | } | ||
| 874 | print(OUTPUT "\n"); | ||
| 875 | } | ||
| 876 | print(OUTPUT "\n"); | ||
| 877 | } | ||
| 878 | close(OUTPUT); | ||
| 879 | } | ||
| 880 | |||
| 881 | return 1; | ||
| 882 |