Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

reading and working with grow.out files

by rmgzsm9 (Novice)
on Apr 23, 2012 at 23:34 UTC ( #966695=perlquestion: print w/replies, xml ) Need Help??
rmgzsm9 has asked for the wisdom of the Perl Monks concerning the following question:

I have a grow.out file showing protein ligand interaction. I converted that file into text file. It looks like:

H P L A 143 TYR 202 OH --> O2 2014 MC9 500 A H P L A 143 TYR 202 OH <-- O2 2014 ASH 500 A H P L A 237 SER 532 OG --> O1 2015 MC9 500 A H P L A 237 SER 532 OG <-- O1 2015 AGM 500 A H P L A 274 ARG 821 NH1 --> O1 2015 MC9 500 A H P L A 278 SER 851 OG --> O2 2014 VIA 500 A

Now what I want is that the program should search for a particular ligand code (Say MC9 here). After searching for MC9 the program should print each line (row) containing MC9 into another output text file. I am a drug design student with biotechnology background. so don't have much knowledge on programming. Please help me. I have a code with me but is not working as the way I wanted.

#!/usr/bin/perl -w use strict; use IO::File; use constant FILE => 'search.txt'; use constant FIND => 'string to find'; IO::File->input_record_separator(FIND); my $fh = IO::File->new(FILE, O_RDONLY) or die 'Could not open file ', FILE, ": $!"; $fh->getline; print IO::File->input_record_separator while $fh->getline; $fh->close;

it is printing MC9 again and again, not the whole row

Replies are listed 'Best First'.
Re: reading and working with grow.out files
by Riales (Hermit) on Apr 23, 2012 at 23:51 UTC

    Is that the code you're actually running? I'm thinking your FIND constant is actually defined as 'MC9' so when you execute the following line, you just print the input_record_separator (which you earlier defined as FIND) once for each line your script reads in.

    print IO::File->input_record_separator while $fh->getline;

    I'm not familiar with IO::File but that's my best guess as to what you're seeing.

    Something like this should work for your purposes:

    use strict; use warnings; my $filename = 'search.txt'; my $find = 'MC9'; open(FILE, '<', $filename) or die "Cannot open file: $!"; while (my $line = <FILE>) { print $line if $line =~ /$find/; } close(FILE);

       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. 

        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

      Thanks I ll try it now

Re: reading and working with grow.out files
by toolic (Bishop) on Apr 23, 2012 at 23:52 UTC
      will that work with windows?
        Did you try it? If it's installed, it should work.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966695]
Approved by ww
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (1)
As of 2018-07-23 06:11 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (459 votes). Check out past polls.