-
Notifications
You must be signed in to change notification settings - Fork 0
/
eippred.py
1533 lines (1410 loc) · 57.4 KB
/
eippred.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
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
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import print_function
import argparse
import warnings
import subprocess
import itertools
from collections import Counter
import pickle
import uuid
from time import sleep
from tqdm import tqdm
from sklearn.ensemble import RandomForestRegressor
import zipfile
import getopt
import sys
import os
import numpy as np
import pandas as pd
import math
from itertools import repeat
import csv
import re
import glob
import time
from argparse import RawTextHelpFormatter
import uuid
import warnings
import os
import random
import shutil
nf_path = os.path.dirname(__file__)
aa = pd.read_csv(nf_path+ '/Data/selected_features_mrmr1000_new.csv')
filtered_aa = aa[aa['SelectedFeatures'].str.startswith('TPC')]
selected_tripeptides = filtered_aa['SelectedFeatures'].tolist()
filtered_bb = aa[aa['SelectedFeatures'].str.startswith('DPC')]
selected_dipeptides = filtered_bb['SelectedFeatures'].tolist()
def features_calulate(file_path, output_file):
# Standard amino acid codes
std = "ACDEFGHIKLMNPQRSTVWY"
def aac_comp(file, out):
filename, _ = os.path.splitext(file)
f = open(out, 'w')
sys.stdout = f
df = pd.read_csv(file, header=None)
zz = df.iloc[:, 0]
print("AAC_A,AAC_C,AAC_D,AAC_E,AAC_F,AAC_G,AAC_H,AAC_I,AAC_K,AAC_L,AAC_M,AAC_N,AAC_P,AAC_Q,AAC_R,AAC_S,AAC_T,AAC_V,AAC_W,AAC_Y,")
for j in zz:
for i in std:
count = 0
for k in j:
temp1 = k
if temp1 == i:
count += 1
composition = (count / len(j)) * 100
print("%.2f" % composition, end=",")
print("")
f.truncate()
def dpc_comp(file, q, out, selected_dipeptides):
# Open the input file and read the data
df = pd.read_csv(file, header=None)
df1 = pd.DataFrame(df[0].str.upper())
zz = df1.iloc[:, 0]
# Open the output file
with open(out, 'w') as f:
# Redirect stdout to the output file
original_stdout = sys.stdout
sys.stdout = f
# Print the header for selected dipeptides
headers = [f"{dp}" for dp in selected_dipeptides]
print(",".join(headers) + ",")
# Calculate and print dipeptide composition for selected dipeptides
for sequence in zz:
compositions = []
for dp in selected_dipeptides:
count = 0
for m3 in range(len(sequence) - q):
b = sequence[m3:m3 + q + 1:q]
if b == dp:
count += 1
composition = (count / (len(sequence) - q)) * 100
compositions.append(f"{composition:.2f}")
print(",".join(compositions) + ",")
# Restore original stdout
sys.stdout = original_stdout
def tpc_comp(file, out, selected_tripeptides):
df = pd.read_csv(file, header=None)
zz = df.iloc[:, 0]
with open(out, 'w') as f:
original_stdout = sys.stdout
sys.stdout = f
# Print header for selected tripeptides
for tripeptide in selected_tripeptides:
print(tripeptide, end=",")
print("")
# Calculate and print tripeptide composition for selected tripeptides
for i in range(len(zz)):
sequence = zz[i]
seq_len = len(sequence)
for tripeptide in selected_tripeptides:
count = 0
for m3 in range(seq_len - 2):
b = sequence[m3:m3 + 3]
if b == tripeptide:
count += 1
composition = (count / (seq_len - 2)) * 100
print(f"{composition:.2f}", end=',')
print("")
sys.stdout = original_stdout
def atc(file,out):
filename,file_ext = os.path.splitext(file)
atom=pd.read_csv(nf_path+"/Data/atom.csv",header=None)
at=pd.DataFrame()
i = 0
C_atom = []
H_atom = []
N_atom = []
O_atom = []
S_atom = []
while i < len(atom):
C_atom.append(atom.iloc[i,1].count("C"))
H_atom.append(atom.iloc[i,1].count("H"))
N_atom.append(atom.iloc[i,1].count("N"))
O_atom.append(atom.iloc[i,1].count("O"))
S_atom.append(atom.iloc[i,1].count("S"))
i += 1
atom["C_atom"]=C_atom
atom["O_atom"]=O_atom
atom["H_atom"]=H_atom
atom["N_atom"]=N_atom
atom["S_atom"]=S_atom
##############read file ##########
test1 = pd.read_csv(file,header=None)
dd = []
for i in range(0, len(test1)):
dd.append(test1[0][i].upper())
test = pd.DataFrame(dd)
count_C = 0
count_H = 0
count_N = 0
count_O = 0
count_S = 0
count = 0
i1 = 0
j = 0
k = 0
C_ct = []
H_ct = []
N_ct = []
O_ct = []
S_ct = []
while i1 < len(test) :
while j < len(test[0][i1]) :
while k < len(atom) :
if test.iloc[i1,0][j]==atom.iloc[k,0].replace(" ","") :
count_C = count_C + atom.iloc[k,2]
count_H = count_H + atom.iloc[k,3]
count_N = count_N + atom.iloc[k,4]
count_O = count_O + atom.iloc[k,5]
count_S = count_S + atom.iloc[k,6]
#count = count_C + count_H + count_S + count_N + count_O
k += 1
k = 0
j += 1
C_ct.append(count_C)
H_ct.append(count_H)
N_ct.append(count_N)
O_ct.append(count_O)
S_ct.append(count_S)
count_C = 0
count_H = 0
count_N = 0
count_O = 0
count_S = 0
j = 0
i1 += 1
test["C_count"]=C_ct
test["H_count"]=H_ct
test["N_count"]=N_ct
test["O_count"]=O_ct
test["S_count"]=S_ct
ct_total = []
m = 0
while m < len(test) :
ct_total.append(test.iloc[m,1] + test.iloc[m,2] + test.iloc[m,3] + test.iloc[m,4] + test.iloc[m,5])
m += 1
test["count"]=ct_total
##########final output#####
final = pd.DataFrame()
n = 0
p = 0
C_p = []
H_p = []
N_p = []
O_p = []
S_p = []
while n < len(test):
C_p.append((test.iloc[n,1]/test.iloc[n,6])*100)
H_p.append((test.iloc[n,2]/test.iloc[n,6])*100)
N_p.append((test.iloc[n,3]/test.iloc[n,6])*100)
O_p.append((test.iloc[n,4]/test.iloc[n,6])*100)
S_p.append((test.iloc[n,5]/test.iloc[n,6])*100)
n += 1
final["ATC_C"] = C_p
final["ATC_H"] = H_p
final["ATC_N"] = N_p
final["ATC_O"] = O_p
final["ATC_S"] = S_p
(final.round(2)).to_csv(out, index = None, encoding = 'utf-8')
def bond(file,out) :
tota = []
hy = []
Si = []
Du = []
b1 = []
b2 = []
b3 = []
b4 = []
bb = pd.DataFrame()
filename, file_extension = os.path.splitext(file)
df = pd.read_csv(file, header = None)
bonds=pd.read_csv(nf_path+"/Data/bonds.csv", sep = ",")
for i in range(0,len(df)) :
tot = 0
h = 0
S = 0
D = 0
tota.append([i])
hy.append([i])
Si.append([i])
Du.append([i])
for j in range(0,len(df[0][i])) :
temp = df[0][i][j]
for k in range(0,len(bonds)) :
if bonds.iloc[:,0][k] == temp :
tot = tot + bonds.iloc[:,1][k]
h = h + bonds.iloc[:,2][k]
S = S + bonds.iloc[:,3][k]
D = D + bonds.iloc[:,4][k]
tota[i].append(tot)
hy[i].append(h)
Si[i].append(S)
Du[i].append(D)
for m in range(0,len(df)) :
b1.append(tota[m][1])
b2.append(hy[m][1])
b3.append(Si[m][1])
b4.append(Du[m][1])
bb["BTC_T"] = b1
bb["BTC_H"] = b2
bb["BTC_S"] = b3
bb["BTC_D"] = b4
bb.to_csv(out, index=None, encoding="utf-8")
############################PhysicoChemical Properties###################################
import pandas as pd
PCP= pd.read_csv(nf_path+'/Data/PhysicoChemical.csv', header=None)
headers = ['PCP_PC','PCP_NC','PCP_NE','PCP_PO','PCP_NP','PCP_AL','PCP_CY','PCP_AR','PCP_AC','PCP_BS','PCP_NE_pH','PCP_HB','PCP_HL','PCP_NT','PCP_HX','PCP_SC','PCP_SS_HE','PCP_SS_ST','PCP_SS_CO','PCP_SA_BU','PCP_SA_EX','PCP_SA_IN','PCP_TN','PCP_SM','PCP_LR','PCP_Z1','PCP_Z2','PCP_Z3','PCP_Z4','PCP_Z5'];
def encode(peptide):
l=len(peptide);
encoded=np.zeros(l);
for i in range(l):
if(peptide[i]=='A'):
encoded[i] = 0;
elif(peptide[i]=='C'):
encoded[i] = 1;
elif(peptide[i]=='D'):
encoded[i] = 2;
elif(peptide[i]=='E'):
encoded[i] = 3;
elif(peptide[i]=='F'):
encoded[i] = 4;
elif(peptide[i]=='G'):
encoded[i] = 5;
elif(peptide[i]=='H'):
encoded[i] = 6;
elif(peptide[i]=='I'):
encoded[i] = 7;
elif(peptide[i]=='K'):
encoded[i] = 8;
elif(peptide[i]=='L'):
encoded[i] = 9;
elif(peptide[i]=='M'):
encoded[i] = 10;
elif(peptide[i]=='N'):
encoded[i] = 11;
elif(peptide[i]=='P'):
encoded[i] = 12;
elif(peptide[i]=='Q'):
encoded[i] = 13;
elif(peptide[i]=='R'):
encoded[i] = 14;
elif(peptide[i]=='S'):
encoded[i] = 15;
elif(peptide[i]=='T'):
encoded[i] = 16;
elif(peptide[i]=='V'):
encoded[i] = 17;
elif(peptide[i]=='W'):
encoded[i] = 18;
elif(peptide[i]=='Y'):
encoded[i] = 19;
else:
print('Wrong residue!');
return encoded;
def lookup(peptide,featureNum):
l=len(peptide);
peptide = list(peptide);
out=np.zeros(l);
peptide_num = encode(peptide);
for i in range(l):
out[i] = PCP[peptide_num[i]][featureNum];
return sum(out);
def pcp_1(file,out123):
if(type(file) == str):
seq = pd.read_csv(file,header=None);
#seq=seq.T
seq[0].values.tolist()
seq=seq[0];
else:
seq = file;
l = len(seq);
rows = PCP.shape[0]; # Number of features in our reference table
col = 20 ; # Denotes the 20 amino acids
seq=[seq[i].upper() for i in range(l)]
sequenceFeature = [];
sequenceFeature.append(headers); #To put property name in output csv
for i in range(l): # Loop to iterate over each sequence
nfeatures = rows;
sequenceFeatureTemp = [];
for j in range(nfeatures): #Loop to iterate over each feature
featureVal = lookup(seq[i],j)
if(len(seq[i])!=0):
sequenceFeatureTemp.append(round(featureVal/len(seq[i]),3));
else:
sequenceFeatureTemp.append('NaN')
sequenceFeature.append(sequenceFeatureTemp);
out = pd.DataFrame(sequenceFeature);
file = open(out123,'w')
with file:
writer = csv.writer(file);
writer.writerows(sequenceFeature);
return sequenceFeature;
def DDOR(file,out) :
df = pd.read_csv(file, header = None)
df1 = pd.DataFrame(df[0].str.upper())
f = open(out,'w')
sys.stdout = f
for i in std:
print('DDR_'+i, end=",")
print("")
for i in range(0,len(df1)):
s = df1[0][i]
p = s[::-1]
for j in std:
zz = ([pos for pos, char in enumerate(s) if char == j])
pp = ([pos for pos, char in enumerate(p) if char == j])
ss = []
for i in range(0,(len(zz)-1)):
ss.append(zz[i+1] - zz[i]-1)
if zz == []:
ss = []
else:
ss.insert(0,zz[0])
ss.insert(len(ss),pp[0])
cc1= (sum([e for e in ss])+1)
cc = sum([e*e for e in ss])
zz2 = cc/cc1
print("%.2f"%zz2,end=",")
print("")
f.truncate()
##################################
def entropy_single(seq):
seq=seq.upper()
num, length = Counter(seq), len(seq)
return -sum( freq/length * math.log(freq/length, 2) for freq in num.values())
def SE(filename,out):
data=list((pd.read_csv(filename,sep=',',header=None)).iloc[:,0])
# print(data)
Val=[]
header=["SEP"]
for i in range(len(data)):
data1=''
data1=str(data[i])
data1=data1.upper()
allowed = set(('A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y'))
is_data_invalid = set(data1).issubset(allowed)
if is_data_invalid==False:
print("Error: Please check for invalid inputs in the sequence.","\nError in: ","Sequence number=",i+1,",","Sequence = ",data[i],",","\nNOTE: Spaces, Special characters('[@_!#$%^&*()<>?/\|}{~:]') and Extra characters(BJOUXZ) should not be there.")
return
Val.append(round((entropy_single(str(data[i]))),3))
#print(Val[i])
file= open(out,'w', newline='\n')#output file
with file:
writer=csv.writer(file,delimiter='\n');
writer.writerow(header)
writer.writerow(Val);
return Val
def SE_residue_level(filename,out):
data=list((pd.read_csv(filename,sep=',',header=None)).iloc[:,0])
data2=list((pd.read_csv(filename,sep=',',header=None)).iloc[:,0])
Val=np.zeros(len(data))
GH=[]
for i in range(len(data)):
my_list={'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}
data1=''
data1=str(data[i])
data1=data1.upper()
allowed = set(('A','C','D','E','F','G','H','I','K','L','M','N','P','Q','R','S','T','V','W','Y'))
is_data_invalid = set(data1).issubset(allowed)
if is_data_invalid==False:
print("Error: Please check for invalid inputs in the sequence.","\nError in: ","Sequence number=",i+1,",","Sequence = ",data[i],",","\nNOTE: Spaces, Special characters('[@_!#$%^&*()<>?/\|}{~:]') and Extra characters(BJOUXZ) should not be there.")
return
seq=data[i]
seq=seq.upper()
num, length = Counter(seq), len(seq)
num=dict(sorted(num.items()))
C=list(num.keys())
F=list(num.values())
for key, value in my_list.items():
for j in range(len(C)):
if key == C[j]:
my_list[key] = round(((F[j]/length)* math.log(F[j]/length, 2)),3)
GH.append(list(my_list.values()))
file= open(out,'w', newline='')#output file
with file:
writer=csv.writer(file);
writer.writerow(('SER_A','SER_C','SER_D','SER_E','SER_F','SER_G','SER_H','SER_I','SER_K','SER_L','SER_M','SER_N','SER_P','SER_Q','SER_R','SER_S','SER_T','SER_V','SER_W','SER_Y'));
writer.writerows(GH);
return(GH)
def RAAC(file,out):
filename, file_extension = os.path.splitext(file)
df = pd.read_csv(file, header = None)
df1 = pd.DataFrame(df[0].str.upper())
count = 0
cc = []
i = 0
x = 0
temp = pd.DataFrame()
f = open(out,'w')
sys.stdout = f
print("RRI_A,RRI_C,RRI_D,RRI_E,RRI_F,RRI_G,RRI_H,RRI_I,RRI_K,RRI_L,RRI_M,RRI_N,RRI_P,RRI_Q,RRI_R,RRI_S,RRI_T,RRI_V,RRI_W,RRI_Y,")
for q in range(0,len(df1)):
while i < len(std):
cc = []
for j in df1[0][q]:
if j == std[i]:
count += 1
cc.append(count)
else:
count = 0
while x < len(cc) :
if x+1 < len(cc) :
if cc[x]!=cc[x+1] :
if cc[x] < cc[x+1] :
cc[x]=0
x += 1
cc1 = [e for e in cc if e!= 0]
cc = [e*e for e in cc if e != 0]
zz= sum(cc)
zz1 = sum(cc1)
if zz1 != 0:
zz2 = zz/zz1
else:
zz2 = 0
print("%.2f"%zz2,end=',')
i += 1
i = 0
print(" ")
f.truncate()
PCP= pd.read_csv(nf_path+'/Data/PhysicoChemical.csv', header=None) #Our reference table for properties
headers_1 = ['PRI_PC','PRI_NC','PRI_NE','PRI_PO','PRI_NP','PRI_AL','PRI_CY','PRI_AR','PRI_AC','PRI_BS','PRI_NE_pH','PRI_HB','PRI_HL','PRI_NT','PRI_HX','PRI_SC','PRI_SS_HE','PRI_SS_ST','PRI_SS_CO','PRI_SA_BU','PRI_SA_EX','PRI_SA_IN','PRI_TN','PRI_SM','PRI_LR']
# Amino acid encoding dictionary
amino_acid_dict = {
'A': 0, 'C': 1, 'D': 2, 'E': 3, 'F': 4,
'G': 5, 'H': 6, 'I': 7, 'K': 8, 'L': 9,
'M': 10, 'N': 11, 'P': 12, 'Q': 13, 'R': 14,
'S': 15, 'T': 16, 'V': 17, 'W': 18, 'Y': 19
}
def encode(peptide):
# Use a list comprehension for fast lookup
encoded = np.array([amino_acid_dict.get(aa, -1) for aa in peptide])
# Check for invalid residues
if -1 in encoded:
invalid_residues = [peptide[i] for i in range(len(peptide)) if encoded[i] == -1]
for residue in invalid_residues:
print(residue, 'is a wrong residue!')
return encoded
def lookup_1(peptide,featureNum):
l=len(peptide);
peptide = list(peptide);
out=[];
peptide_num = encode(peptide);
for i in range(l):
out.append(PCP[peptide_num[i]][featureNum]);
return out;
def binary_profile_1(file,featureNumb):
if(type(file) == str):
seq = pd.read_csv(file,header=None, sep=',');
seq=seq.T
seq[0].values.tolist()
seq=seq[0];
else:
seq = file;
l = len(seq);
bin_prof = [];
for i in range(0,l):
temp = lookup_1(seq[i],featureNumb);
bin_prof.append(temp);
return bin_prof;
def repeats(file,out123):
if(type(file) == str):
seq = pd.read_csv(file,header=None, sep=',');
#seq=seq.T
seq[0].values.tolist()
seq=seq[0];
else:
seq = file;
seq=[seq[i].upper() for i in range(len(seq))]
dist =[];
dist.append(headers_1);
l = len(seq);
for i in range(l):
temp=[];
for j in range(25):
bin_prof = binary_profile_1(seq, j);
if(j>=25):
print('Error! Feature Number must be between 0-24');
break;
k=0;
num=0;
denom=0;
ones=0;
zeros=0;
for j in range(len(bin_prof[i])):
if(bin_prof[i][j]==0):
num+=k*k;
denom+=k;
k=0;
zeros+=1;
elif(j==len(bin_prof[i])-1):
k+=1;
num+=k*k;
denom+=k;
else:
k+=1;
ones+=1;
if(ones!=0):
answer = num/(ones*ones)
temp.append(round(num/(ones*ones),2));
elif(ones==0):
temp.append(0);
dist.append(temp)
out = pd.DataFrame(dist)
file1 = open(out123,'w')
with file1:
writer = csv.writer(file1);
writer.writerows(dist);
return out
def lookup(peptide,featureNum):
l=len(peptide);
peptide = list(peptide);
out=np.zeros(l);
peptide_num = encode(peptide);
for i in range(l):
out[i] = PCP[peptide_num[i]][featureNum];
return sum(out);
def pcp(file):
SEP_headers = ['SEP_PC','SEP_NC','SEP_NE','SEP_PO','SEP_NP','SEP_AL','SEP_CY','SEP_AR','SEP_AC','SEP_BS','SEP_NE_pH','SEP_HB','SEP_HL','SEP_NT','SEP_HX','SEP_SC','SEP_SS_HE','SEP_SS_ST','SEP_SS_CO','SEP_SA_BU','SEP_SA_EX','SEP_SA_IN','SEP_TN','SEP_SM','SEP_LR']
if(type(file) == str):
seq = pd.read_csv(file,header=None, sep=',');
seq=seq.T
seq[0].values.tolist()
seq=seq[0];
else:
seq = file;
l = len(seq);
rows = PCP.shape[0]; # Number of features in our reference table
col = 20 ; # Denotes the 20 amino acids
seq=[seq[i].upper() for i in range(l)]
sequenceFeature = [];
sequenceFeature.append(SEP_headers); #To put property name in output csv
for i in range(l): # Loop to iterate over each sequence
nfeatures = rows;
sequenceFeatureTemp = [];
for j in range(nfeatures): #Loop to iterate over each feature
featureVal = lookup(seq[i],j)
if(len(seq[i])!=0):
sequenceFeatureTemp.append(featureVal/len(seq[i]))
else:
sequenceFeatureTemp.append('NaN')
sequenceFeature.append(sequenceFeatureTemp);
out = pd.DataFrame(sequenceFeature);
return sequenceFeature;
def phyChem(file,mode='all',m=0,n=0):
if(type(file) == str):
seq1 = pd.read_csv(file,header=None, sep=',');
seq1 = pd.DataFrame(seq1[0].str.upper())
seq=[]
[seq.append(seq1.iloc[i][0]) for i in range(len(seq1))]
else:
seq = file;
l = len(seq);
newseq = [""]*l; # To store the n-terminal sequence
for i in range(0,l):
l = len(seq[i]);
if(mode=='NT'):
n=m;
if(n!=0):
newseq[i] = seq[i][0:n];
elif(n>l):
print('Warning! Sequence',i,"'s size is less than n. The output table would have NaN for this sequence");
else:
print('Value of n is mandatory, it cannot be 0')
break;
elif(mode=='CT'):
n=m;
if(n!=0):
newseq[i] = seq[i][(len(seq[i])-n):]
elif(n>l):
print('WARNING: Sequence',i+1,"'s size is less than the value of n given. The output table would have NaN for this sequence");
else:
print('Value of n is mandatory, it cannot be 0')
break;
elif(mode=='all'):
newseq = seq;
elif(mode=='rest'):
if(m==0):
print('Kindly provide start index for rest, it cannot be 0');
break;
else:
if(n<=len(seq[i])):
newseq[i] = seq[i][m-1:n+1]
elif(n>len(seq[i])):
newseq[i] = seq[i][m-1:len(seq[i])]
print('WARNING: Since input value of n for sequence',i+1,'is greater than length of the protein, entire sequence starting from m has been considered')
else:
print("Wrong Mode. Enter 'NT', 'CT','all' or 'rest'");
output = pcp(newseq);
return output
def shannons(filename,out123):
SEP_headers = ['SEP_PC','SEP_NC','SEP_NE','SEP_PO','SEP_NP','SEP_AL','SEP_CY','SEP_AR','SEP_AC','SEP_BS','SEP_NE_pH','SEP_HB','SEP_HL','SEP_NT','SEP_HX','SEP_SC','SEP_SS_HE','SEP_SS_ST','SEP_SS_CO','SEP_SA_BU','SEP_SA_EX','SEP_SA_IN','SEP_TN','SEP_SM','SEP_LR']
if(type(filename) == str):
seq1 = pd.read_csv(filename,header=None, sep=',');
seq1 = pd.DataFrame(seq1[0].str.upper())
else:
seq1 = filename;
seq=[]
[seq.append(seq1.iloc[i][0]) for i in range(len(seq1))]
comp = phyChem(seq);
new = [comp[i][0:25] for i in range(len(comp))]
entropy = [];
entropy.append(SEP_headers[0:25])
for i in range(1,len(new)):
seqEntropy = [];
for j in range(len(new[i])):
p = new[i][j];
if((1-p) == 0. or p==0.):
temp = 0;#to store entropy of each sequence
else:
temp = -(p*math.log2(p)+(1-p)*math.log2(1-p));
seqEntropy.append(round(temp,3));
entropy.append(seqEntropy);
out = pd.DataFrame(entropy);
file = open(out123,'w')
with file:
writer = csv.writer(file);
writer.writerows(entropy);
return entropy;
##################paac####################
def val(AA_1, AA_2, aa, mat):
return sum([(mat[i][aa[AA_1]] - mat[i][aa[AA_2]]) ** 2 for i in range(len(mat))]) / len(mat)
def paac_1(file,lambdaval,w=0.05):
data1 = pd.read_csv(nf_path+"/Data/data", sep = "\t")
filename, file_extension = os.path.splitext(file)
df = pd.read_csv(file, header = None)
df1 = pd.DataFrame(df[0].str.upper())
dd = []
cc = []
pseudo = []
aa = {}
for i in range(len(std)):
aa[std[i]] = i
for i in range(0,3):
mean = sum(data1.iloc[i][1:])/20
rr = math.sqrt(sum([(p-mean)**2 for p in data1.iloc[i][1:]])/20)
dd.append([(p-mean)/rr for p in data1.iloc[i][1:]])
zz = pd.DataFrame(dd)
head = []
for n in range(1, lambdaval + 1):
head.append('_lam' + str(n))
head = ['PAAC'+str(lambdaval)+sam for sam in head]
pp = pd.DataFrame()
ee = []
for k in range(0,len(df1)):
cc = []
pseudo1 = []
for n in range(1,lambdaval+1):
cc.append(sum([val(df1[0][k][p], df1[0][k][p + n], aa, dd) for p in range(len(df1[0][k]) - n)]) / (len(df1[0][k]) - n))
qq = pd.DataFrame(cc)
pseudo = pseudo1 + [(w * p) / (1 + w * sum(cc)) for p in cc]
ee.append(pseudo)
ii = round(pd.DataFrame(ee, columns = head),4)
ii.to_csv(filename+".lam",index = None)
def paac(file,lambdaval,out,w=0.05):
filename, file_extension = os.path.splitext(file)
paac_1(file,lambdaval,w=0.05)
aac_comp(file,filename+".aac")
data1 = pd.read_csv(filename+".aac")
header = ['PAAC'+str(lambdaval)+'_A','PAAC'+str(lambdaval)+'_C','PAAC'+str(lambdaval)+'_D','PAAC'+str(lambdaval)+'_E','PAAC'+str(lambdaval)+'_F','PAAC'+str(lambdaval)+'_G','PAAC'+str(lambdaval)+'_H','PAAC'+str(lambdaval)+'_I','PAAC'+str(lambdaval)+'_K','PAAC'+str(lambdaval)+'_L','PAAC'+str(lambdaval)+'_M','PAAC'+str(lambdaval)+'_N','PAAC'+str(lambdaval)+'_P','PAAC'+str(lambdaval)+'_Q','PAAC'+str(lambdaval)+'_R','PAAC'+str(lambdaval)+'_S','PAAC'+str(lambdaval)+'_T','PAAC'+str(lambdaval)+'_V','PAAC'+str(lambdaval)+'_W','PAAC'+str(lambdaval)+'_Y','Un']
data1.columns = header
data2 = pd.read_csv(filename+".lam")
data3 = pd.concat([data1.iloc[:,:-1],data2], axis = 1).reset_index(drop=True)
data3.to_csv(out,index=None)
os.remove(filename+".lam")
os.remove(filename+".aac")
######################apaac############################
def apaac_1(file,lambdaval,w=0.05):
data1 = pd.read_csv(nf_path+"/Data/data", sep = "\t")
filename, file_extension = os.path.splitext(file)
df = pd.read_csv(file, header = None)
df1 = pd.DataFrame(df[0].str.upper())
dd = []
cc = []
pseudo = []
aa = {}
for i in range(len(std)):
aa[std[i]] = i
for i in range(0,3):
mean = sum(data1.iloc[i][1:])/20
rr = math.sqrt(sum([(p-mean)**2 for p in data1.iloc[i][1:]])/20)
dd.append([(p-mean)/rr for p in data1.iloc[i][1:]])
zz = pd.DataFrame(dd)
head = []
for n in range(1, lambdaval + 1):
for e in ('HB','HL','SC'):
head.append(e+'_lam' + str(n))
head = ['APAAC'+str(lambdaval)+'_'+sam for sam in head]
pp = pd.DataFrame()
ee = []
for k in range(0,len(df1)):
cc = []
for n in range(1,lambdaval+1):
for b in range(0,len(zz)):
cc.append(sum([zz.loc[b][aa[df1[0][k][p]]] * zz.loc[b][aa[df1[0][k][p + n]]] for p in range(len(df1[0][k]) - n)]) / (len(df1[0][k]) - n))
qq = pd.DataFrame(cc)
pseudo = [(w * p) / (1 + w * sum(cc)) for p in cc]
ee.append(pseudo)
ii = round(pd.DataFrame(ee, columns = head),4)
ii.to_csv(filename+".plam",index = None)
def apaac(file,lambdaval,out,w=0.05):
filename, file_extension = os.path.splitext(file)
apaac_1(file,lambdaval,w=0.05)
aac_comp(file,filename+".aac")
data1 = pd.read_csv(filename+".aac")
headaac = []
for i in std:
headaac.append('APAAC'+str(lambdaval)+'_'+i)
headaac.insert(len(headaac),0)
data1.columns = headaac
data2 = pd.read_csv(filename+".plam")
data3 = pd.concat([data1.iloc[:,:-1],data2], axis = 1).reset_index(drop=True)
data3.to_csv(out, index = None)
os.remove(filename+".plam")
os.remove(filename+".aac")
###################################qos#######################################
def qos(file,gap,out,w=0.1):
ff = []
filename, file_extension = os.path.splitext(file)
df = pd.read_csv(file, header = None)
df2 = pd.DataFrame(df[0].str.upper())
for i in range(0,len(df2)):
ff.append(len(df2[0][i]))
if min(ff) < gap:
print("Error: All sequences' length should be higher than :", gap)
else:
mat1 = pd.read_csv(nf_path+"/Data/Schneider-Wrede.csv", index_col = 'Name')
mat2 = pd.read_csv(nf_path+"/Data/Grantham.csv", index_col = 'Name')
s1 = []
s2 = []
for i in range(0,len(df2)):
for n in range(1, gap+1):
sum1 = 0
sum2 = 0
for j in range(0,(len(df2[0][i])-n)):
sum1 = sum1 + (mat1[df2[0][i][j]][df2[0][i][j+n]])**2
sum2 = sum2 + (mat2[df2[0][i][j]][df2[0][i][j+n]])**2
s1.append(sum1)
s2.append(sum2)
zz = pd.DataFrame(np.array(s1).reshape(len(df2),gap))
zz["sum"] = zz.sum(axis=1)
zz2 = pd.DataFrame(np.array(s2).reshape(len(df2),gap))
zz2["sum"] = zz2.sum(axis=1)
c1 = []
c2 = []
c3 = []
c4 = []
h1 = []
h2 = []
h3 = []
h4 = []
for aa in std:
h1.append('QSO'+str(gap)+'_SC_' + aa)
for aa in std:
h2.append('QSO'+str(gap)+'_G_' + aa)
for n in range(1, gap+1):
h3.append('SC' + str(n))
h3 = ['QSO'+str(gap)+'_'+sam for sam in h3]
for n in range(1, gap+1):
h4.append('G' + str(n))
h4 = ['QSO'+str(gap)+'_'+sam for sam in h4]
for i in range(0,len(df2)):
AA = {}
for j in std:
AA[j] = df2[0][i].count(j)
c1.append(AA[j] / (1 + w * zz['sum'][i]))
c2.append(AA[j] / (1 + w * zz2['sum'][i]))
for k in range(0,gap):
c3.append((w * zz[k][i]) / (1 + w * zz['sum'][i]))
c4.append((w * zz[k][i]) / (1 + w * zz['sum'][i]))
pp1 = np.array(c1).reshape(len(df2),len(std))
pp2 = np.array(c2).reshape(len(df2),len(std))
pp3 = np.array(c3).reshape(len(df2),gap)
pp4 = np.array(c4).reshape(len(df2),gap)
zz5 = round(pd.concat([pd.DataFrame(pp1, columns = h1),pd.DataFrame(pp2,columns = h2),pd.DataFrame(pp3, columns = h3),pd.DataFrame(pp4, columns = h4)], axis = 1),4)
zz5.to_csv(out, index = None, encoding = 'utf-8')
##########################soc################
def soc(file,gap,out):
ff = []
filename, file_extension = os.path.splitext(file)
df = pd.read_csv(file, header = None)
df2 = pd.DataFrame(df[0].str.upper())
for i in range(0,len(df2)):
ff.append(len(df2[0][i]))
if min(ff) < gap:
print("Error: All sequences' length should be higher than :", gap)
return 0
mat1 = pd.read_csv(nf_path+"/Data/Schneider-Wrede.csv", index_col = 'Name')
mat2 = pd.read_csv(nf_path+"/Data/Grantham.csv", index_col = 'Name')
h1 = []
h2 = []
for n in range(1, gap+1):
h1.append('SC' + str(n))
for n in range(1, gap + 1):
h2.append('G' + str(n))
h1 = ['SOC'+str(gap)+'_'+sam for sam in h1]
h2 = ['SOC'+str(gap)+'_'+sam for sam in h2]
s1 = []
s2 = []
for i in range(0,len(df2)):
for n in range(1, gap+1):
sum = 0
sum1 =0
sum2 =0
sum3 =0
for j in range(0,(len(df2[0][i])-n)):
sum = sum + (mat1[df2[0][i][j]][df2[0][i][j+n]])**2
sum1 = sum/(len(df2[0][i])-n)
sum2 = sum2 + (mat2[df2[0][i][j]][df2[0][i][j+n]])**2
sum3 = sum2/(len(df2[0][i])-n)
s1.append(sum1)
s2.append(sum3)
zz = np.array(s1).reshape(len(df2),gap)
zz2 = np.array(s2).reshape(len(df2),gap)
zz3 = round(pd.concat([pd.DataFrame(zz, columns = h1),pd.DataFrame(zz2,columns = h2)], axis = 1),4)
zz3.to_csv(out, index = None, encoding = 'utf-8')
##########################################CTC###################################
import pandas as pd
import csv
def repstring(string):
string = string.upper()
char = {"A":"1", "G":"1", "V":"1", "I":"2", "L":"2", "F":"2", "P":"2", "Y":"3", "M":"3", "T":"3", "S":"3", "H":"4", "N":"4", "Q":"4", "W":"4", "R":"5", "K":"5", "D":"6", "E":"6", "C":"7"}
string = list(string)
for index, item in enumerate(string):
for key, value in char.items():
if item == key:
string[index] = value
return "".join(string)
def occurrences(string, sub_string):
count = 0
beg = 0
while(string.find(sub_string, beg) != -1):
count += 1
beg = string.find(sub_string, beg)
beg += 1
return count
def CTC(filename, out, triads):
data = list((pd.read_csv(filename, sep=',', header=None)).iloc[:, 0])
result = []
for sequence in data:
sequence = sequence.upper()
Y = repstring(sequence)
row = []
for triad in triads:
row.append(round(occurrences(Y, triad) / len(Y), 3))
result.append(row)
headers = ['CTC_' + triad for triad in triads]
with open(out, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(headers)
writer.writerows(result)
# List of triads for which you want to calculate features
triads_to_calculate = [
'642', '464', '735', '517', '326', '246', '552', '452', '127', '273',
'547', '557', '251', '725', '472', '115', '274', '574', '665', '666',
'333', '525', '575', '512', '155', '471', '644', '626', '211', '255',
'225', '566', '227', '745', '715', '677', '522', '713', '112', '122',
'174', '221', '173', '545', '661', '355'
]
######################################CETD###################################
def ctd(file,out):
attr=pd.read_csv(nf_path+"/Data/aa_attr_group.csv", sep="\t")
filename, file_extension = os.path.splitext(file)
df1 = pd.read_csv(file, header = None)
df = pd.DataFrame(df1[0].str.upper())
n = 0
stt1 = []
m = 1
for i in range(0,len(attr)) :
st =[]
stt1.append([])
for j in range(0,len(df)) :
stt1[i].append([])
for k in range(0,len(df[0][j])) :
while m < 4 :
while n < len(attr.iloc[i,m]) :
if df[0][j][k] == attr.iloc[i,m][n] :
st.append(m)
stt1[i][j].append(m)
n += 2
n = 0
m += 1
m = 1
#####################Composition######################
f = open("compout_1", 'w')
sys.stdout = f
std = [1,2,3]
print("1,2,3,")
for p in range (0,len(df)) :
for ii in range(0,len(stt1)) :