Skip to content

Commit

Permalink
will need to improve filtering in the imputation module
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonfparil committed Nov 9, 2023
1 parent ae07074 commit 4005297
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod base;
mod gp;
mod gwas;
mod imputation;
mod simulation;
mod popgen;
mod tables;
use base::{ChunkyReadAnalyseWrite, CrossValidation, LoadAll, Parse, SaveCsv};
Expand All @@ -17,6 +18,7 @@ use gp::{
};
use gwas::*;
use imputation::*;
use simulation::*;

Check warning on line 21 in src/main.rs

View workflow job for this annotation

GitHub Actions / Compile

unused import: `simulation::*`

Check warning on line 21 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused import: `simulation::*`

Check warning on line 21 in src/main.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused import: `simulation::*`
use popgen::*;

#[derive(Parser, Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub use self::{simulate_genotypes::*};
pub use self::simulate_genotypes::*;

mod simulate_genotypes;
49 changes: 46 additions & 3 deletions src/simulation/simulate_genotypes.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,57 @@
use crate::base::*;

Check warning on line 1 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Compile

unused import: `crate::base`

Check warning on line 1 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused import: `crate::base`

Check warning on line 1 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused import: `crate::base`
use ndarray::prelude::*;
use std::io;
use statrs::distribution::{MultivariateNormal, Continuous};
use statrs::distribution::{Uniform, MultivariateNormal, Continuous};

Check warning on line 4 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Compile

unused import: `Continuous`

Check warning on line 4 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused import: `Continuous`

Check warning on line 4 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused import: `Continuous`
use rand::distributions::Distribution;
use rand::{seq::IteratorRandom, thread_rng};

Check warning on line 6 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Compile

unused import: `thread_rng`

Check warning on line 6 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused import: `thread_rng`

Check warning on line 6 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused import: `thread_rng`

// Simulate genotypes with some linkage disequilibrium

fn simulate_genotypes(n: u64, p: u64, n_chr: u64, max_bp: u64, r2_50_perc_bp: u64) -> io::Result<Array2<f64>> {
pub fn simulate_genotypes(n: usize, p: usize, n_chr: usize, max_bp: usize, r2_50_perc_bp: usize) -> io::Result<Array2<f64>> {

Check warning on line 10 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Compile

unused variable: `n`

Check warning on line 10 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused variable: `n`

Check warning on line 10 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused variable: `n`
// Define approximately equally sized chromosomes and coordinates of the characterised loci or markers or SNPs
let mut chromosome_sizes = vec![p/n_chr; n_chr];
let p_ = chromosome_sizes.iter().fold(0, |sum, &x| sum + x);
// If the we have less or more than the required number of loci we add or subtract loci from the last chromosome
if p_ < p {
chromosome_sizes[n_chr-1] += p - p_;
} else if p_ > p {
chromosome_sizes[n_chr-1] -= p_ - p;
}
let max_bp_per_chr = max_bp/n_chr;
println!("Chromosome sizes: {:?}", chromosome_sizes);
// Sample uniformly distributed loci per chromosome
let dist_unif = Uniform::new(0.0, max_bp_per_chr as f64).unwrap();
println!("Initialised the uniform distribution");
let mut rng = rand::thread_rng();
let mut idx: usize = 0;
let mut chromosomes: Array1<String> = Array1::from_elem(p, "".to_owned());
let mut positions: Array1<usize> = Array1::from_elem(p, 0);
let mut alleles: Array1<String> = Array1::from_elem(p, "".to_owned());
let atcgd = vec!["A", "T", "C", "G", "D"].iter().map(|&x| x.to_owned()).collect::<Vec<String>>();
for i in 0..n_chr {
// Total number of sites we want to sample in the chromosome
let s = chromosome_sizes[i];
let mut tmp_positions: Vec<usize> = vec![];
for _ in 0..s {
tmp_positions.push(dist_unif.sample(&mut rng).floor() as usize);
}
tmp_positions.sort();
// println!("tmp_positions={:?}", tmp_positions);
for j in 0..s {
chromosomes[idx] = "chr".to_owned() + &i.to_string();
positions[idx] = tmp_positions[j];
alleles[idx] = atcgd.iter().choose_multiple(&mut rng, 1)[0].to_owned();
idx += 1;
}
}
// println!("chromosomes:\n{:?}", chromosomes);
// println!("positions:\n{:?}", positions);
// println!("alleles:\n{:?}", alleles);

let mvn = MultivariateNormal::new(vec![0., 0.], vec![1., 0., 0., 1.]).unwrap();

Check warning on line 51 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Compile

unused variable: `mvn`

Check warning on line 51 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, stable)

unused variable: `mvn`

Check warning on line 51 in src/simulation/simulate_genotypes.rs

View workflow job for this annotation

GitHub Actions / Test (windows-latest, stable)

unused variable: `mvn`
return(Array2::from_shape_elem((n, p), f64::NAN))


return Ok(Array2::from_elem((1, 1), f64::NAN))
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -23,6 +60,12 @@ mod tests {
use super::*;
#[test]
fn test_simulate_genotypes() {
let n = 100;
let p = 10_000;
let n_chr = 7;
let max_bp = 2.2e9 as usize;
let r2_50_perc_bp = 10e6 as usize;
let q = simulate_genotypes(n, p, n_chr, max_bp, r2_50_perc_bp).unwrap();
assert_eq!(0, 1);
}
}

0 comments on commit 4005297

Please sign in to comment.