-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict_genes_to_GO_process_old.py
726 lines (582 loc) · 26.9 KB
/
predict_genes_to_GO_process_old.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
'''
Consider biological process X
Training example: gene
Label (binary): 1 if gene is associated with X in GO,
0 otherwise. Can obtain negative examples by randomly sampling
the genes that are not known to be associated with X
# TODO: cross-validation, AUC score, clean up pipeline, add in tissue
selection to the pipeline
# TODO: use only 53 tissues
'''
import numpy as np
import random
from sklearn import linear_model
from goatools.associations import read_ncbi_gene2go
from goatools.base import download_ncbi_associations
import math
from sklearn.metrics import roc_auc_score
from sklearn.metrics import mean_squared_error
from sklearn.svm import SVC
#from goatools.base import download
#obo_fname = download_go_basic_obo()
def rand_sample_exclude(list, num_samples, exclude=None):
'''
Generates a list of unique randomly sampled values from |list|
:param list: The list to sample from
:param num_samples: Number of samples to take
:param exclude: List of samples that should not be taken
:return: The list of |num_samples| samples
'''
samples = random.sample(list, num_samples)
if exclude:
# Check list for any values that should be excluded
for sample in samples:
if sample in exclude:
samples.remove(sample)
while len(samples) < num_samples:
sample = random.choice(list)
if sample not in samples and sample not in exclude:
samples.append(sample)
return samples
def map_entrez_to_ensembl(path):
dict = {}
file = open(path)
for line in file:
vals = line.split('\t')
ens_gene_id = vals[0]
entrez_id = vals[2]
dict[entrez_id] = ens_gene_id
file.close()
return dict
def get_ensembl_ids(go_process_id, gene2go_fpath, biomart_fpath, ev_codes=None):
entrez_to_ensembl = map_entrez_to_ensembl(biomart_fpath)
# taxids=[9606] means select only human.
go_to_entrez_ids_human = read_ncbi_gene2go(gene2go_fpath, taxids=[9606], go2geneids=True)
print("{N} GO terms associated with human NCBI Entrez GeneIDs".format(N=len(go_to_entrez_ids_human)))
entrez_ids = go_to_entrez_ids_human[GO_PROCESS_ID]
print '# of Entrez IDs associated with ', GO_PROCESS_ID, ' = ', len(entrez_ids)
ensembl_ids = []
for ent_id in entrez_ids:
if str(ent_id) in entrez_to_ensembl:
ensembl_ids.append(entrez_to_ensembl[str(ent_id)])
print '# of Ensembl IDs associated with ', GO_PROCESS_ID, ' = ', len(ensembl_ids)
return ensembl_ids
def refine_expression(exp_levels, tissue_index_map):
new_levels = []
tissue_names = []
for tissue in sorted(tissue_index_map):
tissue_names.append(tissue)
# take average
tissue_exp_levels = [exp_levels[i] for i in tissue_index_map[tissue]]
new_levels.append(np.mean(tissue_exp_levels))
if np.isnan(np.mean(tissue_exp_levels)):
print 'invalid_mean in:', tissue
print tissue_index_map[tissue]
exit(1)
return tissue_names, new_levels
def get_positive_examples(rpkm_path, sample_tissue_path, ens_ids):
gene_features = np.empty((0, NUM_FEATURES))
positive_example_rows = []
gene_ids_ordered = []
i = 0
# TODO move elsewhere
# load sample to tissue map
sample_tissue_map = {}
sample_tissue_file = open(sample_tissue_path)
firstLine = True
for line in sample_tissue_file:
if firstLine:
firstLine = False
continue
terms = line.split('\t')
tissue = terms[1].split('\n')[0]
sample_tissue_map[terms[0]] = tissue
sample_tissue_file.close()
tissue_index_map = {}
rpkm_file = open(rpkm_file_path)
firstLine = True
for line in rpkm_file:
if firstLine:
# TODO: move elsewhere
# get sample ids
sampleIDs = line.rstrip().split('\t')[4:]
# create mapping
for i in range(len(sampleIDs)):
sampleID = sampleIDs[i]
if sampleID not in sample_tissue_map:
print sampleID, 'not found'
exit(1)
else:
tissue = sample_tissue_map[sampleID];
if tissue not in tissue_index_map:
tissue_index_map[tissue] = []
else:
tissue_index_map[tissue].append(i)
# print tissue_index_map
firstLine = False
continue
tab1_index = line.find('\t')
tab2_index = line.find('\t', tab1_index+1)
cur_ens_id = line[tab1_index+1:tab2_index]
# Remove decimal from the ensembl ID
if '.' in cur_ens_id:
cur_ens_id = cur_ens_id[0:cur_ens_id.index('.')]
if cur_ens_id in ens_ids:
# This is IF condition prevents using the same gene for multiple
# features. TODO: better method for accounting for multiple transcripts
# mapping to same gene.
if cur_ens_id not in gene_ids_ordered:
positive_example_rows.append(i)
gene_ids_ordered.append(cur_ens_id)
exp_levels_str = line.rstrip().split('\t')[4:]
exp_levels = [float(exp_level) for exp_level in exp_levels_str]
# TODO Jason: compute avg of exp_levels by tissue
tissue_names, exp_levels = refine_expression(exp_levels, tissue_index_map)
# print len(exp_levels)
# function that takes map from columns to tissues or whatever and return vector of averages for each tissue
gene_features = np.append(gene_features, [exp_levels], axis=0)
i += 1
rpkm_file.close()
return gene_features, positive_example_rows, gene_ids_ordered, i
def get_negative_examples(rpkm_path, sample_tissue_path, neg_ex_rows):
# TODO move elsewhere
# load sample to tissue map
sample_tissue_map = {}
sample_tissue_file = open(sample_tissue_path)
firstLine = True
for line in sample_tissue_file:
if firstLine:
firstLine = False
continue
terms = line.split('\t')
tissue = terms[1].split('\n')[0]
sample_tissue_map[terms[0]] = tissue
sample_tissue_file.close()
tissue_index_map = {}
gene_features_neg = np.empty((0, NUM_FEATURES))
gene_ids_ordered_neg = []
rpkm_file = open(rpkm_file_path)
i = 0
firstLine = True
for line in rpkm_file:
if firstLine:
# TODO: move elsewhere
# get sample ids
sampleIDs = line.rstrip().split('\t')[4:]
# create mapping
for i in range(len(sampleIDs)):
sampleID = sampleIDs[i]
if sampleID not in sample_tissue_map:
print sampleID, 'not found'
exit(1)
else:
tissue = sample_tissue_map[sampleID];
if tissue not in tissue_index_map:
tissue_index_map[tissue] = []
else:
tissue_index_map[tissue].append(i)
# print tissue_index_map
firstLine = False
continue
if i in neg_ex_rows:
vals = line.rstrip().split('\t')
cur_ens_id = vals[1]
# Remove decimal from the ensembl ID
if '.' in cur_ens_id:
cur_ens_id = cur_ens_id[0:cur_ens_id.index('.')]
gene_ids_ordered_neg.append(cur_ens_id)
exp_levels_str = vals[4:]
exp_levels = [float(exp_level) for exp_level in exp_levels_str]
# TODO Jason: compute avg of exp_levels by tissue
tissue_names, exp_levels = refine_expression(exp_levels, tissue_index_map)
# print len(exp_levels)
gene_features_neg = np.append(gene_features_neg, [exp_levels], axis=0)
i += 1
rpkm_file.close()
return gene_features_neg, gene_ids_ordered_neg
def print_prediction_results(model, labels, predictions, other_info=None):
print 20*'-'
print model
print 20*'-'
print 'Root Mean Square Error: ', math.sqrt(mean_squared_error(labels, predictions))
print 'ROC AUC Score: ', roc_auc_score(labels, predictions)
false_positives = 0
false_negatives = 0
for label, pred in zip(labels, predictions):
if label == 0 and pred == 1:
false_positives += 1
elif label == 1 and pred == 0:
false_negatives += 1
print 'False positive rate: ', 1.0 * false_positives / len(labels)
print 'False negative rate: ', 1.0 * false_negatives / len(labels)
if other_info:
print other_info
def get_go_terms(biomart_fpath, gene2go_fpath, gene_count_fpath, top=1):
entrez_to_ensembl = map_entrez_to_ensembl(biomart_fpath)
# taxids=[9606] means select only human.
go_to_entrez_ids_human = read_ncbi_gene2go(gene2go_fpath, taxids=[9606], go2geneids=True)
print("{N} GO terms associated with human NCBI Entrez GeneIDs".format(N=len(go_to_entrez_ids_human)))
# Get the |top| GO terms with the most gene annotations
gene_cnt_file = open(gene_count_fpath)
top_GO_ids = []
atLine = 0
skipLines = 1
for line in gene_cnt_file:
if atLine < skipLines:
atLine += 1
continue
elif atLine > top:
break
atLine += 1
GO_id = line.split('\t')[0]
entrez_ids = go_to_entrez_ids_human[GO_id]
#print '# of Entrez IDs associated with ', GO_id, ' = ', len(entrez_ids)
ensembl_ids = []
for ent_id in entrez_ids:
if str(ent_id) in entrez_to_ensembl:
ensembl_ids.append(entrez_to_ensembl[str(ent_id)])
top_GO_ids.append((GO_id, ensembl_ids))
#print '# of Ensembl IDs associated with ', GO_id, ' = ', len(ensembl_ids)
return top_GO_ids
"""
*********************
Main
*********************
"""
if __name__ == "__main__":
# biomart_file_path = 'data/biomart_ensembl_to_entrez.txt'
gene2go_file_path = 'data/gene2go.txt' # If file doesn't exist, then run gene2go = download_ncbi_associations()
# rpkm_file_path = '../../../Downloads/GTEx_Analysis_v6_RNA-seq_Flux1.6_transcript_rpkm.txt'
gene_count_file_path = 'data/GO_term_gene_counts.txt'
biomart_file_path = 'data/biomart_ensembl_to_entrez.txt'
gene2go_file_path = '../local_data/gene2go.txt' # If file doesn't exist, then run gene2go = download_ncbi_associations()
rpkm_file_path = '../local_data/transcript_rpkm_in_go.txt'
sample_tissue_path = 'data/sampleID_tissue.txt'
GO_PROCESS_IDs = get_go_terms(biomart_file_path, gene2go_file_path, gene_count_file_path, top=10)
# GO:0007596
(GO_PROCESS_ID, ensembl_ids) = GO_PROCESS_IDs[7]
print GO_PROCESS_ID
#GO_PROCESS_ID = 'GO:0001889' # Biological Process ID in Gene Ontology
#rpkm_file_path = '../../../Documents/Stanford/CS341_Data/transcript_rpkm_top_10000_var.txt'
go_evidence_codes = ['EXP', 'IDA', 'IPI', 'IMP', 'IGI', 'IEP']
#ensembl_ids = get_ensembl_ids(GO_PROCESS_ID, biomart_file_path, ev_codes=go_evidence_codes)
# NUM_FEATURES = 8555
NUM_FEATURES = 53
# 1st Pass Through Dataset: Obtain positive training examples
gene_features, positive_example_rows, gene_ids_ordered, num_transcripts = get_positive_examples(rpkm_file_path, sample_tissue_path, ensembl_ids)
print 'After pass 1 (inserting positive examples), gene feature matrix has dimension: ', gene_features.shape
num_positive_examples = len(positive_example_rows)
num_negative_examples = num_positive_examples
num_examples = num_positive_examples + num_negative_examples
# 2nd Pass through dataset: Obtain an equal number of negative training exmaples
max_row_id = num_transcripts-1
negative_example_rows = rand_sample_exclude(range(0, max_row_id+1), num_positive_examples, exclude=positive_example_rows)
gene_features_neg, gene_ids_ordered_neg = get_negative_examples(rpkm_file_path, sample_tissue_path, negative_example_rows)
gene_features = np.append(gene_features, gene_features_neg, axis=0)
gene_ids_ordered += gene_ids_ordered_neg
print 'After pass 2 (inserting negative examples), gene feature matrix has dimension: ', gene_features.shape
# Vector of labels for each example
labels = num_positive_examples * [1] + num_negative_examples * [0]
# Split into training and test sets
# gene_features_train, labels_train, gene_features_test, labels_test =
# split_to_train_and_test(gene_features, TRAIN_SET_SIZE)
TRAIN_SET_SIZE = 0.7 # Fraction of genes used for training set
num_train_examples = int(math.ceil(TRAIN_SET_SIZE*num_examples))
train_indeces = random.sample(range(0, num_examples), num_train_examples)
gene_features_train = np.empty((0, NUM_FEATURES))
gene_features_test = np.empty((0, NUM_FEATURES))
labels_train = []
labels_test = []
gene_ids_ordered_train = []
gene_ids_ordered_test = []
num_examples = 315
print 'num ex: ', num_examples
for idx in range(0, num_examples):
if idx in train_indeces:
gene_features_train = np.append(gene_features_train, [gene_features[idx]], axis=0)
labels_train.append(labels[idx])
gene_ids_ordered_train.append(gene_ids_ordered[idx])
else:
gene_features_test = np.append(gene_features_test, [gene_features[idx]], axis=0)
labels_test.append(labels[idx])
gene_ids_ordered_test.append(gene_ids_ordered[idx])
print 'Dimensionality of training set: ', gene_features_train.shape
print 'Dimensionality of test set: ', gene_features_test.shape
'''
logreg = linear_model.LogisticRegression(C=1e5)
logreg.fit(gene_features_train, labels_train)
pred_lr = logreg.predict(gene_features_test)
print_prediction_results('Logistic Regression', labels_test, pred_lr)
'''
# Logistic Regression with Cross-Validation, L1 Norm (must use liblinear solver for L1)
#costs = []
'''
num_folds = 5 # number of folds to use for cross-validation
loss_function = 'l1' # Loss function to use. Must be either 'l1' or 'l2'
logreg_cv_L1 = linear_model.LogisticRegressionCV(cv=num_folds, penalty=loss_function, solver='liblinear')
logreg_cv_L1.fit(gene_features_train, labels_train)
pred_lr_cv_L1 = logreg_cv_L1.predict(gene_features_test)
print_prediction_results('Cross-Validated Logistic Regression', labels_test, pred_lr_cv_L1,
other_info='Norm: ' + loss_function + ', # of Folds: ' + str(num_folds))
'''
num_folds = 5 # number of folds to use for cross-validation
loss_function = 'l2' # Loss function to use. Must be either 'l1' or 'l2'
logreg_cv_L2 = linear_model.LogisticRegressionCV(cv=num_folds, penalty=loss_function)
logreg_cv_L2.fit(gene_features_train, labels_train)
pred_lr_cv_L2 = logreg_cv_L2.predict(gene_features_test)
print_prediction_results('Cross-Validated Logistic Regression', labels_test, pred_lr_cv_L2
, other_info='Norm: ' + loss_function + ', # of Folds: ' + str(num_folds))
# SVM
clf = SVC()
clf.fit(gene_features_train, labels_train)
pred_svm = clf.predict(gene_features_test)
print_prediction_results('SVM', labels_test, pred_svm)
'''
Consider biological process X
Training example: gene
Label (binary): 1 if gene is associated with X in GO,
0 otherwise. Can obtain negative examples by randomly sampling
the genes that are not known to be associated with X
# TODO: cross-validation, AUC score, clean up pipeline, add in tissue
selection to the pipeline
# TODO: use only 53 tissues
'''
import numpy as np
import random
from sklearn import linear_model
from goatools.associations import read_ncbi_gene2go
from goatools.base import download_ncbi_associations
import math
from sklearn.metrics import roc_auc_score
from sklearn.metrics import mean_squared_error
from sklearn.svm import SVC
#from goatools.base import download
#obo_fname = download_go_basic_obo()
def rand_sample_exclude(list, num_samples, exclude=None):
'''
Generates a list of unique randomly sampled values from |list|
:param list: The list to sample from
:param num_samples: Number of samples to take
:param exclude: List of samples that should not be taken
:return: The list of |num_samples| samples
'''
samples = random.sample(list, num_samples)
if exclude:
# Check list for any values that should be excluded
for sample in samples:
if sample in exclude:
samples.remove(sample)
while len(samples) < num_samples:
sample = random.choice(list)
if sample not in samples and sample not in exclude:
samples.append(sample)
return samples
def map_entrez_to_ensembl(path):
dict = {}
file = open(path)
for line in file:
vals = line.split('\t')
ens_gene_id = vals[0]
entrez_id = vals[2]
dict[entrez_id] = ens_gene_id
file.close()
return dict
def get_ensembl_ids(go_process_id, biomart_fpath, ev_codes=None):
entrez_to_ensembl = map_entrez_to_ensembl(biomart_fpath)
gene2go = 'data/gene2go.txt' # If file doesn't exist, then replace this line with gene2go = download_ncbi_associations()
# taxids=[9606] means select only human.
go_to_entrez_ids_human = read_ncbi_gene2go(gene2go, taxids=[9606], go2geneids=True)
print("{N} GO terms associated with human NCBI Entrez GeneIDs".format(N=len(go_to_entrez_ids_human)))
entrez_ids = go_to_entrez_ids_human[GO_PROCESS_ID]
print '# of Entrez IDs associated with ', GO_PROCESS_ID, ' = ', len(entrez_ids)
ensembl_ids = []
for ent_id in entrez_ids:
if str(ent_id) in entrez_to_ensembl:
ensembl_ids.append(entrez_to_ensembl[str(ent_id)])
print '# of Ensembl IDs associated with ', GO_PROCESS_ID, ' = ', len(ensembl_ids)
return ensembl_ids
def get_positive_examples(rpkm_path, ens_ids):
gene_features = np.empty((0, NUM_SAMPLES))
positive_example_rows = []
gene_ids_ordered = []
i = 0
rpkm_file = open(rpkm_file_path)
firstLine = True
for line in rpkm_file:
if firstLine:
firstLine = False
continue
tab1_index = line.find('\t')
tab2_index = line.find('\t', tab1_index+1)
cur_ens_id = line[tab1_index+1:tab2_index]
# Remove decimal from the ensembl ID
if '.' in cur_ens_id:
cur_ens_id = cur_ens_id[0:cur_ens_id.index('.')]
if cur_ens_id in ens_ids:
# This is IF condition prevents using the same gene for multiple
# features. TODO: better method for accounting for multiple transcripts
# mapping to same gene.
if cur_ens_id not in gene_ids_ordered:
positive_example_rows.append(i)
gene_ids_ordered.append(cur_ens_id)
exp_levels_str = line.rstrip().split('\t')[4:]
# TODO Jason: compute avg of exp_levels by tissue
# function that takes map from columns to tissues or whatever and return vector of averages for each tissue
#
exp_levels = [float(exp_level) for exp_level in exp_levels_str]
gene_features = np.append(gene_features, [exp_levels], axis=0)
i += 1
rpkm_file.close()
return gene_features, positive_example_rows, gene_ids_ordered, i
def get_negative_examples(rpkm_path, neg_ex_rows):
gene_features_neg = np.empty((0, NUM_SAMPLES))
gene_ids_ordered_neg = []
rpkm_file = open(rpkm_file_path)
i = 0
firstLine = True
for line in rpkm_file:
if firstLine:
firstLine = False
continue
if i in neg_ex_rows:
vals = line.rstrip().split('\t')
cur_ens_id = vals[1]
# Remove decimal from the ensembl ID
if '.' in cur_ens_id:
cur_ens_id = cur_ens_id[0:cur_ens_id.index('.')]
gene_ids_ordered_neg.append(cur_ens_id)
exp_levels_str = vals[4:]
exp_levels = [float(exp_level) for exp_level in exp_levels_str]
gene_features_neg = np.append(gene_features_neg, [exp_levels], axis=0)
i += 1
rpkm_file.close()
return gene_features_neg, gene_ids_ordered_neg
def print_prediction_results(model, labels, predictions, other_info=None):
print 20*'-'
print model
print 20*'-'
print 'Root Mean Square Error: ', math.sqrt(mean_squared_error(labels, predictions))
print 'ROC AUC Score: ', roc_auc_score(labels, predictions)
false_positives = 0
false_negatives = 0
for label, pred in zip(labels, predictions):
if label == 0 and pred == 1:
false_positives += 1
elif label == 1 and pred == 0:
false_negatives += 1
print 'False positive rate: ', 1.0 * false_positives / len(labels)
print 'False negative rate: ', 1.0 * false_negatives / len(labels)
if other_info:
print other_info
def get_go_terms(biomart_fpath, gene2go_fpath, gene_count_fpath, top=1):
entrez_to_ensembl = map_entrez_to_ensembl(biomart_fpath)
# taxids=[9606] means select only human.
#go_evidence_codes = ['EXP', 'IDA', 'IPI', 'IMP', 'IGI', 'IEP']
go_to_entrez_ids_human = read_ncbi_gene2go(gene2go_fpath, taxids=[9606], go2geneids=True, evidence_set=None)
print("{N} GO terms associated with human NCBI Entrez GeneIDs".format(N=len(go_to_entrez_ids_human)))
# Get the |top| GO terms with the most gene annotations
gene_cnt_file = open(gene_count_fpath)
top_GO_ids = []
atLine = 0
skipLines = 1
for line in gene_cnt_file:
if atLine < skipLines:
atLine += 1
continue
elif atLine > top:
break
atLine += 1
GO_id = line.split('\t')[0]
entrez_ids = go_to_entrez_ids_human[GO_id]
#print '# of Entrez IDs associated with ', GO_id, ' = ', len(entrez_ids)
ensembl_ids = []
for ent_id in entrez_ids:
if str(ent_id) in entrez_to_ensembl:
ensembl_ids.append(entrez_to_ensembl[str(ent_id)])
top_GO_ids.append((GO_id, ensembl_ids))
#print '# of Ensembl IDs associated with ', GO_id, ' = ', len(ensembl_ids)
return top_GO_ids
"""
*********************
Main
*********************
"""
if __name__ == "__main__":
biomart_file_path = 'data/biomart_ensembl_to_entrez.txt'
gene2go_file_path = 'data/gene2go.txt' # If file doesn't exist, then run gene2go = download_ncbi_associations()
gene_count_file_path = 'data/GO_term_gene_counts.txt'
GO_PROCESS_IDs = get_go_terms(biomart_file_path, gene2go_file_path, gene_count_file_path, top=10)
# GO:0007596
(GO_PROCESS_ID, ensembl_ids) = GO_PROCESS_IDs[7]
print GO_PROCESS_ID
print len(ensembl_ids)
rpkm_file_path = '../../../Documents/Stanford/CS341_Data/transcript_rpkm_in_go_nonzero_exp.txt'
#rpkm_file_path = '../../../Downloads/GTEx_Analysis_v6_RNA-seq_Flux1.6_transcript_rpkm.txt'
go_evidence_codes = ['EXP', 'IDA', 'IPI', 'IMP', 'IGI', 'IEP']
#ensembl_ids = get_ensembl_ids(GO_PROCESS_ID, biomart_file_path, ev_codes=go_evidence_codes)
NUM_SAMPLES = 8555
# 1st Pass Through Dataset: Obtain positive training examples
gene_features, positive_example_rows, gene_ids_ordered, num_transcripts = get_positive_examples(rpkm_file_path, ensembl_ids)
print 'After pass 1 (inserting positive examples), gene feature matrix has dimension: ', gene_features.shape
num_positive_examples = len(positive_example_rows)
num_negative_examples = num_positive_examples
num_examples = num_positive_examples + num_negative_examples
# 2nd Pass through dataset: Obtain an equal number of negative training exmaples
max_row_id = num_transcripts-1
negative_example_rows = rand_sample_exclude(range(0, max_row_id+1), num_positive_examples, exclude=positive_example_rows)
gene_features_neg, gene_ids_ordered_neg = get_negative_examples(rpkm_file_path, negative_example_rows)
gene_features = np.append(gene_features, gene_features_neg, axis=0)
gene_ids_ordered += gene_ids_ordered_neg
print 'After pass 2 (inserting negative examples), gene feature matrix has dimension: ', gene_features.shape
# Vector of labels for each example
labels = num_positive_examples * [1] + num_negative_examples * [0]
# Split into training and test sets
# gene_features_train, labels_train, gene_features_test, labels_test =
# split_to_train_and_test(gene_features, TRAIN_SET_SIZE)
TRAIN_SET_SIZE = 0.7 # Fraction of genes used for training set
num_train_examples = int(math.ceil(TRAIN_SET_SIZE*num_examples))
train_indeces = random.sample(range(0, num_examples), num_train_examples)
gene_features_train = np.empty((0, NUM_SAMPLES))
gene_features_test = np.empty((0, NUM_SAMPLES))
labels_train = []
labels_test = []
gene_ids_ordered_train = []
gene_ids_ordered_test = []
print 'num ex: ', num_examples
for idx in range(0, num_examples):
if idx in train_indeces:
gene_features_train = np.append(gene_features_train, [gene_features[idx]], axis=0)
labels_train.append(labels[idx])
gene_ids_ordered_train.append(gene_ids_ordered[idx])
else:
gene_features_test = np.append(gene_features_test, [gene_features[idx]], axis=0)
labels_test.append(labels[idx])
gene_ids_ordered_test.append(gene_ids_ordered[idx])
print 'Dimensionality of training set: ', gene_features_train.shape
print 'Dimensionality of test set: ', gene_features_test.shape
# Logistic Regression with Cross-Validation, L1 Norm (must use liblinear solver for L1)
#costs = []
'''
num_folds = 10 # number of folds to use for cross-validation
loss_function = 'l1' # Loss function to use. Must be either 'l1' or 'l2'
costs = [1, 10]
logreg_cv_L1 = linear_model.LogisticRegressionCV(Cs=costs, cv=num_folds, penalty=loss_function, solver='liblinear')
print 'a'
logreg_cv_L1.fit(gene_features_train, labels_train)
print 'b'
pred_lr_cv_L1 = logreg_cv_L1.predict(gene_features_test)
print_prediction_results('Cross-Validated Logistic Regression', labels_test, pred_lr_cv_L1,
other_info='Norm: ' + loss_function + ', # of Folds: ' + str(num_folds))
'''
num_folds = 10 # number of folds to use for cross-validation
loss_function = 'l2' # Loss function to use. Must be either 'l1' or 'l2'
logreg_cv_L2 = linear_model.LogisticRegressionCV(cv=num_folds, penalty=loss_function)
logreg_cv_L2.fit(gene_features_train, labels_train)
pred_lr_cv_L2 = logreg_cv_L2.predict(gene_features_test)
print_prediction_results('Cross-Validated Logistic Regression', labels_test, pred_lr_cv_L2
, other_info='Norm: ' + loss_function + ', # of Folds: ' + str(num_folds))
# SVM
clf = SVC()
clf.fit(gene_features_train, labels_train)
pred_svm = clf.predict(gene_features_test)
print_prediction_results('SVM', labels_test, pred_svm)