-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpct_reads_per_species.py
executable file
·228 lines (198 loc) · 7.3 KB
/
pct_reads_per_species.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/env python3
import sys
import argparse
import pandas as pd
import os
import re
from distutils.version import LooseVersion
def reads_per_prot(infile, identity):
reads_per_prot = {}
with open(infile, 'r') as input:
for line in input:
blast_results = line.strip().split('\t')
if float(blast_results[2]) >= identity:
prot_id = blast_results[1].split('|')[1]
if prot_id in reads_per_prot:
reads_per_prot[prot_id] += 1
else:
reads_per_prot[prot_id] = 1
return reads_per_prot
def prot_tax_map(taxonmap):
prot_tax_map = {}
with open(taxonmap, 'r') as map:
for line in map:
if '[' in line:
record = re.split('[\[\]\|]', line.strip())
prot_id, tax = record[1], record[4]
prot_tax_map[prot_id] = tax
return prot_tax_map
def reads_per_tax(prot_tax_map, reads_per_prot):
reads_per_tax = {}
total_reads = 0
reads_per_tax['other'] = 0
for prot_id, reads in reads_per_prot.items():
total_reads += reads
if prot_id in prot_tax_map:
species = prot_tax_map[prot_id]
if prot_id in reads_per_tax:
reads_per_tax[species] += reads
else:
reads_per_tax[species] = reads
else:
reads_per_tax['other'] += reads
return reads_per_tax, total_reads
def pct_reads_per_tax(reads_per_tax, total_reads):
pct_reads_per_tax = {}
for species, reads in reads_per_tax.items():
pct = round(reads/total_reads * 100, 3)
if pct >= 0.0001:
pct_reads_per_tax[species] = pct
return pct_reads_per_tax
def output(pct_reads_per_tax, prefix):
out_df = pd.DataFrame.from_dict(pct_reads_per_tax, orient='index')
if LooseVersion(str(pd.__version__)) >= LooseVersion("0.17.0"):
out_df.sort_values(by=[0], ascending=False, inplace=True)
else:
out_df.sort(by=[0], ascending=False, inplace=True)
out_df.to_csv(prefix+'_pct_species.tsv', sep='\t', header=None)
return 1
def main(taxonids, diamond_blastx_tsv, prefix, identity):
prot_tax_map_dict = prot_tax_map(taxonids)
reads_per_prot_dict = reads_per_prot(diamond_blastx_tsv, identity)
(reads_per_tax_dict, total_reads) = reads_per_tax(
prot_tax_map_dict, reads_per_prot_dict)
pct_reads_per_tax_dict = pct_reads_per_tax(reads_per_tax_dict, total_reads)
output(pct_reads_per_tax_dict, prefix)
return 1
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=(
'Parse diamond alignment results and derive percent of reads'
'from each ')
)
# required arguments
required = parser.add_argument_group('Required arguments')
required.add_argument(
'-i', '--diamond_blastx_tsv', required=True, nargs='+',
help='Diamond output blast tabular format'
)
required.add_argument(
'--taxonids', required=True,
help=('Path to mapping file that maps NCBI protein accession numbers'
' to taxon ids')
)
# optional arguments
parser.add_argument(
'-d', '--out_dir', help='', default='.'
)
parser.add_argument(
'-p', '--prefix', help='Output prefix', default='outfile'
)
parser.add_argument(
'--identity', help='identity cutoff', default=0.9
)
args = parser.parse_args()
if not os.path.exists(args.out_dir):
raise ValueError(args.out_dir+"not exist.")
main(args.taxonids, args.diamond_blastx_tsv,
os.path.join(args.out_dir, args.prefix+"_"+str(args.identity)+'iden'),
args.identity)
#!/usr/bin/env python3
import sys
import argparse
import pandas as pd
import os
import re
from distutils.version import LooseVersion
def reads_per_prot(infile, identity):
reads_per_prot = {}
with open(infile, 'r') as input:
for line in input:
blast_results = line.strip().split('\t')
if float(blast_results[2]) >= identity:
prot_id = blast_results[1].split('|')[1]
if prot_id in reads_per_prot:
reads_per_prot[prot_id] += 1
else:
reads_per_prot[prot_id] = 1
return reads_per_prot
def prot_tax_map(taxonmap):
prot_tax_map = {}
with open(taxonmap, 'r') as map:
for line in map:
if '[' in line:
record = re.split('[\[\]\|]', line.strip())
prot_id, tax = record[1], record[4]
prot_tax_map[prot_id] = tax
return prot_tax_map
def reads_per_tax(prot_tax_map, reads_per_prot):
reads_per_tax = {}
total_reads = 0
reads_per_tax['other'] = 0
for prot_id, reads in reads_per_prot.items():
total_reads += reads
if prot_id in prot_tax_map:
species = prot_tax_map[prot_id]
if prot_id in reads_per_tax:
reads_per_tax[species] += reads
else:
reads_per_tax[species] = reads
else:
reads_per_tax['other'] += reads
return reads_per_tax, total_reads
def pct_reads_per_tax(reads_per_tax, total_reads):
pct_reads_per_tax = {}
for species, reads in reads_per_tax.items():
pct = round(reads/total_reads * 100, 3)
if pct >= 0.0001:
pct_reads_per_tax[species] = pct
return pct_reads_per_tax
def output(pct_reads_per_tax, prefix):
out_df = pd.DataFrame.from_dict(pct_reads_per_tax, orient='index')
if LooseVersion(str(pd.__version__)) >= LooseVersion("0.17.0"):
out_df.sort_values(by=[0], ascending=False, inplace=True)
else:
out_df.sort(by=[0], ascending=False, inplace=True)
out_df.to_csv(prefix+'_pct_species.tsv', sep='\t', header=None)
return 1
def main(taxonids, diamond_blastx_tsv, prefix, identity):
prot_tax_map_dict = prot_tax_map(taxonids)
reads_per_prot_dict = reads_per_prot(diamond_blastx_tsv, identity)
(reads_per_tax_dict, total_reads) = reads_per_tax(
prot_tax_map_dict, reads_per_prot_dict)
pct_reads_per_tax_dict = pct_reads_per_tax(reads_per_tax_dict, total_reads)
output(pct_reads_per_tax_dict, prefix)
return 1
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description=(
'Parse diamond alignment results and derive percent of reads'
'from each ')
)
# required arguments
required = parser.add_argument_group('Required arguments')
required.add_argument(
'-i', '--diamond_blastx_tsv', required=True, nargs='+',
help='Diamond output blast tabular format'
)
required.add_argument(
'--taxonids', required=True,
help=('Path to mapping file that maps NCBI protein accession numbers'
' to taxon ids')
)
# optional arguments
parser.add_argument(
'-d', '--out_dir', help='', default='.'
)
parser.add_argument(
'-p', '--prefix', help='Output prefix', default='outfile'
)
parser.add_argument(
'--identity', help='identity cutoff', default=0.9
)
args = parser.parse_args()
if not os.path.exists(args.out_dir):
raise ValueError(args.out_dir+"not exist.")
main(args.taxonids, args.diamond_blastx_tsv,
os.path.join(args.out_dir, args.prefix+"_"+str(args.identity)+'iden'),
args.identity)