Difference between revisions of "Source code"

From Biolecture.org
imported>Baik BuKyung
(Created page with "<div>import random</div> <div> </div> <div>def random_sequence():<br />     SL = open('sequence_list.txt','w')<br />     numb...")
 
imported>Baik BuKyung
(Blanked the page)
 
Line 1: Line 1:
<div>import random</div>
 
  
<div>&nbsp;</div>
 
 
<div>def random_sequence():<br />
 
&nbsp;&nbsp;&nbsp; SL = open(&#39;sequence_list.txt&#39;,&#39;w&#39;)<br />
 
&nbsp;&nbsp;&nbsp; number = int(input(&#39;How many genes do you want?: &#39;))<br />
 
&nbsp;&nbsp;&nbsp; for n in range(number):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gene_name = input(&#39;input gene name: &#39;)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SL.write(&#39;&gt;&#39; + gene_name + &#39;\n&#39;)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(&#39;input the length of &#39;,gene_name,&#39;: &#39;,end=&#39;&#39;)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; length = int(input())<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sequence = str()<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in range(length):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sequence += random_base()<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SL.write(sequence + &#39;\n&#39;)<br />
 
&nbsp;&nbsp;&nbsp; SL.close()<br />
 
&nbsp;&nbsp;&nbsp; return</div>
 
 
<div>&nbsp;</div>
 
 
<div><br />
 
def random_base():<br />
 
&nbsp;&nbsp;&nbsp; i = random.randrange(1,5)<br />
 
&nbsp;&nbsp;&nbsp; if i == 1:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &#39;A&#39;<br />
 
&nbsp;&nbsp;&nbsp; elif i == 2:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &#39;T&#39;<br />
 
&nbsp;&nbsp;&nbsp; elif i == 3:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &#39;G&#39;<br />
 
&nbsp;&nbsp;&nbsp; elif i == 4:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return &#39;C&#39;</div>
 
 
<div>&nbsp;</div>
 
 
<div>def reverse():<br />
 
&nbsp;&nbsp;&nbsp; SL = open(&#39;sequence_list.txt&#39;,&#39;r&#39;)<br />
 
&nbsp;&nbsp;&nbsp; RL = open(&#39;reverse_list.txt&#39;,&#39;w&#39;)<br />
 
&nbsp;&nbsp;&nbsp; read = SL.readlines()<br />
 
&nbsp;&nbsp;&nbsp; rev_name = list()<br />
 
&nbsp;&nbsp;&nbsp; rev = list()</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;&nbsp;&nbsp; for i in range(0,len(read)):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i % 2 == 0:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rev_name.append(read[i])<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rev.append(read[i])</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;&nbsp;&nbsp; for k in range(0,len(rev)):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rev_seq = str()<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for i in range(0,len(rev[k])):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if rev[k][i] == &#39;A&#39;:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rev_seq = &#39;T&#39; + rev_seq<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif rev[k][i] == &#39;T&#39;:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rev_seq = &#39;A&#39; + rev_seq<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif rev[k][i] == &#39;G&#39;:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rev_seq = &#39;C&#39; + rev_seq<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif rev[k][i] == &#39;C&#39;:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rev_seq = &#39;G&#39; + rev_seq<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RL.write(rev_name[k][:-1]+&#39;_rev\n&#39;)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; RL.write(rev_seq + &#39;\n&#39;)<br />
 
&nbsp;&nbsp;&nbsp; SL.close()<br />
 
&nbsp;&nbsp;&nbsp; RL.close()<br />
 
&nbsp;&nbsp;&nbsp; return<br />
 
&nbsp;&nbsp;&nbsp;<br />
 
&nbsp;&nbsp;&nbsp;<br />
 
def ORF_seq(name,seq):<br />
 
&nbsp;&nbsp;&nbsp; file_name = name[1:-1]<br />
 
&nbsp;&nbsp;&nbsp; ORF_file = open((file_name)+&#39;_ORF.txt&#39;,&#39;w&#39;)<br />
 
&nbsp;&nbsp;&nbsp; ini_cod = list()<br />
 
&nbsp;&nbsp;&nbsp; ter_cod = list()<br />
 
&nbsp;&nbsp;&nbsp;<br />
 
&nbsp;&nbsp;&nbsp; for i in range(0,len(seq)-2):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if seq[i:i+3] == &#39;ATG&#39;:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ini_cod.append(i)</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;&nbsp;&nbsp; for i in range(0,len(seq)-2):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if seq[i:i+3] == &#39;TGA&#39;:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ter_cod.append(i)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif seq[i:i+3] == &#39;TAA&#39;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ter_cod.append(i)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif seq[i:i+3] == &#39;TAG&#39;:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ter_cod.append(i)<br />
 
&nbsp;&nbsp;&nbsp; p=0<br />
 
&nbsp;&nbsp;&nbsp; for i in range(0,len(ini_cod)):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for j in range(0,len(ter_cod)):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ter_cod[j] &gt; ini_cod[i] and (ter_cod[j] - ini_cod[i]) % 3 == 0:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; p+=1<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORF_file.write(&quot;&gt;ORF_&quot;+str(p)+&quot;\n&quot;)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORF_file.write(seq[ini_cod[i]:(ter_cod[j]+3)]+&#39;\n&#39;)<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; break</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;&nbsp;&nbsp; ORF_file.close<br />
 
&nbsp;&nbsp;&nbsp; return</div>
 
 
<div>&nbsp;</div>
 
 
<div>def ORF():<br />
 
&nbsp;&nbsp;&nbsp; file_name = input(&#39;input file name to find ORF in directory: &#39;)<br />
 
&nbsp;&nbsp;&nbsp; fseq = open(file_name,&#39;r&#39;)<br />
 
&nbsp;&nbsp;&nbsp; gene_list = fseq.readlines()<br />
 
&nbsp;&nbsp;&nbsp; name_list = list()<br />
 
&nbsp;&nbsp;&nbsp; seq_list = list()<br />
 
&nbsp;&nbsp;&nbsp;<br />
 
&nbsp;&nbsp;&nbsp; for i in range(0,len(gene_list)):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i % 2 == 0:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name_list.append(gene_list[i])<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; seq_list.append(gene_list[i])</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;&nbsp;&nbsp; for i in range(0,len(seq_list)):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ORF_seq(name_list[i],seq_list[i])<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
 
&nbsp;&nbsp;&nbsp; fseq.close()</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;&nbsp;&nbsp; return<br />
 
&nbsp;&nbsp;&nbsp;<br />
 
def TRANS():<br />
 
&nbsp;&nbsp;&nbsp; file_name = input(&#39;input ORF file name to translate in directory: &#39;)<br />
 
&nbsp;&nbsp;&nbsp; fseq = open(file_name,&#39;r&#39;)<br />
 
&nbsp;&nbsp;&nbsp; trans_file = open((file_name[:-4] + &#39;_trans.txt&#39;),&#39;w&#39;)<br />
 
&nbsp;&nbsp;&nbsp; seq = fseq.readlines()<br />
 
&nbsp;&nbsp;&nbsp;<br />
 
&nbsp;&nbsp;&nbsp; codon_table = {&quot;TTT&quot;:&quot;F&quot;, &quot;TTC&quot;:&quot;F&quot;, &quot;TTA&quot;:&quot;L&quot;, &quot;TTG&quot;:&quot;L&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;TCT&quot;:&quot;S&quot;, &quot;TCC&quot;:&quot;s&quot;, &quot;TCA&quot;:&quot;S&quot;, &quot;TCG&quot;:&quot;S&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;TAT&quot;:&quot;Y&quot;, &quot;TAC&quot;:&quot;Y&quot;, &quot;TAA&quot;:&quot;STOP&quot;, &quot;TAG&quot;:&quot;STOP&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;TGT&quot;:&quot;C&quot;, &quot;TGC&quot;:&quot;C&quot;, &quot;TGA&quot;:&quot;STOP&quot;, &quot;TGG&quot;:&quot;W&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;CTT&quot;:&quot;L&quot;, &quot;CTC&quot;:&quot;L&quot;, &quot;CTA&quot;:&quot;L&quot;, &quot;CTG&quot;:&quot;L&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;CCT&quot;:&quot;P&quot;, &quot;CCC&quot;:&quot;P&quot;, &quot;CCA&quot;:&quot;P&quot;, &quot;CCG&quot;:&quot;P&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;CAT&quot;:&quot;H&quot;, &quot;CAC&quot;:&quot;H&quot;, &quot;CAA&quot;:&quot;Q&quot;, &quot;CAG&quot;:&quot;Q&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;CGT&quot;:&quot;R&quot;, &quot;CGC&quot;:&quot;R&quot;, &quot;CGA&quot;:&quot;R&quot;, &quot;CGG&quot;:&quot;R&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;ATT&quot;:&quot;I&quot;, &quot;ATC&quot;:&quot;I&quot;, &quot;ATA&quot;:&quot;I&quot;, &quot;ATG&quot;:&quot;M&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;ACT&quot;:&quot;T&quot;, &quot;ACC&quot;:&quot;T&quot;, &quot;ACA&quot;:&quot;T&quot;, &quot;ACG&quot;:&quot;T&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;AAT&quot;:&quot;N&quot;, &quot;AAC&quot;:&quot;N&quot;, &quot;AAA&quot;:&quot;K&quot;, &quot;AAG&quot;:&quot;K&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;AGT&quot;:&quot;S&quot;, &quot;AGC&quot;:&quot;S&quot;, &quot;AGA&quot;:&quot;R&quot;, &quot;AGG&quot;:&quot;R&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;GTT&quot;:&quot;V&quot;, &quot;GTC&quot;:&quot;V&quot;, &quot;GTA&quot;:&quot;V&quot;, &quot;GTG&quot;:&quot;V&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;GCT&quot;:&quot;A&quot;, &quot;GCC&quot;:&quot;A&quot;, &quot;GCA&quot;:&quot;A&quot;, &quot;GCG&quot;:&quot;A&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;GAT&quot;:&quot;D&quot;, &quot;GAC&quot;:&quot;D&quot;, &quot;GAA&quot;:&quot;E&quot;, &quot;GAG&quot;:&quot;E&quot;,<br />
 
&nbsp;&nbsp;&nbsp; &quot;GGT&quot;:&quot;G&quot;, &quot;GGC&quot;:&quot;G&quot;, &quot;GGA&quot;:&quot;G&quot;, &quot;GGG&quot;:&quot;G&quot;,}</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;&nbsp;&nbsp; for i in range(0,len(seq)):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; seq_line = str()<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if((i%2)==1):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for j in range(0,len(seq[i])-1,3):<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; seq_line += (codon_table[seq[i][j:j+3]])<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else:<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; seq_line=&quot;&gt;ORF_&quot;+str(int(i/2+1))<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; trans_file.write(seq_line+&#39;\n&#39;)<br />
 
&nbsp;&nbsp;&nbsp; fseq.close()<br />
 
&nbsp;&nbsp;&nbsp; trans_file.close()<br />
 
&nbsp;&nbsp;&nbsp; return<br />
 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>
 
 
<div>&nbsp;</div>
 
 
<div>&nbsp;</div>
 

Latest revision as of 04:28, 20 May 2016