fn fetch_position(bam: & mut bam::IndexedReader, chr: &str, pos:&u64, ref_file: Option<IndexedReader<fs::File>>) -> Pileup {
	// NOTE:
	// SAM is 1 based and not 0 based, need to correct for that in
	let start = *pos ;
	let end   = *pos +1 ;
	let mut with_ref = false;
	// this obtains now the pileup at that
	// given position
	bam.fetch((chr,start,end)).expect("ERROR: could not fetch region");
	// currently we ignore completely clipping
	let mut collection : Pileup = Pileup { vaf: 0_f32, raf: 1_f32, ..Default::default() };
	match ref_file {
		Some(mut x) => {
				with_ref = true;
				x.fetch(chr,*pos,pos+1).expect("ERROR: could not fetch interval on reference ");
				let mut ref_seq = Vec::new();
				x.read(&mut ref_seq).expect("ERROR: could not read sequence from reference");
				collection.reference = String::from(from_utf8(&ref_seq).unwrap())
			}
		None => {collection.reference = String::from("NA"); collection.mutated = false }
	}
	collection.chromosome = chr.to_string();
	collection.start   = start;
	collection.end     = end;
	for pile in bam.pileup().map(|x| x.expect("ERROR: could not parse BAM file")){
		// now we only care about the position we inquire
		//dbg!(&pile);
		if pile.pos() as u64 == start {
			collection.depth = pile.depth();
			for alignment in pile.alignments(){
				// some have none, no idea why
				match alignment.indel() {
					bam::pileup::Indel::Ins(_len) => {collection.ins += 1; continue } ,
    				bam::pileup::Indel::Del(_len) => {collection.del += 1; continue },
    				bam::pileup::Indel::None => ()
				}
				// sometimes we get reads with 0 length, no idea why
				if alignment.record().seq_len() == 0 { continue }
				let qpos = match  alignment.qpos(){
					Some(q) => q,
					_ =>  continue,
				};
				let nuc  = &alignment.record().seq().as_bytes()[qpos..qpos+1];
				let nuc1 = from_utf8(nuc).unwrap();
				match nuc1.to_uppercase().as_str() {
					"A" => collection.nuc_a += 1  ,
					"T" => collection.nuc_t += 1  ,
					"C" => collection.nuc_c += 1  ,
					"G" => collection.nuc_g += 1  ,
					_ => collection.ambigious += 1  ,
				}
			}
			break;
		}
	};
	if with_ref {
		eval_mutation(collection)
	}else{
		collection
	}
}
// this sub-function gets the iterator result from the positions
// and return the result of pileup analysis at that given position
// allows to potentially already parallelize over each position analyzed
this sub-function gets the iterator result from the positions
and return the result of pileup analysis at that given position
allows to potentially already parallelize over each position analyzed
this sub - function gets the iterator result from the positions and return the result of pileup analysis at that given position allows to potentially already parallelize over each position analyzed
fn fetch_position(bam: & mut bam::IndexedReader, chr: &str, pos:&u64, ref_file: Option<IndexedReader<fs::File>>) -> Pileup {
	// NOTE:
	// SAM is 1 based and not 0 based, need to correct for that in
	let start = *pos ;
	let end   = *pos +1 ;
	let mut with_ref = false;
	// this obtains now the pileup at that
	// given position
	bam.fetch((chr,start,end)).expect("ERROR: could not fetch region");
	// currently we ignore completely clipping
	let mut collection : Pileup = Pileup { vaf: 0_f32, raf: 1_f32, ..Default::default() };
	match ref_file {
		Some(mut x) => {
				with_ref = true;
				x.fetch(chr,*pos,pos+1).expect("ERROR: could not fetch interval on reference ");
				let mut ref_seq = Vec::new();
				x.read(&mut ref_seq).expect("ERROR: could not read sequence from reference");
				collection.reference = String::from(from_utf8(&ref_seq).unwrap())
			}
		None => {collection.reference = String::from("NA"); collection.mutated = false }
	}
	collection.chromosome = chr.to_string();
	collection.start   = start;
	collection.end     = end;
	for pile in bam.pileup().map(|x| x.expect("ERROR: could not parse BAM file")){
		// now we only care about the position we inquire
		//dbg!(&pile);
		if pile.pos() as u64 == start {
			collection.depth = pile.depth();
			for alignment in pile.alignments(){
				// some have none, no idea why
				match alignment.indel() {
					bam::pileup::Indel::Ins(_len) => {collection.ins += 1; continue } ,
    				bam::pileup::Indel::Del(_len) => {collection.del += 1; continue },
    				bam::pileup::Indel::None => ()
				}
				// sometimes we get reads with 0 length, no idea why
				if alignment.record().seq_len() == 0 { continue }
				let qpos = match  alignment.qpos(){
					Some(q) => q,
					_ =>  continue,
				};
				let nuc  = &alignment.record().seq().as_bytes()[qpos..qpos+1];
				let nuc1 = from_utf8(nuc).unwrap();
				match nuc1.to_uppercase().as_str() {
					"A" => collection.nuc_a += 1  ,
					"T" => collection.nuc_t += 1  ,
					"C" => collection.nuc_c += 1  ,
					"G" => collection.nuc_g += 1  ,
					_ => collection.ambigious += 1  ,
				}
			}
			break;
		}
	};
	if with_ref {
		eval_mutation(collection)
	}else{
		collection
	}
}
fn fetch_position ( bam : & mut bam :: IndexedReader , chr : & str , pos : & u64 , ref_file : Option < IndexedReader < fs :: File > > ) -> Pileup { // NOTE: // SAM is 1 based and not 0 based, need to correct for that in let start = * pos ; let end = * pos + 1 ; let mut with_ref = false ; // this obtains now the pileup at that // given position bam . fetch ( ( chr , start , end ) ) . expect ( " " ) ; // currently we ignore completely clipping let mut collection : Pileup = Pileup { vaf : 0_f32 , raf : 1_f32 , .. Default :: default ( ) } ; match ref_file { Some ( mut x ) => { with_ref = true ; x . fetch ( chr , * pos , pos + 1 ) . expect ( " " ) ; let mut ref_seq = Vec :: new ( ) ; x . read ( & mut ref_seq ) . expect ( " " ) ; collection . reference = String :: from ( from_utf8 ( & ref_seq ) . unwrap ( ) ) } None => { collection . reference = String :: from ( " " ) ; collection . mutated = false } } collection . chromosome = chr . to_string ( ) ; collection . start = start ; collection . end = end ; for pile in bam . pileup ( ) . map ( | x | x . expect ( " " ) ) { // now we only care about the position we inquire //dbg!(&pile); if pile . pos ( ) as u64 == start { collection . depth = pile . depth ( ) ; for alignment in pile . alignments ( ) { // some have none, no idea why match alignment . indel ( ) { bam :: pileup :: Indel :: Ins ( _len ) => { collection . ins += 1 ; continue } , bam :: pileup :: Indel :: Del ( _len ) => { collection . del += 1 ; continue } , bam :: pileup :: Indel :: None => ( ) } // sometimes we get reads with 0 length, no idea why if alignment . record ( ) . seq_len ( ) == 0 { continue } let qpos = match alignment . qpos ( ) { Some ( q ) => q , _ => continue , } ; let nuc = & alignment . record ( ) . seq ( ) . as_bytes ( ) [ qpos .. qpos + 1 ] ; let nuc1 = from_utf8 ( nuc ) . unwrap ( ) ; match nuc1 . to_uppercase ( ) . as_str ( ) { " " => collection . nuc_a += 1 , " " => collection . nuc_t += 1 , " " => collection . nuc_c += 1 , " " => collection . nuc_g += 1 , _ => collection . ambigious += 1 , } } break ; } } ; if with_ref { eval_mutation ( collection ) } else { collection } }
this sub-function gets the iterator result from the positions
and return the result of pileup analysis at that given position
allows to potentially already parallelize over each position analyzed
this sub - function gets the iterator result from the positions and return the result of pileup analysis at that given position allows to potentially already parallelize over each position analyzed
bam
& mut bam::IndexedReader
chr
&str
pos
&u64
ref_file
Option<IndexedReader<fs::File>>
bam
& mut bam::IndexedReader
chr
&str
pos
&u64
ref_file
Option<IndexedReader<fs::File>>