Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change format and combine info from 2 columns

First time posting here so please be patient!

I have a file that looks like that:

POS {ALLELE:COUNT}  
1   G:27    A:11
2   C:40    T:0
3   C:40    A:0
4   T:40    G:0
5   G:0 C:40
6   C:40    T:0
7   G:24    A:14
8   G:40    A:0
9   A:40    G:0
...

I want to combine the information from the second and third column for each line in the following format: "number[A],number[C],number[G],number[T]" so that the example above would look like that:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

POS {ALLELE:COUNT}
1   11,0,27,0
2   0,40,0,0
3   0,40,0,0
4   0,0,0,40
5   0,40,0,0
6   0,40,0,0
7   14,0,24,0
8   0,0,40,0
9   40,0,0,0
...

Any idea on how I could do that would be much appreciated!

>Solution :

Here’s a method that works:

lines = open('test.txt','r').read().splitlines()

place = {'A':0,'C':1,'G':2,'T':3}
counts = [[0 for _ in range(4)] for _ in range(len(lines[1:]))]
for i,row in enumerate(lines[1:]):
    for ct in row.split()[1:]:
        a,b = ct.split(':')
        counts[i][place[a]] = int(b)

out_str = '\n'.join([lines[0]] + ['{:<4}{},{},{},{}'.format(i+1,*ct) 
    for i,ct in enumerate(counts)])

with open('output.txt','w') as f:
    f.write(out_str)

The resulting file reads

POS {ALLELE:COUNT}  
1   11,0,27,0
2   0,40,0,0
3   0,40,0,0
4   0,0,0,40
5   0,40,0,0
6   0,40,0,0
7   14,0,24,0
8   0,0,40,0
9   40,0,0,0
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading