Spaces:
Running
Running
File size: 20,620 Bytes
c2a02c6 |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
from Bio import Align
from Bio.Align import substitution_matrices
from pathlib import Path
import streamlit as st
from Bio.pairwise2 import format_alignment
from Bio import pairwise2
from Bio import pairwise2
from Bio.SubsMat import MatrixInfo as matlist
"""
def do_alignment(identifier, uniprotSequence, pdbSequence, alignment_path):
aligner = Align.PairwiseAligner()
#print(f'Aligning Datapoint: {identifier}')
if len(pdbSequence) >= 1:
f = open(Path(alignment_path / f'{identifier}_alignment.txt'), "w")
aligner.mode = 'local'
aligner.substitution_matrix = substitution_matrices.load("BLOSUM62")
aligner.open_gap_score = -11
aligner.extend_gap_score = -1
alignments = aligner.align(uniprotSequence, pdbSequence)
alignments = (list(alignments))
merge_in_threes = str(alignments[0]).split('\n')
K = 3
res = ["".join(str(alignments[0]).split('\n')[idx: idx + K]) for idx in range(len(str(alignments[0]).split('\n')) - K + 1)]
slice_val = slice(0,len(res),4)
writtenlist = res[slice_val]
new_alignment = []
for i in writtenlist:
cont1 = list(filter(None, i.split('target')))
cont2 = cont1[0].split('query')
target_pos = (list(filter(None,cont2[0].split(' '))))[0]
target = (list(filter(None,cont2[0].split(' '))))[1]
alg_pos = (list(filter(None,cont2[0].split(' '))))[2]
alg = (list(filter(None,cont2[0].split(' '))))[3]
query_pos = (list(filter(None,cont2[1].split(' '))))[0]
query = (list(filter(None,cont2[1].split(' '))))[1]
if int(target_pos)>0:
new_target = int(target_pos) * 'X' + target
else:
new_target = int(target_pos) * ' ' + target
if int(alg_pos)>0:
new_alg = int(target_pos) * 'X' + target
else:
new_alg = int(target_pos) * ' ' + alg
if int(query_pos)>0:
new_query = int(target_pos) * 'X' + target
else:
new_query = int(target_pos) * ' ' + target
new_alignment.append(new_target+'\n' +new_alg +'\n' +new_query)
alignment_list = []
k = 0
for alignment in new_alignment:
k += 1
st.write('COUNT', k)
st.write('alignment')
st.write(alignment)
f.write(str(alignment))
f.write('\n')
f.write('\n')
alignment = (str(alignment).strip().split('\n'))
alignment = [''.join(['.' if m == ' ' else m for m in x]) for x in alignment]
st.write('alignment_updated')
st.write(alignment)
alignment_list.append(alignment)
return alignment_list
"""
def do_alignment(identifier, uniprotSequence, pdbSequence, alignment_path):
aligner = Align.PairwiseAligner()
#print(f'Aligning Datapoint: {identifier}')
if len(pdbSequence) >= 1:
f = open(Path(alignment_path / f'{identifier}_alignment.txt'), "w")
aligner.mode = 'local'
aligner.substitution_matrix = substitution_matrices.load("BLOSUM62")
aligner.open_gap_score = -11
aligner.extend_gap_score = -1
alignments = aligner.align(uniprotSequence, pdbSequence)
sub_matrix = matlist.blosum62
alignments2 = pairwise2.align.localds(uniprotSequence, pdbSequence, sub_matrix, -11, -1)
alignment_list = []
k = 0
for alignment in alignments:
f.write(str(alignment))
f.write('\n')
f.write('\n')
alignment = (str(alignment).strip().split('\n'))
alignment = [''.join(['.' if m == ' ' else m for m in x]) for x in alignment]
alignment_list.append(alignment)
return alignment_list
def mutation_position_on_pdb(alignment_list, pos):
which_alignment_to_go = 0
for alignment in alignment_list:
#char_list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
#for char in alignment[1]:
# if char in char_list:
# alignment[1] = alignment[1].replace(char, '.')
which_alignment_to_go += 1
alignment_uniprot = alignment[0]
alignment_pdb = alignment[2]
startGap = 0
if alignment_uniprot.startswith('.') or alignment_uniprot.startswith('-'):
for k in alignment_uniprot:
if k == '.' or k == '-':
startGap += 1
else:
break
countGap = startGap
countResidue = 0
canonicalRes = ' '
pdbRes = ' '
for j in alignment_uniprot[startGap:]:
if j == '.' or j == '-':
countGap += 1
else:
countResidue += 1
if int(countResidue) == int(pos):
canonicalRes = alignment_uniprot[countResidue + countGap - 1]
try:
pdbRes = alignment_pdb[countResidue + countGap - 1]
except:
IndexError
pdbRes = 'nan'
break
if (alignment[1][countResidue + countGap - 1] == '|') or (alignment[1][countResidue + countGap - 1] == 'X'):
if canonicalRes == pdbRes:
pdb_alignStatus = 'aligned'
elif canonicalRes != pdbRes:
pdb_alignStatus = 'aligned*'
countGap_pdb = 0
countResidue_pdb = 0
pdbRes = ' '
for j in alignment_pdb[0:countResidue + countGap - 1]:
if j == '.' or j == '-':
countGap_pdb += 1
if alignment_pdb[countResidue + countGap - 1] == '.' or alignment_pdb[
countResidue + countGap - 1] == '-':
mutationPositionOnPDB = 'nan'
posPDB = 'nan'
else:
posPDB = countResidue + countGap - countGap_pdb
mutationPositionOnPDB = str(posPDB)
break
elif (canonicalRes == pdbRes) and ((alignment[1][countResidue + countGap - 1] == '.') or (
alignment[1][poscountResidue+ countGap - 1] == '-')):
pdb_alignStatus = 'not_aligned'
mutationPositionOnPDB = 'nan'
elif (canonicalRes != pdbRes) and ((alignment[1][countResidue + countGap - 1] == '.') or (
alignment[1][countResidue + countGap - 1] == '-')):
pdb_alignStatus = 'not_aligned'
mutationPositionOnPDB = 'nan'
elif alignment_pdb[countResidue + countGap - 1] == '.' or alignment_pdb[
countResidue + countGap - 1] == '-':
mutationPositionOnPDB = 'nan'
posPDB = 'nan'
return (pdb_alignStatus, mutationPositionOnPDB, startGap, alignment_list[which_alignment_to_go - 1])
def find_position_on_pdb_for_range_annotations(posAnnotation, startGap, alignment_to_use):
annotation_on_pdb_start = 'nan'
annotation_on_pdb_end = 'nan'
pos1 = int(posAnnotation.split('-')[0])
count_gap = startGap
count_residue = 0
for j in alignment_to_use[0][startGap:]:
if j == '.' or j == '-':
count_gap += 1
else:
count_residue += 1
if int(count_residue) == int(pos1): # count gaps until the first position
break
annotation_on_up_start = int(pos1) + int(count_gap)
pos2 = int(posAnnotation.split('-')[1])
count_gap = startGap
count_residue = 0
for j in alignment_to_use[0][startGap:]:
if j == '.' or j == '-':
count_gap += 1
else:
count_residue += 1
if int(count_residue) == int(pos2): # count gaps until the first position
break
annotation_on_up_end = int(pos2) + int(count_gap)
try:
pdb_residue_start = alignment_to_use[2][annotation_on_up_start - 1].strip()
if (pdb_residue_start == '.') or (pdb_residue_start == '-'):
for ran in range(len(alignment_to_use[2][(annotation_on_up_start - 1):annotation_on_up_end])):
if (alignment_to_use[2][(annotation_on_up_start - 1):annotation_on_up_end][ran] != '.') and \
(alignment_to_use[2][(annotation_on_up_start - 1):annotation_on_up_end][ran] != '-') and \
((alignment_to_use[1][(annotation_on_up_start - 1):annotation_on_up_end][ran] == '|') or
(alignment_to_use[1][(annotation_on_up_start - 1):annotation_on_up_end][ran] == 'X')):
annotation_on_up_start += ran
break
elif (pdb_residue_start != '.') and (pdb_residue_start != '-') and \
((alignment_to_use[1][annotation_on_up_start - 1] == '.') or (
alignment_to_use[1][annotation_on_up_start - 1] == '-')):
for ran in range(len(alignment_to_use[2][(annotation_on_up_start - 1):annotation_on_up_end])):
if ((alignment_to_use[1][(annotation_on_up_start - 1):annotation_on_up_end][ran] == '|') or
(alignment_to_use[1][(annotation_on_up_start - 1):annotation_on_up_end][ran] == 'X')):
annotation_on_up_start += ran
break
count_gap_pdb = 0
if annotation_on_up_start != 'nan':
for q in alignment_to_use[2][0:annotation_on_up_start - 1]:
if q == '.' or q == '-':
count_gap_pdb += 1
if alignment_to_use[1][annotation_on_up_start] == '-' or alignment_to_use[1][annotation_on_up_start] == '.':
annotation_on_pdb_start = 'nan'
else:
annotation_on_pdb_start = int(annotation_on_up_start) - count_gap_pdb
else:
annotation_on_pdb_start = 'nan'
except:
IndexError
try:
pdb_residue_end = alignment_to_use[2][annotation_on_up_end - 1].strip()
if pdb_residue_end == '.' or pdb_residue_end == '-':
for ran in range(len(alignment_to_use[2][(annotation_on_up_start - 1):annotation_on_up_end])):
if ((alignment_to_use[1][annotation_on_up_start - 1:annotation_on_up_end][ran] == '.') or
(alignment_to_use[1][(annotation_on_up_start - 1):][ran] == '-')):
annotation_on_up_start += (ran - 1)
annotation_on_up_end = annotation_on_up_start
break
elif (pdb_residue_end != '.') and (pdb_residue_end != '-') and \
((alignment_to_use[1][annotation_on_up_end - 1] == '.') or (
alignment_to_use[1][annotation_on_up_end - 1] == '-')):
for ran in range(len(alignment_to_use[2][(annotation_on_up_start - 1):annotation_on_up_end])):
if ((alignment_to_use[1][annotation_on_up_start - 1:annotation_on_up_end][ran] == '.') or
(alignment_to_use[1][(annotation_on_up_start - 1):][ran] == '-')):
annotation_on_up_start += (ran - 1)
annotation_on_up_end = annotation_on_up_start
break
count_gap_pdb = 0
if annotation_on_up_end != 'nan':
for q in alignment_to_use[2][0:annotation_on_up_end - 1]:
if q == '.' or q == '-':
count_gap_pdb += 1
if alignment_to_use[1][annotation_on_up_end - 1] == '-' or alignment_to_use[1][
annotation_on_up_end - 1] == '.' and annotation_on_pdb_start == 'nan':
annotation_on_pdb_end = 'nan'
elif alignment_to_use[1][annotation_on_up_end - 1] == '-' or alignment_to_use[1][
annotation_on_up_end - 1] == '.' and annotation_on_pdb_start != 'nan':
annotation_on_pdb_end = int(annotation_on_up_end) - count_gap_pdb
else:
annotation_on_pdb_end = int(annotation_on_up_end) - count_gap_pdb
else:
annotation_on_pdb_end = 'nan'
except:
IndexError # Say isoform 2 is matched with the length 100, but canonical is 150 aa long. If there is an annotation at 105. position, for the isoform it throws an index error.
if annotation_on_pdb_start == 'nan' and annotation_on_pdb_end != 'nan':
annotation_on_pdb_start = annotation_on_up_start - count_gap_pdb
if annotation_on_pdb_start == annotation_on_pdb_end:
annotation_on_pdb_start = 'nan'
annotation_on_pdb_end = 'nan'
return annotation_on_up_start, annotation_on_up_end, annotation_on_pdb_start, annotation_on_pdb_end
def annotation_pos_on_pdb(annot_positions, startGap, alignment_to_use, identifier):
newpos = []
if annot_positions != 'nan':
annot_positions = (str(annot_positions).replace("'", ''))
annot_positions = (str(annot_positions).replace('[', ''))
annot_positions = (str(annot_positions).replace("]", ''))
positionList_perAnnotation = annot_positions.split(',')
positionList_perAnnotation = [h.strip() for h in positionList_perAnnotation]
position_start_on_pdb = 'nan'
position_end_on_pdb = 'nan'
try:
positionList_perAnnotation = [i for i in positionList_perAnnotation if i != 'nan']
except:
TypeError
for position in range(len(positionList_perAnnotation)):
if ('-' not in str(positionList_perAnnotation[position])) and (str(positionList_perAnnotation[position]) != '?') and (str(positionList_perAnnotation[position]) != '') and (len(str(positionList_perAnnotation[position])) != 0):
count_gap = startGap
count_residue = 0
for j in alignment_to_use[0][startGap:]:
if j == '.' or j == '-':
count_gap += 1
else:
count_residue += 1
try:
if int(count_residue) == int(positionList_perAnnotation[position]):
break
except:
ValueError
annotation_on_up = int(positionList_perAnnotation[position]) + int(count_gap)
try:
pdb_residue_start = alignment_to_use[2][annotation_on_up - 1].strip()
except:
IndexError
pdb_residue_start = 'nan'
if pdb_residue_start != 'nan':
try:
if (pdb_residue_start == '.') or (pdb_residue_start == '-'):
for ran in range(len(alignment_to_use[2][(annotation_on_up - 1):annotation_on_up])):
if (alignment_to_use[2][(annotation_on_up - 1):annotation_on_up][ran] != '.') and \
(alignment_to_use[2][(annotation_on_up - 1):annotation_on_up][
ran] != '-') and \
((alignment_to_use[1][(annotation_on_up - 1):annotation_on_up][
ran] == '|') or
(alignment_to_use[1][(annotation_on_up - 1):annotation_on_up][
ran] == 'X')):
annotation_on_up += ran
break
elif (pdb_residue_start != '.') and (pdb_residue_start != '-') and \
((alignment_to_use[1][annotation_on_up - 1] == '.') or (
alignment_to_use[1][annotation_on_up - 1] == '-')):
for ran in range(len(alignment_to_use[2][(annotation_on_up - 1):annotation_on_up])):
if ((alignment_to_use[1][(annotation_on_up - 1):annotation_on_up][ran] == '|') or
(alignment_to_use[1][(annotation_on_up - 1):annotation_on_up][ran] == 'X')):
annotation_on_up += ran
break
count_gap_pdb = 0
for q in alignment_to_use[2][0:annotation_on_up - 1]:
if q == '.' or q == '-':
count_gap_pdb += 1
if alignment_to_use[1][annotation_on_up] == '-' or alignment_to_use[1][
annotation_on_up] == '.':
annotation_on_pdb = 'nan'
else:
annotation_on_pdb = int(annotation_on_up) - count_gap_pdb
if count_gap_pdb == annotation_on_up:
annotation_on_pdb = 'nan'
try:
if alignment_to_use[2][count_gap_pdb + annotation_on_pdb - 1] == '.' or alignment_to_use[2][
count_gap_pdb + annotation_on_pdb - 1] == '-':
annotation_on_pdb = 'nan'
except:
IndexError
annotation_on_pdb = 'nan'
except:
IndexError
annotation_on_pdb = 'nan'
newpos.append(annotation_on_pdb)
elif ('-' in str(positionList_perAnnotation[position])) and (
str(positionList_perAnnotation[position]) != '?') and (
str(positionList_perAnnotation[position]) != ' ') and (
len(str(positionList_perAnnotation[position])) != 0):
try:
position_start_on_pdb = \
find_position_on_pdb_for_range_annotations(positionList_perAnnotation[position],
startGap, alignment_to_use)[2]
position_end_on_pdb = \
find_position_on_pdb_for_range_annotations(positionList_perAnnotation[position],
startGap, alignment_to_use)[3]
except:
ValueError
newpositions = str(position_start_on_pdb) + '-' + str(position_end_on_pdb)
newpos.append(newpositions)
else:
pass
try:
newpos = [i for i in newpos if i != 'nan']
except:
TypeError
return newpos
def final_stage(df, annotation_list, alignment_path):
for i in df.index:
identifier = df.at[i, 'uniprotID'] + '_' + df.at[i, 'pdbID'] + '_' + df.at[i, 'chain'] + '_'
alignment_list = do_alignment(identifier, df.at[i, 'uniprotSequence'], df.at[i, 'pdbSequence'], alignment_path)
df.at[i, 'pdb_alignStatus'] = mutation_position_on_pdb(alignment_list, df.at[i, 'pos'])[0]
print()
df.at[i, 'mutationPositionOnPDB'] = mutation_position_on_pdb(alignment_list, df.at[i, 'pos'])[1]
startGap = mutation_position_on_pdb(alignment_list, df.at[i, 'pos'])[2]
alignment_to_use = mutation_position_on_pdb(alignment_list, df.at[i, 'pos'])[3]
for annot in annotation_list:
df.at[i, annot] = annotation_pos_on_pdb(df.at[i, annot], startGap, alignment_to_use, identifier)
if str(df.at[i, 'domStart']) != 'nan' and str(df.at[i, 'domEnd']) != 'nan' and \
((str(df.at[i, 'domStart']) != '-1' and str(df.at[i, 'domEnd']) != '-1' and
str(df.at[i, 'domStart']) != '-1.0' and str(df.at[i, 'domEnd']) != '-1.0')):
domainLoc = str(df.at[i, 'domStart']).split('.')[0] + '-' + str(df.at[i, 'domEnd']).split('.')[0]
domain_pos = find_position_on_pdb_for_range_annotations(domainLoc, startGap, alignment_to_use)
df.at[i, 'domainStartonPDB'] = domain_pos[2]
df.at[i, 'domainEndonPDB'] = domain_pos[3]
elif str(df.at[i, 'domStart']) != '-1' or str(df.at[i, 'domEnd']) != '-1' or \
str(df.at[i, 'domStart']) != '-1.0' or str(df.at[i, 'domEnd']) != '-1.0':
df.at[i, 'domainStartonPDB'] = 'nan'
df.at[i, 'domainEndonPDB'] = 'nan'
df = df.astype(str)
return df
def alignment(dataframe_to_align, annotation_list, alignment_path):
domainList = ['domStart', 'domEnd']
result = final_stage(dataframe_to_align, annotation_list, alignment_path)
return result
# |