def build_index(fa, chrom, site, strand, rlen, thread, out_dir, seq, seq_flag): print('Build index...') if strand == '+': start = site - (rlen - 10) end = site + (rlen - 20) offset = rlen - 10 else: start = site - (rlen - 20) end = site + (rlen - 10) offset = rlen - 20 index_path = os.path.join(out_dir, 'sgRNA.fa') if seq_flag: os.symlink(seq, index_path) else: # fetch sgRNA region sequence with open(index_path, 'w') as out: out.write('>sgRNA_region\n') out.write( dna_to_rna(fa.fetch(chrom, start, end), strand=strand) + '\n') # build index if which('bowtie2-build'): command = 'bowtie2-build -q --threads %s %s %s' command = command % (thread, index_path, index_path) run_command(command, 'Error: cannot build index for sgRNA!') else: sys.exit('Error: no bowtie2-build installed!') return index_path, offset
def run_fseq(folder, bed, strand, flength, wig_flag, percent): prefix = os.path.splitext(bed)[0] # create bed files temp_dir = tempfile.mkdtemp() bed_f = os.path.join(folder, bed) # run fseq command = 'fseq -f 0 -l %s -of bed -o %s %s' % (flength, temp_dir, bed_f) run_command(command, 'Error in F-seq!') # cat fseq files peak_f = os.path.join(folder, prefix + '_fseq.bed') cat_files(temp_dir, peak_f) # resize peaks resized_peak_f = os.path.join(folder, prefix + '_peak.bed') resize_peak(peak_f, bed_f, resized_peak_f, strand, percent) # create wig files if wig_flag: # run fseq temp_dir = tempfile.mkdtemp() command = 'fseq -f 0 -l %s -o %s %s' % (flength, temp_dir, bed_f) run_command(command, 'Error in F-seq!') # cat fseq wig files wig = prefix + '_fseq.wig' wig_f = os.path.join(folder, wig) cat_files(temp_dir, wig_f, is_wig=True, name=wig) return resized_peak_f
def bg_to_bw(bg, chrom_size): if not os.path.isfile(chrom_size): sys.exit('No chrom size file: %s!' % chrom_size) prefix = os.path.splitext(os.path.split(bg)[-1])[0] bw = prefix + '.bw' command = 'bedGraphToBigWig %s %s %s' % (bg, chrom_size, bw) run_command(command, 'Could not convert bg to bw!') return bw
def run_stringtie(bam, gtf, dir_path, thread): if not os.path.isfile(bam): sys.exit('No BAM file: %s!' % bam) if not os.path.isfile(gtf): sys.exit('No GTF file: %s' % gtf) fname = os.path.basename(bam) prefix = os.path.splitext(fname)[0] outf = os.path.join(dir_path, prefix + '_stringtie.gtf') command = 'stringtie -G %s -p %s -o %s --rf %s' % (gtf, thread, outf, bam) run_command(command, 'Error in StringTie!') return outf
def bowtie2_align(index, read, thread, out_dir): print('Align reads...') if which('bowtie2'): bam = os.path.join(out_dir, 'cs.bam') sam = tempfile.NamedTemporaryFile('w+') command = 'bowtie2 --quiet --end-to-end -p %s -x %s -U %s -S %s' command = command % (thread, index, read, sam.name) run_command(command, 'Error in bowtie2 alignment!') sam.seek(0) with pysam.AlignmentFile(sam.name, 'r') as sam_f: with pysam.AlignmentFile(bam, 'wb', template=sam_f) as bam_f: for read in sam_f: if not read.is_unmapped: bam_f.write(read) sam.close() return bam else: sys.exit('Error: no bowtie2 installed!')
def merge_stringtie(gtf_list, gtf, dir_path, prefix, thread): outf = os.path.join(dir_path, prefix + '.gtf') command = 'stringtie --merge -G %s -p %s -o %s %s' % (gtf, thread, outf, '\t'.join(gtf_list)) run_command(command, 'Error in StringTie --merge!') return outf