http://www.perlmonks.org?node_id=966749


in reply to Re^2: reading and working with grow.out files
in thread reading and working with grow.out files

Hi, take a look at this code:
use strict; use warnings; use Carp qw(croak); { my $input_file = "input_file.txt"; my @lines = slurp($input_file); for my $line (@lines){ my ($filename, $ligand) = split(/\t/, $line); open(FILE, '<', $filename) or die "Cannot open file: $!"; while (my $line = <FILE>) { print $line if $line =~ /$ligand/; } close(FILE); } } ##Slurps a file into a list sub slurp { my ($file) = @_; my (@data, @data_chomped); open IN, "<", $file or croak "can't open $file\n"; @data = <IN>; for my $line (@data){ chomp($line); push (@data_chomped, $line); } close IN; return (@data_chomped); }
It assumes that the list of text files and ligand codes are in a tab delimited file called input_file.txt. It will look like this
file1.txt MC9 file2.txt ASH
You can also delimit it by ',' or any other delimiter, but you will have to update the split function
The program will match each ligand code to the given file and print out the lines where there is a match
Good luck with your research
Mr Guy

Replies are listed 'Best First'.
Re^4: reading and working with grow.out files
by rmgzsm9 (Novice) on Apr 24, 2012 at 07:22 UTC

    It worked. Superb. 
     Thank you so much. 
     
     Cheers 
     Miss Girl 

Re^4: reading and working with grow.out files
by rmgzsm9 (Novice) on Apr 24, 2012 at 07:39 UTC

    Can I ask you for a stupid type of help? Can you explain me the program as I am new to Perl programming. I just need to know how sub-function is working?

    Thanks and Regards, 
     Rmgzsm9
      The sub function basically receives the file as input, and then parses the data into an array, or list. The list is returned as output and is easier to go over.
      This type of file parsing is called slurp

      Also, if you want to get better at Perl, I highly recommend this book.
      It really helped me out when I started learning Perl, and it will give you many valuable tools when you need to process biological data.
      Good luck
      Mr Guy

        Thanks

Re^4: reading and working with grow.out files
by rmgzsm9 (Novice) on Apr 29, 2012 at 13:12 UTC

    Can I ask you for little more help? The program helped me so much. Thanks for that. Now I need to modify it a bit more. I tried myself but got stuck at one point. Can u help me out?

     
    so now I don't want to print the line containing ligand name , rather now i want to count no of lines and want to count amongst those lines, the lines containing H as a 1st character of each row. I successfully counted the lines by using following code
    
    
    # changed the code from line 10 of above written code while (my $line= <FILE> { $ count=0; if $line=~/$ligand/; { $count++;} } Print"total no of interaction are $count"; }close (FILE); }

    this counts only no of lines. Now, I want while the program is reading code line by line it should check the first character and if it is H the it should count that line and put the count in another variable say count2 or something.

     
     Please help 

Re^4: reading and working with grow.out files
by rmgzsm9 (Novice) on Apr 29, 2012 at 14:00 UTC

    I made this program, but it is giving so many errors. Can you help me in debugging it.

    use strict; use warnings; use Carp qw(croak); { my $input_file = "input_file.txt"; my @lines = slurp($input_file); for my $line (@lines){ my ($filename, $ligand) = split(/\t/, $line); open(FILE, '<', $filename) or die "Cannot open file: $!"; while (my $line = <FILE>) { if ($line =~ /$ligand/) { $count++; $first=substr($line, 0,1); if($first=='H'); { $sum++; } } print "Total No. of interactions are $count"; print " No. of Hydrogen bonds are $sum"; } close(FILE); } } ##Slurps a file into a list sub slurp { my ($file) = @_; my (@data, @data_chomped); open IN, "<", $file or croak "can't open $file\n"; @data = <IN>; for my $line (@data){ chomp($line); push (@data_chomped, $line); } close IN; return (@data_chomped); }

      The messages you receive tell exactly what's wrong:

      Global symbol "$count" requires explicit package name at test.pl line +14. Global symbol "$first" requires explicit package name at test.pl line +15. Global symbol "$first" requires explicit package name at test.pl line +16. syntax error at test.pl line 16, near ");" Global symbol "$sum" requires explicit package name at test.pl line 18 +. Global symbol "$count" requires explicit package name at test.pl line +21. Global symbol "$sum" requires explicit package name at test.pl line 22 +.

      Define $count, $first and $sum, like the other variables in your script. For example, add the following line in the appropriate place:

      my ($count, $first, $sum);

      Next, No semi colon required here:

      if ($first=='H'); {

      Should be:

      if ($first=='H') {

      I suggest spending some time working on the basics:

        Thanks. It worked. Not exactly I wanted, but it worked