in reply to Re^12: Read, match string and print
in thread Read, match string and print

I guess that's because the VERSION information never gets set, or maybe gets set only once. But only you can tell that because you are the only one who has the input file. Consider looking at your input file, printing out each line of the input file, and then also printing out what the script does (ignore, capture GI info, capture CDS info, capture VERSION info), and also outputting the current state as it is kept in %info (using Data::Dumper for example). Then you will understand where your program does the wrong thing.

Replies are listed 'Best First'.
Re^14: Read, match string and print
by sophix (Sexton) on Feb 10, 2010 at 16:06 UTC
    Indeed, the culprit is the VERSION which appears only once and needs to be printed everytime along with the other two variables. So it should look like:
    gi1 VERSION cds1 gi2 VERSION cds2 . . . . . . . . .
    Okay, and here is the code:
    #!/usr/bin/perl use strict; use warnings; my %info = ('gi' => "", 'version' => "", 'cds' => ""); # here we collect all information sub flush_info { # print out all information: print "$info{gi}"."\t"."$info{version}"."\t"."$info{c +ds}\n"; %info = (); }; my $data = '/DATA/GenBankFile.gb'; # GenBank file is located at C:\DAT +A open (INFILE, '<', $data) or die "Cannot!\n"; while (<INFILE>) { last if m!//$!; if (m!^VERSION.*\w:(\d+)! ) { $info{version} = $1; } elsif (m!GI:(\d+)!) { if ($info{cds}) { # we are in a CDS block $info{gi} = $1; }; } elsif (m!^\s+CDS\s+(.*)!) { # a new gene information has started flush_info(); # now remember the CDS $info{cds} = $1; } else {warn "Ignoring unknown line [$_]\n";}; }; # Output any leftover information: flush_info();
    and for this file:
    LOCUS NC_0000230 600020 bp DNA linear CON 21- +APR-2007 VERSION NC_000023.10 GI:123456789 CDS join(11111..222222,333333..444444) /db_xref="GI:55555555" CDS join(66666..7777777,888888..99999) /db_xref="GI:10101010" //
    --this is what we get:
    Ignoring unknown line [LOCUS NC_0000230 600020 bp D +NA linear CON 21-APR-2007 ] Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf16.pl line 12, <INFILE> line 5. Use of uninitialized value $info{"version"} in string at C:\Perl\bin\w +tf16.pl line 12, <INFILE> line 7. 123456789 55555555 join(11111..222222,333333..444444) 10101010 join(66666..7777777,888888..99999)
    First line is ignored as expected. Then it founds the VERSION and keep its value. Once it made its way to flush_info(), it gets printed without waiting for the other two variables, and becomes empty. Since there is no other VERSION for the remainder of the code, it never gets printed (well, it prints "").

    I thought of two ways to overcome this problem, but either they did not work out or I did not implement them properly.

    1. Within the sub routine, I introduced a next along with an if statement as follows:

    sub flush_info { # print out all information: next print "$info{gi}"."\t"."$info{version}"."\t"."$inf +o{cds}\n" if ($info{gi} or $info{version} or $info{cds} eq undef; %info = (); };

    Idea was to skip printing unless all three variables are defined. By doing this, I intended to solve the problem with the first line of output (i,e. only printing one variable)

    2. Within the while loop, I tried to set the VERSION permanently, but I could not do it. I used matching =~ as an assignment and alternatively introduced a fourth variable to keep the value of the VERSION to use this value for printing purposes.

    No progress. =) I am stucked once again, hooray!