Re-scaling values - perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Re-scaling values - perl
# 1  
Old 10-02-2013
Re-scaling values - perl

Hey folks

I have a big tab delimited file with 3 columns looks like this:

Code:
chr2L	552	0.85
chr2R	135	1.06
chr3L	820	2.89
chr3R	581	3.93
chr4	585	0.94
chrX	605	1.93

All I want to do is re-scaling the third column to be between 0-1. Which means that the highest valu in 3rd column will be 1 and the lowest closest to 0 and the rest in between.

Then sorting it by first and second columns.

Desired output:
Code:
chr2L	552	0.22
chr2R	135	0.27
chr3L	820	0.76
chr3R	581	1.00
chr4	585	0.94
chrX	605	0.50

or

I appreciate if you can help me with this perl script (or shell if it's easier for you, I'm kinda learning both).
# 2  
Old 10-02-2013
Hi,
Can you explain how to calculate value in 3rd column ?
Regards.
# 3  
Old 10-02-2013
Quote:
Originally Posted by @man
I appreciate if you can help me with this perl script (or shell if it's easier for you, I'm kinda learning both).
Have you put forth any effort at all? If so, show us what you've tried so far.

Regards,
Alister

---------- Post updated at 01:02 PM ---------- Previous update was at 12:55 PM ----------

Quote:
Originally Posted by @man
Desired output:
Code:
chr2L	552	0.22
chr2R	135	0.27
chr3L	820	0.76
chr3R	581	1.00
chr4	585	0.94
chrX	605	0.50

Also, the highlighted value in your output is almost certainly wrong.

Regards,
Alister
# 4  
Old 10-02-2013
Code:
y=`sort -k3,3nr filename | awk 'NR == 1 { print $3}'`
awk '{$3 = $3 / '$y';printf("%s\t%d\t%0.2f\n", $1, $2, $3);}' filename

output :
Code:
chr2L   552     0.22
chr2R   135     0.27
chr3L   820     0.74
chr3R   581     1.00
chr4    585     0.24
chrX    605     0.49

# 5  
Old 10-31-2013
Dear alister,
I appreciate that you try not to give a fish! Smilie
I should have replied much earlier... but then I managed to write the script and didn't update the thread.

you have always helped me and I am thankful once more! Smilie
Here is what I wrote which seems to work:

Code:
#!/usr/bin/perl
use strict;
use warnings;

open(FH,"inputfilename");
my @LINE;
my @val;
my $counter=0;
open OUT, ">./BG3_H3K27ac_Ave_Rto.to2006_Norm.sgr";
while(my $line=<FH>){
   my @array=split("\t",$line);
   push(@LINE, $line);
   push(@val,$array[2]);
   $counter++;
   }
   close(FH);

   my @valst=sort { $a <=> $b } @val;
   my $idx=$valst[$counter-1];

   foreach my $line(@LINE){
   my @array=split("\t",$line);
   print OUT $array[0],"\t",$array[1],"\t",$array[2]/$idx,"\n";
}

Cheers!

Quote:
Originally Posted by alister
Have you put forth any effort at all? If so, show us what you've tried so far.

Regards,
Alister

---------- Post updated at 01:02 PM ---------- Previous update was at 12:55 PM ----------



Also, the highlighted value in your output is almost certainly wrong.

Regards,
Alister
# 6  
Old 10-31-2013
try also:
Code:
awk '
{a[NR]=$1; b[NR]=$2; c[NR]=$3; $3 > mx ? mx=$3 : 0}
END { for (i=1; i<=NR; i++) printf ("%-6s%6d%7.2f\n", a[i], b[i], c[i]/mx ); }
' infile

(sort aside)
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Data imputation with scaling

Hello masters, this is difficult to explain and maybe complicated to implement...looks beyond what I taught myself (from this forum), some help is greatly appreciated. I have a base file a1 10 a2 15 a3 20 a4 21 I have a non-base file a1 170 b12 175 c12 180 d12 190 a2 ... (3 Replies)
Discussion started by: senhia83
3 Replies

2. Shell Programming and Scripting

Perl :: reading values from Data Dumper reference in Perl

Hi all, I have written a perl code and stored the data into Data structure using Data::Dumper module. But not sure how to retreive the data from the Data::Dumper. Eg. Based on the key value( Here CRYPTO-6-IKMP_MODE_FAILURE I should be able to access the internal hash elements(keys) ... (1 Reply)
Discussion started by: scriptscript
1 Replies

3. Shell Programming and Scripting

perl -write values in a file to @array in perl

Hi can anyone suggest me how to write a file containing values,... say 19 20 21 22 .. 40 to an array @array = (19, 20, ... 40) -- Thanks (27 Replies)
Discussion started by: meghana
27 Replies
Login or Register to Ask a Question