/
ssearch_count.py
187 lines (154 loc) · 5.58 KB
/
ssearch_count.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
# This file is part of Bioy
#
# Bioy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Bioy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Bioy. If not, see <http://www.gnu.org/licenses/>.
"""
Tally ssearch base count by position
Groups results by taxonomy rank
"""
import csv
import logging
import sys
from collections import defaultdict, OrderedDict, Counter
from csv import DictReader, DictWriter
from itertools import groupby
from bioy_pkg.utils import Opener
log = logging.getLogger(__name__)
def build_parser(parser):
parser.add_argument(
'infile',
type=Opener(),
nargs='?',
default=sys.stdin,
help=('csv file with ssearch36 columns '
'[q_name,q_seq,t_name,t_seq,q_al_start,q_al_stop,'
't_al_start,t_al_stop,t_sq_len,sw_zscore]'))
parser.add_argument(
'-i', '--info',
type=Opener(),
metavar='CSV',
help='info file mapping seqname to tax_id')
parser.add_argument(
'-t', '--taxonomy',
metavar='CSV',
type=Opener(),
help='taxonomy file mapping tax_id to taxonomy')
parser.add_argument(
'-o', '--out',
metavar='CSV',
type=Opener('w'),
default=sys.stdout,
help='csv output of bases {tax_id, species, positions,,}')
parser.add_argument(
'-r', '--rank',
default='species',
help='Aggregate primer stats by specified rank. [%(default)s]')
parser.add_argument(
'-f', '--position-freq',
metavar='FLOAT',
default=0.05,
type=float,
help='Minimum base frequency reported for a position [%(default)s]')
parser.add_argument(
'-z', '--min-zscore',
type=float,
help='Minimum z-score value to include alignment in base count.',
default=0)
def action(args):
# organize seq and tax info
tax_info = None
if args.info and args.taxonomy:
tax = {t['tax_id']: t for t in csv.DictReader(args.taxonomy)}
tax_info = {i['seqname']: tax[i['tax_id']]
for i in csv.DictReader(args.info)}
# helper functions
def intify(al):
for k in ['q_al_start', 'q_al_stop',
't_sq_len', 't_al_start', 't_al_stop']:
al[k] = int(al[k])
return al
def get_rank_id(a):
rank = idd = None
if tax_info:
idd = tax_info[a['q_name']][args.rank]
if idd:
rank = args.rank
else:
rank = 'species'
idd = tax_info[a['q_name']][rank]
return idd, rank
def in_range(a):
return a['q_al_stop'] - a['q_al_start'] == a['t_sq_len'] - 1
def pop_base_or_zero(counter, base):
return counter.pop(base) if base in counter else 0
def remove_gaps(a):
a['t_seq'] = a['t_seq'].replace('-', '')
return a
# setup and filtering
aligns = [intify(a) for a in DictReader(args.infile)]
# assert t_seq uniformity
aligns = [remove_gaps(a) for a in aligns]
t_seq = set(a['t_seq'] for a in aligns)
assert len(t_seq) == 1
t_seq = t_seq.pop()
aligns = sorted(aligns, key=get_rank_id)
total = len(aligns)
group_by = groupby(aligns, get_rank_id)
total_by_idd = {idd: sum(1 for a in al) for (idd, _), al in group_by}
# zscore filter
aligns = [a for a in aligns if float(a['sw_zscore']) >= args.min_zscore]
log.info(
'dropping {} alignments under zscore threshold'.format(
total - len(aligns)))
# alignment within q_seq range
aligns = [a for a in aligns if in_range(a)]
log.info('dropping {} partial alignments'.format(total - len(aligns)))
###
# position count
bases = defaultdict(Counter)
for al in aligns:
idd, rank = get_rank_id(al)
# reference sequence, starting at first base of primer alignment
q_start = al['q_al_start'] - 1
q_stop = al['q_al_stop']
qseq = al['q_seq'][q_start:q_stop]
for pos, base in enumerate(qseq):
bases[(pos, idd, rank)][base] += 1
fieldnames = ['tax_name'] if tax_info else []
fieldnames += ['position', 'A', 'T', 'G', 'C', 'N',
'expected', 'naligns', 'nseqs']
fieldnames += ['rank', 'tax_id'] if tax_info else []
out = DictWriter(args.out, fieldnames=fieldnames, extrasaction='ignore')
out.writeheader()
organized_bases = OrderedDict(
sorted(
bases.items(),
key=lambda b: (
b[0][1],
b[0][0])))
for (pos, idd, rank), counter in organized_bases.items():
naligns = sum([v for v in counter.values()])
out.writerow({
'tax_name': tax[idd]['tax_name'] if tax_info else '',
'rank': rank,
'tax_id': idd,
'position': pos + 1,
'expected': t_seq[pos],
'nseqs': total_by_idd[idd],
'naligns': naligns,
'A': pop_base_or_zero(counter, 'A'),
'T': pop_base_or_zero(counter, 'T'),
'G': pop_base_or_zero(counter, 'G'),
'C': pop_base_or_zero(counter, 'C'),
'N': sum(counter.values())
})