-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSEQ_menager.py
More file actions
213 lines (193 loc) · 6.99 KB
/
SEQ_menager.py
File metadata and controls
213 lines (193 loc) · 6.99 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import re
import math
from PDB_menager import AA_ATTRIBUTES
def read_fasta(filename):
"""Reads fasta format file. Return string type seq."""
fasta = ''
with open(filename, 'r') as pdb:
for line in pdb:
if line[:1] != '>':
fasta += line.rstrip('\n')
return fasta
def read_conservation(clustal_aln, skylign):
"""Reads matrix 21 cols x N rows, eg. skylign format file. Return conservation values for each position in query sequence."""
conservation = []
seq = ''
try:
with open(clustal_aln, 'r') as aln:
for line in aln:
if line.startswith('Query'):
seq += line[20:80]
except it:
seq = clustal_aln
except:
print("ERROR: Wrong format of clustal_aln! Provide clustal format file or string sequence matching to skylign file.")
with open(skylign, 'r') as con:
n = 0
for line in con:
if line[:1] != '#':
tokens = line.split()[1:21]
if seq[n] != '.':
conservation.append((seq[n], tokens[AA_ATTRIBUTES[seq[n]][0]]))
n += 1
return conservation
def read_LLPSDB(key):
"""Reads subset of LLPSDB database. @key is a subset name: DESIG, NATUR, C_ONE, C_TWO, C_MOR, P_DNA, P_RNA, P_PRT"""
path="/Users/adawid/Box/GROUP_data/LLPS-bioinformatics/databases/LLPSDB/"
data=['/5_Uniprot_id', '/0_PID', '/14_DisProt_ID', '/7_Species', '/11_Sequence', '/12_IDR', '/13_LCR']
vec=[]
for n1, feature in enumerate(data):
try:
with open(path+key+feature, "r") as f:
for n2, row in enumerate(f):
r=''
if n1 == 0:
vec.append([row.strip()])
elif n1 == 4:
for i in row.strip().split(';'):
if i.isalpha():
r += i
vec[n2].append(r)
vec[n2].append(len(r))
else:
vec[n2].append(row.strip())
except:
print("ERROR: File %s can't be open!"%str(path+key+feature))
try:
with open(path+key+"/1_P_name", "r") as names:
for n, name in enumerate(names):
LLPSDB[name.strip()] = vec[n]
except:
print("ERROR: File %s can't be open!"%str(path+key+'/1_P_name'))
return LLPSDB
def aa_composition(sequence):
"""Prepare statistics based on amino acid sequence."""
counts={}
length=len(sequence)
for a in aa:
ac = sequence.count(a)
if ac != 0:
counts[a] = ac/float(length)
else:
counts[a] = 0.0
return counts
def aa_pattern(sequence):
"""Prepare statistics based on amino acid sequence."""
hp=['V', 'L', 'I', 'M', 'F', 'C', 'W', 'A']
pol=['S', 'D', 'N', 'T', 'E', 'Q', 'H', 'K', 'R', 'Y']
pi=['F', 'Y', 'W', 'H', 'R', 'N', 'Q', 'D', 'E']
ar=['F', 'Y', 'W', 'H']
counts={}
for key in ['hp', 'pol', 'pi', 'ch']:
counts.setdefault(key, [])
counts[key].append(sequence)
counts[key].append(0.0)
length=len(sequence)
for a in aa:
if a in hp:
counts['hp'][0]=counts['hp'][0].replace(a, "1")
counts['hp'][1]+=sequence.count(a)
else:
counts['hp'][0]=counts['hp'][0].replace(a, "0")
if a in pol:
counts['pol'][0]=counts['pol'][0].replace(a, "1")
counts['pol'][1]+=sequence.count(a)
else:
counts['pol'][0]=counts['pol'][0].replace(a, "0")
if a in pi:
counts['pi'][1]+=sequence.count(a)
if a in ar:
counts['pi'][0]=counts['pi'][0].replace(a, "1")
else:
counts['pi'][0]=counts['pi'][0].replace(a, "2")
else:
counts['pi'][0]=counts['pi'][0].replace(a, "0")
if a=='D' or a=='E':
counts['ch'][0]=counts['ch'][0].replace(a, "1")
counts['ch'][1]+=sequence.count(a)
elif a=='K' or a=='R' or a=='H':
counts['ch'][0]=counts['ch'][0].replace(a, "2")
counts['ch'][1]+=sequence.count(a)
else:
counts['ch'][0]=counts['ch'][0].replace(a, "0")
for key in ['hp', 'pol', 'pi', 'ch']:
if counts[key][1] != 0.0:
counts[key][1]/=float(length)
return counts
def search_motif(queries, sequence):
"""Searches of given as a list sequence motifs in given string sequence.
* treats regular expressions in queries
Dictionary output: key - motif, @1 - counts, @2+ - index in seq
"""
counts={}
for query in queries:
s=re.compile(query)
for q in s.findall(sequence):
n=0
if not q in counts.keys():
counts[q] = [sequence.count(q)]
for i in range(0, counts[q][0]):
position=sequence.find(q, n)
counts[q].append(position)
n=position+len(q)
return counts
def sequence_charge_decoration(sequence):
"""Calculates SCD parameter of a given sequence.
float output: SCD value
"""
N=len(sequence)
charge=charge_pattern(sequence)
suma = 0.0
for n,ch in enumerate(charge):
for m in range(n+1,N):
if ch != 0.0 and charge[m] != 0.0:
suma += (charge[m]*ch*math.sqrt(m-n))
return suma/N
def overall_charge_symmetry(sequence):
"""Calculates OCS parameter of a given sequence.
float output: OCS value
"""
N=len(sequence)
blob=[5, 6]
SUM=suma=0.0
pos=(sequence.count('K') + sequence.count('R') + sequence.count('H'))/float(N)
neg=(sequence.count('D') + sequence.count('E'))/float(N)
FCR=pos+neg
sigma=(pos-neg)*(pos-neg)/FCR
for n in blob:
m=n_seg=0
suma=0.0
length=n
while m < N:
if m+length >= N:
length=N-m
frag=sequence[m:m+length]
pos=(frag.count('K') + frag.count('R') + frag.count('H'))/float(length)
neg=(frag.count('D') + frag.count('E'))/float(length)
if pos+neg == 0.0:
sig_i=0.0
else:
sig_i=(pos-neg)*(pos-neg)/float(pos+neg)
suma+=(sig_i-sigma)*(sig_i-sigma)
n_seg+=1
m+=length
SUM+=suma/float(n_seg)
return (FCR, SUM/float(len(blob)))
def charge_pattern(sequence):
seq=list(sequence)
charge=[0.0]*len(seq)
for m,res in enumerate(seq):
if res == 'K' or res == 'R' or res == 'H':
charge[m] = 1.0
elif res == 'D' or res == 'E':
charge[m] = -1.0
return charge
def write_fasta(seq_list):
with open(seq_list, "r") as f:
for row in f:
tokens=row.split()
out=open(tokens[0]+".fasta", 'a')
out.write(">"+str(tokens[0])+"\n"+str(tokens[1])+"\n")
out.close()
aa=['A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'Y']
LLPSDB={}