Okay, and here is the code:gi1 VERSION cds1 gi2 VERSION cds2 . . . . . . . . .
and for this file:#!/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();
--this is what we get: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" //
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 "").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)
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:
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)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 = (); };
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!In reply to Re^14: Read, match string and print
by sophix
in thread Read, match string and print
by sophix
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |