|
12 | 12 | import getopt |
13 | 13 | from typing import Callable, Generator, OrderedDict |
14 | 14 |
|
15 | | -from Bio import SeqIO, StreamModeError, SeqFeature |
| 15 | +from Bio import SeqIO, StreamModeError, SeqFeature, Seq |
16 | 16 | import gffutils |
17 | 17 | from gffutils import biopython_integration |
18 | 18 |
|
@@ -130,22 +130,36 @@ def _allow_single(records): |
130 | 130 | return records |
131 | 131 |
|
132 | 132 |
|
133 | | -def _to_SeqRecord(records): |
| 133 | +def _to_SeqRecord(record): |
| 134 | + """ |
| 135 | + Support generating a single new record in JMESPath |
| 136 | + :param record: dict of SeqRecord constructor parameters |
| 137 | + :return: SeqRecord |
| 138 | + """ |
| 139 | + seq = record.get('seq', {}) |
| 140 | + del record['seq'] |
| 141 | + if isinstance(seq, str): |
| 142 | + seq = Seq.Seq(seq) |
| 143 | + elif isinstance(seq, dict): |
| 144 | + seq = Seq.Seq(**seq) |
| 145 | + return SeqIO.SeqRecord(seq=seq, **record) |
| 146 | + |
| 147 | + |
| 148 | +def _to_SeqRecords(records): |
134 | 149 | """ |
135 | 150 | Helper to convert all output records to SeqRecords |
136 | 151 | :param records: dict or SeqRecord |
137 | 152 | :return: SeqRecord |
138 | 153 | """ |
139 | 154 | if isinstance(records, dict): |
140 | | - # Support generating a single new record in JMESPath |
141 | | - records = SeqIO.SeqRecord(**records) |
| 155 | + records = _to_SeqRecord(records) |
142 | 156 |
|
143 | 157 | records = _allow_single(records) |
144 | 158 |
|
145 | | - return map(lambda r: SeqIO.SeqRecord(**r) if isinstance(records, dict) else r, records) |
| 159 | + return map(lambda r: _to_SeqRecord(r) if isinstance(r, dict) else r, records) |
146 | 160 |
|
147 | 161 |
|
148 | | -def get_records(input_handle, input_type: str, jpath: str = '', xform: Callable = _to_SeqRecord): |
| 162 | +def get_records(input_handle, input_type: str, jpath: str = '', xform: Callable = _to_SeqRecords): |
149 | 163 | """ |
150 | 164 | Read in records and apply optional jmespath |
151 | 165 | :param input_handle: File handle to read data from |
@@ -314,7 +328,7 @@ def convert(input_path: pathlib.Path, input_type: str, output_path: pathlib.Path |
314 | 328 | :param stats: File handle to output GFF3 summary of output records |
315 | 329 | :return: None |
316 | 330 | """ |
317 | | - xform = _to_SeqRecord |
| 331 | + xform = _to_SeqRecords |
318 | 332 | with input_path.open("r") as handle: |
319 | 333 | if output_type == 'text': |
320 | 334 | writer = lambda records, fh, t: fh.write("\n".join(map(str, to_strings(records))) + "\n") |
|
0 commit comments