def miraligner(args):
hairpin, mirna = _download_mirbase(args)<EOL>precursors = _read_precursor(args.hairpin, args.sps)<EOL>matures = _read_mature(args.mirna, args.sps)<EOL>gtf = _read_gtf(args.gtf)<EOL>out_dts = []<EOL>out_files = []<EOL>for bam_fn in args.files:<EOL><INDENT>sample = op.splitext(op.basename(bam_fn))[<NUM_LIT:0>]<EOL>logger.info("<STR_LIT>" % bam_fn)<EOL>if bam_fn.endswith("<STR_LIT>") or bam_fn.endswith("<STR_LIT>"):<EOL><INDENT>bam_fn = _sam_to_bam(bam_fn)<EOL>bam_sort_by_n = op.splitext(bam_fn)[<NUM_LIT:0>] + "<STR_LIT>"<EOL>pysam.sort("<STR_LIT>", bam_fn, bam_sort_by_n)<EOL>reads = _read_bam(bam_sort_by_n + "<STR_LIT>", precursors)<EOL><DEDENT>elif bam_fn.endswith("<STR_LIT>") or bam_fn.endswith("<STR_LIT>") orbam_fn.endswith("<STR_LIT>"):<EOL><INDENT>if args.collapse:<EOL><INDENT>bam_fn = _collapse_fastq(bam_fn)<EOL><DEDENT>out_file = op.join(args.out, sample + "<STR_LIT>")<EOL>bam_fn = _filter_seqs(bam_fn)<EOL>if args.miraligner:<EOL><INDENT>_cmd_miraligner(bam_fn, out_file, args.sps, args.hairpin, args.out)<EOL>reads = _read_miraligner(out_file)<EOL>out_files.append(out_file)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if args.miraligner:<EOL><INDENT>_mirtop(out_files, args.hairpin, args.gtf, args.sps, args.out)<EOL><DEDENT>if not args.miraligner:<EOL><INDENT>reads = _annotate(reads, matures, precursors)<EOL><DEDENT>out_file = op.join(args.out, sample + "<STR_LIT>")<EOL>out_file, dt, dt_pre = _tab_output(reads, out_file, sample)<EOL>try:<EOL><INDENT>vcf_file = op.join(args.out, sample + "<STR_LIT>")<EOL>if not file_exists(vcf_file):<EOL><INDENT>create_vcf(dt_pre, matures, gtf, vcf_file)<EOL><DEDENT>try:<EOL><INDENT>import vcf<EOL>vcf.Reader(filename=vcf_file)<EOL><DEDENT>except Exception as e:<EOL><INDENT>logger.warning(e.__doc__)<EOL>logger.warning(e.message)<EOL><DEDENT><DEDENT>except Exception as e:<EOL><INDENT>logger.warning(e.__doc__)<EOL>logger.warning(e.message)<EOL><DEDENT>if isinstance(dt, pd.DataFrame):<EOL><INDENT>out_dts.append(dt)<EOL><DEDENT><DEDENT>if out_dts:<EOL><INDENT>_create_counts(out_dts, args.out)<EOL><DEDENT>else:<EOL><INDENT>print("<STR_LIT>")<EOL><DEDENT>
Realign BAM hits to miRBAse to get better accuracy and annotation