-
Notifications
You must be signed in to change notification settings - Fork 0
/
halo_GCF_table_add_sps.py
93 lines (63 loc) · 1.89 KB
/
halo_GCF_table_add_sps.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!usr/env/bin python3
"""
Author: Catarina Loureiro
A script to add a column with host sponge sps based on sample column
"""
import os
import argparse
def get_cmds():
"""
Capture args from the cmdline
"""
parser = argparse.ArgumentParser(description='')
parser.add_argument('-g', '-gcf', dest='gcf', help='gcf annotation file',\
required=True, metavar='<file>')
parser.add_argument('-s', '-sps', dest='sps',help='sps sample mapping file',\
required=True, metavar='<file>')
parser.add_argument('-o', '-out', dest='out', help='updated gcf file',\
required=True, metavar='<file>')
# parser.add_argument('-', '-', dest='', help='', required=True, metavar='<>')
return parser.parse_args()
def parse_mapping(map_file):
"""
generate dict from mapping file
map_file: str, filepath
map_dict: dict{sample:sps}
"""
map_dict = {}
fileobj = open(map_file, 'r')
for line in fileobj:
line = line.strip()
elms = line.split('\t')
map_dict[elms[0]] = elms[1]
return map_dict
def update_GCF(gcf_file, map_dict, out_file):
"""
update with sps column
gcf_file, out_file: str, path
map_dict: dict{sample:sps}
"""
gcf_fileobj = open(gcf_file, 'r')
out_fileobj = open(out_file, 'w')
out_fileobj.write(f'GCF\tSamples\tHost_taxa\tCount\tBins\tBin_rep\tBin_taxa\n')
for line in gcf_fileobj:
line = line.strip()
if line.startswith('GCF'):
header = line
else:
elms = line.split('\t')
GCF = elms[0]
samples = elms[1].split(',')
bins = elms[2]
bin_rep = elms[3]
bin_taxa = elms[4]
sps = [map_dict[sample] for sample in samples]
sps = [species for species in set(sps)]
sps_str = ','.join(sps)
sps_len = str(len(sps))
out_fileobj.write(f'{GCF}\t{elms[1]}\t{sps_str}\t{sps_len}\t{bins}\t{bin_rep}\t{bin_taxa}\n')
return None
if __name__ == '__main__':
cmds = get_cmds()
dict_map = parse_mapping(cmds.sps)
update_GCF(cmds.gcf, dict_map, cmds.out)