-
Notifications
You must be signed in to change notification settings - Fork 43
add linked sequence formats, type, tests #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
f5bd19e
593528c
571557a
ad8a227
2e85576
d9dfe8d
f9903fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
|
|
||
| from .. import ( | ||
| TaxonomyFormat, HeaderlessTSVTaxonomyFormat, TSVTaxonomyFormat, | ||
| DNAFASTAFormat, PairedDNASequencesDirectoryFormat, | ||
| DNAFASTAFormat, LinkedDNAFASTAFormat, PairedDNASequencesDirectoryFormat, | ||
| AlignedDNAFASTAFormat, DifferentialFormat, ProteinFASTAFormat, | ||
| AlignedProteinFASTAFormat, RNAFASTAFormat, | ||
| AlignedRNAFASTAFormat, PairedRNASequencesDirectoryFormat, | ||
|
|
@@ -302,6 +302,16 @@ def _series_to_fasta_format(ff, data, sequence_type="DNA", lowercase=False): | |
| skbio.io.write(sequence, format='fasta', into=f) | ||
|
|
||
|
|
||
| def _read_linked_from_fasta(path): | ||
| return skbio.read( | ||
| path, | ||
| format='fasta', | ||
| constructor=skbio.Sequence, | ||
| lowercase=False, | ||
| keep_spaces=True | ||
| ) | ||
|
|
||
|
|
||
| # DNA Transformers | ||
| @plugin.register_transformer | ||
| def _9(ff: DNAFASTAFormat) -> DNAIterator: | ||
|
|
@@ -316,6 +326,47 @@ def _10(data: DNAIterator) -> DNAFASTAFormat: | |
| return ff | ||
|
|
||
|
|
||
| @plugin.register_transformer | ||
| def _231(ff: LinkedDNAFASTAFormat) -> DNAIterator: | ||
| generator = _read_linked_from_fasta(str(ff)) | ||
| return DNAIterator(generator) | ||
|
|
||
|
|
||
| @plugin.register_transformer | ||
| def _232(data: DNAIterator) -> LinkedDNAFASTAFormat: | ||
| ff = LinkedDNAFASTAFormat() | ||
| skbio.io.write(iter(data), format='fasta', into=str(ff)) | ||
| return ff | ||
|
|
||
|
|
||
| @plugin.register_transformer | ||
| def _233(ff: LinkedDNAFASTAFormat) -> pd.Series: | ||
| data = {} | ||
| for sequence in _read_linked_from_fasta(str(ff)): | ||
| id_ = sequence.metadata['id'] | ||
| if id_ in data: | ||
| raise ValueError( | ||
| "FASTA format sequence IDs must be unique. The following ID " | ||
| f"was found more than once: {id_}." | ||
| ) | ||
| data[id_] = sequence | ||
|
|
||
| return pd.Series(data) | ||
|
|
||
|
|
||
| @plugin.register_transformer | ||
| def _234(data: pd.Series) -> LinkedDNAFASTAFormat: | ||
| ff = LinkedDNAFASTAFormat() | ||
| with ff.open() as fh: | ||
| for id_, seq in data.items(): | ||
| sequence = skbio.Sequence( | ||
| str(seq), metadata={'id': id_}, lowercase=False | ||
| ) | ||
| skbio.io.write(sequence, format='fasta', into=fh) | ||
|
Comment on lines
+360
to
+365
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One nitpick I guess, is it beneficial to optimize this so we aren't calling write for every line? Idk how clever skbio.io.write is, but generally it seems wasteful to write each line individually instead of one big write.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it should write it as fast as any other loop, there's an internal C buffer in Python that is actually queuing up ~8kb blocks and dumping those at once (this is why |
||
|
|
||
| return ff | ||
|
|
||
|
|
||
| @plugin.register_transformer | ||
| def _11(df: PairedDNASequencesDirectoryFormat) -> PairedDNAIterator: | ||
| left = df.left_dna_sequences.view(DNAIterator) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.