Hi all. I’m writing a script to plot depth of coverage for each contig in an alignment. So far, I haven’t found a way to subset the BAM IOStream by contig, so the loop I use to iterate through records has to invoke a condition that the record matches a contig of interest at each iteration. Here’s what the code where I invoke the loop, via a function, looks like:
# Parse sample name from file name
sample_name = split(basename(file_name), '.')[1]
create_sample_dir(sample_name)
# Open the BAM file
reader = BAM.Reader(open(file_name, "r"), index="$file_name.bai")
# loop through each contig in the alignment
for (reference, reflength) in zip(reader.refseqnames, reader.refseqlens)
coverage = depth_of_coverage(reader, reflength, reference)
...
And here’s the depth_of_coverage function:
function depth_of_coverage(bam_reader::XAM.BAM.Reader{IOStream}, reflength::Int64, ref_name::String)
coverage_vec = fill(0, reflength)
# Process each record (read) in the BAM file
for record in bam_reader
# make sure the current read is not unmapped
!BAM.ismapped(record) && continue
# Get the reference sequence name for the current read
record_ref = BAM.refname(record)
# make sure we are working with a read mapped to the current contig of interest
record_ref != ref_name && continue
# get start and end positions
start, stop = BAM.position(record), BAM.rightposition(record)
# add 1 to coverage dict for all those positions
coverage_vec[start:stop] .+= 1
end
# return a vector of length reflength, where each position records how many reads are mapped
return coverage_vec
end
I would prefer to subset the BAM by contig prior to looping through records, so I figured I’d pose the question here: Is there a way to subset alignments by reference contig in Julia? And more broadly, is there a pre-existing function I could use to record depth across the contig? I was unable to find one in my last round of searching.
Many thanks! Happy to provide any additional clarifications.
(And also, if any performance optimizations jump out at you in the code I pasted, I would be very interested!)