Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hi all,

I have this large text file in the format shown below -
ID SNP FT SNP 433 FT /note="refAllele: T SNPstrains: 7083_1#5=C 7414_8#8=C 7480_8#4 +9=C " FT /colour=1 FT SNP 442 FT /note="refAllele: T SNPstrains: 7065_8#2=C 7065_8#94=C 7083_1# +2=C 7083_1#3=C 7083_1#41=C 7083_1#42=C 7083_1#43=C " FT /colour=1 FT SNP 460 FT /note="refAllele: T SNPstrains: 7564_8#14=C " FT /colour=1 FT SNP 703 FT /note="refAllele: G SNPstrains: 7521_5#39=A (non-synonymous) ( +AA Ala->Thr) " FT /colour=2 FT SNP 937 FT /note="refAllele: G SNPstrains: 7414_8#30=T (non-synonymous) ( +AA Val->Leu) " FT /colour=2 FT SNP 1269 FT /note="refAllele: G SNPstrains: 7480_7#22=A (synonymous) 7480_ +7#62=A (synonymous) " FT /colour=3 FT SNP 1804 FT /note="refAllele: T SNPstrains: 7414_7#66=A (non-synonymous) ( +AA Ser->Thr) 7414_8#44=A (non-synonymous) (AA Ser->Thr) 7521_6#54=A ( +non-synonymous) (AA Ser->Thr) " FT /colour=2 etc etc...
And this code-
use strict; use warnings; use feature qw(say); my $file = ""; my %cod = ( 1 => "red", 2 => "non", 3 => "green" ); open my $in, "<", "$file"; open my $out, ">", "output.txt"; say $out "Coordinate No of Strains AA Change"; my $SNP; my $count; my $change; while ( my $line = <$in> ) { chomp $line; say qq(DEBUG: Line = "$line"); if ( $line =~ /^FT\s+SNP\s+(\d+)/ ){ $SNP = $1; say qq(\$SNP = $1;); } elsif ( $line =~ /^FT\s+\/note="(.*)"/) { my $note = $1; say qq(my \$note = $1); $count = ($note =~ tr/=/=/); $note =~ /\((AA \w+->\w+)\)\s*$/; $change = $1 || ""; } elsif ( $line =~ /^FT\s+\/colour=(\d+)/ ) { say qq(Code = $1); if ( $cod{$1} eq "non" ) { printf $out "%-12.12s %-15.15s %s\n", $SNP, $count, $chan +ge; } } }

However when I run the above code I receive a "Use of uninitialised value ($count or $change) in printf at Script.txt line 33 error. This occurs at any part of the text file that contains a non-synonymous mutation.

This code works on another text file I have, and the only difference I can see is that in this example file, strain numbers have a format such as 7521_5#39=A, whereas in the file this code worked for they are written as 7521_5_39=A, i.e. the '#' is replaced with a second '_'.

The ideal output from this code would look like this-

Coordinates No of Strains AA Change 703 1 AA Ala->Thr 937 1 AA Val->Leu 1804 3 AA Ser->Thr

Any help would be much appreciated, but please be advised I am very new to perl and programming in general. This code is also not my own work - a code that I had written suffered the same error.

Many thanks in advance!


In reply to Parsing problem by MB123

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (11)
    As of 2018-03-21 15:12 GMT
    Find Nodes?
      Voting Booth?
      When I think of a mole I think of:

      Results (268 votes). Check out past polls.