This is a test version of Biostars. For the public version, visit https://www.biostars.org.
extract data from txt files

I want to extract gene expression values from a txt file based on gene names.

For example:

A 1500
B 2500
C 3500
D 3400

I want to extract A and C gene expression values using gene name file extract it.

A 1500
C 3500

I have written some perl code, but it is not work well. Could you help me modify it or give me a new code?

Thanks,

#!/usr/bin/perl -w
use strict;
open(IN,"file1");
open(NAME,"file2");
open(OUT,">result");
while(<IN>){
  chomp;
  @data=split / /,$_;
  $hash{$data[0]}=$data[1];
}
while(<NAME>){
    chomp;
    print OUT "$_ $hash{$_}\n";
}
close IN;
close NAME;
close OUT;
rna-seq

Please note that I vote for Pierre's answer! Nonetheless, I will try to shed some light on your programming experiments.

I am not sure, that

@data=split / /,$_;

works as you expect. I believe / / really tries to match $_ against the regular expression containing a single space. Using some parenthesis for your code, one gets:

@data=split( $_ =~ / /, $_ );

The reg-ex matching returns the result by means of 1 and '' (or something similar) which evaluate to boolean TRUE and FALSE in Perl. Consequently, Perl will evaluate the statement to:

@data=split('', $_);

or

@data=split('1', $_);

depending on the content of $_, i.e., does $_ contain a single space. Finally, the line will be splitted by '1' or '' which is not what you want, right?!

Tip 1:

Try to debug your script using Data::Dumper but be aware that you might get a long list of results:

#!/usr/bin/perl -w
use strict;
use Data::Dumper;

open(IN,"file1");
open(NAME,"file2");
open(OUT,">result");
while(<IN>){
  chomp;
  @data=split / /,$_;
  $hash{$data[0]}=$data[1];
}
print Dumper(\%hash);
while(<NAME>){
    chomp;
    print OUT "$_ $hash{$_}\n";
}
close IN;
close NAME;
close OUT;

Tip 2:

If you really want to do it by yourself, think about loading the file into memory which is smaller (I guess these are the names). Applying your script to very large data sets may eventually crash your computer.

Tip 3:

Really try to go for Pierre's solution ;-)

1 answer

In the shell:

for i in `cat file2` ;
do awk '$1eq"$i" {print } ' file1 >> result ;
done ;

Using $i like this in awk looks like a bad idea. If both file1 and file2 have too many lines, your solution will take a long time.

True. Didn't know about linux's join command so thanks for that link.

I use this shell only get the name. I cannot get the gene expression values.

Log in to answer this question.