-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathIPMiner.py
2254 lines (2022 loc) · 91.9 KB
/
IPMiner.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
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import LinearSVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.decomposition import PCA
from sklearn import metrics
#from sklearn.metrics import confusion_matrix
import theano
#from pystruct.datasets import load_letters
#from pystruct.models import ChainCRF
#from pystruct.learners import OneSlackSSVM
from sklearn import svm, grid_search
from sklearn.preprocessing import LabelEncoder
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.cross_validation import train_test_split
from sklearn.calibration import CalibratedClassifierCV
from sklearn.cross_validation import StratifiedKFold
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_curve, auc
import gzip
from random import randint
#import xgboost as xgb
import pandas as pd
import pdb
import os
import sys
import random
import argparse
from theano import tensor as T
# keras version is 0.1.2, please install this version of keras
#import keras
#keras_version = keras.__version__
try:
print 'please install Keras 0.1.2'
sys.path.insert(0, '/usr/local/lib/python2.7/dist-packages/Keras-0.1.2-py2.7.egg')
except:
print 'install keras 0.1.2'
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, AutoEncoder, Flatten, Merge
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import PReLU
from keras.utils import np_utils, generic_utils
from keras.optimizers import SGD, RMSprop, Adadelta, Adagrad, Adam
from keras.layers import containers, normalization
#from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.recurrent import LSTM
from keras.layers.embeddings import Embedding
from keras import regularizers
from keras.constraints import maxnorm
from keras.optimizers import kl_divergence
#from sknn.mlp import Classifier, Layer
'''
echo -e "3I5F\n2p4k\n2p4m" | while read I; do curl -s "http://www.rcsb.org/pdb/rest/customReport?pdbids=${I}&customReportColumns=structureId,chainId,entityId,sequence,db_id,
db_name&service=wsdisplay&format=csv"; done >result.csv
$ echo -e "3I5F\n2p4k\n2p4m" | while read I; do curl -s "http://www.rcsb.org/pdb/rest/customReport?pdbids=${I}
&customReportColumns=structureId,chainId,entityId,sequence,db_id,db_name&service=wsdisplay&format=text"
| xsltproc stylesheet.xsl - ; done | fold -w 80
def deep_learning_classifier(X_train, y_train):
nn = Classifier(
layers=[
Layer("Rectifier", units=100),
Layer("Linear")],
learning_rate=0.02,
n_iter=10)
nn.fit(X_train, y_train)
y_valid = nn.predict(X_valid)
score = nn.score(X_test, y_test)
'''
def get_uniq_pdb_protein_rna():
protein_set = set()
with open('ncRNA-protein/NegativePairs.csv', 'r') as fp:
for line in fp:
if 'Protein ID' in line:
continue
pro1, pro2 = line.rstrip().split('\t')
protein_set.add(pro1.split('-')[0])
protein_set.add(pro2.split('-')[0])
with open('ncRNA-protein/PositivePairs.csv', 'r') as fp:
for line in fp:
if 'Protein ID' in line:
continue
pro1, pro2 = line.rstrip().split('\t')
protein_set.add(pro1.split('-')[0])
protein_set.add(pro2.split('-')[0])
return protein_set
def download_seq_from_PDB(protein_set, outfile_name):
fw = open(outfile_name, 'w')
for val in protein_set:
cli_str = 'curl -s "http://www.rcsb.org/pdb/rest/customReport?pdbids='+ val +'&customReportColumns=structureId,chainId,sequence&service=wsdisplay&format=csv" >ncRNA-protein/tmpseq.csv'
cli_fp = os.popen(cli_str, 'r')
cli_fp.close()
#pdb.set_trace()
f_in = open('ncRNA-protein/tmpseq.csv', 'r')
for line in f_in:
values = line.rstrip().split('<br />')
for val in values:
if 'structureId' in val:
continue
if len(val) ==0:
continue
pdbid, chainid, seq = val.split(',')
fasa_name = pdbid[1:-1] + '-' + chainid[1:-1]
fw.write('>' + fasa_name + '\n')
fw.write(seq[1:-1] + '\n')
#pdb.set_trace()
f_in.close()
fw.close()
def get_all_PDB_id():
protein_set = get_uniq_pdb_protein_rna()
download_seq_from_PDB(protein_set, 'ncRNA-protein/all_seq.fa')
def get_protein_rna_id(inputfile):
protein_set = set()
with open(inputfile, 'r') as fp:
for line in fp:
if line[0] == '#':
continue
else:
protein, rna = line.rstrip('\r\n').split()
protein_set.add(protein.split('_')[0])
protein_set.add(rna.split('_')[0])
return protein_set
def judge_RNA_protein(seq):
if 'U' in seq:
return 'RNA'
if all([c in 'AUGCTIN' for c in seq]):
return 'RNA'
else:
return 'PROTEIN'
def generate_negative_samples_RPI2241_RPI369(seq_fasta, interaction_file, whole_file):
seq_dict = read_fasta_file(seq_fasta)
name_strand = {}
type_dict = {}
for key, tmpseq in seq_dict.iteritems():
val, strand = key.split('-')
seqtype = judge_RNA_protein(tmpseq)
name_strand.setdefault(val, []).append(key)
type_dict[key] = seqtype
fw = open(whole_file, 'w')
existing_postive = set()
with open(interaction_file, 'r') as fp:
for line in fp:
if line[0] == '#':
continue
pro1, pro2 = line.rstrip().split('\t')
pro1 = pro1.replace('_', '-').upper()
pro2 = pro2.replace('_', '-').upper()
if type_dict[pro1] == 'RNA' and type_dict[pro2] == 'PROTEIN':
existing_postive.add((pro2, pro1))
fw.write(pro2 + '\t' + pro1 + '\t' + '1' + '\n')
else:
existing_postive.add((pro1, pro2))
fw.write(pro1 + '\t' + pro2 + '\t' + '1' + '\n')
#generate negative samples
all_pairs = list(existing_postive)
num_posi = len(all_pairs)
nega_list = []
for val in all_pairs:
pro1, pro2 = val
for i in range(50):
pro2_pare = pro2.split('-')[0]
if name_strand.has_key(pro2_pare):
for val in name_strand[pro2_pare]:
if type_dict[val] == 'PROTEIN' and val != pro1:
new_sele = (val, pro2)
if new_sele not in existing_postive:
#fw.write(val + '\t' + pro2 + '\t' + '0' + '\n')
nega_list.append(new_sele)
existing_postive.add(new_sele)
random.shuffle(nega_list)
for val in nega_list[:num_posi]:
fw.write(val[0] + '\t' + val[1] + '\t' + '0' + '\n')
fw.close()
#protein_set.add(pro1.replace('_', '-').upper())
#RNA_set.add(pro2.replace('_', '-').upper())
def get_RNA_protein_RPI2241_RPI369(seq_fasta, interaction_file, protein_file, rna_file):
seq_dict = read_fasta_file(seq_fasta)
RNA_set = set()
protein_set = set()
with open(interaction_file, 'r') as fp:
for line in fp:
if line[0] == '#':
continue
pro1, pro2, label = line.rstrip().split('\t')
#protein_set.add(pro1.replace('_', '-').upper())
#RNA_set.add(pro2.replace('_', '-').upper())
protein_set.add(pro1)
RNA_set.add(pro2)
fw_pro = open(protein_file, 'w')
for val in protein_set:
if seq_dict.has_key(val):
fw_pro.write('>' + val + '\n')
fw_pro.write(seq_dict[val] + '\n')
else:
print val
fw_pro.close()
fw_pro = open(rna_file, 'w')
for val in RNA_set:
if seq_dict.has_key(val):
fw_pro.write('>' + val + '\n')
fw_pro.write(seq_dict[val].replace('N', '') + '\n')
else:
print val
fw_pro.close()
def get_RPI2241_RPI369_seq():
print 'downloading seqs'
protein_set = get_protein_rna_id('ncRNA-protein/RPI2241.txt')
download_seq_from_PDB(protein_set, 'ncRNA-protein/RPI2241.fa')
protein_set = get_protein_rna_id('ncRNA-protein/RPI369.txt')
download_seq_from_PDB(protein_set, 'ncRNA-protein/RPI369.fa')
def get_RPI2241_RPI369_ind_file():
get_RNA_protein_RPI2241_RPI369('ncRNA-protein/RPI2241.fa', 'ncRNA-protein/RPI2241_all.txt', 'ncRNA-protein/RPI2241_protein.fa', 'ncRNA-protein/RPI2241_rna.fa')
get_RNA_protein_RPI2241_RPI369('ncRNA-protein/RPI369.fa', 'ncRNA-protein/RPI369_all.txt', 'ncRNA-protein/RPI369_protein.fa', 'ncRNA-protein/RPI369_rna.fa')
def read_fasta_file(fasta_file):
seq_dict = {}
fp = open(fasta_file, 'r')
name = ''
#pdb.set_trace()
for line in fp:
#let's discard the newline at the end (if any)
line = line.rstrip()
#distinguish header from sequence
if line[0]=='>': #or line.startswith('>')
#it is the header
name = line[1:].upper() #discarding the initial >
seq_dict[name] = ''
else:
#it is sequence
seq_dict[name] = seq_dict[name] + line
fp.close()
return seq_dict
def get_RNA_protein():
seq_dict = read_fasta_file('ncRNA-protein/all_seq.fa')
RNA_set = set()
protein_set = set()
with open('ncRNA-protein/NegativePairs.csv', 'r') as fp:
for line in fp:
if 'Protein ID' in line:
continue
pro1, pro2 = line.rstrip().split('\t')
protein_set.add(pro1)
RNA_set.add(pro2)
with open('ncRNA-protein/PositivePairs.csv', 'r') as fp:
for line in fp:
if 'Protein ID' in line:
continue
pro1, pro2 = line.rstrip().split('\t')
protein_set.add(pro1)
RNA_set.add(pro2)
fw_pro = open('ncRNA-protein/protein_seq.fa', 'w')
for val in protein_set:
if seq_dict.has_key(val):
fw_pro.write('>' + val + '\n')
fw_pro.write(seq_dict[val] + '\n')
else:
print val
fw_pro.close()
fw_pro = open('ncRNA-protein/RNA_seq.fa', 'w')
for val in RNA_set:
if seq_dict.has_key(val):
fw_pro.write('>' + val + '\n')
fw_pro.write(seq_dict[val] + '\n')
else:
print val
fw_pro.close()
def read_name_from_fasta(fasta_file):
name_list = []
fp = open(fasta_file, 'r')
for line in fp:
if line[0] == '>':
name = line.rstrip('\r\n')[1:]
name_list.append(name.upper())
fp.close()
return name_list
def get_noncode_seq():
ncRNA_seq_dict = {}
head = True
name = ''
#pdb.set_trace()
with open('ncRNA-protein/ncrna_NONCODE[v3.0].fasta', 'r') as fp:
for line in fp:
if head:
head =False
continue
line = line.rstrip()
if line == 'sequence':
continue
if line[0] == '>':
name1 = line.split('|')
name = name1[0][1:].strip()
ncRNA_seq_dict[name] = ''
else:
#it is sequence
ncRNA_seq_dict[name] = ncRNA_seq_dict[name] + line
return ncRNA_seq_dict
def get_npinter_protein_seq():
pro_dict = {}
target_dir = 'ncRNA-protein/uniprot_seq/'
files = os.listdir(target_dir)
for file_name in files:
protein_name = file_name.split('.')[0]
with open(target_dir + file_name, 'r') as fp:
for line in fp:
line = line.rstrip()
if line[0] == '>':
pro_dict[protein_name] = ''
else:
pro_dict[protein_name] = pro_dict[protein_name] + line
return pro_dict
def read_RNA_pseaac_fea(name_list, pseaac_file='ncRNA-protein/RNA_pse.csv'):
print pseaac_file
data = {}
fp = open(pseaac_file, 'r')
index = 0
for line in fp:
values = line.rstrip('\r\n').split(',')
data[name_list[index]] = [float(val) for val in values]
index = index + 1
fp.close()
return data
def read_RNA_graph_feature(name_list, graph_file='ncRNA-protein/RNA_seq.gz.feature', fea_imp = None):
print graph_file
data = {}
fea_len = 32768
fp = open(graph_file, 'r')
index = 0
for line in fp:
tmp_data = [0] * fea_len
values = line.split()
for value in values:
val = value.split(':')
tmp_data[int(val[0])] = float(val[1])
if fea_imp is None:
data[name_list[index]] = tmp_data
else:
data[name_list[index]] = [tmp_data[val] for val in fea_imp]
index = index + 1
fp.close()
return data
def read_protein_feature(protein_fea_file = 'ncRNA-protein/trainingSetFeatures.csv'):
print protein_fea_file
feature_dict = {}
df = pd.read_csv(protein_fea_file)
X = df.values.copy()
for val in X:
feature_dict[val[0].upper()] = val[2:].tolist()
#pdb.set_trace()
return feature_dict
def read_lncRNA_protein_feature(protein_fea_file = 'ncRNA-protein/trainingSetFeatures.csv'):
print protein_fea_file
feature_dict = {}
df = pd.read_csv(protein_fea_file)
X = df.values.copy()
for val in X:
feature_dict[val[0]] = val[2:].tolist()
#pdb.set_trace()
return feature_dict
def get_4_trids():
nucle_com = []
chars = ['A', 'C', 'G', 'U']
base=len(chars)
end=len(chars)**4
for i in range(0,end):
n=i
ch0=chars[n%base]
n=n/base
ch1=chars[n%base]
n=n/base
ch2=chars[n%base]
n=n/base
ch3=chars[n%base]
nucle_com.append(ch0 + ch1 + ch2 + ch3)
return nucle_com
def get_3_trids():
nucle_com = []
chars = ['A', 'C', 'G', 'U']
base=len(chars)
end=len(chars)**3
for i in range(0,end):
n=i
ch0=chars[n%base]
n=n/base
ch1=chars[n%base]
n=n/base
ch2=chars[n%base]
nucle_com.append(ch0 + ch1 + ch2)
return nucle_com
def get_4_nucleotide_composition(tris, seq, pythoncount = True):
seq_len = len(seq)
tri_feature = []
if pythoncount:
for val in tris:
num = seq.count(val)
tri_feature.append(float(num)/seq_len)
else:
k = len(tris[0])
tmp_fea = [0] * len(tris)
for x in range(len(seq) + 1- k):
kmer = seq[x:x+k]
if kmer in tris:
ind = tris.index(kmer)
tmp_fea[ind] = tmp_fea[ind] + 1
tri_feature = [float(val)/seq_len for val in tmp_fea]
#pdb.set_trace()
return tri_feature
def TransDict_from_list(groups):
transDict = dict()
tar_list = ['0', '1', '2', '3', '4', '5', '6']
result = {}
index = 0
for group in groups:
g_members = sorted(group) #Alphabetically sorted list
for c in g_members:
# print('c' + str(c))
# print('g_members[0]' + str(g_members[0]))
result[c] = str(tar_list[index]) #K:V map, use group's first letter as represent.
index = index + 1
return result
def translate_sequence (seq, TranslationDict):
'''
Given (seq) - a string/sequence to translate,
Translates into a reduced alphabet, using a translation dict provided
by the TransDict_from_list() method.
Returns the string/sequence in the new, reduced alphabet.
Remember - in Python string are immutable..
'''
import string
from_list = []
to_list = []
for k,v in TranslationDict.items():
from_list.append(k)
to_list.append(v)
# TRANS_seq = seq.translate(str.maketrans(zip(from_list,to_list)))
TRANS_seq = seq.translate(string.maketrans(str(from_list), str(to_list)))
#TRANS_seq = maketrans( TranslationDict, seq)
return TRANS_seq
def get_protein_trids(seq, group_dict):
#protein='MQNEEDACLEAGYCLGTTLSSWRLHFMEEQSQSTMLMGIGIGALLTLAFVGIFFFVYRR'
tran_seq = translate_sequence (seq, group_dict)
#pdb.set_trace()
return tran_seq
def get_3_protein_trids():
nucle_com = []
chars = ['0', '1', '2', '3', '4', '5', '6']
base=len(chars)
end=len(chars)**3
for i in range(0,end):
n=i
ch0=chars[n%base]
n=n/base
ch1=chars[n%base]
n=n/base
ch2=chars[n%base]
nucle_com.append(ch0 + ch1 + ch2)
return nucle_com
def get_4_protein_trids():
nucle_com = []
chars = ['0', '1', '2', '3', '4', '5', '6']
base=len(chars)
end=len(chars)**4
for i in range(0,end):
n=i
ch0=chars[n%base]
n=n/base
ch1=chars[n%base]
n=n/base
ch2=chars[n%base]
n=n/base
ch3=chars[n%base]
nucle_com.append(ch0 + ch1 + ch2 + ch3)
return nucle_com
def get_NPinter_interaction():
RNA_set = set()
protein_set = set()
with open('ncRNA-protein/NPInter10412_dataset.txt', 'r') as fp:
head = True
for line in fp:
if head:
head = False
continue
pro1, pro1_len, pro2, pro2_len, org = line.rstrip().split('\t')
protein_set.add(pro2)
RNA_set.add(pro1)
pro_dict = get_npinter_protein_seq()
fw_pro = open('ncRNA-protein/NPinter_protein_seq.fa', 'w')
for val in protein_set:
if pro_dict.has_key(val):
fw_pro.write('>' + val + '\n')
fw_pro.write(pro_dict[val] + '\n')
else:
print val
fw_pro.close()
ncRNA_dict = get_noncode_seq()
fw_pro = open('ncRNA-protein/NPinter_RNA_seq.fa', 'w')
for val in RNA_set:
if ncRNA_dict.has_key(val):
fw_pro.write('>' + val + '\n')
seq = ncRNA_dict[val].replace('T', 'U')
#seq = seq.replace('N', '')get_RPI2241_RPI369_seq()
fw_pro.write( seq + '\n')
else:
print val
fw_pro.close()
def get_own_lncRNA_protein(datafile = 'ncRNA-protein/lncRNA-protein-488.txt'):
protein_seq = {}
RNA_seq = {}
interaction_pair = {}
with open(datafile, 'r') as fp:
for line in fp:
if line[0] == '>':
values = line[1:].strip().split('|')
label = values[1]
name = values[0].split('_')
protein = name[0] + '-' + name[1]
RNA = name[0] + '-' + name[2]
if label == 'interactive':
interaction_pair[(protein, RNA)] = 1
else:
interaction_pair[(protein, RNA)] = 0
index = 0
else:
seq = line[:-1]
if index == 0:
protein_seq[protein] = seq
else:
RNA_seq[RNA] = seq
index = index + 1
#pdb.set_trace()
fw = open('ncRNA-protein/lncRNA_protein.fa', 'w')
for key, val in protein_seq.iteritems():
fw.write('>' + key + '\n')
fw.write(val + '\n')
fw.close()
fw = open('ncRNA-protein/lncRNA_RNA.fa', 'w')
for key, val in RNA_seq.iteritems():
fw.write('>' + key + '\n')
fw.write(val.replace('N', '') + '\n')
fw.close()
'''
cli_str = "python ProFET/ProFET/feat_extract/pipeline.py --trainingSetDir 'ncRNA-protein/lncRNA-protein/' \
--trainFeatures True --resultsDir 'ncRNA-protein/lncRNA-protein/' --classType file"
fcli = os.popen(cli_str, 'r')
fcli.close()
'''
def read_name_from_lncRNA_fasta(fasta_file):
name_list = []
fp = open(fasta_file, 'r')
for line in fp:
if line[0] == '>':
name = line.rstrip('\r\n')[1:]
name_list.append(name)
fp.close()
return name_list
def prepare_complex_feature(rna_file, protein_file, seperate = False):
RNA_seq_dict = read_fasta_file(rna_file)
protein_seq_dict = read_fasta_file(protein_file)
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
tris = get_4_trids()
pairs = []
train = []
label = []
for RNA, RNA_seq in RNA_seq_dict.iteritems():
RNA_seq = RNA_seq.replace('T', 'U')
for protein, protein_seq in protein_seq_dict.iteritems():
pairs.append((RNA, protein))
protein_seq1 = translate_sequence (protein_seq, group_dict)
RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount =False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq1, pythoncount =False)
if seperate:
tmp_fea = (protein_tri_fea, RNA_tri_fea)
else:
tmp_fea = protein_tri_fea + RNA_tri_fea
train.append(tmp_fea)
return np.array(train), pairs
def prepare_RPI488_feature(extract_only_posi = False,
pseaac_file = None, deepmind = False, seperate = False, chem_fea = True):
print 'RPI488 dataset'
interaction_pair = {}
RNA_seq_dict = {}
protein_seq_dict = {}
with open('ncRNA-protein/lncRNA-protein-488.txt', 'r') as fp:
for line in fp:
if line[0] == '>':
values = line[1:].strip().split('|')
label = values[1]
name = values[0].split('_')
protein = name[0] + '-' + name[1]
RNA = name[0] + '-' + name[2]
if label == 'interactive':
interaction_pair[(protein, RNA)] = 1
else:
interaction_pair[(protein, RNA)] = 0
index = 0
else:
seq = line[:-1]
if index == 0:
protein_seq_dict[protein] = seq
else:
RNA_seq_dict[RNA] = seq
index = index + 1
#name_list = read_name_from_lncRNA_fasta('ncRNA-protein/lncRNA_RNA.fa')
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
tris = get_4_trids()
#tris3 = get_3_trids()
train = []
label = []
chem_fea = []
for key, val in interaction_pair.iteritems():
protein, RNA = key[0], key[1]
#pdb.set_trace()
if RNA_seq_dict.has_key(RNA) and protein_seq_dict.has_key(protein): #and protein_fea_dict.has_key(protein) and RNA_fea_dict.has_key(RNA):
label.append(val)
RNA_seq = RNA_seq_dict[RNA]
protein_seq = translate_sequence (protein_seq_dict[protein], group_dict)
if deepmind:
RNA_tri_fea = get_RNA_seq_concolutional_array(RNA_seq)
protein_tri_fea = get_RNA_seq_concolutional_array(protein_seq)
train.append((RNA_tri_fea, protein_tri_fea))
else:
#pdb.set_trace()
RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount =False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
#RNA_tri3_fea = get_4_nucleotide_composition(tris3, RNA_seq, pythoncount =False)
#RNA_fea = [RNA_fea_dict[RNA][ind] for ind in fea_imp]
#tmp_fea = protein_fea_dict[protein] + tri_fea #+ RNA_fea_dict[RNA]
if seperate:
tmp_fea = (protein_tri_fea, RNA_tri_fea)
#chem_tmp_fea = (protein_fea_dict[protein], RNA_fea_dict[RNA])
else:
tmp_fea = protein_tri_fea + RNA_tri_fea
#chem_tmp_fea = protein_fea_dict[protein] + RNA_fea_dict[RNA]
train.append(tmp_fea)
#chem_fea.append(chem_tmp_fea)
else:
print RNA, protein
return np.array(train), label
def prepare_RPIntDB_feature(extract_only_posi = False,
pseaac_file = None, deepmind = False, seperate = False, chem_fea = True):
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
tris = get_4_trids()
#tris3 = get_3_trids()
train = []
label = []
chem_fea = []
head = True
with open('ncRNA-protein/RPIntDB_interactions_new.txt', 'r') as fp:
for line in fp:
if head:
head = False
continue
values = line.rstrip('\r\n').split('\t')
protein = values[0]
protein_seq = values[1]
RNA = values[2]
RNA_seq = values[3]
label.append(1)
RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount =False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
if seperate:
tmp_fea = (protein_tri_fea, RNA_tri_fea)
#chem_tmp_fea = (protein_fea_dict[protein], RNA_fea_dict[RNA])
else:
tmp_fea = protein_tri_fea + RNA_tri_fea
#chem_tmp_fea = protein_fea_dict[protein] + RNA_fea_dict[RNA]
train.append(tmp_fea)
return np.array(train), label
def prepare_RPI2241_369_feature(rna_fasta_file, data_file, protein_fasta_file, extract_only_posi = False,
graph = False, deepmind = False, seperate = False, chem_fea = True):
seq_dict = read_fasta_file(rna_fasta_file)
protein_seq_dict = read_fasta_file(protein_fasta_file)
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
tris = get_4_trids()
train = []
label = []
chem_fea = []
#posi_set = set()
#pro_set = set()
with open(data_file, 'r') as fp:
for line in fp:
if line[0] == '#':
continue
protein, RNA, tmplabel = line.rstrip('\r\n').split('\t')
if seq_dict.has_key(RNA) and protein_seq_dict.has_key(protein):
label.append(int(tmplabel))
RNA_seq = seq_dict[RNA]
protein_seq = translate_sequence (protein_seq_dict[protein], group_dict)
if deepmind:
RNA_tri_fea = get_RNA_seq_concolutional_array(RNA_seq)
protein_tri_fea = get_RNA_seq_concolutional_array(protein_seq)
train.append((RNA_tri_fea, protein_tri_fea))
else:
RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount =False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
if seperate:
tmp_fea = (protein_tri_fea, RNA_tri_fea)
else:
tmp_fea = protein_tri_fea + RNA_tri_fea
train.append(tmp_fea)
else:
print RNA, protein
return np.array(train), label
def get_npinter_interaction(uniq_nid_dict = {}):
pair_interaction = set()
org_list = []
pair_list = []
with open('ncRNA-protein/NPInter10412_dataset.txt', 'r') as fp:
head = True
for line in fp:
if head:
head = False
continue
RNA, RNA_len, protein, protein_len, org = line.rstrip().split('\t')
org_list.append(org)
pair_list.append((RNA, protein))
if uniq_nid_dict.has_key(RNA):
uniq_name = uniq_nid_dict[RNA]
pair_interaction.add((uniq_name, protein))
return pair_list, org_list
def get_RPI367_interaction():
org_list = []
with open('ncRNA-protein/RPI367.txt', 'r') as fp:
head = True
for line in fp:
if head:
head = False
continue
values = line.rstrip().split('\t')
org_list.append(values[2])
return org_list
def prepare_RPI367_feature(extract_only_posi = True, graph = False, deepmind = False, seperate = False, chem_fea = False):
print 'RPI367 data'
seq_dict = {}
uniq_nid_dict = {}
fzip = open('ncRNA-protein/ncrna_NONCODE[v1.0].fasta', 'r')
for line in fzip:
if line[0] == '#':
continue
if line[0] == '>':
values = line.rstrip('\r\n').split(',')
name = values[1]
uniq_nid_dict[name] = values[0][1:]
else:
seq_dict[name] = line[:-1].replace('T', 'U')
fzip.close()
protein_seq_dict = get_npinter_protein_seq()
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
#pdb.set_trace()
train = []
label = []
posi_set = set()
pro_set = set()
tris = get_4_trids()
with open('ncRNA-protein/RPI367.txt', 'r') as fp:
head = True
for line in fp:
if head:
head = False
continue
#RNA, RNA_len, protein, protein_len, org = line.rstrip().split('\t')
#RNA = RNA.upper()
values = line.rstrip('\r\n').split('\t')
protein = values[-1]
RNA = values[-2]
protein = protein.upper()
posi_set.add((RNA, protein))
pro_set.add(protein)
if seq_dict.has_key(RNA) and protein_seq_dict.has_key(protein):
label.append(1)
RNA_seq = seq_dict[RNA]
protein_seq = translate_sequence (protein_seq_dict[protein], group_dict)
if deepmind:
RNA_tri_fea = get_RNA_seq_concolutional_array(RNA_seq)
protein_tri_fea = get_RNA_seq_concolutional_array(protein_seq)
train.append((RNA_tri_fea, protein_tri_fea))
else:
RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount =False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
if seperate:
tmp_fea = (protein_tri_fea, RNA_tri_fea)
else:
tmp_fea = protein_tri_fea + RNA_tri_fea
train.append(tmp_fea)
else:
print RNA, protein
return np.array(train), label
def read_orf_seq(fasta_file, RNA = False):
protein_seq_dict = {}
with open(fasta_file, 'r') as fp:
for line in fp:
line = line.rstrip()
if line[0] == '>':
name1 = line.split()
name = name1[0][1:].strip()
protein_seq_dict[name] = ''
else:
if RNA:
line = line.replace('T', 'U')
protein_seq_dict[name] = protein_seq_dict[name] + line
return protein_seq_dict
def read_orf_interaction(interaction_file):
interacton_pair = []
with open(interaction_file, 'r') as fp:
head = True
for line in fp:
if head:
head = False
continue
values = line.rstrip().split()
protein, RNA = values[0].split('_')
interacton_pair.append((protein, RNA))
return interacton_pair
def prepare_RPI13254_feature(deepmind = False, seperate = False, extract_only_posi = False, indep_test = False):
protein_seq_dict = read_orf_seq('ncRNA-protein/RPI13254_RNA_seq.fa')
RNA_seq_dict = read_orf_seq('ncRNA-protein/RPI13254_protein_seq.fa', RNA = True)
positive_pairs = read_orf_interaction('ncRNA-protein/RPI13254_positive.txt')
negative_pairs = read_orf_interaction('ncRNA-protein/RPI13254_negative.txt')
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
tris = get_4_trids()
train = []
label = []
if not indep_test:
random.shuffle(positive_pairs)
nega_num =len(negative_pairs)
positive_pairs = positive_pairs[:nega_num]
#pdb.set_trace()
for val in positive_pairs:
protein, RNA = val
if RNA_seq_dict.has_key(RNA) and protein_seq_dict.has_key(protein):
label.append(1)
#RNA_fea = [RNA_fea_dict[RNA][ind] for ind in fea_imp]
RNA_seq = RNA_seq_dict[RNA]
protein_seq = translate_sequence (protein_seq_dict[protein], group_dict)
if deepmind:
RNA_tri_fea = get_RNA_seq_concolutional_array(RNA_seq)
protein_tri_fea = get_RNA_seq_concolutional_array(protein_seq)
train.append((RNA_tri_fea, protein_tri_fea))
else:
RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount =False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
if seperate:
tmp_fea = (protein_tri_fea, RNA_tri_fea)
else:
tmp_fea = protein_tri_fea + RNA_tri_fea
train.append(tmp_fea)
else:
print RNA, protein
if not extract_only_posi:
for val in negative_pairs:
protein, RNA = val
if RNA_seq_dict.has_key(RNA) and protein_seq_dict.has_key(protein):
label.append(0)
#RNA_fea = [RNA_fea_dict[RNA][ind] for ind in fea_imp]
RNA_seq = RNA_seq_dict[RNA]
protein_seq = translate_sequence (protein_seq_dict[protein], group_dict)
if deepmind:
RNA_tri_fea = get_RNA_seq_concolutional_array(RNA_seq)
protein_tri_fea = get_RNA_seq_concolutional_array(protein_seq)
train.append((RNA_tri_fea, protein_tri_fea))
else:
RNA_tri_fea = get_4_nucleotide_composition(tris, RNA_seq, pythoncount =False)
protein_tri_fea = get_4_nucleotide_composition(protein_tris, protein_seq, pythoncount =False)
if seperate:
tmp_fea = (protein_tri_fea, RNA_tri_fea)
else:
tmp_fea = protein_tri_fea + RNA_tri_fea
train.append(tmp_fea)
else:
print RNA, protein
return np.array(train), label
def prepare_NPinter_feature(extract_only_posi = False, graph = False, deepmind = False, seperate = False, chem_fea = True):
print 'NPinter data'
name_list = read_name_from_fasta('ncRNA-protein/NPinter_RNA_seq.fa')
seq_dict = read_fasta_file('ncRNA-protein/NPinter_RNA_seq.fa')
protein_seq_dict = read_fasta_file('ncRNA-protein/NPinter_protein_seq.fa')
groups = ['AGV', 'ILFP', 'YMTS', 'HNQW', 'RK', 'DE', 'C']
group_dict = TransDict_from_list(groups)
protein_tris = get_3_protein_trids()
#pdb.set_trace()
train = []
label = []
chem_fea = []
posi_set = set()
pro_set = set()
tris = get_4_trids()
with open('ncRNA-protein/NPInter10412_dataset.txt', 'r') as fp:
head = True
for line in fp:
if head:
head = False
continue
RNA, RNA_len, protein, protein_len, org = line.rstrip().split('\t')
RNA = RNA.upper()
protein = protein.upper()
posi_set.add((RNA, protein))
pro_set.add(protein)
if seq_dict.has_key(RNA) and protein_seq_dict.has_key(protein):
label.append(1)
#RNA_fea = [RNA_fea_dict[RNA][ind] for ind in fea_imp]
RNA_seq = seq_dict[RNA]
protein_seq = translate_sequence (protein_seq_dict[protein], group_dict)
if deepmind:
RNA_tri_fea = get_RNA_seq_concolutional_array(RNA_seq)
protein_tri_fea = get_RNA_seq_concolutional_array(protein_seq)
train.append((RNA_tri_fea, protein_tri_fea))