Skip to content
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

Fasta more dict-like behaviour #156

Merged
merged 7 commits into from
Dec 10, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions pyfaidx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -997,13 +997,13 @@ def __init__(self,
sequence_always_upper=sequence_always_upper,
rebuild=rebuild,
build_index=build_index)
self.keys = self.faidx.index.keys
if not self.mutable:
self.records = dict(
[(rname, FastaRecord(rname, self)) for rname in self.keys()])
self.records = {rname: FastaRecord(rname, self)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! You can ignore my previous comment.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like dictionary key insertion order is the issue, and I will update here to do something similar to what the Fasta.keys method currently relies on:

self.index = OrderedDict()

for rname in self.faidx.index.keys()}
elif self.mutable:
self.records = dict([(rname, MutableFastaRecord(rname, self))
for rname in self.keys()])
self.records = {rname: MutableFastaRecord(rname, self)
for rname in self.faidx.index.keys()}


def __contains__(self, rname):
"""Return True if genome contains record."""
Expand Down Expand Up @@ -1060,6 +1060,15 @@ def get_spliced_seq(self, name, intervals, rc=False):
# len(Sequence.seq) != end - start
return Sequence(name=name, seq=seq, start=None, end=None)

def keys(self):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that this brings clarity to the methods.

return self.records.keys()

def values(self):
return self.records.values()

def items(self):
return self.records.items()

def close(self):
self.__exit__()

Expand Down