in reply to Re: Compare hash with arrays and print
in thread Compare hash with arrays and print

Thanks for your reply!

I am still not able to print anything to my files.

my %hash = &readFile(); my @fasta = glob('Data_Test/*.fa'); foreach my $f (@fasta){ open FILE, $f or die "Cannot open $fastaname for reading: $!\n"; local $/ = "\n>"; while (<FILE>) { s/^>//mg; my ($name) = /^(\w+)/; if($hash{$name} == 10){ select FILE1; } if($hash{$name} == 20){ select FILE2; } if($hash{$name} == 30){ select FILE3; } print ">$_"; } } close(FILE1); close(FILE2); close(FILE3);

Replies are listed 'Best First'.
Re^3: Compare hash with arrays and print
by almut (Canon) on Jul 12, 2010 at 19:44 UTC

    Add some debugging prints (print STDERR ... preferably, as you're manipulating the default output file handle) to figure out what's different from the sample I've given (which does work fine for me).

    For example, print the records ($_), $name, the corresponding hash values $hash{$name}, etc.  Are they what you'd expect?

      I added some print statements, and now I am able to get the required contents in the output files. Thanks a lot!

      Also, I have some mixed fasta header data. For eg:

      >aw1.a1 bhi|tn|56564 pairs:40098 ATGCTAGATGCTAGCTAGCTAGCACTGAT CGATGCTAGCGTAGTCAGCTGATGCTGTA CGATGCTAGTCGTACG >aw1.b1 bhi|tn|56565 pairs:40099 CGAGCTAGTCGTAGTCGTGATGCTGATTA CGATGCTAGTCGTAGCTAGCTGATGCTGC CGATGCTAGTCGTAGTC >dd3.a1 bhi|tn|56566 pairs:40100 CGTAGTCGTAGTCGTAGTCGATGCTGATG GCTAGTCGATGCTAGCTAGTCGATGCTGG CGATGCTGAT >dd3.b1 bhi|tn|56567 pairs:40101 CGTAGTCGTAGTCGTACGTAGTCGTGAGT CGATTATTTAGGAGGGACAAGGATAGTA >hg5.a1 bhi|tn|56568 pairs:40102 CGTAGTCGTAGTCTAGTCGTGATGCTAGA >dfd6.a1 bhi|tn|56569 pairs:40103 CGATGCTACGTACGTAGTCAGTCGTGATG AATTAGAGCAGATAGAGGGGGAAAGGGTT AAACCCC >ght5.a1 bhi|tn|56564 ATGCTAGTCGTAGTCGATGTCGTAGCTGT CGTAGCTGATGCATGCTAGTCCGTAGCTG >tgt6.X bhi|tn|56564 pairs:56478 CGTAGCTGATGCTGATGCTGATGCTGTGT CGTAGCTGATGCTGATGCTTAGCTGATGC CGTAGCTGATCGTAGCTATCGTAGCTAGG >tgt6.Y bhi|tn|56564 pairs:56479 CGTAGTCGTAGTCGTAGTCGATGCTAGTG CGATGCTGATCGTGATGCTATGCTAGCGT CAGTCGTAGTCGTACGTAGTCGTGTGTGG

      I want to write the complete header line (starting with '>') in my output files.

      Can I use the split function to divide the header line into an array and then try printing it?

      Thanks!

        I want to write the complete header line (starting with '>') in my output files.  Can I use the split function ...?

        Sure you can use the split function, but if you just want to print the header line as is (i.e. copy it from the input), you wouldn't need to split up the record. As you have it, the header would be printed already without further ado.  Think of it this way: $_ holds an entire record, with the leading '>' removed (to more easily handle the edge cases that result from the way the input is being split by $/).  For example, for the first record, $_ would hold the string (including the newlines)

        "aw1.a1 bhi|tn|56564 pairs:40098 ATGCTAGATGCTAGCTAGCTAGCACTGAT CGATGCTAGCGTAGTCAGCTGATGCTGTA CGATGCTAGTCGTACG "

        You can do with it whatever you like before you print it out, e.g. take it apart using split or via regex captures, perform regex substitutions on it, etc.


        Some more notes: The key for the hash is extracted via regex capture

        my ($name) = /^(\w+)/;

        which would extract "aw1" in this case, because \w+ stops matching at the dot. In case you'd need to extract keys as "aw1.a1" (or some such — I'm no fasta expert), you could modify the regex to also capture the dots

        my ($name) = /^([\w.]+)/;

        or up until the first whitespace char in the line

        my ($name) = /^(\S+)/;

        Or in case you'd want to print the headers only (which I'm not quite sure from your description), you could extract it similarly with a regex

        my ($header) = /^([^\n]+)/;

        or by splitting on newlines

        my ($header) = split /\n/;

        And so on...
        Hope this gives you some starting points to tailor it to your specific requirements.

Re^3: Compare hash with arrays and print
by Marshall (Canon) on Jul 12, 2010 at 23:19 UTC
    update: oops I do see that you do have a print statement. and looks like it should work.

    This "select FILE3;" statement by itself does nothing useful. I actually wouldn't use select at all in this situation.

    The Perl print statement is a "smart" critter. If the first arg of print is the file handle of some open file, Perl will print to that file handle. print FILE3 "abc"; will print "abc" to FILE3. The Perl default is essentially "print stdout "abc";". Select makes the default print go to wherever you want (instead of stdout), but here it appears easier to just put the file handle in the print statement.

    print FILE3 $_; or similar will work fine. A plain "print;" sends $_ to the default file handle. With a file handle specified, I think you have to explicitly say $_ for the same effect.