Debian Codegraph @

weblogo/_transformseq.py

raw
1 #!/usr/bin/env python
2
3 # Copyright (c) 2006, The Regents of the University of California, through
4 # Lawrence Berkeley National Laboratory (subject to receipt of any required
5 # approvals from the U.S. Dept. of Energy). All rights reserved.
6
7 # This software is distributed under the new BSD Open Source License.
8 # <http://www.opensource.org/licenses/bsd-license.html>
9 # Please see the LICENSE.txt file that should have been included
10 # as part of this package.
11
12
13 """A tool for converting multiple sequence alignments from
14 one format to another.
15 """
16
17 import sys
18 from optparse import OptionGroup
19
20 from weblogo import seq_io
21 from weblogo.seq import Seq, SeqList, nucleic_alphabet
22 from weblogo.transform import mask_low_complexity
23 from weblogo.utils.deoptparse import DeOptionParser
24
25 __version__ = "1.0.0"
26 description = """ A tool for converting multiple sequence alignments from
27 one format to another. """
28
29
30 def main() -> None:
31 # ------ Parse Command line ------
32 parser = _build_option_parser()
33 (opts, args) = parser.parse_args(sys.argv[1:])
34 if args:
35 parser.error("Unparsable arguments: %s " % args)
36
37 seqs = opts.reader.read(opts.fin)
38
39 if opts.trans_seg:
40 seqs = SeqList([mask_low_complexity(s) for s in seqs])
41
42 if opts.subsample is not None:
43 from random import random
44
45 frac = opts.subsample
46 ss = []
47 for s in seqs:
48 if random() < frac:
49 ss.append(s)
50 seqs = SeqList(ss)
51
52 if opts.reverse:
53 seqs = SeqList([s.reverse() for s in seqs])
54
55 if opts.complement:
56 seqs = SeqList([Seq(s, alphabet=nucleic_alphabet) for s in seqs])
57 seqs = SeqList([s.complement() for s in seqs])
58
59 opts.writer.write(opts.fout, seqs)
60
61
62 def _build_option_parser() -> DeOptionParser:
63 parser = DeOptionParser(
64 usage="%prog [options] < sequence_data.fa > sequence_logo.eps",
65 description=description,
66 version=__version__,
67 add_verbose_options=False,
68 )
69
70 io_grp = OptionGroup(
71 parser,
72 "Input/Output Options",
73 )
74 parser.add_option_group(io_grp)
75
76 io_grp.add_option(
77 "-f",
78 "--fin",
79 dest="fin",
80 action="store",
81 type="file_in",
82 default=sys.stdin,
83 help="Sequence input file (default: stdin)",
84 metavar="FILENAME",
85 )
86
87 io_grp.add_option(
88 "",
89 "--format-in",
90 dest="reader",
91 action="store",
92 type="dict",
93 default=seq_io,
94 choices=seq_io.format_names(),
95 help="Multiple sequence alignment format: (%s)"
96 % ", ".join([f.names[0] for f in seq_io.formats]), # type: ignore
97 metavar="FORMAT",
98 )
99
100 io_grp.add_option(
101 "-o",
102 "--fout",
103 dest="fout",
104 type="file_out",
105 default=sys.stdout,
106 help="Output file (default: stdout)",
107 metavar="FILENAME",
108 )
109
110 trans_grp = OptionGroup(
111 parser,
112 "Transformations",
113 )
114 parser.add_option_group(trans_grp)
115
116 trans_grp.add_option(
117 "",
118 "--seg",
119 dest="trans_seg",
120 action="store_true",
121 default=False,
122 help="Mask low complexity regions in protein sequences.",
123 metavar="TRUE/FALSE",
124 )
125
126 trans_grp.add_option(
127 "",
128 "--subsample",
129 dest="subsample",
130 action="store",
131 type="float",
132 default=None,
133 help="Return a random subsample of the sequences.",
134 metavar="FRACTION",
135 )
136
137 trans_grp.add_option(
138 "",
139 "--reverse",
140 dest="reverse",
141 action="store_true",
142 default=False,
143 help="reverse sequences",
144 metavar="TRUE/FALSE",
145 )
146
147 trans_grp.add_option(
148 "",
149 "--complement",
150 dest="complement",
151 action="store_true",
152 default=False,
153 help="complement DNA sequences",
154 metavar="TRUE/FALSE",
155 )
156
157 # Writers
158 out_formats = []
159 for f in seq_io.formats:
160 if hasattr(f, "write"):
161 out_formats.append(f)
162 out_choices = {}
163 for f in out_formats:
164 out_choices[f.names[0]] = f # type: ignore
165 out_names = [f.names[0] for f in out_formats] # type: ignore
166
167 io_grp.add_option(
168 "-F",
169 "--format-out",
170 dest="writer",
171 action="store",
172 type="dict",
173 default=seq_io.fasta_io,
174 choices=out_choices,
175 help="Multiple sequence alignment output format: (%s) (Default: fasta)"
176 % ", ".join(out_names),
177 metavar="FORMAT",
178 )
179
180 return parser