-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdSeq2pSeq.py
87 lines (67 loc) · 2.65 KB
/
dSeq2pSeq.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import requests
import re
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from pathlib import Path
import sys
#dSeq2pSeq v0.0
#Created by P.D.M.Dilan
#2020/05/01
banner=(r"""
__ ____ ___ ____
/\ \/\ _`\ /'___`\ /\ _`\
\_\ \ \,\L\_\ __ __ /\_\ /\ \ _____\ \,\L\_\ __ __
/'_` \/_\__ \ /'__`\ /'__`\\/_/// /__/\ '__`\/_\__ \ /'__`\ /'__`\
/\ \L\ \/\ \L\ \/\ __//\ \L\ \ // /_\ \ \ \L\ \/\ \L\ \/\ __//\ \L\ \
\ \___,_\ `\____\ \____\ \___, \/\______/\ \ ,__/\ `\____\ \____\ \___, \
\/__,_ /\/_____/\/____/\/___/\ \/_____/ \ \ \/ \/_____/\/____/\/___/\ \
\ \_\ \ \_\ \ \_\
\/_/ \/_/ \/_/
~~ Nucleotide (DNA/RNA) sequence to a protein sequence translation tool ~~
~~ dSeq2pSeq version 0.0 ~~
~~ Select .fasta file or text file for translation ~~
""")
print(banner)
print('\n')
try:
#graphical window open
Tk().withdraw()
filename = askopenfilename( title='Choose a file')
if not filename:
print("Cancelled")
sys.exit()
#read DNA sequence
sequences = []
if filename.endswith('.fasta'):
with open(filename, "r") as f:
ls = f.readlines()[1:]
for i in ls:
sequences.append(i.rstrip("\n"))
elif filename.endswith('.txt'):
with open(filename, "r") as f:
ls = f.readlines()
for i in ls:
sequences.append(i.rstrip("\n"))
else:
print("File extension is not a .fasta or .txt !")
sys.exit()
print('Starting the process :\n')
#get data from https://web.expasy.org API
url='https://web.expasy.org/cgi-bin/translate/dna2aa.cgi?dna_sequence=%s&output_format=%s' % (sequences,'fasta')
data=requests.get(url)
sData=(data.content).decode('utf-8')
cleanedSdata=re.sub(r'\>.*?\:', '', sData)
#save data to text file
if filename.endswith('.txt'):
filename=re.sub(r'\.*?', '', filename)+'out'
filenameSave =Path(filename).with_suffix('.txt')
else:
filenameSave =Path(filename).with_suffix('.txt')
with open(filenameSave, "w") as text_file:
text_file.write(cleanedSdata)
#print data
print(cleanedSdata)
print('Finished !')
except (requests.HTTPError , requests.ConnectionError):
print('Please check your internet connection !\n')
exit()