Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: reading and working with grow.out files

by rmgzsm9 (Novice)
on Apr 24, 2012 at 01:33 UTC ( #966712=note: print w/ replies, xml ) Need Help??


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

 It worked. Thank you so much. 
 Can you help me little bit further? I ll be grateful. 
 This program was for 1 text file and 1 ligand code. I want to make it general. I have a text file having names of all protein text files along with their ligand codes. I want this program to pick first text file name and its respective ligand code and then run this (coded by you) program for that. Then it moves to second text file name and ligand code and repeat it again. Please help. 


Comment on Re^2: reading and working with grow.out files
Re^3: reading and working with grow.out files
by mrguy123 (Hermit) on Apr 24, 2012 at 06:55 UTC
    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

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

      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

      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 

      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:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://966712]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-08-30 13:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (293 votes), past polls