lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I am having trouble getting my if-elsif-else to function as I would like.
The first part returns the name and percent_identity, but not the
actual dereferenced object. I need the name,percent_identity, and
the dereferenced glob object.
my $in = new Bio::SearchIO(-format => 'blast', -file => DATA, -report_type => 'blastn'); while(my $result = $in->next_result){ if(my $hit = $result->next_hit){ if(my $hsp = $hit->hsp('best')){ if(my $hu_name = $hit->name){ open (my $hu_out,">".$maid_dir."\\".$maid."_hu_par +sed.out"); $hu_name =~ /^>.*HU/; my $hu_Percent_id = $hsp->percent_identity; print $hu_out "Name: ", $hu_name, "\n"; + print $hu_out "Percent Identity: ", $hu_Percent_i +d, "\n"; my $hu_aln = $hsp->get_aln; #my $hu_aln_ref = \$hu_aln; my $alnIO = Bio::AlignIO->new(-format =>"clustalw" +, -file => ">$hu_out"); $alnIO->write_aln($hu_aln); print $hu_out "Alignment: ", $hu_aln, "\n"; close $hu_out; } elsif(my $hd_name = $hit->name) { open (my $hd_out,">".$maid_dir."\\".$maid."_hd_par +sed.out"); $hd_name = ~ /^>.*HD/; my $hd_Percent_id = $hsp->percent_identity; print $hd_out "Name: ", $hd_name, "\n"; print $hd_out "Percent Identity: ", $hd_Percent_i +d, "\n"; my $hd_aln = $hsp->get_aln; #my $hd_aln_ref = \$hd_aln; my $alnIO = Bio::AlignIO->new(-format =>"clustalw" +, -file => ">$hd_out"); $alnIO->write_aln($hd_aln); print $hd_out "Alignment: ", $hd_aln, "\n"; close $hd_out; } else { last; } } } } __DATA__ BLASTN 2.2.20 [Feb-08-2009] Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaff +er, Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), "Gapped BLAST and PSI-BLAST: a new generation of protein database sear +ch programs", Nucleic Acids Res. 25:3389-3402. Query= i:\13426 Chchd1 (22)\sequencing\13426_fasta.Contig1 (2477 letters) Database: c:\blast_temp\hu_hdBLASTdb 2 sequences; 4150 total letters Searching..................................................done Score + E Sequences producing significant alignments: (bits +) Value 13426|HU +868 0.0 13426|HD + 24 0.50 >13426|HU Length = 2075 Score = 868 bits (438), Expect = 0.0 Identities = 441/442 (99%) Strand = Plus / Minus + Query: 1163 ccattttgcacagcgttcccagtaggctccgcggcgccgtgcgtgacccctcttgacc +tc 1222 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1076 ccattttgcacagcgttcccagtaggctccgcggcgccgtgcgtgacccctcttgacc +tc 1017 + Query: 1223 tacgcgcttggcgtgcggacaacgggcagctgtgcgcgcactttccgggtcttggcct +ca 1282 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1016 tacgcgcttggcgtgcggacaacgggcagctgtgcgcgcactttccgggtcttggcct +ca 957 + Query: 1283 cggtgacactggattcgaaccccgtcgggccggcagccgacagcaggttaggatgcta +ta 1342 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 956 cggtgacactggattcgaaccccgtcgggccggcagccgacagcaggttaggatgcta +ta 897 + Query: 1343 ggttgtgcgcgccctttgcgtgctcgggacccggcaggattcaggggccccgtgtccg +cg 1402 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 896 ggttgtgcgcgccctttgcgtgctcgggacccggcaggattcaggggccccgtgtccg +cg 837 + Query: 1403 gctgccacttatttgtgagtcggtgtctgcgtgtgggcaccacgtgagtgccgcgtcc +gt 1462 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 836 gctgccacttatttgtgagtcggtgtctgcgtgtgggcaccacgtgagtgccgcgtcc +gt 777 + Query: 1463 ggcggccaggggaaggcgttggatcccatggggctggagttaaggattttggattgca +aa 1522 ||||||||||||||||||||||||||| |||||||||||||||||||||||||||||| +|| Sbjct: 776 ggcggccaggggaaggcgttggatcccgtggggctggagttaaggattttggattgca +aa 717 + Query: 1523 ggacttttgttttaacaagtctgtgagaatgatgattccttttgccaggcggtggtga +cg 1582 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 716 ggacttttgttttaacaagtctgtgagaatgatgattccttttgccaggcggtggtga +cg 657 Query: 1583 ctcgcctttaaatcccggcact 1604 |||||||||||||||||||||| Sbjct: 656 ctcgcctttaaatcccggcact 635 Score = 63.9 bits (32), Expect = 6e-013 Identities = 70/80 (87%), Gaps = 2/80 (2%) Strand = Plus / Minus + Query: 1731 tgctttttacatctcttacatggcctgctatgtagatcctcgttactcgtttgcaatg +aa 1790 ||||||||||| ||||||||||| |||||| |||||| || ||||||||||| |||| + | Sbjct: 508 tgctttttacaactcttacatggactgctaggtagatactgtttactcgtttggaatg +-a 450 Query: 1791 tcgcaccagtagggggaacg 1810 ||||| |||||||| ||||| Sbjct: 449 tcgca-cagtagggtgaacg 431 Score = 28.2 bits (14), Expect = 0.032 Identities = 14/14 (100%) Strand = Plus / Plus Query: 2417 ctttattagatact 2430 |||||||||||||| Sbjct: 24 ctttattagatact 37 Score = 24.3 bits (12), Expect = 0.50 Identities = 12/12 (100%) Strand = Plus / Plus Query: 1587 cctttaaatccc 1598 |||||||||||| Sbjct: 1812 cctttaaatccc 1823 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Minus Query: 1877 tatagacctcc 1887 ||||||||||| Sbjct: 368 tatagacctcc 358 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Plus Query: 1434 tgtgggcacca 1444 ||||||||||| Sbjct: 1931 tgtgggcacca 1941 >13426|HD Length = 2075 Score = 24.3 bits (12), Expect = 0.50 Identities = 12/12 (100%) Strand = Plus / Plus Query: 1587 cctttaaatccc 1598 |||||||||||| Sbjct: 506 cctttaaatccc 517 Score = 24.3 bits (12), Expect = 0.50 Identities = 12/12 (100%) Strand = Plus / Minus Query: 1490 atggggctggag 1501 |||||||||||| Sbjct: 1168 atggggctggag 1157 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Plus Query: 1434 tgtgggcacca 1444 ||||||||||| Sbjct: 625 tgtgggcacca 635 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Plus Query: 2346 ttcatttcctt 2356 ||||||||||| Sbjct: 1299 ttcatttcctt 1309 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Plus Query: 1 tttttgttttt 11 ||||||||||| Sbjct: 1322 tttttgttttt 1332 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Plus Query: 1 tttttgttttt 11 ||||||||||| Sbjct: 1333 tttttgttttt 1343 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Minus Query: 557 gccacatatcc 567 ||||||||||| Sbjct: 1986 gccacatatcc 1976 BLASTN 2.2.20 [Feb-08-2009] Reference: Altschul, Stephen F., Thomas L. Madden, Alejandro A. Schaff +er, Jinghui Zhang, Zheng Zhang, Webb Miller, and David J. Lipman (1997), "Gapped BLAST and PSI-BLAST: a new generation of protein database sear +ch programs", Nucleic Acids Res. 25:3389-3402. Query= i:\13426 Chchd1 (22)\sequencing\13426_fasta.Contig2 (2498 letters) Database: c:\blast_temp\hu_hdBLASTdb 2 sequences; 4150 total letters Searching..................................................done Score + E Sequences producing significant alignments: (bits +) Value 13426|HD +938 0.0 13426|HU + 94 7e-022 >13426|HD Length = 2075 Score = 938 bits (473), Expect = 0.0 Identities = 533/549 (97%), Gaps = 3/549 (0%) Strand = Plus / Minus + Query: 436 atcgtatatatctggctcttaattgcagtggtatcaggctccctgggaataggtgcaa +gg 495 |||||||||||||| |||| ||||||| || ||||||||||||||| | ||| ||||| +| Sbjct: 1905 atcgtatatatctgtctct-aattgcactgctatcaggctccctggtactagttgcaa +g- 1848 + Query: 496 aaggtgtaaggacagcaaggatggtcactgtggaacccaaggtcagtcctctacaaaa +gg 555 ||| ||| ||| |||| ||||||||||||||||||||||| ||||||||||||||||| +| Sbjct: 1847 aagctgtgagg-cagctaggatggtcactgtggaacccaatgtcagtcctctacaaaa +gt 1789 + Query: 556 agcaagtacccttaaatggagtcagctctccagcccctaagtatattaactgtacaaa +ta 615 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1788 agcaagtacccttaaatggagtcagctctccagcccctaagtatattaactgtacaaa +ta 1729 + Query: 616 aaataatatgtatctacagaatctctgggtatcagtatatgcacctgcttcagtaagc +ac 675 |||||||||||||||||||||||||||||||||||||||||||| ||||||||||||| +|| Sbjct: 1728 aaataatatgtatctacagaatctctgggtatcagtatatgcacttgcttcagtaagc +ac 1669 + Query: 676 tttggacagactatttgtgaagtatgccctcactgaatacagctaatggcaaaaaggc +tc 735 |||||||||||||||||||||||||||||||||||||||||||| ||||||||||||| +|| Sbjct: 1668 tttggacagactatttgtgaagtatgccctcactgaatacagctgatggcaaaaaggc +tc 1609 + Query: 736 tgcatacacagacttcattgctacttatgtatctaaaagccaaaggactaaatgatgt +aa 795 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1608 tgcatacacagacttcattgctacttatgtatctaaaagccaaaggactaaatgatgt +aa 1549 + Query: 796 aggacttgaaaggcaacaaaagacattcagacttgctcatcaaaagcatatttggact +gg 855 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1548 aggacttgaaaggcaacaaaagacattcagacttgctcatcaaaagcatatttggact +gg 1489 + Query: 856 gcagtggtggcgcatgcctttaatcccagcacttgggagaaagaggcaggcgatctcg +ga 915 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1488 gcagtggtggcgcatgcctttaatcccagcacttgggagaaagaggcaggcgatctcg +ga 1429 + Query: 916 gtttgcgactggcctggtctacagagtgagttacaggacagccaggaatacacagaga +aa 975 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1428 gtttgcgactggcctggtctacagagtgagttacaggacagccaggaatacacagaga +aa 1369 Query: 976 ctctctttc 984 ||||||||| Sbjct: 1368 ctctctttc 1360 Score = 609 bits (307), Expect = e-177 Identities = 307/307 (100%) Strand = Plus / Minus + Query: 1036 ggaaatgaagagcatcccaggaataaagaacttccagggtttggggagattatagagc +cc 1095 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1307 ggaaatgaagagcatcccaggaataaagaacttccagggtttggggagattatagagc +cc 1248 + Query: 1096 aagaaaggacgtgctcaggttggctcagaggttgggtgcttgcccagtgaagggaaag +gc 1155 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1247 aagaaaggacgtgctcaggttggctcagaggttgggtgcttgcccagtgaagggaaag +gc 1188 + Query: 1156 agagatattcctcttcaggatggggctggaggtgtgaggagaagcagctaattagaag +aa 1215 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1187 agagatattcctcttcaggatggggctggaggtgtgaggagaagcagctaattagaag +aa 1128 + Query: 1216 atggccaacttctttttagaggattttatttcaagacagcatccagaatacgattcat +tc 1275 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1127 atggccaacttctttttagaggattttatttcaagacagcatccagaatacgattcat +tc 1068 + Query: 1276 aaaagagcaaaattaagtattttgcctcattttacccttcaaaaactcgtagcaatgc +aa 1335 |||||||||||||||||||||||||||||||||||||||||||||||||||||||||| +|| Sbjct: 1067 aaaagagcaaaattaagtattttgcctcattttacccttcaaaaactcgtagcaatgc +aa 1008 Query: 1336 atgcaaa 1342 ||||||| Sbjct: 1007 atgcaaa 1001 Score = 93.7 bits (47), Expect = 7e-022 Identities = 102/119 (85%), Gaps = 1/119 (0%) Strand = Plus / Plus + Query: 859 gtggtggcgcatgcctttaatcccagcacttgggagaaagaggcagg-cgatctcggag +t 917 ||||||||||| |||||||||||||||||||||||| ||||||||| ||| || ||| +| Sbjct: 173 gtggtggcgcacgcctttaatcccagcacttgggaggcagaggcaggtggatttctgag +t 232 Query: 918 ttgcgactggcctggtctacagagtgagttacaggacagccaggaatacacagagaaac + 976 | | | | |||||||||||| ||||||| ||||||||||||| ||||||||||||| Sbjct: 233 tcgaggccagcctggtctacaaagtgagtgccaggacagccagggctacacagagaaac + 291 Score = 24.3 bits (12), Expect = 0.50 Identities = 12/12 (100%) Strand = Plus / Minus Query: 391 aaacaaaacaaa 402 |||||||||||| Sbjct: 1324 aaacaaaacaaa 1313 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Plus Query: 2467 cctttcttggg 2477 ||||||||||| Sbjct: 1240 cctttcttggg 1250 >13426|HU Length = 2075 Score = 93.7 bits (47), Expect = 7e-022 Identities = 102/119 (85%), Gaps = 1/119 (0%) Strand = Plus / Plus + Query: 859 gtggtggcgcatgcctttaatcccagcacttgggagaaagaggcagg-cgatctcgga +gt 917 ||||||||||| |||||||||||||||||||||||| ||||||||| ||| || || +|| Sbjct: 1479 gtggtggcgcacgcctttaatcccagcacttgggaggcagaggcaggtggatttctga +gt 1538 + Query: 918 ttgcgactggcctggtctacagagtgagttacaggacagccaggaatacacagagaaa +c 976 | | | | |||||||||||| ||||||| ||||||||||||| |||||||||||| +| Sbjct: 1539 tcgaggccagcctggtctacaaagtgagtgccaggacagccagggctacacagagaaa +c 1597 Score = 38.2 bits (19), Expect = 3e-005 Identities = 28/31 (90%) Strand = Plus / Minus Query: 949 caggacagccaggaatacacagagaaactct 979 |||||| |||||| |||||||||||||||| Sbjct: 569 caggactgccagggctacacagagaaactct 539 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Minus Query: 1223 acttcttttta 1233 ||||||||||| Sbjct: 377 acttcttttta 367 Score = 22.3 bits (11), Expect = 2.0 Identities = 11/11 (100%) Strand = Plus / Minus Query: 1176 tggggctggag 1186 ||||||||||| Sbjct: 748 tggggctggag 738 Database: c:\blast_temp\hu_hdBLASTdb Posted date: May 18, 2009 6:05 PM Number of letters in database: 4150 Number of sequences in database: 2 Lambda K H 1.37 0.711 1.31 Gapped Lambda K H 1.37 0.711 1.31 Matrix: blastn matrix:1 -3 Gap Penalties: Existence: 5, Extension: 2 Number of Sequences: 2 Number of Hits to DB: 531 Number of extensions: 26 Number of successful extensions: 26 Number of sequences better than 10.0: 2 Number of HSP's gapped: 24 Number of HSP's successfully gapped: 24 Length of database: 4150 X1: 10 (19.8 bits) X2: 15 (29.7 bits) X3: 50 (99.1 bits) S1: 10 (20.3 bits) S2: 10 (20.3 bits)
I would appreciate your help in getting the correct output to my 2 out files.

Replies are listed 'Best First'.
Re: Problem with getting the right object
by chromatic (Archbishop) on May 18, 2009 at 23:09 UTC

    I'm not sure you've given us the right information to help you debug this, but a few lines of your code confuse me.

    When do you expect this condition to be false?

    if(my $hsp = $hit->hsp('best')){

    When do you expect this condition to be false?

    if(my $hu_name = $hit->name){

    ... and when do you expect it to differ from this condition?

    elsif(my $hd_name = $hit->name)
      Hello Chromatic!
      I have revised it to make it simpler. I need to parse a blast report. I am using an if/else to print to 2 different
      out files. $hd_out should have:
      Name: 13426|HD
      Percent Identity: 97.0856102003643
      Alignment: Bio::SimpleAlign=HASH(0x4012f2c)

      and $hu_out should have:
      Name: 13426|HU
      Percent Identity: 99.7737556561086
      Alignment: Bio::SimpleAlign=HASH(0x401965c)

      Where "Bio::SimpleAlign=HASH(object)" is should have the actual data, which is an alignment.
      I am confused about dereferencing the hash object since it is a scalar($hu_aln, $hd_aln) in my code


      I am expecting to get two files. Each with the name, Percent ID, and the sequence alignment.
      Your help is appreciated!
      LomSpace

        $hu_aln and $hd_aln contain Bio::SimpleAlign objects. If you read the linked documentation, you may find that one of the methods of that class gives you access to the data you need. It doesn't support a default stringification, so you can't print the object directly and get what you expect. (I don't know what you expect, so I can't tell you which method is likely to help.)

Re: Problem with getting the right object
by ELISHEVA (Prior) on May 18, 2009 at 23:29 UTC

    When your post contains a very long code sample (and this is several pages), it a good idea to surround the code section with <readmore> tags, like this:

    <readmore> <code> ... my code here ... </code> </readmore>

    Using readmore tags can help people skimming the SOPW page focus on your question rather than being overwhelmed by the long code.

    Best, beth

Re: Problem with getting the right object
by toolic (Bishop) on May 18, 2009 at 23:43 UTC
    A style note completely unrelated to your problem. You can interpolate your variables into quotes in a number of spots. For example,
    print $hu_out "Name: ", $hu_name, "\n";

    can be simplified as:

    print $hu_out "Name: $hu_name\n";

    Also, it is good practice to check the success of open.

      Hi Toolic!
      Open works. I will reformat the post using 'read more' tags.
      Can you help me get the two files with the variables that I want? Also, I am getting a list of glob objects, which contain the two alignment strings. I want to return the alignment strings to the hu_aln and and hd_aln respectively. I appreciate your help.