fn analyze_bam_positions (input: &str , positions: &HashMap<String,Vec<u64>>, ref_fasta: &str, threads: &usize) -> Vec<Pileup>{
	assert!(
		Path::new(input).exists(),
		"ERROR: BAM file {} does not exist!",
		input,
	);
	// for the threads we need to have to first clone everything
	// otherwise the "scope" part cant share the common variables
	let input_local     = &input.to_owned();
	let positions_local = positions.clone();
	// less ideal but we have thanks to clap a reference 
	// but want to avoid double referencing, therefore the
	// work-around here
	let ref_local       = <&str>::clone(&ref_fasta);
	//let cpus = 2;
	//let n_chrom =positions_local.len();
	//let chunk_len = (n_chrom as u32/threads) as usize + 1 ;
	//eprintln!("INFO: Foundchromosomes: {} divided by threads {}", n_chrom, chunk_len);
	//let reference_local = ref_fasta.to_owned();
	let thread_results : Vec<Pileup> ;
	let (snd, rxv) = unbounded();
	rayon::ThreadPoolBuilder::new().num_threads(*threads).build_global().unwrap();
	eprintln!("Current thread-pool number: {:?}",rayon::current_num_threads());
	// now we launch for each position a new
	// pileup call and send the result to the receiver
	rayon::scope( |child| {
		for chr in positions_local.keys().sorted() {
			for pos in positions_local.get(chr).unwrap().iter().sorted(){
				// cloning the sender will still send everything into same receiver
				let snd_local = snd.clone();
				// now we define how many threads should be used
				child.spawn( move |_| {
					//eprintln!("Current thread index: {:?}",rayon::current_thread_index());
					let mut bam_file = bam::IndexedReader::from_path(input_local).unwrap();
					let tmp_result = match ref_local { 
						"NONE" => {
									fetch_position(&mut bam_file,chr,pos,None)
								}
								,
						_     => {
									let faidx = IndexedReader::from_file(&ref_local).unwrap();
									fetch_position(&mut bam_file,chr,pos,Some(faidx))
								}
					};
					snd_local.send(tmp_result).expect("ERROR: thread could not communicate result!");
				});	
			}
		}
	
	});
	// we need to close afterwards 
	// the original sender as it otherwise still waits
	drop(snd);
	thread_results = rxv.iter().collect();
	thread_results
}
// This reads now the BAM file and uses the positions
// to check for each position the observed number of nucleotides
// It sorts as well again by chromosome and position to be safe
This reads now the BAM file and uses the positions
to check for each position the observed number of nucleotides
It sorts as well again by chromosome and position to be safe
This reads now the BAM file and uses the positions to check for each position the observed number of nucleotides It sorts as well again by chromosome and position to be safe
fn analyze_bam_positions (input: &str , positions: &HashMap<String,Vec<u64>>, ref_fasta: &str, threads: &usize) -> Vec<Pileup>{
	assert!(
		Path::new(input).exists(),
		"ERROR: BAM file {} does not exist!",
		input,
	);
	// for the threads we need to have to first clone everything
	// otherwise the "scope" part cant share the common variables
	let input_local     = &input.to_owned();
	let positions_local = positions.clone();
	// less ideal but we have thanks to clap a reference 
	// but want to avoid double referencing, therefore the
	// work-around here
	let ref_local       = <&str>::clone(&ref_fasta);
	//let cpus = 2;
	//let n_chrom =positions_local.len();
	//let chunk_len = (n_chrom as u32/threads) as usize + 1 ;
	//eprintln!("INFO: Foundchromosomes: {} divided by threads {}", n_chrom, chunk_len);
	//let reference_local = ref_fasta.to_owned();
	let thread_results : Vec<Pileup> ;
	let (snd, rxv) = unbounded();
	rayon::ThreadPoolBuilder::new().num_threads(*threads).build_global().unwrap();
	eprintln!("Current thread-pool number: {:?}",rayon::current_num_threads());
	// now we launch for each position a new
	// pileup call and send the result to the receiver
	rayon::scope( |child| {
		for chr in positions_local.keys().sorted() {
			for pos in positions_local.get(chr).unwrap().iter().sorted(){
				// cloning the sender will still send everything into same receiver
				let snd_local = snd.clone();
				// now we define how many threads should be used
				child.spawn( move |_| {
					//eprintln!("Current thread index: {:?}",rayon::current_thread_index());
					let mut bam_file = bam::IndexedReader::from_path(input_local).unwrap();
					let tmp_result = match ref_local { 
						"NONE" => {
									fetch_position(&mut bam_file,chr,pos,None)
								}
								,
						_     => {
									let faidx = IndexedReader::from_file(&ref_local).unwrap();
									fetch_position(&mut bam_file,chr,pos,Some(faidx))
								}
					};
					snd_local.send(tmp_result).expect("ERROR: thread could not communicate result!");
				});	
			}
		}
	});
	// we need to close afterwards 
	// the original sender as it otherwise still waits
	drop(snd);
	thread_results = rxv.iter().collect();
	thread_results
}
fn analyze_bam_positions ( input : & str , positions : & HashMap < String , Vec < u64 > > , ref_fasta : & str , threads : & usize ) -> Vec < Pileup > { assert ! ( Path new ( input ) exists ( ) " " input ) ; // for the threads we need to have to first clone everything // otherwise the "scope" part cant share the common variables let input_local = & input . to_owned ( ) ; let positions_local = positions . clone ( ) ; // less ideal but we have thanks to clap a reference  // but want to avoid double referencing, therefore the // work-around here let ref_local = < & str > :: clone ( & ref_fasta ) ; //let cpus = 2; //let n_chrom =positions_local.len(); //let chunk_len = (n_chrom as u32/threads) as usize + 1 ; //eprintln!("INFO: Foundchromosomes: {} divided by threads {}", n_chrom, chunk_len); //let reference_local = ref_fasta.to_owned(); let thread_results : Vec < Pileup > ; let ( snd , rxv ) = unbounded ( ) ; rayon :: ThreadPoolBuilder :: new ( ) . num_threads ( * threads ) . build_global ( ) . unwrap ( ) ; eprintln ! ( " " rayon current_num_threads ( ) ) ; // now we launch for each position a new // pileup call and send the result to the receiver rayon :: scope ( | child | { for chr in positions_local . keys ( ) . sorted ( ) { for pos in positions_local . get ( chr ) . unwrap ( ) . iter ( ) . sorted ( ) { // cloning the sender will still send everything into same receiver let snd_local = snd . clone ( ) ; // now we define how many threads should be used child . spawn ( move | _ | { //eprintln!("Current thread index: {:?}",rayon::current_thread_index()); let mut bam_file = bam :: IndexedReader :: from_path ( input_local ) . unwrap ( ) ; let tmp_result = match ref_local { " " => { fetch_position ( & mut bam_file , chr , pos , None ) } , _ => { let faidx = IndexedReader :: from_file ( & ref_local ) . unwrap ( ) ; fetch_position ( & mut bam_file , chr , pos , Some ( faidx ) ) } } ; snd_local . send ( tmp_result ) . expect ( " " ) ; } ) ; } } } ) ; // we need to close afterwards  // the original sender as it otherwise still waits drop ( snd ) ; thread_results = rxv . iter ( ) . collect ( ) ; thread_results }
This reads now the BAM file and uses the positions
to check for each position the observed number of nucleotides
It sorts as well again by chromosome and position to be safe
This reads now the BAM file and uses the positions to check for each position the observed number of nucleotides It sorts as well again by chromosome and position to be safe
input
&str
positions
&HashMap<String,Vec<u64>>
ref_fasta
&str
threads
&usize
input
&str
positions
&HashMap<String,Vec<u64>>
ref_fasta
&str
threads
&usize