def write_multi_table(table_loc, multi_loc, key_dict, annot_dict): new = open(multi_loc, mode="w") base, ext = os.path.splitext(table_loc) kicked_loc = base + '_kick'+ext kicked = open(kicked_loc, mode="w") snp_index = 2 chr_index = 3 bp_index = 4 line1 = True with open(table_loc, mode="r") as old: for line in old: lsplit = line.strip().split() if line1: snp_index = lsplit.index('SNP') chr_index = lsplit.index('CHR') bp_index = lsplit.index('BP') lsplit[snp_index]=AP_SNP_TITLE lsplit.append(IM_SNP_TITLE) kicked.write('\t'.join(lsplit)+'\n') lsplit.extend([JB_SNP_TITLE,LZ_TITLE,ANNOT_TITLE,EXTREME_LZ]) new.write('\t'.join(lsplit)+'\n') line1 = False else: snp = lsplit[snp_index] im = key_dict[snp] lsplit.append(im) ## chrom = lsplit[chr_index] ## pos = lsplit[bp_index] ## elz = "chr{0}:{1}".format(chrom,bp) ## lsplit.append(elz) try: annot = fix_it.get_lz_annot(annot_dict,im) lz = annot_dict[im].lz chrom = annot_dict[im].hg19_chr pos = annot_dict[im].hg19_pos elz = "chr{0}:{1}".format(chrom,pos) lsplit.extend([lz,lz,annot,elz]) ## lsplit.append(annot) ## lsplit.append(elz) except KeyError: kicked.write('\t'.join(lsplit)+'\n') lz_jb = 'NA' lz = snp chrom = lsplit[chr_index] pos = lsplit[bp_index] elz = "chr{0}:{1}".format(chrom,pos) annot = 'no-annotation' lsplit.extend([lz_jb,lz,annot,elz]) new.write('\t'.join(lsplit)+'\n') kicked.close() new.close()
def give_table_annotation(table_loc,new_table_loc,index_dict, annot_dict): new = open(new_table_loc, mode = "w") line1 = True with open(table_loc, mode="r") as table: for line in table: line_list = line.strip().split() if line1: line_list.append('annotation') line1 = False else: snp = line_list[index_dict['snp']] annot = fix_it.get_lz_annot(annot_dict,snp) line_list.append(annot) new.write('\t'.join(line_list)+'\n')