def trim_adapter_pe(fastq1, fastq2, adapter1, adapter2, adapter_for_all, cutadapt_param, out_dir): if adapter1 and adapter2: prefix1 = os.path.join(out_dir, os.path.basename(strip_ext_fastq(fastq1))) prefix2 = os.path.join(out_dir, os.path.basename(strip_ext_fastq(fastq2))) trimmed1 = '{}.trim.fastq.gz'.format(prefix1) trimmed2 = '{}.trim.fastq.gz'.format(prefix2) cmd = 'cutadapt {} -a {} -A {} {} {} -o {} -p {}'.format( cutadapt_param, adapter_for_all if adapter_for_all else adapter1, adapter_for_all if adapter_for_all else adapter2, fastq1, fastq2, trimmed1, trimmed2) run_shell_cmd(cmd) return [trimmed1, trimmed2] else: fq1 = copy_f_to_dir(fastq1, out_dir) fq2 = copy_f_to_dir(fastq2, out_dir) return [fq1, fq2]
def trim_adapter_se(fastq, adapter, adapter_for_all, cutadapt_param, out_dir): if adapter: prefix = os.path.join(out_dir, os.path.basename(strip_ext_fastq(fastq))) trimmed = '{}.trim.fastq.gz'.format(prefix) cmd = 'cutadapt {} -a {} {} | gzip -nc > {}'.format( cutadapt_param, adapter_for_all if adapter_for_all else adapter, fastq, trimmed) run_shell_cmd(cmd) return trimmed else: return copy_f_to_dir(fastq, out_dir)
def main(): # filt_bam - dupmark_bam - nodup_bam # \ dup_qc \ pbc_qc # read params args = parse_arguments() log.info('Initializing and making output directory...') mkdir_p(args.out_dir) # declare temp arrays temp_files = [] # files to deleted later at the end log.info('Removing unmapped/low-quality reads...') if args.paired_end: filt_bam = rm_unmapped_lowq_reads_pe(args.bam, args.multimapping, args.mapq_thresh, args.nth, args.mem_gb, args.out_dir) else: filt_bam = rm_unmapped_lowq_reads_se(args.bam, args.multimapping, args.mapq_thresh, args.nth, args.mem_gb, args.out_dir) log.info('Checking if filtered BAM file is empty...') if bam_is_empty(filt_bam, args.nth): help_msg = ( 'No reads found in filtered BAM. ' 'Low quality sample? ' 'Or no reads passing criteria "samtools view -F 1804"? ' 'Check samtools flags at ' 'https://broadinstitute.github.io/picard/explain-flags.html. ') if args.paired_end: help_msg += ( 'Or is this truely PE BAM? ' 'All unpaired SE reads could be removed by "samtools view -f 2". ' ) raise ValueError(help_msg) log.info('Marking dupes with {}...'.format(args.dup_marker)) if args.dup_marker == 'picard': dupmark_bam, dup_qc = mark_dup_picard(filt_bam, args.out_dir, args.picard_java_heap) elif args.dup_marker == 'sambamba': dupmark_bam, dup_qc = mark_dup_sambamba(filt_bam, args.nth, args.out_dir) else: raise argparse.ArgumentTypeError('Unsupported --dup-marker {}'.format( args.dup_marker)) if args.no_dup_removal: nodup_bam = filt_bam else: temp_files.append(filt_bam) log.info('Removing dupes...') if args.paired_end: nodup_bam = rm_dup_pe(dupmark_bam, args.nth, args.out_dir) else: nodup_bam = rm_dup_se(dupmark_bam, args.nth, args.out_dir) samtools_index(dupmark_bam) temp_files.append(dupmark_bam + '.bai') temp_files.append(dupmark_bam) if len(args.filter_chrs) > 0: final_bam = remove_chrs_from_bam(nodup_bam, args.filter_chrs, args.chrsz, args.nth, args.out_dir) temp_files.append(nodup_bam) else: final_bam = nodup_bam log.info('Checking if final BAM file is empty...') if bam_is_empty(final_bam, args.nth): raise ValueError('No reads found in final (filtered/deduped) BAM. ' 'Low quality sample? ' 'Or BAM with duplicates only? ') log.info('samtools index (final_bam)...') samtools_index(final_bam, args.nth, args.out_dir) log.info('samstat...') samstat(final_bam, args.nth, args.mem_gb, args.out_dir) log.info('Generating PBC QC log...') if args.paired_end: pbc_qc_pe(dupmark_bam, args.mito_chr_name, args.nth, args.out_dir) else: pbc_qc_se(dupmark_bam, args.mito_chr_name, args.out_dir) log.info('samtools index (raw bam)...') bam = copy_f_to_dir(args.bam, args.out_dir) bai = samtools_index(bam, args.nth, args.out_dir) temp_files.extend([bam, bai]) log.info('Removing temporary files...') rm_f(temp_files) log.info('List all files in output directory...') ls_l(args.out_dir) log.info('All done.')
def main(): # filt_bam - dupmark_bam - nodup_bam # \ dup_qc \ pbc_qc # read params args = parse_arguments() log.info('Initializing and making output directory...') mkdir_p(args.out_dir) # declare temp arrays temp_files = [] # files to deleted later at the end log.info('Removing unmapped/low-quality reads...') if args.paired_end: filt_bam = rm_unmapped_lowq_reads_pe( args.bam, args.multimapping, args.mapq_thresh, args.nth, args.out_dir) else: filt_bam = rm_unmapped_lowq_reads_se( args.bam, args.multimapping, args.mapq_thresh, args.nth, args.out_dir) log.info('Marking dupes with {}...'.format(args.dup_marker)) if args.dup_marker == 'picard': dupmark_bam, dup_qc = mark_dup_picard( filt_bam, args.out_dir) elif args.dup_marker == 'sambamba': dupmark_bam, dup_qc = mark_dup_sambamba( filt_bam, args.nth, args.out_dir) else: raise argparse.ArgumentTypeError( 'Unsupported --dup-marker {}'.format(args.dup_marker)) if args.no_dup_removal: nodup_bam = filt_bam else: temp_files.append(filt_bam) log.info('Removing dupes...') if args.paired_end: nodup_bam = rm_dup_pe( dupmark_bam, args.nth, args.out_dir) else: nodup_bam = rm_dup_se( dupmark_bam, args.nth, args.out_dir) samtools_index(dupmark_bam) temp_files.append(dupmark_bam+'.bai') temp_files.append(dupmark_bam) if len(args.filter_chrs) > 0: final_bam = remove_chrs_from_bam(nodup_bam, args.filter_chrs, args.chrsz, args.nth, args.out_dir) temp_files.append(nodup_bam) else: final_bam = nodup_bam log.info('samtools index (final_bam)...') samtools_index(final_bam, args.nth, args.out_dir) log.info('samstat...') samstat(final_bam, args.nth, args.out_dir) log.info('Generating PBC QC log...') if args.paired_end: pbc_qc_pe(dupmark_bam, args.mito_chr_name, args.nth, args.out_dir) else: pbc_qc_se(dupmark_bam, args.mito_chr_name, args.out_dir) log.info('samtools index (raw bam)...') bam = copy_f_to_dir(args.bam, args.out_dir) bai = samtools_index(bam, args.nth, args.out_dir) temp_files.extend([bam, bai]) log.info('Removing temporary files...') rm_f(temp_files) log.info('List all files in output directory...') ls_l(args.out_dir) log.info('All done.')