Mercurial > repos > blankenberg > naive_variant_caller
comparison tools/naive_variant_caller.py @ 0:0095f76a5d05 draft
planemo upload for repository https://github.com/blankenberg/tools-blankenberg/tree/master/tools/naive_variant_caller commit cf5c0f6fb2651adf72e317a1cf77805fca30d84e
| author | blankenberg |
|---|---|
| date | Thu, 17 Sep 2015 11:13:08 -0400 |
| parents | |
| children | cbb8bfd46a80 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:0095f76a5d05 |
|---|---|
| 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=None, help='dtype to use for coverage array' ) | |
| 22 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' ) | |
| 23 parser.add_option( '--safe', dest='safe', action='store_true', default = False, help='Perform checks to prevent certain errors. Is slower.' ) | |
| 24 parser.add_option( '--region', dest='region', action='append', type="string", default=[], help='region' ) | |
| 25 (options, args) = parser.parse_args() | |
| 26 | |
| 27 if len( options.bam_file ) == 0: | |
| 28 print >>sys.stderr, 'You must provide at least one bam (-b) file.' | |
| 29 parser.print_help( sys.stderr ) | |
| 30 sys.exit( 1 ) | |
| 31 if options.index_file: | |
| 32 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." | |
| 33 bam_files = zip( options.bam_file, options.index_file ) | |
| 34 else: | |
| 35 bam_files = [ ( x, ) for x in options.bam_file ] | |
| 36 if not options.reference_genome_filename: | |
| 37 print >> sys.stderr, "Warning: Reference file has not been specified. Providing a reference genome is highly recommended." | |
| 38 if options.output_vcf_filename: | |
| 39 out = open( options.output_vcf_filename, 'wb' ) | |
| 40 else: | |
| 41 out = sys.stdout | |
| 42 | |
| 43 regions = [] | |
| 44 if options.region: | |
| 45 for region in options.region: | |
| 46 region_split = region.split( ":" ) | |
| 47 region = region_split.pop( 0 ) | |
| 48 if region_split: | |
| 49 region_split = filter( bool, region_split[0].split( '-' ) ) | |
| 50 if region_split: | |
| 51 if len( region_split ) != 2: | |
| 52 print >> sys.stderr, "You must specify both a start and an end, or only a chromosome when specifying regions." | |
| 53 cleanup_before_exit( tmp_dir ) | |
| 54 sys.exit( 1 ) | |
| 55 region = tuple( [ region ] + map( int, region_split ) ) | |
| 56 regions.append( region ) | |
| 57 | |
| 58 coverage = VCFReadGroupGenotyper( map( lambda x: Reader( *x ), bam_files ), options.reference_genome_filename, dtype=options.coverage_dtype, | |
| 59 min_support_depth=options.min_support_depth, min_base_quality=options.min_base_quality, | |
| 60 min_mapping_quality=options.min_mapping_quality, restrict_regions=regions, use_strand=options.use_strand, | |
| 61 allow_out_of_bounds_positions=options.allow_out_of_bounds_positions, safe=options.safe ) | |
| 62 for line in coverage.iter_vcf( ploidy=options.ploidy, variants_only=options.variants_only ): | |
| 63 out.write( "%s\n" % line ) | |
| 64 out.close() | |
| 65 | |
| 66 if __name__ == "__main__": main() |
