forked from rahi-lab/PIPOline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
142 lines (127 loc) · 6 KB
/
Copy pathutils.py
File metadata and controls
142 lines (127 loc) · 6 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import re
import numpy as np
def read_from_fsa(fsa_file_path):
"""Reads a FASTA file and returns the description (header) and the sequence"""
if not fsa_file_path:
return '',''
try:
with open(fsa_file_path) as f:
name = f.readline()[1:-1]
sequence = f.read().replace('\n', '')
print(name)
print(sequence)
print('Length:', len(sequence))
return name, sequence.upper()
except Exception as e:
print('Unable to read:', fsa_file_path,e)
def read_enzyme_list(enzymefile):
"""Reads in and processes the enzyme list"""
with open(enzymefile) as file:
lines=file.readlines()
enzymes=[line.rstrip() for line in lines]
rsitelist = []
enamelist = []
if (len(enzymes) % 2) == 1:
print('Enzyme list has an odd number of lines. Enzyme list should be a list of enzyme names and restriction sites in each line.')
else:
for linei in range(1, len(enzymes), 2):
rsite = enzymes[linei]
ename = enzymes[linei-1]
# check length of restriction site
if len(rsite) == 6 or len(rsite) == 8:
# check whether any non-ACGT characters
if len(re.sub('[ACGT]', '', rsite)) == 0:
#check whether palindromic so can focus on only one strand
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
reverse_complement = "".join(complement.get(base, base) for base in reversed(rsite))
if rsite==reverse_complement:
# add name only if enzyme already in the list
rsiteexists=0
for linej in range(len(rsitelist)):
if rsitelist[linej] == rsite:
rsiteexists=1
enamelist[linej] = enamelist[linej]+', '+ename
break
#add new entry if not yet there
if rsiteexists==0:
rsitelist.append(rsite)
enamelist.append(ename)
return (np.array(rsitelist), np.array(enamelist))
def read_enzyme_list(enzymefile):
"""Reads in and processes the enzyme list"""
with open(enzymefile) as file:
lines=file.readlines()
enzymes=[line.rstrip() for line in lines]
rsitelist = []
enamelist = []
if (len(enzymes) % 2) == 1:
print('Enzyme list has an odd number of lines. Enzyme list should be a list of enzyme names and restriction sites in each line.')
else:
for linei in range(1, len(enzymes), 2):
rsite = enzymes[linei]
ename = enzymes[linei-1]
# check length of restriction site
if len(rsite) == 6 or len(rsite) == 8:
# check whether any non-ACGT characters
if len(re.sub('[ACGT]', '', rsite)) == 0:
#check whether palindromic so can focus on only one strand
complement = {'A': 'T', 'C': 'G', 'G': 'C', 'T': 'A'}
reverse_complement = "".join(complement.get(base, base) for base in reversed(rsite))
if rsite==reverse_complement:
# add name only if enzyme already in the list
rsiteexists=0
for linej in range(len(rsitelist)):
if rsitelist[linej] == rsite:
rsiteexists=1
enamelist[linej] = enamelist[linej]+', '+ename
break
#add new entry if not yet there
if rsiteexists==0:
rsitelist.append(rsite)
enamelist.append(ename)
return (np.array(rsitelist), np.array(enamelist))
def read_gene_plus_string(Gene_plus):
"""Splits gene sequence into 3 pieces: ORF, 1000 bp upstream ("Left_of_gene") and 1000 bp downstream ("Right_of_gene")"""
# # Import Gene -+1000 base pairs - clean in the same way
# with open(genefile, 'r') as file:
# Gene_plus = file.read().replace('\n', '')
# Clean away numbers and spaces
Gene_plus = Gene_plus.replace(' ', '') # spaces
remove_digits = str.maketrans('', '', '0123456789')
Gene_plus = Gene_plus.translate(remove_digits) # numbers
Left_of_gene = Gene_plus[0:1000] # 1000 bp's to the left of the Gene
Right_of_gene = Gene_plus[(len(Gene_plus)-1000):] # 1000 bp's to the right of the Gene (includes 3' UTR segment)
Gene = Gene_plus[1000:(len(Gene_plus)-1000)]
return (Left_of_gene, Gene, Right_of_gene)
def dna_to_protein(dna):
"""Translates DNA into a Protein. It truncates the 3' tail that doesn't make a full codon.
\n STOP is denoted by '*'"""
genetic_code = {
'ATA':'I', 'ATC':'I', 'ATT':'I', 'ATG':'M',
'ACA':'T', 'ACC':'T', 'ACG':'T', 'ACT':'T',
'AAC':'N', 'AAT':'N', 'AAA':'K', 'AAG':'K',
'AGC':'S', 'AGT':'S', 'AGA':'R', 'AGG':'R',
'CTA':'L', 'CTC':'L', 'CTG':'L', 'CTT':'L',
'CCA':'P', 'CCC':'P', 'CCG':'P', 'CCT':'P',
'CAC':'H', 'CAT':'H', 'CAA':'Q', 'CAG':'Q',
'CGA':'R', 'CGC':'R', 'CGG':'R', 'CGT':'R',
'GTA':'V', 'GTC':'V', 'GTG':'V', 'GTT':'V',
'GCA':'A', 'GCC':'A', 'GCG':'A', 'GCT':'A',
'GAC':'D', 'GAT':'D', 'GAA':'E', 'GAG':'E',
'GGA':'G', 'GGC':'G', 'GGG':'G', 'GGT':'G',
'TCA':'S', 'TCC':'S', 'TCG':'S', 'TCT':'S',
'TTC':'F', 'TTT':'F', 'TTA':'L', 'TTG':'L',
'TAC':'Y', 'TAT':'Y', 'TAA':'*', 'TAG':'*',
'TGC':'C', 'TGT':'C', 'TGA':'*', 'TGG':'W',
}
protein = ''
if(len(dna)%3 != 0):
dna = dna[: -(len(dna)%3)]
for i in range(0, len(dna), 3):
code = dna[i:i+3]
if(code in genetic_code.keys()):
protein += genetic_code[code]
else:
protein = ''
break
return protein