Question: How To Re-Scale The Values In A Tabular File
1
gravatar for 2011101101
6.6 years ago by
2011101101100
2011101101100 wrote:

Thank you for your attention first.I have a file,there are some columns in the file.I need to be calculated in accordance with column.There is an example:

motif    WSB-1    WSB-2    SM-1    SM-2    PH-1    PH-2    MY-1    MY-2    total 
ATCGAGACTGGGATTTGTCACTCC    62    98    76    64    141    286    124    169    1020
GAACCGGGACTAATGGGCTGACCC    12    26    6    7    36    107    42    32    268
TATTAGTTTACAGAGGGAGTA    10    26    13    10    49    39    46    26    219
ATAGCCACCCAGTTACGTTGTGAC    8    24    8    5    26    71    33    31    206

I need the column of WSB-1 Divided by(/)100,WSB-2/200,SM-1/300,SM-2/400,PH-1/500,PH-2/600,MY-1/700,MY-2/800.

The result should be this

motif    WSB-1    WSB-2    SM-1    SM-2    PH-1    PH-2    MY-1    MY-2    total 
ATCGAGACTGGGATTTGTCACTCC    0.620     0.490     0.253     0.160     0.282     0.477     0.177     0.211     1020
GAACCGGGACTAATGGGCTGACCC    0.120     0.130     0.020     0.018     0.072     0.178     0.060     0.040     268
TATTAGTTTACAGAGGGAGTA    0.100     0.130     0.043     0.025     0.098     0.065     0.066     0.033     219
ATAGCCACCCAGTTACGTTGTGAC    0.080     0.120     0.027     0.013     0.052     0.118     0.047     0.039     206
• 1.4k views
ADD COMMENTlink modified 6.6 years ago by brentp23k • written 6.6 years ago by 2011101101100
3
gravatar for zx8754
6.6 years ago by
zx87548.2k
London
zx87548.2k wrote:

If you want to keep the header

awk '{OFS="\t"; if (NR==1) print $0; else print $1,$2/100,$3/200,$4/300,$5/400,$6/500,$7/600,$8/700,$9/800,$10};' input.txt

motif    WSB-1    WSB-2    SM-1    SM-2    PH-1    PH-2    MY-1    MY-2    total
ATCGAGACTGGGATTTGTCACTCC        0.62    0.49    0.253333        0.16    0.282  0.476667 0.177143        0.21125 1020
GAACCGGGACTAATGGGCTGACCC        0.12    0.13    0.02    0.0175  0.072   0.1783330.06    0.04    268
TATTAGTTTACAGAGGGAGTA   0.1     0.13    0.0433333       0.025   0.098   0.065  0.0657143        0.0325  219
ATAGCCACCCAGTTACGTTGTGAC        0.08    0.12    0.0266667       0.0125  0.052  0.118333 0.0471429       0.03875 206
ADD COMMENTlink written 6.6 years ago by zx87548.2k
2
gravatar for AGS
6.6 years ago by
AGS230
Brooklyn, ny
AGS230 wrote:
awk 'NR>1' yourfile | awk '{print $1"\t"$2/100"\t"$3/200"\t"$4/300"\t"$5/400"\t"$6/500"\t"$7/600"\t"$8/700"\t"$9/800"\t"$10}'

ATCGAGACTGGGATTTGTCACTCC    0.62    0.49    0.253333    0.16    0.282    0.476667    0.177143    0.21125    1020
GAACCGGGACTAATGGGCTGACCC    0.12    0.13    0.02        0.0175    0.072    0.178333    0.06        0.04    268
TATTAGTTTACAGAGGGAGTA        0.1     0.13    0.0433333    0.025    0.098    0.065        0.0657143    0.0325    219
ATAGCCACCCAGTTACGTTGTGAC    0.08    0.12    0.0266667    0.0125    0.052    0.118333    0.0471429    0.03875    206
ADD COMMENTlink modified 6.6 years ago • written 6.6 years ago by AGS230
1
gravatar for brentp
6.6 years ago by
brentp23k
Salt Lake City, UT
brentp23k wrote:

In python:

from toolshed import reader

for i, d in enumerate(reader("-", header="ordered")):
    if i == 0: print "\t".join(d.keys())
    vals = d.values()
    vals[2:-1] = ["%.3f" % (j * v / 100.) for j, v \
                    in enumerate(map(float, vals[2:-1]), 1)]
    print "\t".join(vals)

reads from stdin and writes to stdout. awk is better until you want to do something slightly different or add/remove columns.

ADD COMMENTlink written 6.6 years ago by brentp23k
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: 2618 users visited in the last hour