generated from bokulich-lab/q2-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 13
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
ENH: add 'filter-contigs' action #92
Draft
misialq
wants to merge
4
commits into
bokulich-lab:main
Choose a base branch
from
misialq:filter-contigs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# ---------------------------------------------------------------------------- | ||
# Copyright (c) 2023, QIIME 2 development team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file LICENSE, distributed with this software. | ||
# ---------------------------------------------------------------------------- | ||
|
||
from .filter import filter_contigs | ||
|
||
__all__ = [ | ||
"filter_contigs", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# ---------------------------------------------------------------------------- | ||
# Copyright (c) 2023, QIIME 2 development team. | ||
# | ||
# Distributed under the terms of the Modified BSD License. | ||
# | ||
# The full license is in the file LICENSE, distributed with this software. | ||
# ---------------------------------------------------------------------------- | ||
import os | ||
|
||
import skbio | ||
from q2_types.per_sample_sequences import ContigSequencesDirFmt | ||
from qiime2 import Metadata | ||
from qiime2.util import duplicate | ||
|
||
|
||
def _find_empty_samples(samples: dict) -> set: | ||
empty_samples = set() | ||
for sample_id, sample_fp in samples.items(): | ||
if os.path.getsize(sample_fp) == 0: | ||
empty_samples.add(sample_id) | ||
return empty_samples | ||
|
||
|
||
def _filter_by_length( | ||
contigs: ContigSequencesDirFmt, threshold: int | ||
) -> ContigSequencesDirFmt: | ||
results = ContigSequencesDirFmt() | ||
print( | ||
f"Filtering contigs by length - only contigs >= {threshold} bp long will " | ||
f"be retained." | ||
) | ||
for sample_id, sample_fp in contigs.sample_dict().items(): | ||
out_fp = os.path.join(str(results), f"{sample_id}_contigs.fa") | ||
keep, remove = 0, 0 | ||
with open(out_fp, "w") as f_out: | ||
for contig in skbio.io.read(sample_fp, format="fasta"): | ||
if len(contig) >= threshold: | ||
skbio.io.write(contig, format="fasta", into=f_out) | ||
keep += 1 | ||
else: | ||
remove += 1 | ||
print( | ||
f"Sample {sample_id}: {remove + keep} contigs\n {remove} contigs " | ||
f"removed\n {keep} contigs retained" | ||
) | ||
return results | ||
|
||
|
||
def filter_contigs( | ||
contigs: ContigSequencesDirFmt, | ||
metadata: Metadata = None, | ||
where: str = None, | ||
exclude_ids: bool = False, | ||
length_threshold: int = 0, | ||
remove_empty: bool = False, | ||
) -> ContigSequencesDirFmt: | ||
if length_threshold > 0: | ||
contigs = _filter_by_length(contigs, length_threshold) | ||
|
||
results = ContigSequencesDirFmt() | ||
samples = contigs.sample_dict() | ||
ids_to_keep = set(samples.keys()) | ||
if remove_empty: | ||
ids_to_remove = _find_empty_samples(samples) | ||
ids_to_keep -= ids_to_remove | ||
if ids_to_remove: | ||
print(f"Removing empty samples: {', '.join(sorted(ids_to_remove))}") | ||
|
||
if metadata: | ||
selected_ids = metadata.get_ids(where=where) | ||
if not selected_ids: | ||
print("The filter query returned no IDs to filter out.") | ||
|
||
if exclude_ids: | ||
ids_to_keep -= set(selected_ids) | ||
else: | ||
ids_to_keep &= set(selected_ids) | ||
|
||
if len(ids_to_keep) == 0: | ||
raise ValueError("No samples remain after filtering.") | ||
|
||
try: | ||
for _id in ids_to_keep: | ||
duplicate(samples[_id], os.path.join(str(results), f"{_id}_contigs.fa")) | ||
except KeyError: | ||
raise ValueError(f"{_id!r} is not a sample present in the contig data.") | ||
|
||
return results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default is already displayed