-
Notifications
You must be signed in to change notification settings - Fork 0
/
fasta_to_majority_consensus.py
135 lines (98 loc) · 3.5 KB
/
fasta_to_majority_consensus.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
# Python 2.7.6
# fasta_to_majority_consensus.py
# Mathias Scharmann
# 3 July 2015
"""
example usage:
cat blasthits_557475_L96.fasta | python $tb/fasta_to_majority_consensus.py -out fufufu.txt
"""
import argparse
import sys
#######
def get_commandline_arguments ():
parser = argparse.ArgumentParser()
parser.add_argument("-out", required=True,
dest="out", help="name of output file", metavar="FILE")
args = parser.parse_args()
return args
def make_cluster_consensus(cluster_dict):
# make consensus sequence out of individual sequences in the binned_dict:
consensus_dict = {}
for cluster, seqs in cluster_dict.items():
outseq = []
print "working on cluster {0}".format(cluster)
# print seqs[0]
if len(seqs) > 0:
for column_index in range(0, len(seqs[0])): # to loop over all columns
aligned_column = "".join([ seq[column_index] for seq in seqs ])
# print aligned_column
maxcount = 0
for nuc in ["A","T","G","C"]:
if aligned_column.count(nuc) > maxcount:
maxcount = aligned_column.count(nuc)
consensus = nuc
outseq.append(consensus)
outseqstring = "".join(outseq)
# print outseqstring
consensus_dict[cluster] = outseqstring
return consensus_dict
#####
def readfq(fp): # this is a generator function
last = None # this is a buffer keeping the last unprocessed line
while True: # mimic closure; is it a bad idea?
if not last: # the first record or a record following a fastq
for l in fp: # search for the start of the next record
if l[0] in '>@': # fasta/q header line
last = l[:-1] # save this line
break
if not last: break
name, seqs, last = last[1:].partition(" ")[0], [], None
for l in fp: # read the sequence
if l[0] in '@+>':
last = l[:-1]
break
seqs.append(l[:-1])
if not last or last[0] != '+': # this is a fasta record
yield name, ''.join(seqs), None # yield a fasta record
if not last: break
else: # this is a fastq record
seq, leng, seqs = ''.join(seqs), 0, []
for l in fp: # read the quality
seqs.append(l[:-1])
leng += len(l) - 1
if leng >= len(seq): # have read enough quality
last = None
yield name, seq, ''.join(seqs); # yield a fastq record
break
if last: # reach EOF before reading enough quality
yield name, seq, None # yield a fasta record instead
break
######### MAIN ########
if sys.stdin.isatty(): # returns False if something is in STDIN
print "no input data piped, dying"
exit()
args = get_commandline_arguments ()
with open(args.out, "w") as FASTA:
n, slen, qlen = 0, 0, 0
fastadict = {}
seqs = []
for name, seq, qual in readfq(sys.stdin):
fastadict[name] = seq
seqs.append(seq)
outseq = []
if len(seqs) > 0:
for column_index in range(0, len(seqs[0])): # to loop over all columns
aligned_column = "".join([ seq[column_index] for seq in seqs ])
# print aligned_column
maxcount = 0
for nuc in ["A","T","G","C","-"]:
if aligned_column.count(nuc) > maxcount:
maxcount = aligned_column.count(nuc)
consensus = nuc
outseq.append(consensus)
outseqstring = "".join(outseq)
header_line = ">{0}\n".format(name)
seq_line = outseqstring + "\n"
FASTA.write(header_line)
FASTA.write(seq_line)
print "Done!"