Skip to content

Instantly share code, notes, and snippets.

@lomereiter
Created August 27, 2015 20:42
Show Gist options
  • Save lomereiter/e4f4f2b0a6cf9cee4287 to your computer and use it in GitHub Desktop.
Save lomereiter/e4f4f2b0a6cf9cee4287 to your computer and use it in GitHub Desktop.
// compilation: rdmd --build-only -O -release -inline -IBioD sambamba_161.d
// to use LDC: rdmd --compiler=ldmd2 [--force] ...
import bio.bam.reader, bio.bam.writer, std.parallelism;
void main(string[] args) {
// boilerplate
defaultPoolThreads = 8;
auto input = new BamReader(args[1]); // use std.getopt for better args handling
auto output = new BamWriter(args[2]);
output.writeSamHeader(input.header);
output.writeReferenceSequenceInfo(input.reference_sequences);
scope(exit) output.finish();
// filtering
immutable threshold = 90;
foreach (read; input.reads) {
// Warning: M corresponds to match/mismatch in BAM spec.
// If you are interested only in matches, use
// read.extended_cigar instead (requires ubiquitous MD tag)
// and '=' operation type.
uint total_len = 0;
foreach (op; read.cigar)
if (op.type == 'M')
total_len += op.length;
if (total_len > threshold)
output.writeRecord(read);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment