forked from VUmcCGP/wisecondor
-
Notifications
You must be signed in to change notification settings - Fork 46
Code restructuring #151
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
Open
matthdsm
wants to merge
16
commits into
master
Choose a base branch
from
refactor/restructure
base: master
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.
Open
Code restructuring #151
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7c289ce
Restructure code
matthdsm bb8fab5
update Agent instructions
matthdsm e8571b4
restructure code, add refqc subcmd
matthdsm a83190e
Update AGENTS.md
matthdsm 1f73e59
fix imports for tests
matthdsm 164f511
convert: code cleanup, add tests
matthdsm 6f6656f
Add version flag + test
matthdsm 26bd06a
newref: code cleanup, add tests
matthdsm 5dc5585
Drop example scripts from docs
matthdsm 61b679d
predict: code cleanup, add tests
matthdsm 4fce027
refqc: code cleanup, add tests
matthdsm fef83ed
Replace string formatting with f-strings, bump python to >= 3.12
matthdsm 30d9a09
Update src/wisecondorx/newref.py
matthdsm 801bc04
Update src/wisecondorx/newref.py
matthdsm 8050d4c
fix: use elif/else in _generate_regions_bed to prevent X/Y chr overwrite
Copilot 9a5cde1
fix: extract .value from Sex enum when overriding gender in predict
Copilot 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,163 @@ | ||
| # WisecondorX | ||
|
|
||
| import logging | ||
|
|
||
| import sys | ||
|
|
||
| import numpy as np | ||
| import pysam | ||
| import typer | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| from typing import Annotated | ||
|
|
||
|
|
||
| def wcx_convert( | ||
| infile: Annotated[ | ||
| Path, | ||
| typer.Argument( | ||
| help="aligned reads input for conversion (.bam or .cram)", | ||
| exists=True, | ||
| file_okay=True, | ||
| dir_okay=False, | ||
| readable=True, | ||
| resolve_path=True, | ||
| ), | ||
| ], | ||
| prefix: Annotated[Path, typer.Argument(help="Output prefix")], | ||
| reference: Annotated[ | ||
| Path, | ||
| typer.Option( | ||
| "-r", | ||
| "--reference", | ||
| help="Fasta reference to be used during cram conversion", | ||
| exists=True, | ||
| file_okay=True, | ||
| dir_okay=False, | ||
| readable=True, | ||
| resolve_path=True, | ||
| ), | ||
| ] = None, | ||
| binsize: Annotated[ | ||
| int, typer.Option("--binsize", help="Bin size (bp)") | ||
| ] = 5000, | ||
| normdup: Annotated[ | ||
| bool, typer.Option("--normdup", help="Do not remove duplicates") | ||
| ] = False, | ||
| ) -> None: | ||
| """ | ||
| Convert and filter aligned reads to .npz format. | ||
| """ | ||
|
|
||
| # check if infile exists and has an index | ||
| if not (infile.exists() and infile.is_file()): | ||
| logging.error(f"Input file {infile} does not exist or is not a file.") | ||
| sys.exit(1) | ||
|
|
||
| logging.info("Importing data ...") | ||
|
|
||
| reads_per_chromosome_bin: dict[str, np.ndarray] = { | ||
| str(i): None for i in range(1, 25) | ||
| } | ||
|
|
||
| reads_seen = 0 | ||
| reads_kept = 0 | ||
| reads_mapq = 0 | ||
| reads_rmdup = 0 | ||
| reads_pairf = 0 | ||
|
|
||
| logging.info("Converting aligned reads") | ||
|
|
||
| mode = "rb" if infile.suffix == ".bam" else "rc" | ||
| ref_filename = str(reference) if reference else None | ||
|
|
||
| if mode == "rc" and not ref_filename: | ||
| logging.error( | ||
| "Cram inputs need a reference fasta provided through the '--reference' flag." | ||
| ) | ||
| sys.exit(1) | ||
|
|
||
| with pysam.AlignmentFile( | ||
| infile, mode, reference_filename=ref_filename | ||
| ) as reads_file: | ||
| for index, chr in enumerate(reads_file.references): | ||
| larp = -1 | ||
| larp2 = -1 | ||
| chr_name = chr | ||
| if chr_name[:3].lower() == "chr": | ||
| chr_name = chr_name[3:] | ||
|
|
||
| if chr_name == "X": | ||
| chr_name = "23" | ||
| elif chr_name == "Y": | ||
| chr_name = "24" | ||
|
|
||
| if chr_name not in reads_per_chromosome_bin: | ||
| continue | ||
|
|
||
| logging.info( | ||
| f"Working at {chr}; processing {int(reads_file.lengths[index] / float(binsize) + 1)} bins" | ||
| ) | ||
| counts = np.zeros( | ||
| int(reads_file.lengths[index] / float(binsize) + 1), | ||
| dtype=np.int32, | ||
| ) | ||
| bam_chr = reads_file.fetch(chr) | ||
|
|
||
| for read in bam_chr: | ||
| if read.is_paired: | ||
| if not read.is_proper_pair: | ||
| reads_pairf += 1 | ||
| continue | ||
| if ( | ||
| not normdup | ||
| and larp == read.pos | ||
| and larp2 == read.next_reference_start | ||
| ): | ||
| reads_rmdup += 1 | ||
| else: | ||
| if read.mapping_quality >= 1: | ||
| location = read.pos / binsize | ||
| counts[int(location)] += 1 | ||
| else: | ||
| reads_mapq += 1 | ||
|
|
||
| larp2 = read.next_reference_start | ||
| reads_seen += 1 | ||
| larp = read.pos | ||
| else: | ||
| if not normdup and larp == read.pos: | ||
| reads_rmdup += 1 | ||
| else: | ||
| if read.mapping_quality >= 1: | ||
| location = read.pos / binsize | ||
| counts[int(location)] += 1 | ||
| else: | ||
| reads_mapq += 1 | ||
|
|
||
| reads_seen += 1 | ||
| larp = read.pos | ||
|
|
||
| reads_per_chromosome_bin[chr_name] = counts | ||
| reads_kept += sum(counts) | ||
|
|
||
| qual_info: dict[str, int] = { | ||
| "mapped": reads_file.mapped, | ||
| "unmapped": reads_file.unmapped, | ||
| "no_coordinate": reads_file.nocoordinate, | ||
| "filter_rmdup": reads_rmdup, | ||
| "filter_mapq": reads_mapq, | ||
| "pre_retro": reads_seen, | ||
| "post_retro": reads_kept, | ||
| "pair_fail": reads_pairf, | ||
| } | ||
|
|
||
| np.savez_compressed( | ||
| Path(f"{prefix}.npz"), | ||
| binsize=binsize, | ||
| reads_per_bin=reads_per_chromosome_bin, | ||
| quality=qual_info, | ||
| ) | ||
|
|
||
| logging.info("Finished conversion") | ||
Oops, something went wrong.
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.
Input format detection treats any non-
.bamfile as CRAM (mode = "rb" if ... else "rc"). This misclassifies unsupported extensions and produces a misleading “Cram inputs need a reference” error. Add an explicit suffix check for.bam/.cramand error out for anything else.