Skip to content

Instantly share code, notes, and snippets.

@dpryan79
Last active May 4, 2017 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpryan79/036da16d5e0930ae67d53a59b8915665 to your computer and use it in GitHub Desktop.
Save dpryan79/036da16d5e0930ae67d53a59b8915665 to your computer and use it in GitHub Desktop.
estimate read filtering using deepTools
#!/usr/bin/env python
import argparse
import numpy as np
import sys
from deeptools import parserCommon, bamHandler, utilities
from deeptools.mapReduce import mapReduce
from deeptools._version import __version__
import deeptools.config as cfg
def parseArguments():
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
description="""
This tool estimates the number of reads that would be filtered given a set of
settings. Further, it tracks the number of singleton reads. The following metrics will always be tracked regardless of what you specify (the order output also matches this):
sys.stdout.write("Sample\tTotal Reads\tMapped Reads\tBelow MAPQ\tMissing Flags\tExcluded Flags\tInternally-determined Duplicates\tMarked Duplicates\tSingletons\tWrong strand\n")
* Total reads (including unmapped)
* Unmapped reads
* Reads in blacklisted regions (--blackListFileName)
The following metrics are estimated according to the --binSize and --distanceBetweenBins parameters
* Estimated mapped reads filtered (the total number of mapped reads filtered for any reason)
* Alignments with a below threshold MAPQ (--minMappingQuality)
* Alignments with at least one missing flag (--samFlagInclude)
* Alignments with undesirable flags (--samFlagExclude)
* Duplicates determined by deepTools (--ignoreDuplicates)
* Duplicates marked externally (e.g., by picard)
* Singletons (paired-end reads with only one mate aligning)
* Wrong strand (due to --filterRNAstrand)
The sum of these may be more than the total number of reads. Note that alignments are sampled from bins of size --binSize spaced --distanceBetweenBins apart.
""",
usage='Example usage: estimateReadFiltering.py -b sample1.bam sample2.bam > log.txt')
required = parser.add_argument_group('Required arguments')
required.add_argument('--bamfiles', '-b',
metavar='FILE1 FILE2',
help='List of indexed bam files separated by spaces.',
nargs='+',
required=True)
general = parser.add_argument_group('General arguments')
general.add_argument('--sampleLabels',
help='Labels for the samples. The '
'default is to use the file name of the '
'sample. The sample labels should be separated '
'by spaces and quoted if a label itself'
'contains a space E.g. --sampleLabels label-1 "label 2" ',
nargs='+')
general.add_argument('--binSize', '-bs',
metavar='INT',
help='Length in bases of the window used to sample the genome. (default 1000000)',
default=1000000,
type=int)
general.add_argument('--distanceBetweenBins', '-n',
metavar='INT',
help='To reduce the computation time, not every possible genomic '
'bin is sampled. This option allows you to set the distance '
'between bins actually sampled from. Larger numbers are sufficient '
'for high coverage samples, while smaller values are useful for '
'lower coverage samples. Note that if you specify a value that '
'results in too few (<1000) reads sampled, the value will be '
'decreased. (default 10000)',
default=10000,
type=int)
general.add_argument('--numberOfProcessors', '-p',
help='Number of processors to use. Type "max/2" to '
'use half the maximum number of processors or "max" '
'to use all available processors.',
metavar="INT",
type=parserCommon.numberOfProcessors,
default=cfg.config.get('general',
'default_proc_number'),
required=False)
general.add_argument('--verbose', '-v',
help='Set to see processing messages.',
action='store_true')
filtering = parser.add_argument_group('Optional arguments')
filtering.add_argument('--filterRNAstrand',
help='Selects RNA-seq reads (single-end or paired-end) in '
'the given strand.',
choices=['forward', 'reverse'],
default=None)
filtering.add_argument('--ignoreDuplicates',
help='If set, reads that have the same orientation '
'and start position will be considered only '
'once. If reads are paired, the mate\'s position '
'also has to coincide to ignore a read.',
action='store_true')
filtering.add_argument('--minMappingQuality',
metavar='INT',
help='If set, only reads that have a mapping '
'quality score of at least this are '
'considered.',
type=int)
filtering.add_argument('--samFlagInclude',
help='Include reads based on the SAM flag. For example, '
'to get only reads that are the first mate, use a flag of 64. '
'This is useful to count properly paired reads only once, '
'as otherwise the second mate will be also considered for the '
'coverage.',
metavar='INT',
default=None,
type=int,
required=False)
filtering.add_argument('--samFlagExclude',
help='Exclude reads based on the SAM flag. For example, '
'to get only reads that map to the forward strand, use '
'--samFlagExclude 16, where 16 is the SAM flag for reads '
'that map to the reverse strand.',
metavar='INT',
default=None,
type=int,
required=False)
filtering.add_argument('--blackListFileName', '-bl',
help="A BED or GTF file containing regions that should be excluded from all analyses. Currently this works by rejecting genomic chunks that happen to overlap an entry. Consequently, for BAM files, if a read partially overlaps a blacklisted region or a fragment spans over it, then the read/fragment might still be considered. Please note that you should adjust the effective genome size, if relevant.",
metavar="BED file",
nargs="+",
required=False)
return parser
def getFiltered_worker(arglist):
chrom, start, end, args = arglist
# Fix the bounds
end -= args.distanceBetweenBins
if end <= start:
end = start + 1
o = []
for fname in args.bamfiles:
fh = bamHandler.openBam(fname)
chromUse = utilities.mungeChromosome(chrom, fh.references)
prev_pos = set()
lpos = None
minMapq = 0
samFlagInclude = 0
samFlagExclude = 0
internalDupes = 0
externalDupes = 0
singletons = 0
filterRNAstrand = 0
nFiltered = 0
total = 0 # This is only used to estimate the percentage affected
for read in fh.fetch(chromUse, start, end):
filtered = 0
if read.pos < start:
# ensure that we never double count (in case distanceBetweenBins == 0)
continue
if read.flag & 4:
# Ignore unmapped reads, they were counted already
continue
if args.minMappingQuality and read.mapq < args.minMappingQuality:
filtered = 1
minMapq += 1
if args.samFlagInclude and read.flag & args.samFlagInclude != args.samFlagInclude:
filtered = 1
samFlagInclude += 1
if args.samFlagExclude and read.flag & args.samFlagExclude != 0:
filtered = 1
samFlagExclude += 1
if args.ignoreDuplicates:
# Assuming more or less concordant reads, use the fragment bounds, otherwise the start positions
if read.tlen >= 0:
s = read.pos
e = s + read.tlen
else:
s = read.pnext
e = s - read.tlen
if read.reference_name != read.next_reference_name:
e = read.pnext
if lpos is not None and lpos == read.reference_start \
and (s, e, read.next_reference_name, read.is_reverse) in prev_pos:
filtered = 1
internalDupes += 1
if lpos != read.reference_start:
prev_pos.clear()
lpos = read.reference_start
prev_pos.add((s, e, read.next_reference_name, read.is_reverse))
if read.is_duplicate:
filtered = 1
externalDupes += 1
if read.is_paired and read.mate_is_unmapped:
filtered = 1
singletons += 1
# filterRNAstrand
if args.filterRNAstrand:
if read.is_paired:
if args.filterRNAstrand == 'forward':
if read.flag & 144 == 128 or read.flag & 96 == 64:
pass
else:
filtered = 1
filterRNAstrand += 1
elif args.filterRNAstrand == 'reverse':
if read.flag & 144 == 144 or read.flag & 96 == 96:
pass
else:
filtered = 1
filterRNAstrand += 1
else:
if args.filterRNAstrand == 'forward':
if read.flag & 16 == 16:
pass
else:
filtered = 1
filterRNAstrand += 1
elif args.filterRNAstrand == 'reverse':
if read.flag & 16 == 0:
pass
else:
filtered = 1
filterRNAstrand += 1
total += 1
nFiltered += filtered
fh.close()
# Append a tuple to the output
tup = (total, nFiltered, minMapq, samFlagInclude, samFlagExclude, internalDupes, externalDupes, singletons, filterRNAstrand)
o.append(tup)
return o
def main(args=None):
args = parseArguments().parse_args(args)
if args.sampleLabels and len(args.sampleLabels) != len(args.bamfiles):
sys.stderr.write("\nError: --sampleLabels specified but it doesn't match the number of BAM files!\n")
sys.exit(1)
bhs = [bamHandler.openBam(x) for x in args.bamfiles]
# Get the reads in blacklisted regions
if args.blackListFileName:
blacklisted = []
for bh in bhs:
blacklisted.append(utilities.bam_blacklisted_reads(bh, None, args.blackListFileName, args.numberOfProcessors))
else:
blacklisted = [0] * len(bhs)
# Get the total and mapped reads
total = []
mapped = []
for bh in bhs:
total.append(bh.mapped + bh.unmapped)
mapped.append(bh.mapped)
chrom_sizes = list(zip(bhs[0].references, bhs[0].lengths))
for x in bhs:
x.close()
# Get the remaining metrics
res = mapReduce([args],
getFiltered_worker,
chrom_sizes,
genomeChunkLength=args.binSize + args.distanceBetweenBins,
blackListFileName=args.blackListFileName,
numberOfProcessors=args.numberOfProcessors,
verbose=args.verbose)
totals = [0] * len(args.bamfiles)
nFiltered = [0] * len(args.bamfiles)
MAPQs = [0] * len(args.bamfiles)
flagIncludes = [0] * len(args.bamfiles)
flagExcludes = [0] * len(args.bamfiles)
internalDupes = [0] * len(args.bamfiles)
externalDupes = [0] * len(args.bamfiles)
singletons = [0] * len(args.bamfiles)
rnaStrand = [0] * len(args.bamfiles)
for x in res:
for idx, r in enumerate(x):
totals[idx] += r[0]
nFiltered[idx] += r[1]
MAPQs[idx] += r[2]
flagIncludes[idx] += r[3]
flagExcludes[idx] += r[4]
internalDupes[idx] += r[5]
externalDupes[idx] += r[6]
singletons[idx] += r[7]
rnaStrand[idx] += r[8]
# Print some output
sys.stdout.write("Sample\tTotal Reads\tMapped Reads\tAlignments in blacklisted regions\tEstimated mapped reads filtered\tBelow MAPQ\tMissing Flags\tExcluded Flags\tInternally-determined Duplicates\tMarked Duplicates\tSingletons\tWrong strand\n")
for idx, _ in enumerate(args.bamfiles):
if args.sampleLabels:
sys.stdout.write(args.sampleLabels[idx])
else:
sys.stdout.write(args.bamfiles[idx])
sys.stdout.write("\t{}\t{}\t{}".format(total[idx], mapped[idx], blacklisted[idx]))
# nFiltered
metric = blacklisted[idx] + float(nFiltered[idx]) / float(totals[idx]) * nFiltered[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
# MAPQ
metric = float(MAPQs[idx]) / float(totals[idx]) * mapped[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
# samFlagInclude
metric = float(flagIncludes[idx]) / float(totals[idx]) * mapped[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
# samFlagExclude
metric = float(flagExcludes[idx]) / float(totals[idx]) * mapped[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
# Internally determined duplicates
metric = float(internalDupes[idx]) / float(totals[idx]) * mapped[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
# Externally marked duplicates
metric = float(externalDupes[idx]) / float(totals[idx]) * mapped[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
# Singletons
metric = float(singletons[idx]) / float(totals[idx]) * mapped[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
# filterRNAstrand
metric = float(rnaStrand[idx]) / float(totals[idx]) * mapped[idx]
sys.stdout.write("\t{}".format(round(metric, 1)))
sys.stdout.write("\n")
return 0
if __name__ == "__main__":
args = None
if len(sys.argv) == 1:
args = ["--help"]
main(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment