lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I have a method that returns two values that are supposed to hold the contents of a file.
When I insert the returned values into my db only the file names appears in the table fields.
The datatype for the fields are text.

My question is how do I get the file contents into those fields?
sub msa{ # perform clustalw multiple sequence alignment my ($maid, $maid_dir)=@_; my $contig1 = "$maid_dir\\$maid"."contig1"; my $contig1_out = "$maid_dir\\$maid"."contig1.aln"; my $contig2 = "$maid_dir\\$maid"."contig2"; my $contig2_out = "$maid_dir\\$maid"."contig2.aln"; my $out1 = "$maid_dir\\$maid"."contig1.txt"; my $out2 = "$maid_dir\\$maid"."contig2.txt"; if($contig1=~ m/contig1$/){ my @command_contig1 = ('c:\\Clustalw\clustalw2.exe', $contig1, + '/align', "/outfile=$contig1_out"); system(@command_contig1); } if($contig1=~ m/contig2$/){ my @command_contig2 = ('c:\\Clustalw\clustalw2.exe', $contig2, + '/align', "/outfile=$contig2_out"); system(@command_contig2); } return ($out1, $out2); }

Any help will be greatly appreciated!

Replies are listed 'Best First'.
Re: Trouble getting the file contents from return values
by lostjimmy (Chaplain) on Jun 12, 2009 at 00:11 UTC

    The two values you are returning are simply the names of those files, and not actually the contents. You are setting $out1 and $out2 as the name you want for the output files, then telling clustalw2 to use those names for the output files, but you're missing the step where you read those files into your program.

    You can do something like the following before you return (note: untested code):

    my ($result1, $result2); { local $/; open my $fh1, "<", $out1 or die "Could not open $out1: $!"; $result1 = <$fh1>; open my $fh2, "<", $out2 or die "Could not open $out2: $!"; $result2 = <$fh2>; } return ($result1, $result2);

    As a side note, I'm a little confused by some of the things you are doing. Firstly, what is the point of if ($contig1 =~ m/contig1$/)? Don't you already know that $contig1 ends with "contig1", since you set it at the beginning of the sub? Secondly, you never use the $contig*_out variables. Did you mean to do something with them?

      Hi lostjimmy!
      First, the logic was to find the file by that name. You may be correct about the logic
      because I already have the file info passed in.

      Second, the $contig1_out is used as the output file for clustalw. The aln file is what I want to insert into the db.

      I will try your suggestion.
      Thanks!
Re: Trouble getting the file contents from return values
by toolic (Bishop) on Jun 12, 2009 at 00:05 UTC
    It seems your sub returns the contents of two scalar variables, $out1 and $out2, which you set to be equal to two strings which look like file names. Look at the output of print:
    my $out1 = "$maid_dir\\$maid"."contig1.txt"; my $out2 = "$maid_dir\\$maid"."contig2.txt"; print "out1=$out1\n" print "out2=$out2\n"

    Update: it is also generally a good idea to check the success of system calls.

      Hello Toolic!

      My system call works. My issue is getting the contents of the file for inserting into my db table field. Currently, all I am inserting is the file name.
Re: Trouble getting the file contents from return values
by Anonymous Monk on Jun 12, 2009 at 00:07 UTC
    Hello, I have a method that returns two values that are supposed to hold the contents of a file. When I insert the returned values into my db only the file names appears in the table fields.

    So your method returns filenames instead of file contents? Maybe you want to read perlopentut.

    The datatype for the fields are text. My question is how do I get the file contents into those fields?

    What kind of database is it?

      The db is a mysql. I am storing genomic data. I am trying to insert output from an multiple sequence alignment in clustalw format.