-
Notifications
You must be signed in to change notification settings - Fork 0
/
greylag_flatten_fasta.py
executable file
·147 lines (120 loc) · 5.19 KB
/
greylag_flatten_fasta.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
#!/usr/bin/env python
"""
Convert between FASTA-formatted input and one-sequence-per-line output so that
the sequences can be easily manipulated with UNIX text tools (e.g., grep,
head, wc, split, sort, etc.).
In order for '--inverse' to work correctly, the same flags must be supplied as
were supplied during the forward conversion (the script does not try to
guess). With '--defline=after', the conversion should be perfectly
invertible, modulo whitespace and wrapping. For '--defline=omit', an
artificial defline will be constructed based on the filename and line number.
"""
__copyright__ = '''
greylag, a collection of programs for MS/MS protein analysis
Copyright (C) 2006-2008 Stowers Institute for Medical Research
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
Contact: Mike Coleman
Stowers Institute for Medical Research
1000 East 50th Street
Kansas City, Missouri 64110
USA
'''
import fileinput
import optparse
import re
import sys
import greylag
# no backtrace on SIGPIPE
try:
import signal
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except Exception:
pass
def error(s, *args):
"fatal error"
# if we're unit testing, just throw an exception
if __name__ != "__main__":
raise Exception((s + " (fatal error)") % args)
print >> sys.stderr, ("error: " + s) % args
sys.exit(1)
# errors are fatal
greylag.chase_error = error
def warn(message):
print >> sys.stderr, "warning: %s [at %s:%s]" \
% (message, fileinput.filename(), fileinput.filelineno())
def write_flattened_locus(options, defline, sequence):
if options.defline == 'after':
print '%s%s>%s' % (sequence, options.delimiter, defline)
elif options.defline == 'before':
if options.delimiter in defline:
warn("delimiter present in defline")
print '>%s%s%s' % (defline, options.delimiter, sequence)
else:
print sequence
def _main():
parser = optparse.OptionParser(usage="usage: %prog [options] [<file>...]",
description=__doc__)
parser.add_option("-d", "--delimiter", dest="delimiter", default='\t',
help="delimiter between defline and sequence"
" [default TAB]", metavar="STRING")
parser.add_option("-D", "--defline", dest="defline",
choices=('before', 'after', 'omit'), default="after",
help="position of defline with respect to sequence, one"
" of 'before', 'after' [default], or 'omit'",
metavar="POSITION")
parser.add_option("-i", "--inverse", dest="inverse", action="store_true",
help="do the inverse transformation (flat to FASTA)")
DEFAULT_WRAP = 80
parser.add_option("-w", "--wrap", dest="wrap", type="int",
default=DEFAULT_WRAP,
help="for --inverse, wrap sequence to specified width"
" [default %s, 0 means don't wrap at all]" % DEFAULT_WRAP,
metavar="COLUMNS")
parser.add_option("-v", "--verbose", dest="verbose", action="store_true",
help="be verbose")
parser.add_option("--copyright", action="store_true", dest="copyright",
help="print copyright and exit")
options, args = parser.parse_args()
if options.wrap < 0:
parser.print_help()
sys.exit(1)
if not options.inverse:
if not args:
files = [ sys.stdin ]
else:
files = [ open(fn) for fn in args ]
for f in files:
for locusname, defline, sequence in greylag.read_fasta_file(f):
write_flattened_locus(options, defline, sequence)
else:
for line in fileinput.input(args):
if options.defline != 'omit':
parts = line.split(options.delimiter, 1)
if len(parts) < 2:
error("input line lacks delimiter")
if options.defline == 'before':
defline, sequence = parts
else:
sequence, defline = parts
else:
sequence = line
defline = "%s:%s" % (fileinput.filename(),
fileinput.filelineno())
sequence = sequence.strip()
print defline.strip()
if options.wrap:
for start in range(0, len(sequence), options.wrap):
print sequence[start:start+options.wrap]
else:
print sequence
if __name__ == '__main__':
_main()