diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 964b0df..c8af8bc 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -21,11 +21,11 @@ jobs: with: rust-version: stable - - name: cargo test - run: cargo test --all-features - - name: rustfmt run: cargo fmt --all -- --check - name: clippy - run: cargo clippy --all --all-features --tests -- -D warnings \ No newline at end of file + run: cargo clippy --all --all-features --tests -- -D warnings + + - name: cargo test + run: cargo test --all-features \ No newline at end of file diff --git a/.gitignore b/.gitignore index 99e209b..ef1781c 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ tarpaulin-*.html edge-cases.md *.fasta -*.msh \ No newline at end of file +*.msh +uv.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 1ea1677..0c49ffb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "grumpy" -version = "1.2.3" +version = "1.2.4" edition = "2021" description = "Genetic analysis in Rust." license-file = "LICENSE" diff --git a/pyproject.toml b/pyproject.toml index 3c28588..0d17c77 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -18,7 +18,7 @@ classifiers = [ description = "gumpy re-implemented in Rust" readme = "README-python.md" license = "University of Oxford, see license file for details." -license-files = ["LICENSE"] +license-file = "LICENSE" dynamic = ["version"] authors = [ { name = "Jeremy Westhead", email = "jeremy.westhead@ndm.ox.ac.uk" } diff --git a/src/gene.rs b/src/gene.rs index c129d2b..63f8e14 100644 --- a/src/gene.rs +++ b/src/gene.rs @@ -18,6 +18,40 @@ pub enum GenePos { Codon(CodonType), } +#[pymethods] +impl GenePos { + pub fn __repr__(&self) -> String { + match self { + GenePos::Nucleotide(nuc) => format!("GenePos::Nucleotide({:?})", nuc), + GenePos::Codon(codon) => format!("GenePos::Codon({:?})", codon), + } + } + pub fn get_bases(&self) -> Vec { + match self { + GenePos::Nucleotide(nuc) => vec![nuc.reference], + GenePos::Codon(codon) => codon.codon.iter().map(|n| n.reference).collect(), + } + } + pub fn get_aa(&self) -> Option { + match self { + GenePos::Nucleotide(_) => None, + GenePos::Codon(codon) => Some(codon.amino_acid), + } + } + pub fn get_start_index(&self) -> i64 { + match self { + GenePos::Nucleotide(nuc) => nuc.nucleotide_index, + GenePos::Codon(codon) => codon.codon[0].nucleotide_index, + } + } + pub fn get_end_index(&self) -> i64 { + match self { + GenePos::Nucleotide(nuc) => nuc.nucleotide_index, + GenePos::Codon(codon) => codon.codon[2].nucleotide_index, + } + } +} + #[pyclass(eq, from_py_object)] #[derive(Clone, Debug, Eq, PartialEq)] /// Tracks each constituent nucleotide in the codon, along with the amino acid it codes for diff --git a/src/genome.rs b/src/genome.rs index 762b97c..f53b270 100644 --- a/src/genome.rs +++ b/src/genome.rs @@ -359,27 +359,39 @@ impl Genome { let mut nucleotide_index = Vec::new(); let mut genome_positions: Vec = Vec::new(); if !gene_def.reverse_complement && gene_def.promoter_start != -1 { - for idx in - (gene_def.promoter_start as usize).saturating_sub(1)..gene_def.ranges[0].0 as usize + for position in self + .genome_positions + .iter() + .take(gene_def.ranges[0].0 as usize) + .skip((gene_def.promoter_start as usize).saturating_sub(1)) { - nucleotide_sequence.push(self.genome_positions[idx].reference); - nucleotide_index.push(self.genome_positions[idx].genome_idx); - genome_positions.push(self.genome_positions[idx].clone()); + nucleotide_sequence.push(position.reference); + nucleotide_index.push(position.genome_idx); + genome_positions.push(position.clone()); } } for (start, end) in gene_def.ranges.iter() { - for i in *start as usize..*end as usize { - nucleotide_sequence.push(self.genome_positions[i].reference); - nucleotide_index.push(self.genome_positions[i].genome_idx); - genome_positions.push(self.genome_positions[i].clone()); + for position in self + .genome_positions + .iter() + .take(*end as usize) + .skip(*start as usize) + { + nucleotide_sequence.push(position.reference); + nucleotide_index.push(position.genome_idx); + genome_positions.push(position.clone()); } } if gene_def.reverse_complement && gene_def.promoter_start != -1 { - for idx in gene_def.ranges.last().unwrap().1 as usize..=gene_def.promoter_start as usize + for position in self + .genome_positions + .iter() + .take(gene_def.promoter_start as usize + 1) + .skip(gene_def.ranges.last().unwrap().1 as usize) { - nucleotide_sequence.push(self.genome_positions[idx].reference); - nucleotide_index.push(self.genome_positions[idx].genome_idx); - genome_positions.push(self.genome_positions[idx].clone()); + nucleotide_sequence.push(position.reference); + nucleotide_index.push(position.genome_idx); + genome_positions.push(position.clone()); } } // I hate implicit returns, but appease clippy diff --git a/src/lib.rs b/src/lib.rs index 972eb53..746176d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -63,6 +63,10 @@ fn grumpy(m: &Bound<'_, PyModule>) -> PyResult<()> { m.add_class::()?; m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; m.add_class::()?;