import sys, getopt myopts, args = getopt.getopt(sys.argv[1:],"n:o:h") o = '' n = 0 for u, a in myopts: if u == '-n': n = a elif u == '-o': o = a elif u == '-h': print '-n minimum_UTR_length -o output_index -h help' exit(0) else: print("Usage: -n minimum_UTR_length -o output_index input_fasta.fa") print "-n is set to 0 by default, to search for all ORFs within a sequence." exit(0) if args == []: print("Usage: -n minimum_UTR_length -o output_index input_fasta.fa") print ("input_fasta.fa is required") print "-n is set to 0 by default, to search for all ORFs within a sequence." exit(0) else: x = args[0] #the input fasta file if o != '': o2 = o + '_tab.txt' #tab delimited output o1 = o + '.fa' #fasta output else: o2 = "translation_tab.txt" o1 = "translation.fa" DNA_seq_file = open(x, 'r+') DNA_seq_lines = DNA_seq_file.readlines() protein_dic = {} protein_dic['TTT'] = 'F' protein_dic['TTC'] = 'F' protein_dic['TTA'] = 'L' protein_dic['TTG'] = 'L' protein_dic['CTT'] = 'L' protein_dic['CTC'] = 'L' protein_dic['CTA'] = 'L' protein_dic['CTG'] = 'L' protein_dic['ATT'] = 'I' protein_dic['ATC'] = 'I' protein_dic['ATA'] = 'I' protein_dic['ATG'] = 'M' protein_dic['GTT'] = 'V' protein_dic['GTC'] = 'V' protein_dic['GTA'] = 'V' protein_dic['GTG'] = 'V' protein_dic['TCT'] = 'S' protein_dic['TCC'] = 'S' protein_dic['TCA'] = 'S' protein_dic['TCG'] = 'S' protein_dic['CCT'] = 'P' protein_dic['CCC'] = 'P' protein_dic['CCA'] = 'P' protein_dic['CCG'] = 'P' protein_dic['ACT'] = 'T' protein_dic['ACC'] = 'T' protein_dic['ACA'] = 'T' protein_dic['ACG'] = 'T' protein_dic['GCT'] = 'A' protein_dic['GCC'] = 'A' protein_dic['GCA'] = 'A' protein_dic['GCG'] = 'A' protein_dic['TAT'] = 'Y' protein_dic['TAC'] = 'Y' protein_dic['TAA'] = '*' protein_dic['TAG'] = '*' protein_dic['CAT'] = 'H' protein_dic['CAC'] = 'H' protein_dic['CAA'] = 'Q' protein_dic['CAG'] = 'Q' protein_dic['AAT'] = 'N' protein_dic['AAC'] = 'N' protein_dic['AAA'] = 'K' protein_dic['AAG'] = 'K' protein_dic['GAT'] = 'D' protein_dic['GAC'] = 'D' protein_dic['GAA'] = 'E' protein_dic['GAG'] = 'E' protein_dic['TGT'] = 'C' protein_dic['TGC'] = 'C' protein_dic['TGA'] = '*' protein_dic['TGG'] = 'W' protein_dic['CGT'] = 'R' protein_dic['CGC'] = 'R' protein_dic['CGA'] = 'R' protein_dic['CGG'] = 'R' protein_dic['AGT'] = 'S' protein_dic['AGC'] = 'S' protein_dic['AGA'] = 'R' protein_dic['AGG'] = 'R' protein_dic['GGT'] = 'G' protein_dic['GGC'] = 'G' protein_dic['GGA'] = 'G' protein_dic['GGG'] = 'G' StopCodons = ['TAA', 'TAG', 'TGA'] def find_all(a_str, sub): start = 0 ret_list = [] while True: start = a_str.find(sub, start) if start == -1: break ret_list.append(start) start += len(sub) return ret_list def translate(nuseq, pos): prot_seq = '' seqlen = len(nuseq) ret_list = [] new_seq = nuseq[pos:] cpos = 0 ret_list.append(pos) while cpos < (seqlen -2): cur_range = nuseq[cpos: cpos + 3] if protein_dic.has_key(cur_range): prot_seq = prot_seq + protein_dic[cur_range] else: #break ret_list.append('Has_unknown_sequences: NN') break if cur_range in StopCodons: ret_list.append(cpos) break else: cpos = cpos + 3 if prot_seq.find('*') == -1: ret_list.append(cpos) ret_list.append('No_Stop_Codon') else: ret_list.append('OK') ret_list.append(len(prot_seq)-1) #dont count the * for the stop codon ret_list.append(prot_seq) return ret_list min_UTR_pos = int(n) def translate_all(myseq): seqtolook = myseq[min_UTR_pos:] all_look_pos = find_all(seqtolook, 'ATG') if all_look_pos == []: return translate_dic = {} for curpos in all_look_pos: translate_dic[curpos] = [] curseq = seqtolook[curpos:] translate_dic[curpos] = translate(curseq, curpos) if str(translate_dic[curpos][1]).find('Has_unknown_sequences: NN') == -1: realStopCodonPos = min_UTR_pos + curpos + translate_dic[curpos][1] translate_dic[curpos][0] = translate_dic[curpos][0] + min_UTR_pos translate_dic[curpos].insert(4, realStopCodonPos) return translate_dic protein_len_pos = 3 seq_pos = 5 def longest_translation(my_translate_dic): if my_translate_dic != None: keys = my_translate_dic.keys() longest_key = 0 longest = 0 for mykey in keys: if str(my_translate_dic[mykey][1]).find('Has_unknown_sequences: NN') == -1: if my_translate_dic[mykey][2].find('No_Stop_Codon') == -1: if my_translate_dic[mykey][protein_len_pos] > longest: longest_key = mykey longest = my_translate_dic[mykey][protein_len_pos] if my_translate_dic.has_key(longest_key): return my_translate_dic[longest_key] else: return [] else: return [] outputlines = [] outputlines_2 = [] i = 0 print len(DNA_seq_lines) while i < (len(DNA_seq_lines)-1): line = DNA_seq_lines[i] if line.find('>') != -1: header = line i = i + 1 seqBody = '' cur_line = DNA_seq_lines[i] while (cur_line.find('>') == -1): if (i < (len(DNA_seq_lines))-1) & (cur_line.find('>') == -1): seqBody = seqBody + cur_line.strip('\n') i = i + 1 cur_line = DNA_seq_lines[i].strip('\n') elif (i == (len(DNA_seq_lines)-1)): seqBody = seqBody + DNA_seq_lines[i].strip('\n') break mydic = translate_all(seqBody) myprotein_list = longest_translation(mydic) newlist = [] for item in myprotein_list: newlist.append(str(item)) final_prot_seq = '\t'.join(newlist) if len(newlist) != 0: outline = header + newlist[-1] + '\n' outputlines.append(outline) else: outline = header + 'No translation' + '\n' outline2 = header.strip('\n') + '\t' + seqBody + '\t' + final_prot_seq + '\n' outputlines_2.append(outline2) else: i = i + 1 fasta_file = open(o1, 'w+') fasta_file.writelines(outputlines) tab_file = open(o2, 'w+') tab_file.writelines(outputlines_2)