-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfastaFormatter.py
executable file
·145 lines (131 loc) · 3.74 KB
/
fastaFormatter.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
#!/usr/bin/python
import sys
import os
import getopt
from textwrap import wrap
def main():
params = parseArgs()
if params.many2one:
seqs=dict()
for f in read_fasta(params.many2one):
seqs[f[0]] = f[1]
write_fasta(params.out, seqs)
elif params.one2many:
seqs=dict()
for f in read_fasta(params.one2many):
seqs[f[0]] = f[1]
write_fasta(params.out, seqs, params.width)
#Function to write fasta-formatted sequences
def write_fasta(f, aln, width=None):
with open(f, 'w') as fh:
try:
for samp in aln.keys():
if width:
ol = ">" + str(samp) + "\n"
chunks=wrap(aln[samp], width=width, break_on_hyphens=False, drop_whitespace=False)
for chunk in chunks:
ol=ol + str(chunk) + "\n"
else:
ol = ">" + str(samp) + "\n" + str(aln[samp]) + "\n"
fh.write(ol)
except IOError as e:
print("Could not read file %s: %s"%(f,e))
sys.exit(1)
except Exception as e:
print("Unexpected error reading file %s: %s"%(f,e))
sys.exit(1)
finally:
fh.close()
#Read samples as FASTA. Generator function
def read_fasta(fas):
if os.path.exists(fas):
with open(fas, 'r') as fh:
try:
contig = ""
seq = ""
for line in fh:
line = line.strip()
if not line:
continue
#print(line)
if line[0] == ">": #Found a header line
#If we already loaded a contig, yield that contig and
#start loading a new one
if contig:
yield([contig,seq]) #yield
contig = "" #reset contig and seq
seq = ""
split_line = line.split()
contig = (split_line[0].replace(">",""))
else:
seq += line
#Iyield last sequence, if it has both a header and sequence
if contig and seq:
yield([contig,seq])
except IOError:
print("Could not read file ",fas)
sys.exit(1)
finally:
fh.close()
else:
raise FileNotFoundError("File %s not found!"%fas)
#Object to parse command-line arguments
class parseArgs():
def __init__(self):
#Define options
try:
options, remainder = getopt.getopt(sys.argv[1:], 'h1:M:w:o:', \
["help", "one2many=","many2one=","width=","out="])
except getopt.GetoptError as err:
print(err)
self.display_help("\nExiting because getopt returned non-zero exit status.")
#Default values for params
#Input params
self.one2many=None
self.many2one=None
self.width=60
self.out="out.fas"
#First pass to see if help menu was called
for o, a in options:
if o in ("-h", "-help", "--help"):
self.display_help("Exiting because help menu was called.")
#Second pass to set all args.
for opt, arg_raw in options:
arg = arg_raw.replace(" ","")
arg = arg.strip()
opt = opt.replace("-","")
#print(opt,arg)
if opt == "h" or opt == "help":
continue
elif opt=="one2many" or opt=="1":
self.one2many=arg
elif opt=="many2one" or opt=="M":
self.many2one=arg
elif opt=="width" or opt=="w":
self.width=int(arg)
elif opt=="out" or opt=="o":
self.out=arg
else:
assert False, "Unhandled option %r"%opt
#Check manditory options are set
if not self.one2many and not self.many2one:
self.display_help("No files provided.")
def display_help(self, message=None):
if message is not None:
print()
print (message)
print ("\nfastaFormatter.py\n")
print("Author: Tyler K Chafin, University of Arkansas")
print ("Contact: [email protected]")
print ("Description:Right now just converts b/n multi-line and one-line fasta formats, might add later")
print("""
-1,--one2many : Path to fasta file to multi-line format
-M,--many2one : Path to fasta file to convert to one-line format
-w,--width : Characters per line for multi-line (default: 60)
-o,--out : Output file name (default=out.fas)
""")
print()
sys.exit()
#Call main function
if __name__ == '__main__':
main()