Skip to content

Commit

Permalink
feat: anonymize bam - adapt mandatory and aux fields (#219)
Browse files Browse the repository at this point in the history
* adapt mandatory fields

* make release-please happy

* remove variable

* move to distinct function
  • Loading branch information
FelixMoelder authored Dec 19, 2021
1 parent 93f5f5c commit e6ffdea
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
37 changes: 27 additions & 10 deletions src/bam/anonymize_reads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,17 @@ fn init_altered_bases(

fn build_record(record: &bam::Record, artificial_seq: &[u8], offset: i64) -> Result<bam::Record> {
let mut artificial_record = bam::record::Record::new();
if let Ok(mate_cigar) = record.aux(b"MC") {
artificial_record.push_aux(b"MC", mate_cigar)?;
}
artificial_record.set(
record.qname(),
Some(&record.cigar()),
artificial_seq,
record.qual(),
);
artificial_record.set_pos(record.pos() - offset);
artificial_record.set_tid(0);
artificial_record.set_mtid(0);
artificial_record.set_mpos(record.mpos() - offset);
artificial_record.set_flags(record.flags());
artificial_record.set_insert_size(record.insert_size());
artificial_record.set_mapq(record.mapq());
set_mandatory_fields(&mut artificial_record, record, offset)?;
for aux_result in record.aux_iter() {
let (tag, aux_field) = aux_result?;
artificial_record.push_aux(tag, aux_field)?;
}
Ok(artificial_record)
}

Expand Down Expand Up @@ -169,6 +164,28 @@ fn build_sequence(
Ok(artificial_seq)
}

fn set_mandatory_fields(
target_rec: &mut bam::Record,
source_rec: &bam::Record,
offset: i64,
) -> Result<()> {
target_rec.set_pos(source_rec.pos() - offset);
target_rec.set_tid(0);
let (mtid, mpos) = if source_rec.mtid() == -1 {
(-1, 0)
} else if source_rec.mtid() == source_rec.tid() {
(0, source_rec.mpos() - offset)
} else {
(1, source_rec.mpos())
};
target_rec.set_mtid(mtid);
target_rec.set_mpos(mpos);
target_rec.set_flags(source_rec.flags());
target_rec.set_insert_size(source_rec.insert_size());
target_rec.set_mapq(source_rec.mapq());
Ok(())
}

fn add_random_bases(
length: u64,
seq: &mut Vec<u8>,
Expand Down
2 changes: 0 additions & 2 deletions src/bcf/match_variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ pub fn match_variants<P: AsRef<Path>>(matchbcf: P, max_dist: u32, max_len_diff:
#[derive(Debug)]
pub struct Variant {
id: u32,
rid: u32,
pos: u64,
alleles: Vec<VariantType>,
}
Expand Down Expand Up @@ -206,7 +205,6 @@ impl Variant {
};
let var = Variant {
id: *id,
rid: rec.rid().unwrap(),
pos: pos as u64,
alleles: _alleles,
};
Expand Down
2 changes: 0 additions & 2 deletions src/bcf/report/oncoprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -987,8 +987,6 @@ struct Record {
sample: String,
gene: String,
#[new(default)]
dna_alteration: Vec<String>,
#[new(default)]
variants: Vec<String>,
}

Expand Down
12 changes: 5 additions & 7 deletions src/fastq/collapse_reads_to_fragments/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ fn isize_pmf(value: f64, mean: f64, sd: f64) -> LogProb {
#[derive(Debug)]
struct FastqStorage {
db: DB,
storage_dir: std::path::PathBuf,
}

impl FastqStorage {
Expand All @@ -74,8 +73,7 @@ impl FastqStorage {
// in turn deleting the tempdir
let storage_dir = tempdir()?.path().join("db");
Ok(FastqStorage {
db: DB::open_default(storage_dir.clone())?,
storage_dir,
db: DB::open_default(storage_dir)?,
})
}

Expand Down Expand Up @@ -351,11 +349,11 @@ impl<'a, R: io::Read + io::BufRead, W: io::Write> CallConsensusReads<'a, R, W>
}

fn fq1_reader(&mut self) -> &mut fastq::Reader<R> {
&mut self.fq1_reader
self.fq1_reader
}

fn fq2_reader(&mut self) -> &mut fastq::Reader<R> {
&mut self.fq2_reader
self.fq2_reader
}

fn umi_len(&self) -> usize {
Expand Down Expand Up @@ -506,11 +504,11 @@ impl<'a, R: io::Read + io::BufRead, W: io::Write> CallConsensusReads<'a, R, W>
}

fn fq1_reader(&mut self) -> &mut fastq::Reader<R> {
&mut self.fq1_reader
self.fq1_reader
}

fn fq2_reader(&mut self) -> &mut fastq::Reader<R> {
&mut self.fq2_reader
self.fq2_reader
}

fn umi_len(&self) -> usize {
Expand Down

0 comments on commit e6ffdea

Please sign in to comment.