def main(): if len(sys.argv) < 2: bb.fun_print_help("species_query", "species_target", "path_out", "chain_files(eg: rn5ToRn6,rn6ToMm10)","[query.ns.bed]", "[query.bed]") global_para() if not os.path.exists(PATH_OUT): bb.fun_print("create out path......", "red", "black", 1) os.mkdir(PATH_OUT) dict_pi = get_pi() psl_map() dict_exon_intron = read_exon_intron() write_exon_intron_length(dict_exon_intron) dict_cover = calculate_coverage(dict_exon_intron) list_ortho = find_ortho_pi(dict_cover) dict_synteny = get_synteny(dict_pi, list_ortho) run_lastz(dict_synteny, dict_pi, dict_cover) for i in dict_pi: if i not in list_ortho and i not in dict_synteny: LIST_OUT.append([i, 0, "none", "0", "0", "0", "chrNA", 1, 1, "+"]) file_out = bb.fun_open_file("{0}/{1}_{2}.conserve.tab".format(PATH_OUT, SPECIES1, SPECIES2), "w") for i in LIST_OUT: file_out.write(i[0]) for j in range(1, len(i)): file_out.write("\t" + str(i[j])) file_out.write("\n") file_out.close() os.system("rm {0}/temp*".format(PATH_OUT))
def run_lastz(dict_synteny, dict_pi, dict_cover): for pi in dict_synteny: bb.fun_print("start check conservation for " + pi, "green", "black", 1) LIST_OUT.append([pi, 3, "none", dict_synteny[pi][1][4], dict_cover[pi][0], dict_cover[pi][1], dict_synteny[pi][1][0], dict_synteny[pi][1][1], dict_synteny[pi][1][2], dict_synteny[pi][1][3]])
def psl_map(): TEMP_PSL_IN = PATH_QUERY_NS_BED for chains in PATH_CHAIN: TEMP_CHROM_SIZE = chains.split("To")[0] + ".chrom.size" command = "BB_PslMap {0} {1} {2} {3}/temp_pslmap_out 0.1".format(TEMP_PSL_IN, TEMP_CHROM_SIZE, chains, PATH_OUT) if os.system(command) != 0: bb.fun_print(PATH_CHAIN) bb.fun_print_error("BB_PslMap Error, please check chain files") os.system("cp {0}/temp_pslmap_out.psl {0}/temp_pslmap_in.psl".format(PATH_OUT)) TEMP_PSL_IN = "{0}/temp_pslmap_in.psl".format(PATH_OUT) os.system("cp {0}/temp_pslmap_out.psl {0}/{1}_{2}.psl && cp {0}/temp_pslmap_out.bed {0}/{1}_{2}.bed".format(PATH_OUT, SPECIES1, SPECIES2))
def main(): args = get_args() args.align = prepare_align_file(args) list_gene = read_gene_list(args) dict_matrix = {} for gn in list_gene: dict_matrix[gn] = [] cmd = ["bedtools", "coverage", "-d", "-a", args.gene_position, "-b", args.align] bb.fun_print("run bedtools coverage", "green", "black", 1) ret = subprocess.Popen(cmd, stdout=subprocess.PIPE) factor = 0 for l in bb.fun_open_file(args.align): factor += 1 print "unique mapped reads number: %s"%factor c = 0 r = 0 name = "0" bb.fun_print("make matrix", "green", "black", 1) tt = 0 for l in ret.stdout: tt += 1 if tt % 1000000 == 0: bb.fun_print("%s million lines processed......"%(tt/1000000)) line = l.strip().split() if line[3] != name and name != "0" and r != 0: dict_matrix[name].append(str(float(c)/args.resolution*1000000/factor)) r = 0 c = 0 name = line[3] r += 1 c += int(line[7]) if r == args.resolution: dict_matrix[name].append(str(float(c)/args.resolution*1000000/factor)) r = 0 c = 0 file_matrix = bb.fun_open_file(args.output, "w") for gn in list_gene: file_matrix.write(gn) if DICT_STRAND[gn] == "+": for dp in dict_matrix[gn]: file_matrix.write("\t" + dp) file_matrix.write("\n") else: for dp in dict_matrix[gn][::-1]: file_matrix.write("\t" + dp) file_matrix.write("\n") file_matrix.close() if args.format in ["sam", "bam"]: subprocess.check_call("rm %s_temp*"%args.align)
def run_lastz(dict_synteny, dict_pi, dict_cover): for pi in dict_synteny: bb.fun_print("start check conservation for " + pi, "green", "black", 1) bb.fun_quick_write( "%s\t%s\t%s\t%s\t0\t%s\n" % (dict_synteny[pi][0][0], dict_synteny[pi][0][1], dict_synteny[pi][0][2], pi, dict_synteny[pi][0][3]), "%s/temp1.bed" % PATH_OUT) bb.fun_quick_write( "%s\t%s\t%s\t%s\t0\t%s\n" % (dict_pi[pi][0], max(1, dict_pi[pi][1] - 10000), min(DICT_CHROM1[dict_pi[pi][0]], dict_pi[pi][2] + 10000), pi, dict_pi[pi][4]), "%s/temp.shuffle1.bed" % PATH_OUT) bb.fun_quick_write( "%s\t%s\t%s\t%s\t0\t%s\n" % (dict_synteny[pi][1][0], dict_synteny[pi][1][1], dict_synteny[pi][1][2], pi, dict_synteny[pi][1][3]), "%s/temp2.bed" % PATH_OUT) command = "bedtools getfasta -fi /data/tongji2/Annotation/Fasta/{0}.fa \ -fo {1}/temp1.fa -bed {1}/temp1.bed -name -s".format( DICT_SP[SPECIES1], PATH_OUT) if os.system(command) != 0: bb.fun_print_error("getfasta Error") command = "bedtools getfasta -fi /data/tongji2/Annotation/Fasta/{0}.fa \ -fo {1}/temp.shuffle1.fa -bed {1}/temp.shuffle1.bed -name -s".format( DICT_SP[SPECIES1], PATH_OUT) if os.system(command) != 0: bb.fun_print_error("getfasta Error") command = "bedtools getfasta -fi /data/tongji2/Annotation/Fasta/{0}.fa \ -fo {1}/temp2.fa -bed {1}/temp2.bed -name -s".format( DICT_SP[SPECIES2], PATH_OUT) if os.system(command) != 0: bb.fun_print_error("getfasta Error") # shuffle bb.fun_print("start shuffling......", "blue", "black", font=1) shuffle_scores = [0] for i in range(0): command = "bedtools shuffle -i {0}/temp.shuffle1.bed -g /data/tongji2/Annotation/ChromSize/{1}.chrom.size > {0}/temp.shuffle2.bed".format( PATH_OUT, DICT_SP[SPECIES2]) if os.system(command) != 0: bb.fun_print_error("bedtools shuffle Error") command = "bedtools getfasta -fi /data/tongji2/Annotation/Fasta/{0}.fa \ -fo {1}/temp.shuffle2.fa -bed {1}/temp.shuffle2.bed -name -s".format( DICT_SP[SPECIES2], PATH_OUT) if os.system(command) != 0: bb.fun_print_error("getfasta Error") command = "lastz {0}/temp.shuffle1.fa {0}/temp.shuffle2.fa --strand=plus --chain \ --output={0}/temp.shuffle.lastz --format=general:score".format( PATH_OUT) os.system(command) file_shuffle = bb.fun_open_file( "{0}/temp.shuffle.lastz".format(PATH_OUT)) lines = file_shuffle.readlines() if len(lines) == 1: shuffle_scores.append(0) else: for record in lines: if record[0] != "#": shuffle_scores.append(float(record.strip())) shuffle_scores.sort() cutoff = shuffle_scores[int(len(shuffle_scores) * 0.95)] #p-value=0.05 bb.fun_print("p 0.05 cutoff mapping score: " + str(cutoff), "blue", "black", font=1) # end shuffle command = "lastz {0}/temp1.fa {0}/temp2.fa --strand=plus --chain \ --rdotplot={0}/temp.rplot --output={0}/temp.lastz \ --format=general:name1,start1,end1,name2,start2,end2,nmatch,identity,score".format( PATH_OUT) if os.path.getsize( "{0}/temp1.fa".format(PATH_OUT)) and os.path.getsize( "{0}/temp1.fa".format(PATH_OUT)): if os.system(command) != 0: bb.fun_print_error("lastz Error") else: LIST_OUT.append([pi, 1, "none", "0", "0", "0", "chrNA", 1, 1, "+"]) continue os.system("sort -k2,2n {0}/temp.lastz > {0}/temp.sort.lastz".format( PATH_OUT)) file_in = bb.fun_open_file("%s/temp.lastz" % PATH_OUT) list_lastz = [] start = dict_synteny[pi][0][1] end = dict_synteny[pi][0][2] k = 0 for l in file_in: if l[0] == "#": continue line = l.strip().split() if float(line[-1]) > cutoff: list_lastz.append([int(line[1]), int(line[2])]) if dict_synteny[pi][0][3] == "+": pi_start = dict_pi[pi][1] - start pi_end = dict_pi[pi][2] - start else: pi_start = end - dict_pi[pi][2] pi_end = end - dict_pi[pi][1] for i in list_lastz: if pi_end < i[0] or pi_start > i[1]: continue else: LIST_OUT.append([ pi, 3, "none", dict_synteny[pi][1][4], dict_cover[pi][0], dict_cover[pi][1], dict_synteny[pi][1][0], dict_synteny[pi][1][1], dict_synteny[pi][1][2], dict_synteny[pi][1][3] ]) k = 1 break if k == 0: LIST_OUT.append([pi, 2, "none", "0", "0", "0", "chrNA", 1, 1, "+"])