generated from snakemake-workflows/snakemake-workflow-template
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: use buffered and script based get-reads implementation #126
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
Merged
Merged
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b415c7c
fix: use buffered and script based get-reads implementation
johanneskoester 30b94d8
Update workflow/scripts/get-reads.py
johanneskoester 38ee6f5
Update workflow/scripts/get-reads.py
johanneskoester 27bad31
Update workflow/rules/download.smk
johanneskoester 7f7a4c6
mode
johanneskoester e063c93
fixes
johanneskoester d0820d3
fix
johanneskoester 0f7538f
remove read from buffer when writing fastq record
johanneskoester 11961b9
Update workflow/scripts/get-reads.py
johanneskoester 8c7c4a9
dbg
johanneskoester 582e1fc
Merge branch 'fix/get-reads-buffered' of github.com:snakemake-workflo…
johanneskoester 9cdd2c9
fix lookup default
johanneskoester 3c3e533
assertion
johanneskoester fbef952
fix
johanneskoester 2e86719
fmt
johanneskoester 7b2cbcc
update wrapper
johanneskoester 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 hidden or 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 |
---|---|---|
|
@@ -5,4 +5,5 @@ channels: | |
dependencies: | ||
- python =3.9 | ||
- pysam =0.18 | ||
- pandas =1.3 | ||
- pandas =1.3 | ||
- dnaio =1.2 |
This file contains hidden or 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 hidden or 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 hidden or 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,47 @@ | ||
import sys | ||
|
||
sys.stderr = open(snakemake.log[0], "w") | ||
|
||
import pysam | ||
import dnaio | ||
|
||
|
||
def aln_to_fq(qname, aln): | ||
return dnaio.SequenceRecord( | ||
name=qname, | ||
sequence=aln.get_forward_sequence(), | ||
qualities="".join( | ||
map(lambda qual: chr(qual + 33), aln.get_forward_qualities()) | ||
), | ||
) | ||
johanneskoester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
limit = snakemake.params.limit | ||
bam = pysam.AlignmentFile(snakemake.params.bam_url) | ||
johanneskoester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
buffer = {} | ||
n_written = 0 | ||
with dnaio.open(snakemake.output[0], snakemake.output[1], mode="w") as fqwriter: | ||
for aln in bam: | ||
if limit is not None and n_written >= limit: | ||
johanneskoester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break | ||
if aln.is_secondary or aln.is_supplementary: | ||
continue | ||
|
||
# Some aligners (e.g. minimap2) add /1 and /2 to the read name. | ||
# We remove them here to get the same name for both reads of a pair. | ||
qname = aln.query_name.removesuffix("/1").removesuffix("/2") | ||
|
||
mate_aln = buffer.get(qname) | ||
if mate_aln is None: | ||
buffer[qname] = aln | ||
else: | ||
if aln.is_read2: | ||
aln, mate_aln = mate_aln, aln | ||
del buffer[qname] | ||
|
||
fqwriter.write(aln_to_fq(qname, aln), aln_to_fq(qname, mate_aln)) | ||
n_written += 1 | ||
johanneskoester marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if buffer: | ||
print(f"Warning: {len(buffer)} reads had no mate pairs and were skipped", file=sys.stderr) |
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.
Uh oh!
There was an error while loading. Please reload this page.