Mercurial > repos > blankenberg > naive_variant_caller
annotate tools/naive_variant_caller.py @ 13:cfc86c3fc5c8 draft
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit 470c2ac7bfb061e09dfe88ea61d2b8b53d041413
author | blankenberg |
---|---|
date | Fri, 17 Feb 2017 11:42:07 -0500 |
parents | ac0235d2d459 |
children |
rev | line source |
---|---|
12
ac0235d2d459
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit ce964ed3ab7e390754fa03bb32a593fbe79dcf04
blankenberg
parents:
8
diff
changeset
|
1 #!/usr/bin/env python |
1
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
2 #Dan Blankenberg |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
3 import sys |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
4 import optparse |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
5 |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
6 from pyBamParser.bam import Reader |
12
ac0235d2d459
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit ce964ed3ab7e390754fa03bb32a593fbe79dcf04
blankenberg
parents:
8
diff
changeset
|
7 from pyBamTools.genotyping.naive import VCFReadGroupGenotyper, PROGRAM_NAME, PROGRAM_VERSION |
1
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
8 |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
9 def main(): |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
10 #Parse Command Line |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
11 parser = optparse.OptionParser() |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
12 parser.add_option( '-b', '--bam', dest='bam_file', action='append', type="string", default=[], help='BAM filename, optionally index filename. Multiple allowed.' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
13 parser.add_option( '-i', '--index', dest='index_file', action='append', type="string", default=[], help='optionally index filename. Multiple allowed.' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
14 parser.add_option( '-o', '--output_vcf_filename', dest='output_vcf_filename', action='store', default = None, type="string", help='Output VCF filename' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
15 parser.add_option( '-r', '--reference_genome_filename', dest='reference_genome_filename', action='store', default = None, type="string", help='Input reference file' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
16 parser.add_option( '-v', '--variants_only', dest='variants_only', action='store_true', default = False, help='Report only sites with a possible variant allele.' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
17 parser.add_option( '-s', '--use_strand', dest='use_strand', action='store_true', default = False, help='Report counts by strand' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
18 parser.add_option( '-p', '--ploidy', dest='ploidy', action='store', type="int", default=2, help='Ploidy. Default=2.' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
19 parser.add_option( '-d', '--min_support_depth', dest='min_support_depth', action='store', type="int", default=0, help='Minimum number of reads needed to consider a REF/ALT. Default=0.' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
20 parser.add_option( '-q', '--min_base_quality', dest='min_base_quality', action='store', type="int", default=None, help='Minimum base quality.' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
21 parser.add_option( '-m', '--min_mapping_quality', dest='min_mapping_quality', action='store', type="int", default=None, help='Minimum mapping.' ) |
8 | 22 parser.add_option( '-t', '--coverage_dtype', dest='coverage_dtype', action='store', type="string", default=None, help='dtype to use for coverage array' ) |
1
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
23 parser.add_option( '--allow_out_of_bounds_positions', dest='allow_out_of_bounds_positions', action='store_true', default = False, help='Allows out of bounds positions to not throw fatal errors' ) |
5 | 24 parser.add_option( '--safe', dest='safe', action='store_true', default = False, help='Perform checks to prevent certain errors. Is slower.' ) |
1
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
25 parser.add_option( '--region', dest='region', action='append', type="string", default=[], help='region' ) |
12
ac0235d2d459
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit ce964ed3ab7e390754fa03bb32a593fbe79dcf04
blankenberg
parents:
8
diff
changeset
|
26 parser.add_option( '', '--version', dest='version', action='store_true', default = False, help='Report version and quit' ) |
1
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
27 (options, args) = parser.parse_args() |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
28 |
12
ac0235d2d459
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit ce964ed3ab7e390754fa03bb32a593fbe79dcf04
blankenberg
parents:
8
diff
changeset
|
29 if options.version: |
ac0235d2d459
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit ce964ed3ab7e390754fa03bb32a593fbe79dcf04
blankenberg
parents:
8
diff
changeset
|
30 print "%s version %s" % ( PROGRAM_NAME, PROGRAM_VERSION ) |
ac0235d2d459
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit ce964ed3ab7e390754fa03bb32a593fbe79dcf04
blankenberg
parents:
8
diff
changeset
|
31 sys.exit(0) |
ac0235d2d459
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit ce964ed3ab7e390754fa03bb32a593fbe79dcf04
blankenberg
parents:
8
diff
changeset
|
32 |
1
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
33 if len( options.bam_file ) == 0: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
34 print >>sys.stderr, 'You must provide at least one bam (-b) file.' |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
35 parser.print_help( sys.stderr ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
36 sys.exit( 1 ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
37 if options.index_file: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
38 assert len( options.index_file ) == len( options.bam_file ), "If you provide a name for an index file, you must provide the index name for all bam files." |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
39 bam_files = zip( options.bam_file, options.index_file ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
40 else: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
41 bam_files = [ ( x, ) for x in options.bam_file ] |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
42 if not options.reference_genome_filename: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
43 print >> sys.stderr, "Warning: Reference file has not been specified. Providing a reference genome is highly recommended." |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
44 if options.output_vcf_filename: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
45 out = open( options.output_vcf_filename, 'wb' ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
46 else: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
47 out = sys.stdout |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
48 |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
49 regions = [] |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
50 if options.region: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
51 for region in options.region: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
52 region_split = region.split( ":" ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
53 region = region_split.pop( 0 ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
54 if region_split: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
55 region_split = filter( bool, region_split[0].split( '-' ) ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
56 if region_split: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
57 if len( region_split ) != 2: |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
58 print >> sys.stderr, "You must specify both a start and an end, or only a chromosome when specifying regions." |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
59 cleanup_before_exit( tmp_dir ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
60 sys.exit( 1 ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
61 region = tuple( [ region ] + map( int, region_split ) ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
62 regions.append( region ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
63 |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
64 coverage = VCFReadGroupGenotyper( map( lambda x: Reader( *x ), bam_files ), options.reference_genome_filename, dtype=options.coverage_dtype, |
5 | 65 min_support_depth=options.min_support_depth, min_base_quality=options.min_base_quality, |
66 min_mapping_quality=options.min_mapping_quality, restrict_regions=regions, use_strand=options.use_strand, | |
67 allow_out_of_bounds_positions=options.allow_out_of_bounds_positions, safe=options.safe ) | |
1
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
68 for line in coverage.iter_vcf( ploidy=options.ploidy, variants_only=options.variants_only ): |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
69 out.write( "%s\n" % line ) |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
70 out.close() |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
71 |
ae6edc0012ba
Populate naive_variant_caller repository.
Daniel Blankenberg <dan@bx.psu.edu>
parents:
diff
changeset
|
72 if __name__ == "__main__": main() |