def bwaal(): log.warning( '[Alignment][bwaal] - Currently not available for Windows, sorry.') sys.exit(1) bwa_ext_dir = os.path.join(work_dir, out_dir, 'bwa_alignments') if not os.path.exists(bwa_ext_dir): os.makedirs(bwa_ext_dir) bam_bwa_file = os.path.join(bwa_ext_dir, (prefix + '.bam')) if not os.path.exists(bam_bwa_file): if stats_trigger in ['y', 'yes']: log.debug('[Alignment][bwaal] - bwa mem -x ont2d -t %s %s %s' % (th, ref, fast_Q_file)) bwaline = subprocess.Popen([ 'lib\BWA\bwa.exe', 'mem', '-x', 'ont2d', '-t', str(th), str(ref), str(fast_Q_file) ], stdout=subprocess.PIPE) PairDict = sam_parser(bwaline, bwa_ext_dir) else: sam_bwa_file = os.path.join(bwa_ext_dir, (prefix + '.sam')) log.debug( '[Alignment][bwaal] - bwa mem -x ont2d -t %s %s %s > %s' % (th, ref, fast_Q_file, sam_bwa_file)) bwaline = subprocess.Popen([ 'bwa', 'mem', '-x', 'ont2d', '-t', str(th), str(ref), str(fast_Q_file), '>', str(sam_bwa_file) ], stdout=subprocess.PIPE) outputfilebam = os.path.join(bwa_ext_dir, (prefix + '.tmp.bam')) log.debug( '[Alignment][bwaal] - samtools view -Sb -@ %s %s -o %s' % (th, sam_bwa_file, outputfilebam)) pysam.view("-Sb", "-@%s" % str(th), sam_bwa_file, "-o%s" % outputfilebam, catch_stdout=False) os.remove(sam_bwa_file) pysam.sort(outputfilebam, "-o%s" % bam_bwa_file, catch_stdout=False) log.debug('[Alignment][bwaal] - samtools index %s -@%s' % (bam_bwa_file, str(th))) pysam.index(bam_bwa_file, "-@%s" % str(th), catch_stdout=False) else: log.warning('[Alignment][bwaal] - file %s already exists!' % bam_bwa_file) try: shutil.move(bwa_ext_dir, os.path.join(work_dir, out_dir)) except shutil.Error: log.error("Unable to move %s" % bwa_ext_dir)
def ngmlral(): ngmlr_ext_dir = os.path.join(work_dir, 'ngmlr_alignments') if not os.path.exists(ngmlr_ext_dir): os.makedirs(ngmlr_ext_dir) bam_ngmlr_file = os.path.join(ngmlr_ext_dir, (prefix + '.bam')) if not os.path.exists(bam_ngmlr_file): if stats_trigger in ['y', 'yes']: log.debug('[Alignment][ngmlral] - ngmlr -t %s -r %s -q %s -x ont' % (th, ref, fast_Q_file)) ngmlrline = subprocess.Popen([ 'ngmlr', '-t', str(th), '-r', str(ref), '-q', str(fast_Q_file), '-x ont' ], stdout=subprocess.PIPE) PairDict = sam_parser(ngmlrline, ngmlr_ext_dir) else: sam_nglmr_file = os.path.join(ngmlr_ext_dir, (prefix + '.sam')) log.debug( '[Alignment][ngmlral] - ngmlr -t %s -r %s -q %s -o %s -x ont' % (th, ref, fast_Q_file, sam_ngmlr_file)) ngmlrline = subprocess.Popen([ 'ngmlr', '-t', str(th), '-r', str(ref), '-q', str(fast_Q_file), '-o', str(sam_ngmlr_file), '-x ont' ], stdout=subprocess.PIPE).wait() outputfilebam = os.path.join(ngmlr_ext_dir, (prefix + '.tmp.bam')) log.debug( '[Alignment][ngmlral] - samtools view -Sb -@ %s %s -o %s' % (th, sam_nglmr_file, outputfilebam)) pysam.view("-Sb", "-@%s" % str(th), sam_nglmr_file, "-o%s" % outputfilebam, catch_stdout=False) os.remove(sam_nglmr_file) pysam.sort(outputfilebam, "-o%s" % bam_ngmlr_file, catch_stdout=False) log.debug('[Alignment][ngmlral] - samtools index %s -@%s' % (bam_ngmlr_file, str(th))) pysam.index(bam_ngmlr_file, "-@%s" % str(th), catch_stdout=False) os.remove(outputfilebam) else: log.warning('[Alignment][ngmlral] - file %s already exists!' % bam_ngmlr_file) try: shutil.move(ngmlr_ext_dir, os.path.join(work_dir, out_dir)) except shutil.Error: log.error("Unable to move %s" % ngmlr_ext_dir)
def get_content(ff): Key4 = 'Analyses/Basecall_1D_000/BaseCalled_template/Fastq' for f in ff: with h5py.File(f, 'r') as f5: try: Fqstring = SeqIO.read(StringIO(f5[Key4][()].decode('UTF-8')), "fastq") yield Fqstring except: log.warning("File %s seems doesn't contain any sequence!" % f) pass
def fastq_writer(tmp_dir): finalfile = os.path.join(work_dir, (prefix + '.fastq.gz')) file_list = os.listdir(tmp_dir) file_list.sort(key=lambda x: int(ntpath.basename(x)[4:-9])) with open(finalfile, 'wb') as outfile: for infile in map(lambda x: os.path.join(tmp_dir, x), file_list): shutil.copyfileobj(open(infile, 'rb'), outfile, 1024*1024*10) for b in map(lambda x: os.path.join(tmp_dir, x), file_list): try: os.remove(b) except Exception: log.warning("File %s doesn't exist!" % b) if not os.path.exists(os.path.join(work_dir, out_dir)): os.makedirs(os.path.join(work_dir, out_dir)) try: shutil.move(finalfile, os.path.join(work_dir, out_dir)) except shutil.Error: log.error("Unable to move %s" % finalfile) shutil.rmtree(tmp_dir, ignore_errors=True)
def minimap2al(): mm_ext_dir = os.path.join(work_dir, 'minimap2_alignments') if not os.path.exists(mm_ext_dir): os.makedirs(mm_ext_dir) mmi_ref = os.path.splitext(ref)[0] + '.mmi' if not os.path.exists(mmi_ref): log.debug( '[Alignment][minimap2al] - minimap2 -x map-ont -t %s -d %s %s' % (th, mmi_ref, ref)) miniline = ('minimap2 -x map-ont -t', str(th), '-d', str(mmi_ref), str(ref)) minrun = ' '.join(miniline) subprocess.Popen(minrun, stdout=subprocess.PIPE, shell=True).wait() bam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.bam')) if not os.path.exists(bam_mm2_file): if stats_trigger in ['y', 'yes']: log.debug( '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s' % (th, str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file))) minimap2line = subprocess.Popen([ 'minimap2', '-ax', 'map-ont', '--MD', '-L', '-t', str(th), '-R', str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file) ], stdout=subprocess.PIPE) PairDict = sam_parser(minimap2line, mm_ext_dir) else: sam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.sam')) log.debug( '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s > %s' % (th, str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file), str(sam_mm2_file))) with open(sam_mm2_file, "w") as outfile: minimap2line = subprocess.Popen([ 'minimap2', '-ax', 'map-ont', '--MD', '-L', '-t', str(th), '-R', str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file) ], stdout=outfile).wait() outputfilebam = os.path.join(mm_ext_dir, (prefix + '.tmp.bam')) log.debug( '[Alignment][minimap2al] - samtools view -Sb -@ %s %s -o %s' % (th, sam_mm2_file, outputfilebam)) pysam.view("-Sb", "-@%s" % str(th), sam_mm2_file, "-o%s" % outputfilebam, catch_stdout=False) os.remove(sam_mm2_file) pysam.sort(outputfilebam, "-o%s" % bam_mm2_file, catch_stdout=False) log.debug('[Alignment][minimap2al] - samtools index %s -@%s' % (bam_mm2_file, str(th))) pysam.index(bam_mm2_file, "-@%s" % str(th), catch_stdout=False) os.remove(outputfilebam) else: log.warning('[Alignment][minimap2al] - file %s already exists!' % bam_mm2_file) try: shutil.move(mm_ext_dir, os.path.join(work_dir, out_dir)) except shutil.Error: log.error("Unable to move %s" % mm_ext_dir)
def minimap2al(): mm_ext_dir = os.path.join(work_dir, out_dir, 'minimap2_alignments') if not os.path.exists(mm_ext_dir): os.makedirs(mm_ext_dir) mmi_ref = os.path.splitext(ref)[0] + '.mmi' if not os.path.exists(mmi_ref): log.debug( '[Alignment][minimap2al] - minimap2 -x map-ont -t %s -d %s %s' % (th, mmi_ref, ref)) miniline = (os.path.abspath(os.path.dirname(__file__)) + '\minimap2\minimap2.exe -x map-ont -t', str(th), '-d', str(mmi_ref), str(ref)) minrun = ' '.join(miniline) subprocess.Popen(minrun, stdout=subprocess.PIPE, shell=True).wait() bam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.bam')) if not os.path.exists(bam_mm2_file): if stats_trigger in ['y', 'yes']: log.debug( '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s' % (th, str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file))) minimap2line = subprocess.Popen([ os.path.abspath(os.path.dirname(__file__)) + '\minimap2\minimap2.exe', '-ax', 'map-ont', '--MD', '-L', '-t', str(th), '-R', str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file) ], stdout=subprocess.PIPE) PairDict = sam_parser(minimap2line, mm_ext_dir) else: sam_mm2_file = os.path.join(mm_ext_dir, (prefix + '.sam')) log.debug( '[Alignment][minimap2al] - minimap2 -ax map-ont --MD -L -t %s -R %s %s %s > %s' % (th, str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file), str(sam_mm2_file))) with open(sam_mm2_file, "w") as out: minimap2line = subprocess.Popen([ os.path.abspath(os.path.dirname(__file__)) + '\minimap2\minimap2.exe', '-ax', 'map-ont', '--MD', '-L', '-t', str(th), '-R', str('@RG\\tID:minimap2\\tLB:NxEr\\tPL:MinION\\tPU:NA\\tSM:' + str(prefix)), str(mmi_ref), str(fast_Q_file) ], stdout=out).communicate() outputfilebam = os.path.join(mm_ext_dir, (prefix + '.tmp.bam')) log.debug( '[Alignment][minimap2al] - samtools view -Sb -@ %s %s -o %s' % (th, sam_mm2_file, outputfilebam)) samviewline = subprocess.Popen( [ os.path.abspath(os.path.dirname(__file__)) + '\SAMtools\samtools.exe', 'view', '-Sb', '-@', str(th), sam_mm2_file, '-o', outputfilebam ], stdout=subprocess.PIPE).communicate() os.remove(sam_mm2_file) samsortline = subprocess.Popen( [ os.path.abspath(os.path.dirname(__file__)) + '\SAMtools\samtools.exe', 'sort', '-@', str(th), outputfilebam, '-o', bam_mm2_file ], stdout=subprocess.PIPE).communicate() log.debug('[Alignment][minimap2al] - samtools index %s -@%s' % (bam_mm2_file, str(th))) samindexline = subprocess.Popen( [ os.path.abspath(os.path.dirname(__file__)) + '\SAMtools\samtools.exe', 'index', bam_mm2_file ], stdout=subprocess.PIPE).communicate() os.remove(outputfilebam) else: log.warning('[Alignment][minimap2al] - file %s already exists!' % bam_mm2_file)