comparison tools/naive_variant_detector.py @ 1:55b4460cd0ce

Add naive variant detector tool.
author Daniel Blankenberg <dan@bx.psu.edu>
date Tue, 14 May 2013 10:14:52 -0400
parents
children 4a67169be0ee
comparison
equal deleted inserted replaced
0:4f99c0ee5d2c 1:55b4460cd0ce
1 #Dan Blankenberg
2 import sys
3 import optparse
4
5 from pyBamParser.bam import Reader
6 from pyBamTools.genotyping.naive import VCFReadGroupGenotyper
7
8 def main():
9 #Parse Command Line
10 parser = optparse.OptionParser()
11 parser.add_option( '-b', '--bam', dest='bam_file', action='append', type="string", default=[], help='BAM filename, optionally index filename. Multiple allowed.' )
12 parser.add_option( '-i', '--index', dest='index_file', action='append', type="string", default=[], help='optionally index filename. Multiple allowed.' )
13 parser.add_option( '-o', '--output_vcf_filename', dest='output_vcf_filename', action='store', default = None, type="string", help='Output VCF filename' )
14 parser.add_option( '-r', '--reference_genome_filename', dest='reference_genome_filename', action='store', default = None, type="string", help='Input reference file' )
15 parser.add_option( '-v', '--variants_only', dest='variants_only', action='store_true', default = False, help='Report only sites with a possible variant allele.' )
16 parser.add_option( '-s', '--use_strand', dest='use_strand', action='store_true', default = False, help='Report counts by strand' )
17 parser.add_option( '-p', '--ploidy', dest='ploidy', action='store', type="int", default=2, help='Ploidy. Default=2.' )
18 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.' )
19 parser.add_option( '-q', '--min_base_quality', dest='min_base_quality', action='store', type="int", default=None, help='Minimum base quality.' )
20 parser.add_option( '-m', '--min_mapping_quality', dest='min_mapping_quality', action='store', type="int", default=None, help='Minimum mapping.' )
21 parser.add_option( '-t', '--coverage_dtype', dest='coverage_dtype', action='store', type="string", default='uint8', help='dtype to use for coverage array' )
22 parser.add_option( '--region', dest='region', action='append', type="string", default=[], help='region' )
23 (options, args) = parser.parse_args()
24
25 if len( options.bam_file ) == 0:
26 print >>sys.stderr, 'You must provide at least one bam (-b) file.'
27 parser.print_help( sys.stderr )
28 sys.exit( 1 )
29 if options.index_file:
30 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."
31 bam_files = zip( options.bam_file, options.index_file )
32 else:
33 bam_files = [ ( x, ) for x in options.bam_file ]
34 if not options.reference_genome_filename:
35 print >> sys.stderr, "Warning: Reference file has not been specified. Providing a reference genome is highly recommended."
36 if options.output_vcf_filename:
37 out = open( options.output_vcf_filename, 'wb' )
38 else:
39 out = sys.stdout
40
41 regions = []
42 if options.region:
43 for region in options.region:
44 region_split = region.split( ":" )
45 region = region_split.pop( 0 )
46 if region_split:
47 region_split = filter( bool, region_split[0].split( '-' ) )
48 if region_split:
49 if len( region_split ) != 2:
50 print >> sys.stderr, "You must specify both a start and an end, or only a chromosome when specifying regions."
51 cleanup_before_exit( tmp_dir )
52 sys.exit( 1 )
53 region = tuple( [ region ] + map( int, region_split ) )
54 regions.append( region )
55
56 coverage = VCFReadGroupGenotyper( map( lambda x: Reader( *x ), bam_files ), options.reference_genome_filename, dtype=options.coverage_dtype,
57 min_support_depth=options.min_support_depth, min_base_quality=options.min_base_quality, min_mapping_quality=options.min_mapping_quality,
58 restrict_regions=regions, use_strand=options.use_strand )
59 for line in coverage.iter_vcf( ploidy=options.ploidy, variants_only=options.variants_only ):
60 out.write( "%s\n" % line )
61 out.close()
62
63 if __name__ == "__main__": main()