snape has asked for the wisdom of the Perl Monks concerning the following question:
Hi Perl Monks
I have got 2 files where in file1 (sample.txt) it is the list of samples IDs (which is around 1000). These sample IDs are the column name in the file2 (sampleValue.txt). The file2 is a data matrix of 30000*1500. I am interested in values of all the rows in 1000 columns out of 1500 which are like 1,2,5,6,70,71,75,100,112,114 and so on. There is no pattern on the columns. So, here is what I am doing and would like to know how can I improve it. Here is my code:
use strict; use warnings; ## Variables my %sampleID; my %sampleValue; ## Opening first file open my $IN, "sample.txt" or die $!; my $header = <$IN>; while(<$IN>){ chomp $_; my @line = split('\t', $_); $sampleID{$line[0]} = 1; ## Sample ID and Pam50 prediction } close($IN); print "Total number of sample ID: ", scalar(keys %sampleID),"\n"; ## 1 +000 columns ## Sample Value Data open $IN, "sampleValue.txt" or die $!; ## Columns are sample names from file1 $header = <$IN>; my @samples = split("\t", $header); ## print "Total samples: ",scalar(@samples),"\n"; ## 1500 ## loop for all the samples ids or the columns I am interested in for(my $i = 1; $i <= $#samples; $i++){ ## bcos the first instance is c +alled header of the column 1 my $sample = $samples[$i]; $sampleValue{$sample} = $i if (exists $sampleID{$sample}); } my $col = ""; foreach my $key (keys %sampleValue){ $col = $sampleValue{$key}.",".$col; } chop($col); print $col,"\n"; ## string of all the columns I am interested in ## The reason I do the above loop because I don't want to look for the + interested ## cols thru the hash for every line of the file ## Reading the sample Value file while(<$IN>){ chomp $_; print $_,"\n"; my @line = split("\t", $_); @line = @line[ split /,/, $col]; ## previously it was @line = @lin +e[$col] -- and i was getting error because $col is a string print @line,"\n"; }
So, my question is whether there is an easy way to convert the string $col to numeric cols with commas in it or a better way to get the desired columns ?
UPDATE: I have updated my code. May me it will be of some use to people later.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extracting columns from line
by Athanasius (Archbishop) on Oct 23, 2013 at 02:54 UTC | |
by snape (Pilgrim) on Oct 23, 2013 at 02:58 UTC | |
|
Re: Extracting Columns from line
by kcott (Archbishop) on Oct 23, 2013 at 04:41 UTC | |
by boftx (Deacon) on Oct 23, 2013 at 04:59 UTC | |
by snape (Pilgrim) on Oct 23, 2013 at 05:16 UTC | |
|
Re: Extracting Columns from line
by boftx (Deacon) on Oct 23, 2013 at 04:50 UTC | |
|
Re: Extracting Columns from line
by hdb (Monsignor) on Oct 23, 2013 at 07:35 UTC | |
|
Re: Extracting Columns from line
by Lennotoecom (Pilgrim) on Oct 23, 2013 at 05:55 UTC | |
by Don Coyote (Hermit) on Oct 23, 2013 at 12:22 UTC | |
by Anonymous Monk on Oct 23, 2013 at 20:24 UTC | |
by Don Coyote (Hermit) on Nov 10, 2013 at 20:09 UTC |