-
Notifications
You must be signed in to change notification settings - Fork 2
/
fastaBlocks.py
85 lines (76 loc) · 2.74 KB
/
fastaBlocks.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
##################################
# #
# Last modified 10/31/2013 #
# #
# Georgi Marinov #
# #
##################################
import sys
import string
def main(argv):
if len(argv) < 3:
print 'usage: python %s fasta blockSize outputfilename [-replaceXwithN] [-replaceNames correspondence_file]' % argv[0]
print '\t format of correspondence_file for sequence name replacement: <old name> <tab> <new name>'
print '\t note: if the sequence name replacement option is used, sequences for which no new name is specified will be skipped'
sys.exit(1)
fasta = argv[1]
blocksize = int(argv[2])
outfilename = argv[3]
doXtoN = False
if '-replaceXwithN':
doXtoN = True
doReplaceNames = False
if '-replaceNames':
doReplaceNames = True
print 'will replace sequence names'
linelist = open(argv[argv.index('-replaceNames') + 1])
ReplaceMentDict = {}
for line in linelist:
fields = line.strip().split('\t')
if line.strip() == '':
continue
ReplaceMentDict[fields[0]] = fields[1]
outfile = open(outfilename, 'w')
inputdatafile = open(fasta)
ID=''
for line in inputdatafile:
if line[0]=='>':
if ID == '':
ID = line.strip().split('>')[1]
else:
sequence = ''.join(sequence)
if doXtoN:
sequence = sequence.replace('X','N').replace('x','n')
if doReplaceNames:
if ReplaceMentDict.has_key(ID):
doSkip = False
ID = ReplaceMentDict[ID]
else:
doSkip = True
if doSkip:
pass
else:
outfile.write('>' + ID + '\n')
for i in range(0,len(sequence),blocksize):
outfile.write(sequence[i:min(i+blocksize, len(sequence))] + '\n')
ID = line.strip().split('>')[1]
sequence=[]
else:
sequence.append(line.strip())
sequence = ''.join(sequence)
if doXtoN:
sequence = sequence.replace('X','N').replace('x','n')
if doReplaceNames:
if ReplaceMentDict.has_key(ID):
doSkip = False
ID = ReplaceMentDict[ID]
else:
doSkip = True
if doSkip:
pass
else:
outfile.write('>' + ID + '\n')
for i in range(0,len(sequence),blocksize):
outfile.write(sequence[i:min(i+blocksize, len(sequence))] + '\n')
if __name__ == '__main__':
main(sys.argv)