Question: Perl Script To Make Statistics Of Mirna Abundances For Many Samples
0
gravatar for biolab
4.9 years ago by
biolab1.1k
biolab1.1k wrote:

HI everyone,

I am making statistics of mirna abundances for many samples. Below is an example for input file.

SAMPLE    MIR    ABUNDANCE
sample1   mir1   30
sample1   mir3   100
sample1   mir4   120
sample2   mir1   40
sample2   mir2   200
sample3   mir1   190

......

I need to change the directions of tthe above matrix, and an ideal output is like below.

          sample1    sample2    sample3
mir1      30           40         190
mir2      0            200         0
mir3      190          0           0
mir4      120          0           0
......

i tried to write perl hash of hash to sort out the problem (see below). However, I am new with this relatively complex hash. Could anyone provide suggestions. I believe it's a good stuff to learn for other perl beginners too. Thank you very much!

open FH, '<', $ARGV[0] or die "open failed:$!";
my %h;
while (<>){
        my ($sample, $mir, $abun) = /(.+?)\t(.+)\t(.+)/;
        $h{$sample}{$mir} = $abun; 
}
foreach my $sample (keys %h){
        foreach my $mir (keys %{h{$sample}})
                print "   "      # i am stuck here. Need your help!
}
perl mirna • 2.3k views
ADD COMMENTlink modified 4.6 years ago by Biostar ♦♦ 20 • written 4.9 years ago by biolab1.1k
5

May I ask, is perl an absolute requirement? What you are looking for can be accomplished by a few commands using Pandas in Python, and I am happy to provide code for you if you would like to.

ADD REPLYlink written 4.9 years ago by ericmajinglong100
2

I agree with eric. Though it can be done with perl, it can more easily be accomplished by other means. In my experience, knowing how to work with R is very useful for numerical analyses. Have a look into the aggregate() function.

ADD REPLYlink modified 4.9 years ago • written 4.9 years ago by Irsan6.8k

Thanks Eric and Irsan, I agree. Learning R is really necessary.

ADD REPLYlink written 4.9 years ago by biolab1.1k
7
gravatar for Neilfws
4.9 years ago by
Neilfws48k
Sydney, Australia
Neilfws48k wrote:

I realise that you asked for a Perl solution but as others noted in the comments, sometimes it's good to know that the right tool for the job exists already.

With that in mind, here is acast() from the R/reshape2 package.

library(reshape2)
mi <- read.table("mi.txt", header = T, stringsAsFactors = F)
acast(mi, MIR ~ SAMPLE)

#      sample1 sample2 sample3
# mir1      30      40     190
# mir2      NA     200      NA
# mir3     100      NA      NA
# mir4     120      NA      NA
ADD COMMENTlink written 4.9 years ago by Neilfws48k

Thanks a lot for all solutions. They are really helpful!

ADD REPLYlink written 4.9 years ago by biolab1.1k
4
gravatar for umer.zeeshan.ijaz
4.9 years ago by
Glasgow, UK
umer.zeeshan.ijaz1.7k wrote:

Okay quickly wrote this perl one-liner for you assuming that your input file is tab-delimited (just redirect the output to another file):

<script src="&lt;a href=" 10400852"="">10400852"></script>

Best Wishes, Umer

ADD COMMENTlink written 4.9 years ago by umer.zeeshan.ijaz1.7k
3

This rather pushes the definition of "one liner" :)

ADD REPLYlink written 4.9 years ago by Devon Ryan88k

saves u from one "wget" though ;)

ADD REPLYlink written 4.9 years ago by umer.zeeshan.ijaz1.7k

This answer doesn't make sense.

ADD REPLYlink written 4.6 years ago by Alex Reynolds27k

Looks like something got lost in the transition to the new biostars.

ADD REPLYlink modified 4.6 years ago • written 4.6 years ago by Devon Ryan88k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1723 users visited in the last hour