Skip to content

Commit 4e305f3

Browse files
author
Nolan Woods
committed
Fix handling of returning dict for SeqRecord constructor
1 parent 4e8d1e9 commit 4e305f3

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

biopython_convert/__init__.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import getopt
1313
from typing import Callable, Generator, OrderedDict
1414

15-
from Bio import SeqIO, StreamModeError, SeqFeature
15+
from Bio import SeqIO, StreamModeError, SeqFeature, Seq
1616
import gffutils
1717
from gffutils import biopython_integration
1818

@@ -130,22 +130,36 @@ def _allow_single(records):
130130
return records
131131

132132

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):
134149
"""
135150
Helper to convert all output records to SeqRecords
136151
:param records: dict or SeqRecord
137152
:return: SeqRecord
138153
"""
139154
if isinstance(records, dict):
140-
# Support generating a single new record in JMESPath
141-
records = SeqIO.SeqRecord(**records)
155+
records = _to_SeqRecord(records)
142156

143157
records = _allow_single(records)
144158

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)
146160

147161

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):
149163
"""
150164
Read in records and apply optional jmespath
151165
: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
314328
:param stats: File handle to output GFF3 summary of output records
315329
:return: None
316330
"""
317-
xform = _to_SeqRecord
331+
xform = _to_SeqRecords
318332
with input_path.open("r") as handle:
319333
if output_type == 'text':
320334
writer = lambda records, fh, t: fh.write("\n".join(map(str, to_strings(records))) + "\n")

0 commit comments

Comments
 (0)