-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataProcessing.py
More file actions
707 lines (580 loc) · 20.6 KB
/
dataProcessing.py
File metadata and controls
707 lines (580 loc) · 20.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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
# -*- coding: utf-8 -*-
"""
Created on Thu May 25 11:15:56 2017
@author: lewismoffat
"""
import numpy as np
from collections import defaultdict
from sklearn.feature_extraction import DictVectorizer
import UL_atchFactors as af
from sklearn.cluster import MiniBatchKMeans
import pandas as pd
import sklearn as sk
from tqdm import tqdm
import glob
import matplotlib.pyplot as plt
from matplotlib_venn import venn2
# putting the dictionary globally is naughty but its called so often its worth it
intDict={ 'A': 1 ,
'C': 2 ,
'D': 3 ,
'E': 4 ,
'F': 5 ,
'G': 6 ,
'H': 7 ,
'I': 8 ,
'K': 9 ,
'L': 10 ,
'M': 11 ,
'N': 12 ,
'P': 13 ,
'Q': 14 ,
'R': 15 ,
'S': 16 ,
'T': 17 ,
'V': 18 ,
'W': 19 ,
'Y': 20 ,
'X': 21 ,
'Z': 21 ,
'U': 21 ,
'0': 0 }
# putting the dictionary globally is naughty but its called so often its worth it
intDictzero={ 'A': 0 ,
'C': 0 ,
'D': 0 ,
'E': 0 ,
'F': 0 ,
'G': 0 ,
'H': 0 ,
'I': 0 ,
'K': 0 ,
'L': 0 ,
'M': 0 ,
'N': 0 ,
'P': 0 ,
'Q': 0 ,
'R': 0 ,
'S': 0 ,
'T': 0 ,
'V': 0 ,
'W': 0 ,
'Y': 0 ,
'X': 0 ,
'Z': 0 ,
'U': 0 ,
'0': 0 }
def char2int(seqs, longest, pad=True):
"""converts all characters in a sequnce to integer IDs based on a dict"""
# iterate through sequences
for index, seq in enumerate(seqs):
# pad sequence with zeros to get to the longest length
if pad:
seq=seq.ljust(longest,'0')
# temporary var for storing int seq before replacement
numseq=[]
# go character by character and fill new list with numeric values
for char in seq:
numseq.append(intDict[char])
seqs[index]=numseq
# convert to a numpy array for convenience later
if pad:
seqs=np.array(seqs)
return seqs
def char2ptuple(seqs, n=3):
# encodes feature dictionaries as numpy vectors, needed by scikit-learn.
vectorizer = DictVectorizer(sparse=True)
newSeqs = vectorizer.fit_transform([event_feat(x, n) for x in seqs])
return newSeqs
def clip(seqs, ln):
"""Goes through list of sequences and clips the sequence to ln characters long"""
for idx, seq in enumerate(seqs):
seqs[idx]=seq[:len]
return seqs
def filtr(seqs, ln):
"""Goes through list of sequences and clips the sequence to ln characters long"""
newSeq=[]
for idx, seq in enumerate(seqs):
if len(seq)==ln:
newSeq.append(seq)
return newSeq
def seq2fatch(seqs):
for idx, seq in enumerate(seqs):
vec=[]
for char in seq:
vec=np.concatenate((vec,af.atchleyFactor(char)))
seqs[idx]=vec
seqs=np.array(seqs)
return seqs
def GloVe(seqs, swissprot=True):
if swissprot==True:
df=pd.read_csv('embeddings/protVec_100d_3grams.csv', sep="\t" ,header=None)
notformatted=[]
for row in df.values:
row=row[0].split("\t")
key=row[0]
vals=np.array([float(x) for x in row[1:]])
notformatted.append([key, vals])
dictionary=dict(notformatted)
else:
dictionary=np.load('embeddings/dict_norm.npy')
dictionary=dict(dictionary.item())
if swissprot==True:
for idx, seq in enumerate(seqs):
newSeq=np.zeros((dictionary['AAA'].shape[0]))
tuples=pTuple(seq)
for tup in tuples:
try:
location=dictionary[tup]
except:
location=dictionary["<unk>"]
newSeq+=location
seqs[idx]=newSeq
else:
embeddings=np.load('embeddings/embed.npy')
for idx, seq in enumerate(seqs):
newSeq=np.zeros((embeddings.shape[1]))
tuples=pTuple(seq,4)
for tup in tuples:
try:
location=dictionary[tup]
except:
try:
location=dictionary["UNK"]
except:
import pdb; pdb.set_trace()
newSeq+=embeddings[location]
seqs[idx]=newSeq
return seqs
def expandTuples(seqs,n=4):
seqsNew=[]
for idx, seq in enumerate(seqs):
tuples=pTuple(seq,n)
for tup in tuples:
seqsNew.append(tup)
return seqsNew
#==============================================================================
# One - Hot Related Functions
#==============================================================================
def oneHot(seqs):
newSeqs=[]
for seq in seqs:
newSeq=[]
for char in seq:
zeros=np.zeros((20))
zeros[char-1]=1
newSeq.append(zeros)
newSeqs.append(newSeq)
return newSeqs
def oneHot2AA(seqs):
newSeqs=[]
inv_map = {v: k for k, v in intDict.items()}
for seq in seqs:
newSeq=[]
for onehot in seq:
newSeq.append(inv_map[np.argmax(onehot)+1])
newSeqs.append(newSeq)
return newSeqs
#==============================================================================
# Unsupervized clustering of pTuples using k-means
#==============================================================================
def kmeans(seqs,n=3,sample=10000, num_clusters=100):
# first step is to get ptuples and replace them with atchley vectors
# the idea is a ptuple 1x15 from here is a data point for kmeans so we just want one big list
# first we want to go through each seq in the sequence
#n=3 # size of the tuple
newSeq=[] # temp vector to fill atchely numbers in
for idx, seq in enumerate(seqs):
for i in range(len(seq)-n+1):
tup=seq[i:i+n]
if n==3:
tup=np.concatenate((af.atchleyFactor(tup[0]),af.atchleyFactor(tup[1]),af.atchleyFactor(tup[2])))
else:
tup=np.concatenate((af.atchleyFactor(tup[0]),af.atchleyFactor(tup[1])))
newSeq.append(tup)
newSeq=np.array(newSeq)
# for efficiency we will sample from the data and run on that (essentially boostrapping)
np.random.shuffle(newSeq)
newSeq=newSeq[:sample]
# fit kmeans
kmeans = MiniBatchKMeans(n_clusters=num_clusters, verbose=0).fit(newSeq)
freqVec=np.zeros(num_clusters)
newSeqs=[]
# can now use this to predict points - replace
for idx, seq in enumerate(seqs):
tuples=atch_pTuple(seq,n)
preds=kmeans.predict(tuples)
for val in preds:
freqVec[int(val)]+=1
newSeqs.append(freqVec)
freqVec=np.zeros(num_clusters)
newSeqs=np.array(newSeqs)
return newSeqs
#==============================================================================
# # Helper Functions for constructing pTuples
#==============================================================================
def atch_pTuple(seq,n=3):
point=[]
for i in range(len(seq)-n+1):
tup=seq[i:i+n]
if n==3:
tup=np.concatenate((af.atchleyFactor(tup[0]),af.atchleyFactor(tup[1]),af.atchleyFactor(tup[2])))
else:
tup=np.concatenate((af.atchleyFactor(tup[0]),af.atchleyFactor(tup[1])))
point.append(tup)
point=np.array(point)
return point
def pTuple(vec,n=3):
"""Returns a vector of ptuples from a given sequence"""
return [vec[i:i+n] for i in range(len(vec)-n+1)]
def event_feat(event, n=3):
####### Creates Dictionary ########
result = defaultdict(float)
event=pTuple(event, n)
for tup in event:
if "X" in tup or "Z" in tup or "U" in tup or "B" in tup :
continue
result[tup]+=1
return result
#==============================================================================
# # Helper Functions DataSet Creation
#==============================================================================
def dataReader(files, delim):
"""
This takes a list of file names and a list deliminators which are strings
and reads the data from them. Expects decombinator file names.
THIS IGNORES THE NUCLEOTIDE ADDITION SEQUENCE
"""
cd4=[]
cd8=[]
if not files:
print("please provide a list of file names")
return
else:
# go through each file in the liss
for file in files:
# using an imap to check all delimitors exist
if all(map(file.__contains__, delim)):
with open(file,'r') as infile:
# goes through each of the files specified in read mode and pulls out
# each line and formats it so a list gets X copies of the sequence
for line in infile:
line=lineCleaner(line)
if "CD4" in file:
cd4.append(line)
elif "CD8" in file:
cd8.append(line)
return cd4, cd8
def lineCleaner(string):
"""
Cleaning a line supplied like below and getting a list of important shit
e.g. '20, 6, 4, 0, CTAGGAG, 1, CAWSPKGPQETQYF, 1\n'
"""
string=string.replace("\n","")
string=string.split(", ")
string=string[0:4]+[string[6]]
return string
def dataSpliter(cd, combine=True):
"""
This gets the v region and j region and sequence region
"""
if combine:
vj=[]
seqs=[]
for line in cd:
vj.append([int(line[0]),int(line[1])])
seqs.append(line[4])
return seqs, vj
else:
v=[]
j=[]
seqs=[]
for line in cd:
v.append(int(line[0]))
j.append(int(line[1]))
seqs.append(line[4])
return seqs, v, j
return
def dataCreator(cd4,cd8):
# from this point it is assumed cd4/8 are NUMPY vectors
# labels are created and then the X and Y vectors are shuffled and combo'd
# extra contains a list of anything else to be shuffled
y4=np.zeros((len(cd4)))
y8=np.zeros((len(cd8)))
y4[:]=0
y8[:]=1
# combine classes
Y = np.concatenate((y4,y8),0)
X = np.concatenate((cd4,cd8),0)
return X, Y
def printClassBalance(y):
# this assumes y contains binary classes of 1 and 0
y1=0
y0=0
for yi in y:
if yi==1:
y1+=1
else:
y0+=1
print("Class Balance CD8:CD4 {}:{}".format(y1/(y1+y0),y0/(y1+y0)))
print("Class Sizes CD8:CD4 {}:{}".format(y1,y0))
def printClassBalanceV2(cd4, cd8):
print("CD4 to CD8 Ratio {}:{}".format(len(cd4)/(len(cd4)+len(cd8)),len(cd8)/(len(cd4)+len(cd8))))
print("Class Sizes {}:{}".format(len(cd4),len(cd8)))
print("Total Sequences: {}".format(len(cd4)+len(cd8)))
return None
def removeDup(cd4, cd8, v4, v8):
"""
This takes two lists of strings and removes the common sequences between
the two, and returns them as a separate string for analytics
"""
#print("Removing Shared Sequences")
# set up dictionaries so we have the mapping of seq to v
dicCD4={}
dicCD8={}
for idx, seq in enumerate(cd4):
dicCD4[seq]=v4[idx]
for idx, seq in enumerate(cd8):
dicCD8[seq]=v8[idx]
joint=list(frozenset(cd4).intersection(cd8))
newCD4=list(frozenset(cd4).difference(cd8))
newCD8=list(frozenset(cd8).difference(cd4))
v4new=[dicCD4[seq] for seq in newCD4]
v8new=[dicCD8[seq] for seq in newCD8]
print("Joint Seqs: {}".format(len(joint)))
return newCD4, newCD8, v4new, v8new, joint
#==============================================================================
# Data Loaders
#==============================================================================
"""
Hard coding the links to the data is sloppy but its easy
"""
def loadAllPatients(delim=["naive","beta"]):
files=glob.glob("F:/seqs/*.txt")
cd4, cd8 = dataReader(files,delim)
# focus on just sequence and v region
cd4,cd4vj = dataSpliter(cd4)
cd8,cd8vj = dataSpliter(cd8)
# sequence list to be filled. Make sure file order is the same as a below
seqs=[cd4,cd8] # this contains the sequences
vj=[cd4vj,cd8vj]
return seqs, vj
def extraCDs():
"""
This builds two dictionaries (one for each pop.) that maps a cdr3 to its
cdr1 and cdr2
"""
# File names for different data; A - alpha chain, B - beta chain
#cd4A_file = 'patient1/vDCRe_alpha_EG10_CD4_naive_alpha.txt'
#cd8A_file = 'patient1/vDCRe_alpha_EG10_CD8_naive_alpha.txt'
#cd4B_file = 'patient1/vDCRe_beta_EG10_CD4_naive_beta.txt'
#cd8B_file = 'patient1/vDCRe_beta_EG10_CD8_naive_beta.txt'
#data = 'data/'
#extra = 'extra/'
# Files to be read
#files = [cd4B_file, cd8B_file]
files=glob.glob("F:/maz/*.txt")
# sequence list to be filled. Make sure file order is the same as a below
cd4=[]
cd8=[]
seqs=[cd4,cd8] # this contains the sequences
for index, file in enumerate(files):
#file=data+extra+file
with open(file,'r') as infile:
# goes through each of the files specified in read mode and pulls out
# each line adds each sequence from the line
for line in infile:
threeVals=line.split(",")
threeVals[2]=threeVals[2].replace("\n","")
if "CD4" in file:
seqs[0].append(threeVals)
else:
seqs[1].append(threeVals)
# not going to worry about repeats for now
cddict={}
for cd in seqs:
for seq in cd:
#import pdb; pdb.set_trace()
cddict[seq[2][:-3]]=seq[:2]
return cddict
def addCDextra(seqs, vj, cddict):
"""
this takes the CDR3 and adds the other CDRs based on the dictionary generated
by extra cds
"""
newSeq=[]
newV=[]
fail=0
for idx, seq in enumerate(seqs):
try:
#import pdb; pdb.set_trace()
cds=cddict[seq]
# cds should be [cdr1, cdr2]
# limit it to size [5,6] as there are a few CDR2s that are funky sizes
if len(cds[0])==5 and len(cds[1])==6:
newSeq.append(seq+cds[0]+cds[1])
newV.append(vj[idx][0])
except:
fail+=1
#print("Failures: {}".format(fail))
return newSeq, newV
def subNames(file, c):
"""
Subroutine for getVJnames() that actually reads the file and makes the dict
c - the chain
"""
dic={}
with open(file, 'r') as jf:
for idx, line in enumerate(jf):
line=line.split("|")
# we only care about the second item
line=line[1] # TRBJ1-1
line=line.split("-") #[TRBJ1,1]
first=line[0]
# from here the the code can have two values e.g. TRBJ11
if "/" in first: # sorts out examples like this: TRAV29/DV5
first = first.split('/')
first = first[0]
# sort out double digits if they exist
first=first[-2:] # gets last two digits
try:
first=int(first) # covers double values
except:
try:
first=int(first[-1]) # last of two dig is gonna be an int apart
except:
first=69 # lol
try:#make sure it exists
second=line[1]
if len(second)>1: # can look like 2P
try:
second=int(second)
except:
second=int(second[0])
else:
second=int(second)
except:
second=0
line=[first, second] #[1,1]
# this leaves [family, subfamily]
dic[idx]=line
return dic
def getVJnames(chain='beta', path='data/tags/'):
"""
This generates a dictionary of the family and subfamily of v and j genes
"""
init=path+'human_extended_TR'
if chain=='beta':
init=init+'B'
else:
init=init+'A'
jFile=init+'J.tags'
vFile=init+'V.tags'
"""
format of tag files is e.g.
GGACAAGGCACCAGACTCAC 20 K02545|TRBJ1-1|Homo
"""
jd=subNames(jFile, 'j')
vd=subNames(vFile, 'v')
return jd, vd
def dictEncoder(seqss, vj, filtr=True, clip=True, filtrLen=14):
"""
Takes output of loadAllPatients() and creates a dataset dictionary that maps
a seq. Also expects no duplicates or they'l get overridden anyways
Also will only clip if filtering
"""
mapper={}
for idx, seqs in enumerate(seqss): # this goes CD4 then CD8
for idx2, seq in enumerate(seqs): # this goes individual cdrs
if filtr:
if len(seq)==filtrLen:
if clip:
seq=seq[2:10] # get rid of the first two and last 4
if idx==0: # CD4
mapper[seq]=['CD4', vj[idx][idx2][:]]
else:
mapper[seq]=['CD8', vj[idx][idx2][:]]
else:
if idx==0: # CD4
mapper[seq]=['CD4', vj[idx][idx2][:]]
else:
mapper[seq]=['CD8', vj[idx][idx2][:]]
return mapper
#==============================================================================
# Graphs
#==============================================================================
def venn(cd4, cd8, joint ,Title="for Complete Dataset"):
plt.figure(figsize=(10,10))
v = venn2(subsets = (cd4, cd8, joint), set_labels=('CD4 Sequences', 'CD8 Sequences'))
plt.title("CD4/CD8 Class sizes "+Title)
return None
def lenHisto(cd4, cd8, Title):
# length histogram
lenArr=[]
# create new vector of lengths
for seq in cd4+cd8:
lenArr.append(len(seq))
# convert list to numpy array
lenArr=np.asarray(lenArr)
# bins
binNum=max(lenArr)-min(lenArr)
bins=np.arange(min(lenArr),max(lenArr))-0.5
plt.figure(figsize=(10,5))
binLabs=list(map(int,bins+0.5))
binLabs=[x-2 for x in binLabs]
# setup graph
plt.hist(lenArr, bins=binNum, edgecolor="black")
plt.xticks(bins,binLabs)
plt.xlim([1,max(lenArr)])
plt.title("Sequence Length Histogram"+" "+Title)
plt.xlabel("Sequence Length (AA)")
plt.ylabel("Frequency")
plt.show()
def standardHisto(seqs, Title, xlabel, size):
# this expects a list of preformated numerics
# convert list to numpy array
lenArr=np.asarray(seqs)
# bins
binNum=max(lenArr)-min(lenArr)
bins=np.arange(min(lenArr),max(lenArr))+0.5
plt.figure(figsize=size)
binLabs=list(map(int,bins+0.5))
binLabs=[x-1 for x in binLabs]
binLabs=list(map(str,binLabs))
# setup graph
plt.hist(lenArr, bins=binNum, edgecolor="black")
plt.xticks(bins,binLabs)
#plt.xlim([0,max(lenArr)+1])
plt.title(Title)
plt.xlabel(xlabel)
plt.ylabel("Frequency")
plt.show()
def standardHistoV2(seqs1, seqs2, Title, xlabel, size):
# this expects a list of preformated numerics
# convert list to numpy array
lenArr4=np.asarray(seqs1)
lenArr8=np.asarray(seqs2)
# bins
binNum4=max(lenArr4)-min(lenArr4)
binNum8=max(lenArr8)-min(lenArr8)
hist4=np.histogram(lenArr4,binNum4, density=True)
hist8=np.histogram(lenArr8,binNum8, density=True)
plt.figure(figsize=size)
plt.bar(np.arange(0,binNum4)+0.2,hist4[0],width=0.4,color='b',align='center',label='CD4',edgecolor="black")
plt.bar(np.arange(0,binNum4)-0.2,hist8[0],width=0.4,color='g',align='center',label='CD8',edgecolor="black")
plt.legend(loc='best')
# binLabs=list(map(int,bins+0.5))
# binLabs=[x-1 for x in binLabs]
# binLabs=list(map(str,binLabs))
#
#
#
# # setup graph
# plt.hist(lenArr, bins=binNum, edgecolor="black")
#
plt.xticks(np.arange(0,binNum4))
plt.xlim([-1,binNum4+1])
plt.title(Title)
plt.xlabel(xlabel)
plt.ylabel("Frequency")
plt.show()