/
proc_needleParse.py
180 lines (159 loc) · 4.73 KB
/
proc_needleParse.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
import sys
# ./needle 1hg8.seq 1mty.seq -gapopen 10 -gapextend 0.5 -data combined.matrix.0.002.new -aformat markx3 t.align
# parse emboss needle markx3 format output
# output a flat csv file
'''
########################################
# Program: needle
# Rundate: Thu 1 Dec 2016 23:24:12
# Commandline: needle
# [-asequence] 1hg8.seq
# [-bsequence] 1mty.seq
# -gapopen 10
# -gapextend 0.5
# -datafile combined.matrix.0.002.new
# -aformat markx3
# [-outfile] t.align
# Align_format: markx3
# Report_file: t.align
########################################
#=======================================
#
# Aligned_sequences: 2
# 1:
# 2:
# Matrix: combined.matrix.0.002.new
# Gap_penalty: 10.0
# Extend_penalty: 0.5
#
# Length: 451
# Identity: 76/451 (16.9%)
# Similarity: 125/451 (27.7%)
# Gaps: 186/451 (41.2%)
# Score: 75.5
#
#
#=======================================
> ..
------------------------CKNIVLNGFQVPTGKQL-DLSSLQ--
--------------NDSTVTFKG-------TTTFATTAD----ND-----
S
> ..
ERRRGLTDPEMAAVILKALPEAPLDGNNKMGYFVTPRWKRLTEYEALTVY
AQPNADWIAGGLDWGDWTQKFHGGRPSWGNETTELRTVDWFKHRDPLRRW
-
#---------------------------------------
#---------------------------------------
'''
def parseFasta(lines, i):
AAset = ['A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G', 'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V', 'B', 'Z', 'X', '-',
'a', 'r', 'n', 'd', 'c', 'q', 'e', 'g', 'h', 'i', 'l', 'k', 'm', 'f', 'p', 's', 't', 'w', 'y', 'v', 'b', 'z', 'x']
fastalines = []
while lines[i][0] in AAset:
fastalines.append(lines[i].strip())
i+=1
return (''.join(fastalines), i-1)
def main():
if len(sys.argv) < 2:
print 'proc_needleParse: Too few parameters'
print 'python proc_needleParse.py seq1-seq2.align'
print 'output: seq1-seq2.align.flat'
return
alignfile = sys.argv[1]
outfile = alignfile + '.flat'
print 'alignment file: %s' % alignfile
fin = open(alignfile, 'r')
lines = fin.readlines()
fin.close()
i = 0
out = []
out.append(alignfile)
while i<len(lines):
#print lines[i]
line = lines[i].strip()
i+=1
if len(line) < 1:
continue
# Gap_penalty: 10.0
if 'Gap_penalty:' in line:
tsvline = ' '.join(line.split())
strArray = tsvline.split(' ')
gapopen = strArray[2]
out.append(gapopen)
print 'Gap_penalty: %s' % gapopen
continue
# Extend_penalty: 0.5
if 'Extend_penalty:' in line:
tsvline = ' '.join(line.split())
strArray = tsvline.split(' ')
gapextend = strArray[2]
out.append(gapextend)
print 'Extend_penalty: %s' % gapextend
continue
# Length: 451
if 'Length:' in line:
tsvline = ' '.join(line.split())
strArray = tsvline.split(' ')
length = strArray[2]
out.append(length)
print 'length: %s' % length
continue
# Identity: 76/451 (16.9%)
if 'Identity:' in line:
tsvline = ' '.join(line.split())
strArray = tsvline.split(' ')
ratioStr = strArray[2]
ratioArr = ratioStr.split('/')
out.append(ratioArr[0]) # number of identity
ratio = float(ratioArr[0])/float(ratioArr[1])
identity = '%.1f' % (ratio * 100)
out.append(identity)
print 'identity: %s' % identity
continue
# Similarity: 125/451 (27.7%)
if 'Similarity:' in line:
tsvline = ' '.join(line.split())
strArray = tsvline.split(' ')
ratioStr = strArray[2]
ratioArr = ratioStr.split('/')
out.append(ratioArr[0]) # number of similarity
ratio = float(ratioArr[0])/float(ratioArr[1])
similarity = '%.1f' % (ratio * 100)
out.append(similarity)
print 'similarity: %s' % similarity
continue
# Gaps: 186/451 (41.2%)
if 'Gaps:' in line:
tsvline = ' '.join(line.split())
strArray = tsvline.split(' ')
ratioStr = strArray[2]
ratioArr = ratioStr.split('/')
out.append(ratioArr[0]) # number of gaps
ratio = float(ratioArr[0])/float(ratioArr[1])
gaps = '%.1f' % (ratio * 100)
out.append(gaps)
print 'gaps: %s' % gaps
continue
# Score: 75.5
if 'Score:' in line:
tsvline = ' '.join(line.split())
strArray = tsvline.split(' ')
score = strArray[2]
out.append(score)
print 'score: %s' % score
continue
# > ..
if '>' == line[0]:
fastaline, index = parseFasta(lines, i) # parse fasta sequence from the ith line
out.append(('%d' % len(fastaline.replace('-', '')))) # pure sequence length
out.append(fastaline)
i = index
print 'seq: %s ... len: %d' % (fastaline[0:5], len(fastaline))
continue
#print repr(out)
print 'save file: %s' % outfile
fout = open(outfile, 'w')
fout.write(' '.join(out)+'\n')
fout.close()
if __name__ == '__main__':
main()