-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
7360 lines (6315 loc) · 419 KB
/
main.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
#!/usr/bin/env python3
#AUTHOR: Bryan Laraway
#PROJECT: Comparison of OWLSim and Phenologs for the identification of models of human disease and gene candidates for human disease.
#PURPOSE: This script will call of the functions/methods/scripts for performing the processing required for this analysis.
import json
import urllib.request
import codecs
import time
import gc
#from socket import *
#import os
import sys
import re
import csv
import pickle
import ast
from decimal import Decimal, getcontext
#import cProfile
import numpy
from numpy import random
from scipy.stats import hypergeom, pearsonr
import math
import heapq
import multiprocessing
#from memory_profiler import profile
#from multiprocessing import Pool, Process, Manager, Lock, Value
import itertools
from threading import Thread
#import threading
from ctypes import c_int
from queue import Queue
from collections import *
from functools import reduce
import matplotlib.pyplot as plt
start_time = time.time()
hu_disease_to_phenotype_hash = {'disease_id': {}}
mouse_genotype_to_phenotype_hash = {'genotype_id': {}}
zfin_genotype_to_phenotype_hash = {'genotype_id': {}}
getcontext().prec = 500
#print(getcontext())
#Selected distinct PANTHER IDs from the NIF/DISCO tables.
#TODO: See about getting these numbers from the Panther table to allow for dynamic updating with file updates.
#total_human_mouse_orthologs = 5625, with LDO = 5729
#total_human_zebrafish_orthologs = 5212, with LDO = 5750
#total_mouse_zebrafish_orthologs = 5210, with LDO = 5748
#TODO: Need to pass phenotype/gene labels for identification, or look them up upon final output.
class main():
# NOTE: Could either include the fetch code to retrieve the data from the resources,
# or retrieve them and have the code just open local files, already retrieved.
# Required table from NIF/DISCO
tables = [
'dvp.pr_nlx_151835_1', # HPO: Annotations:DiseasePhenotypes view
'dvp.pr_nlx_151835_2', # HPO: Annotations:Phenotype to gene view
'dvp.pr_nlx_151835_3', # HPO: Annotations:Disease to gene
'dvp.pr_nif_0000_00096_5', # MGI:MouseGenotypes
'dvp.pr_nif_0000_00096_6', # MGI:MousePhenotypes
'dvp.pr_nif_0000_21427_10', # ZFIN:Genotype-Phenotype
'dvp.pr_nif_0000_21427_11', # ZFIN:OrganismGenotypes
'dvp.pr_nlx_84521_1' # PANTHER:Orthologs
]
####### NIF DATA ASSEMBLY #######
####### PHENOTYPE ID TO LABEL ASSEMBLY #######
def assemble_nif_hpo_phenotype_id_to_label(self, limit=None):
"""This function assembles a hash for human phenotype IDs and their labels from the NIF/DISCO flat data file"""
print('INFO:Assembling human phenotype ID to label hash.')
# Set up counters and open required files.
line_counter = 0
raw = 'raw/hpo/dvp.pr_nlx_151835_2'
inter = 'inter/hpo/human_phenotype_id_to_label_hash.txt'
hpo_phenotype_id_to_label_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' human phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.')
row_count = limit
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader, None)
for row in filereader:
# Read in a row and split into individual variables
line_counter += 1
(e_uid, phenotype_id, phenotype_label, gene_id, gene_num,
gene_label, v_uid, v_uuid, v_lastmodified) = row
print('INFO: Processing human phenotype row '+str(line_counter)+' out of '+str(row_count)+'.')
if phenotype_id == '' or phenotype_id is None:
continue
if gene_id == '' or gene_id is None:
continue
# Convert NCBIGene ID prefix.
gene_id = re.sub('NCBI_gene:', 'NCBIGene:', gene_id)
# If phenotype is not in the phenotype to gene hash, add phenotype to hash.
if phenotype_id not in hpo_phenotype_id_to_label_hash:
hpo_phenotype_id_to_label_hash[phenotype_id] = phenotype_label
if limit is not None and line_counter > limit:
break
# Dump files to disk.
with open(inter, 'wb') as handle:
pickle.dump(hpo_phenotype_id_to_label_hash, handle)
print('INFO: Done assembling human phenotype ID to label hash.')
print('INFO: '+str(len(hpo_phenotype_id_to_label_hash.keys()))+' human phenotypes present.')
return
def assemble_nif_zfin_phenotype_id_to_label(self, limit=None):
"""This function assembles a hash for zebrafish phenotype IDs and their labels from the NIF/DISCO flat data file"""
print('INFO:Assembling zebrafish phenotype to ortholog data.')
# Set up counters and open required files.
line_counter = 0
raw = 'raw/zfin/dvp.pr_nif_0000_21427_10'
inter = 'inter/zfin/zebrafish_phenotype_id_to_label_hash.txt'
zfin_phenotype_id_to_label_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' zebrafish phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.' )
row_count = limit
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
# Read in a row and split into individual variables.
line_counter += 1
(e_uid, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_num, intrinsic_genotype_label, intrinsic_genotype_label_html,
extrinsic_genotype_id, extrinsic_genotype_label, extrinsic_genotype_label_html, phenotype_id,
phenotype_label, phenotype_modifier, implicated_gene_ids, implicated_gene_labels, start_stage_id,
start_stage_zfin_id, start_stage_label, end_stage_id ,end_stage_zfin_id, end_stage_label, stages,
genomic_background_id, genomic_background_num, genomic_background_label,
affected_structure_or_process_1_superterm_id, affected_structure_or_process_1_superterm_name,
affected_structure_or_process_1_subterm_id, affected_structure_or_process_1_subterm_name,
quality_id, quality_label, affected_structure_or_process_2_superterm_id,
affected_structure_or_process_2_superterm_name, affected_structure_or_process_2_subterm_id,
affected_structure_or_process_2_subterm_name, environment_id, environment_label, evidence_code_id,
evidence_code_symbol, evidence_code_label, publication_id, publication_label, publication_url,
taxon_id, taxon_label, v_uid, v_uuid, v_lastmodified) = row
print('INFO: Processing phenotype '+str(line_counter)+' out of '+str(row_count)+'.')
# Skip phenotypes without IDs and phenotypes with no associated genes.
if phenotype_id == '' or phenotype_id is None:
continue
if implicated_gene_ids == '' or implicated_gene_ids is None:
continue
# If phenotype is not in the phenotype to gene hash, add phenotype to hash.
if phenotype_id not in zfin_phenotype_id_to_label_hash:
zfin_phenotype_id_to_label_hash[phenotype_id] = phenotype_label
if limit is not None and line_counter > limit:
break
# Dump data to files.
with open(inter, 'wb') as handle:
pickle.dump(zfin_phenotype_id_to_label_hash, handle)
print('INFO: Done assembling zebrafish phenotype to gene/ortholog data.')
print('INFO: '+str(len(zfin_phenotype_id_to_label_hash.keys()))+' zebrafish phenotypes present.')
return
def assemble_nif_mgi_phenotype_id_to_label(self, limit=None):
"""This function assembles a hash for mouse phenotype IDs and their labels from the NIF/DISCO flat data file"""
print('INFO:Assembling mouse phenotype to gene/ortholog data.')
# Set up counters and open required files.
line_counter = 0
raw = 'raw/mgi/dvp.pr_nif_0000_00096_6'
inter = 'inter/mgi/mouse_phenotype_id_to_label_hash.txt'
mgi_phenotype_id_to_label_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' mouse phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.')
row_count = limit
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
# Read in a row and split into individual variables
line_counter += 1
(annotation_id, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_label, intrinsic_genotype_label_html,
genomic_variation_complement_id, genomic_variation_complement_label,
genomic_variation_complement_label_html, implicated_gene_ids, implicated_gene_labels,
implicated_sequence_alteration_ids, implicated_sequence_alteration_labels, genomic_background_id,
genomic_background_label, phenotype_id, phenotype_label, phenotype_description_free_text,
phenotype_modifier, evidence_code_id, evidence_code_symbol, evidence_code_label, environment_id,
environment_label, publication_id, publication_label, publication_url, taxon_id, taxon_label,
e_uid, v_uid, v_uuid, v_lastmodified) = row
print('INFO: Processing phenotype '+str(line_counter)+' out of '+str(row_count)+'.')
# Skip phenotypes without IDs and phenotypes with no associated genes.
if phenotype_id == '' or phenotype_id is None:
continue
if implicated_gene_ids == '' or implicated_gene_ids is None:
continue
# Split the implicated genes list.
genes = implicated_gene_ids.split(',')
# If phenotype is not in the phenotype to gene hash, add phenotype to hash.
if phenotype_id not in mgi_phenotype_id_to_label_hash:
mgi_phenotype_id_to_label_hash[phenotype_id] = phenotype_label
if limit is not None and line_counter > limit:
break
# Dump data to files.
with open(inter, 'wb') as handle:
pickle.dump(mgi_phenotype_id_to_label_hash, handle)
print('INFO: Done assembling mouse phenotype to gene/ortholog data.')
print('INFO: '+str(len(mgi_phenotype_id_to_label_hash.keys()))+' mouse phenotypes present.')
return
####### GENE ID TO LABEL ASSEMBLY #######
def assemble_nif_mgi_gene_id_to_label(self):
"""This function assembles mouse gene id to gene labels from the NIF/DISCO flat data file"""
print('INFO:Assembling mouse gene ID to label hash.')
# Set up counters and open required files.
line_counter = 0
raw = 'raw/mgi/dvp.pr_nif_0000_00096_6'
inter = 'inter/mgi/mouse_gene_id_to_label_hash.txt'
gene_id_to_label_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' mouse rows to process.')
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
# Read in a row and split into individual variables
line_counter += 1
(annotation_id, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_label, intrinsic_genotype_label_html,
genomic_variation_complement_id, genomic_variation_complement_label,
genomic_variation_complement_label_html, implicated_gene_ids, implicated_gene_labels,
implicated_sequence_alteration_ids, implicated_sequence_alteration_labels, genomic_background_id,
genomic_background_label, phenotype_id, phenotype_label, phenotype_description_free_text,
phenotype_modifier, evidence_code_id, evidence_code_symbol, evidence_code_label, environment_id,
environment_label, publication_id, publication_label, publication_url, taxon_id, taxon_label,
e_uid, v_uid, v_uuid, v_lastmodified) = row
#print('INFO: Processing phenotype '+str(line_counter)+' out of '+str(row_count)+'.')
if implicated_gene_ids == '' or implicated_gene_ids is None:
continue
# Split the implicated genes list.
genes = implicated_gene_ids.split(',')
gene_labels = implicated_gene_labels.split(',')
#print(genes)
# If gene is not in the gene ID to label hash, add gene to hash.
for gene in genes:
if gene not in gene_id_to_label_hash:
gene_index = genes.index(gene)
#print(gene_index)
gene_id_to_label_hash[gene] = gene_labels[gene_index]
# Dump data to files.
with open(inter, 'wb') as handle:
pickle.dump(gene_id_to_label_hash, handle)
print('INFO: Done assembling mouse gene ID to label hash.')
return
def assemble_nif_zfin_gene_id_to_label(self):
print('INFO:Assembling zebrafish gene ID to label hash.')
line_counter = 0
raw = 'raw/zfin/dvp.pr_nif_0000_21427_10'
inter = 'inter/zfin/zebrafish_gene_id_to_label_hash.txt'
gene_id_to_label_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' zebrafish gene to phenotype rows to process.')
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(e_uid, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_num, intrinsic_genotype_label, intrinsic_genotype_label_html,
extrinsic_genotype_id, extrinsic_genotype_label, extrinsic_genotype_label_html, phenotype_id,
phenotype_label, phenotype_modifier, implicated_gene_ids, implicated_gene_labels, start_stage_id,
start_stage_zfin_id, start_stage_label, end_stage_id ,end_stage_zfin_id, end_stage_label, stages,
genomic_background_id, genomic_background_num, genomic_background_label,
affected_structure_or_process_1_superterm_id, affected_structure_or_process_1_superterm_name,
affected_structure_or_process_1_subterm_id, affected_structure_or_process_1_subterm_name,
quality_id, quality_label, affected_structure_or_process_2_superterm_id,
affected_structure_or_process_2_superterm_name, affected_structure_or_process_2_subterm_id,
affected_structure_or_process_2_subterm_name, environment_id, environment_label, evidence_code_id,
evidence_code_symbol, evidence_code_label, publication_id, publication_label, publication_url,
taxon_id, taxon_label, v_uid, v_uuid, v_lastmodified) = row
# Skip phenotypes without IDs and phenotypes with no associated genes.
if phenotype_id == '' or phenotype_id is None:
continue
if implicated_gene_ids == '' or implicated_gene_ids is None:
continue
# Split the implicated genes list.
genes = implicated_gene_ids.split(',')
gene_labels = implicated_gene_labels.split(',')
#print(genes)
# If gene is not in the gene ID to label hash, add gene to hash.
for gene in genes:
if gene not in gene_id_to_label_hash:
gene_index = genes.index(gene)
#print(gene_index)
gene_id_to_label_hash[gene] = gene_labels[gene_index]
# Dump data to files.
with open(inter, 'wb') as handle:
pickle.dump(gene_id_to_label_hash, handle)
print('INFO: Done assembling zebrafish gene to label hash.')
return
def assemble_nif_hpo_gene_id_to_label(self):
print('INFO:Assembling human gene ID to label hash.')
line_counter = 0
failure_counter = 0
raw1 = 'raw/hpo/dvp.pr_nlx_151835_3'
raw2 = 'raw/hpo/dvp.pr_nlx_151835_2'
inter = 'inter/hpo/human_gene_id_to_label_hash.txt'
gene_id_to_label_hash = {}
with open(raw1, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' human disease to gene rows to process.')
with open(raw1, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(e_uid, disease_id, disorder_name, disorder_database_link, gene_id,
gene_num, gene_label, v_uid, v_uuid, v_lastmodified) = row
#print(disease_id)
# Convert NCBIGene ID prefix.
gene_id = re.sub('NCBI_gene:', 'NCBIGene:', gene_id)
#print(genes)
if gene_id not in gene_id_to_label_hash:
gene_id_to_label_hash[gene_id] = gene_label
#print(hpo_phenotype_to_gene_hash[genotype_id])
# Set up counters and open required files.
line_counter = 0
with open(raw2, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' human phenotype rows to process.')
with open(raw2, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader, None)
for row in filereader:
# Read in a row and split into individual variables
line_counter += 1
(e_uid, phenotype_id, phenotype_label, gene_id, gene_num,
gene_label, v_uid, v_uuid, v_lastmodified) = row
if phenotype_id == '' or phenotype_id is None:
continue
if gene_id == '' or gene_id is None:
continue
# Convert NCBIGene ID prefix.
gene_id = re.sub('NCBI_gene:', 'NCBIGene:', gene_id)
if gene_id not in gene_id_to_label_hash:
gene_id_to_label_hash[gene_id] = gene_label
# Dump files to disk.
with open(inter, 'wb') as handle:
pickle.dump(gene_id_to_label_hash, handle)
print('INFO: Done assembling human gene ID to label hash.')
return
####### PHENOLOG PHENOTYPE TO GENE/ORTHOLOG #######
def trim_panther_data(self, inter, taxons):
"""
This function trims the PANTHER flat file from NIF/DISCO for a given taxon,
which speeds up data assembly when converting from genes to orthologs.
:param inter: Directory and file name for saving the trimmed PANTHER file.
:param taxons: taxon IDs for filtering.
:return:
"""
print('INFO: Trimming PANTHER data.')
# Set counters and open required files.
line_counter = 0
output_line_counter = 0
raw = 'raw/panther/dvp.pr_nlx_84521_1'
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' PANTHER rows to process.')
with open(inter, 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter='\t', quotechar='\"')
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(panther_speciesa, tax_id_a, taxon_id_a, speciesa, taxon_label_a, genea, gene_id_a, gene_label_a,
proteina, panther_speciesb, tax_id_b, taxon_id_b, speciesb, taxon_label_b, geneb, gene_id_b,
gene_label_b, proteinb, orthology_class, orthology_class_label, ancestor_taxon, panther_id,
e_uid, v_uid, v_uuid, v_lastmodified) = row
#Currently filtering on the big three taxons, and ortholog relations only.
if (taxon_id_a in taxons or taxon_id_b in taxons) and (orthology_class_label == 'Least Diverged Ortholog' or orthology_class_label == 'Ortholog'):
output_row = (panther_speciesa, taxon_id_a, speciesa, taxon_label_a, genea, gene_id_a, gene_label_a,
proteina, panther_speciesb, taxon_id_b, speciesb, taxon_label_b, geneb, gene_id_b,
gene_label_b, proteinb, orthology_class, orthology_class_label, panther_id)
output_line_counter += 1
csvwriter.writerow(output_row)
print('PANTHER file trimmed to '+str(output_line_counter)+' rows.')
return
def get_common_orthologs(self, inter, taxons):
"""
This function takes as input a list of taxons and a the PANTHER flat file,
filters the PANTHER flat file using the provided taxon IDs to obtain
the common orthologs between the taxons, and writes the list to a new output file.
:param inter: directory & file name for the output file.
:param taxons: taxon IDs for filtering the PANTHER flat file.
:return:
"""
print('INFO: Getting common orthologs between species.')
line_counter = 0
ortholog_counter = 0
raw = 'raw/panther/dvp.pr_nlx_84521_1'
common_orthologs = []
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' PANTHER rows to process.')
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(panther_speciesa, tax_id_a, taxon_id_a, speciesa, taxon_label_a, genea, gene_id_a, gene_label_a,
proteina, panther_speciesb, tax_id_b, taxon_id_b, speciesb, taxon_label_b, geneb, gene_id_b,
gene_label_b, proteinb, orthology_class, orthology_class_label, ancestor_taxon, panther_id,
e_uid, v_uid, v_uuid, v_lastmodified) = row
if (taxon_id_a in taxons or taxon_id_b in taxons) and (orthology_class_label == 'Least Diverged Ortholog' or orthology_class_label == 'Ortholog'):
if panther_id not in common_orthologs:
common_orthologs.append(panther_id)
ortholog_counter += 1
with open(inter, 'wb') as handle:
pickle.dump(common_orthologs, handle)
print(str(ortholog_counter)+' common orthologs found.')
return
def get_ortholog(self, query_gene_id, panther):
"""
This function is used when creating the phenotype-ortholog hashes.
It takes a gene ID and, if there is an ortholog match, return the PANTHER ID of the ortholog.
:param query_gene_id: Gene ID used to query for an ortholog.
:param panther: PANTHER file trimmed for the specific taxon.
:return: PANTHER ID if successfail, fail flag if unsuccessful.
"""
with open(panther, 'r') as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
print('query= '+query_gene_id)
for row in filereader:
(panther_speciesa, taxon_id_a, speciesa, taxon_label_a, genea, gene_id_a, gene_label_a,proteina,
panther_speciesb, taxon_id_b, speciesb, taxon_label_b, geneb, gene_id_b,gene_label_b, proteinb,
orthology_class, orthology_class_label, panther_id) = row
if query_gene_id in [genea, gene_id_a, geneb, gene_id_b]:
result_panther_id = panther_id
print('found ortholog for '+query_gene_id+'.')
return(result_panther_id)
print('no ortholog found for '+query_gene_id+'.')
return('fail')
def assemble_nif_zfin_phenotype_to_gene(self, limit=None):
"""This function assembles zebrafish phenotype to gene associations from the NIF/DISCO flat data file"""
print('INFO:Assembling zebrafish phenotype to ortholog data.')
gene_to_ortholog_hash = {}
# Set up counters and open required files.
line_counter = 0
raw = 'raw/zfin/dvp.pr_nif_0000_21427_10'
inter1 = 'inter/zfin/zebrafish_pheno_gene_hash.txt'
inter2 = 'inter/zfin/zebrafish_pheno_ortholog_hash.txt'
zfin_phenotype_to_gene_hash = {}
zfin_phenotype_to_ortholog_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' zebrafish phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.' )
row_count = limit
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
# Read in a row and split into individual variables.
line_counter += 1
(e_uid, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_num, intrinsic_genotype_label, intrinsic_genotype_label_html,
extrinsic_genotype_id, extrinsic_genotype_label, extrinsic_genotype_label_html, phenotype_id,
phenotype_label, phenotype_modifier, implicated_gene_ids, implicated_gene_labels, start_stage_id,
start_stage_zfin_id, start_stage_label, end_stage_id ,end_stage_zfin_id, end_stage_label, stages,
genomic_background_id, genomic_background_num, genomic_background_label,
affected_structure_or_process_1_superterm_id, affected_structure_or_process_1_superterm_name,
affected_structure_or_process_1_subterm_id, affected_structure_or_process_1_subterm_name,
quality_id, quality_label, affected_structure_or_process_2_superterm_id,
affected_structure_or_process_2_superterm_name, affected_structure_or_process_2_subterm_id,
affected_structure_or_process_2_subterm_name, environment_id, environment_label, evidence_code_id,
evidence_code_symbol, evidence_code_label, publication_id, publication_label, publication_url,
taxon_id, taxon_label, v_uid, v_uuid, v_lastmodified) = row
print('INFO: Processing phenotype '+str(line_counter)+' out of '+str(row_count)+'.')
# Skip phenotypes without IDs and phenotypes with no associated genes.
if phenotype_id == '' or phenotype_id is None:
continue
if implicated_gene_ids == '' or implicated_gene_ids is None:
continue
# Split the implicated genes list.
genes = implicated_gene_ids.split(',')
# If phenotype is not in the phenotype to gene hash, add phenotype to hash.
if phenotype_id not in zfin_phenotype_to_gene_hash:
zfin_phenotype_to_gene_hash[phenotype_id] = []
# If phenotype is not in the phenotype to ortholog hash, add phenotype to hash.
if phenotype_id not in zfin_phenotype_to_ortholog_hash:
zfin_phenotype_to_ortholog_hash[phenotype_id] = []
# If gene is not in the phenotype to gene hash, add gene to hash.
for gene in genes:
if gene not in zfin_phenotype_to_gene_hash[phenotype_id]:
zfin_phenotype_to_gene_hash[phenotype_id].append(gene)
if gene not in gene_to_ortholog_hash:
# Convert genes to orthologs using zebrafish-trimmed PANTHER table as lookup.
panther_id = self.get_ortholog(gene, 'inter/panther/panther_zebrafish.txt')
gene_to_ortholog_hash[gene] = panther_id
# If ortholog is not in the phenotype to ortholog hash, add ortholog to hash.
if panther_id != 'fail' and panther_id not in zfin_phenotype_to_ortholog_hash[phenotype_id]:
zfin_phenotype_to_ortholog_hash[phenotype_id].append(panther_id)
else:
panther_id = gene_to_ortholog_hash[gene]
if panther_id != 'fail' and panther_id not in zfin_phenotype_to_ortholog_hash[phenotype_id]:
zfin_phenotype_to_ortholog_hash[phenotype_id].append(panther_id)
if limit is not None and line_counter > limit:
break
# Dump data to files.
with open(inter1, 'wb') as handle:
pickle.dump(zfin_phenotype_to_gene_hash, handle)
with open(inter2, 'wb') as handle:
pickle.dump(zfin_phenotype_to_ortholog_hash, handle)
with open('inter/zfin/zebrafish_gene_to_ortholog_hash.txt', 'wb') as handle:
pickle.dump(gene_to_ortholog_hash, handle)
print('INFO: Done assembling zebrafish phenotype to gene/ortholog data.')
print('INFO: '+str(len(zfin_phenotype_to_gene_hash.keys()))+' zebrafish phenotypes present.')
return
def assemble_nif_mgi_phenotype_to_gene(self, limit=None):
"""This function assembles mouse phenotype to gene associations from the NIF/DISCO flat data file"""
print('INFO:Assembling mouse phenotype to gene/ortholog data.')
# Set up counters and open required files.
line_counter = 0
raw = 'raw/mgi/dvp.pr_nif_0000_00096_6'
inter1 = 'inter/mgi/mouse_pheno_gene_hash.txt'
inter2 = 'inter/mgi/mouse_pheno_ortholog_hash.txt'
mgi_phenotype_to_gene_hash = {}
mgi_phenotype_to_ortholog_hash = {}
gene_to_ortholog_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' mouse phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.')
row_count = limit
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
# Read in a row and split into individual variables
line_counter += 1
(annotation_id, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_label, intrinsic_genotype_label_html,
genomic_variation_complement_id, genomic_variation_complement_label,
genomic_variation_complement_label_html, implicated_gene_ids, implicated_gene_labels,
implicated_sequence_alteration_ids, implicated_sequence_alteration_labels, genomic_background_id,
genomic_background_label, phenotype_id, phenotype_label, phenotype_description_free_text,
phenotype_modifier, evidence_code_id, evidence_code_symbol, evidence_code_label, environment_id,
environment_label, publication_id, publication_label, publication_url, taxon_id, taxon_label,
e_uid, v_uid, v_uuid, v_lastmodified) = row
print('INFO: Processing phenotype '+str(line_counter)+' out of '+str(row_count)+'.')
# Skip phenotypes without IDs and phenotypes with no associated genes.
if phenotype_id == '' or phenotype_id is None:
continue
if implicated_gene_ids == '' or implicated_gene_ids is None:
continue
# Split the implicated genes list.
genes = implicated_gene_ids.split(',')
# If phenotype is not in the phenotype to gene hash, add phenotype to hash.
if phenotype_id not in mgi_phenotype_to_gene_hash:
mgi_phenotype_to_gene_hash[phenotype_id] = []
# If phenotype is not in the phenotype to ortholog hash, add phenotype to hash.
if phenotype_id not in mgi_phenotype_to_ortholog_hash:
mgi_phenotype_to_ortholog_hash[phenotype_id] = []
# If gene is not in the phenotype to gene hash, add gene to hash.
for gene in genes:
if gene not in mgi_phenotype_to_gene_hash[phenotype_id]:
mgi_phenotype_to_gene_hash[phenotype_id].append(gene)
if gene not in gene_to_ortholog_hash:
# Convert genes to orthologs using mouse-trimmed PANTHER table as lookup.
panther_id = self.get_ortholog(gene,'inter/panther/panther_mouse.txt')
gene_to_ortholog_hash[gene] = panther_id
# If ortholog is not in the phenotype to ortholog hash, add ortholog to hash.
if panther_id != 'fail' and panther_id not in mgi_phenotype_to_ortholog_hash[phenotype_id]:
mgi_phenotype_to_ortholog_hash[phenotype_id].append(panther_id)
else:
panther_id = gene_to_ortholog_hash[gene]
if panther_id != 'fail' and panther_id not in mgi_phenotype_to_ortholog_hash[phenotype_id]:
mgi_phenotype_to_ortholog_hash[phenotype_id].append(panther_id)
if limit is not None and line_counter > limit:
break
# Dump data to files.
with open(inter1, 'wb') as handle:
pickle.dump(mgi_phenotype_to_gene_hash, handle)
with open(inter2, 'wb') as handle:
pickle.dump(mgi_phenotype_to_ortholog_hash, handle)
with open('inter/mgi/mouse_gene_to_ortholog_hash.txt', 'wb') as handle:
pickle.dump(gene_to_ortholog_hash, handle)
print('INFO: Done assembling mouse phenotype to gene/ortholog data.')
print('INFO: '+str(len(mgi_phenotype_to_gene_hash.keys()))+' mouse phenotypes present.')
return
def assemble_nif_hpo_phenotype_to_gene(self, limit=None):
"""This function assembles human phenotype to gene associations from the NIF/DISCO flat data file"""
print('INFO:Assembling human phenotype to gene data.')
# Set up counters and open required files.
line_counter = 0
raw = 'raw/hpo/dvp.pr_nlx_151835_2'
inter1 = 'inter/hpo/human_pheno_gene_hash.txt'
inter2 = 'inter/hpo/human_pheno_ortholog_hash.txt'
hpo_phenotype_to_gene_hash = {}
hpo_phenotype_to_ortholog_hash = {}
gene_to_ortholog_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' human phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.')
row_count = limit
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader, None)
for row in filereader:
# Read in a row and split into individual variables
line_counter += 1
(e_uid, phenotype_id, phenotype_label, gene_id, gene_num,
gene_label, v_uid, v_uuid, v_lastmodified) = row
print('INFO: Processing human phenotype row '+str(line_counter)+' out of '+str(row_count)+'.')
if phenotype_id == '' or phenotype_id is None:
continue
if gene_id == '' or gene_id is None:
continue
# Convert NCBIGene ID prefix.
gene_id = re.sub('NCBI_gene:', 'NCBIGene:', gene_id)
# If phenotype is not in the phenotype to gene hash, add phenotype to hash.
if phenotype_id not in hpo_phenotype_to_gene_hash:
hpo_phenotype_to_gene_hash[phenotype_id] = []
# If phenotype is not in the phenotype to ortholog hash, add phenotype to hash.
if phenotype_id not in hpo_phenotype_to_ortholog_hash:
hpo_phenotype_to_ortholog_hash[phenotype_id] = []
# If gene is not in the phenotype to gene hash, add gene to hash.
if gene_id not in hpo_phenotype_to_gene_hash[phenotype_id]:
hpo_phenotype_to_gene_hash[phenotype_id].append(gene_id)
if gene_id not in gene_to_ortholog_hash:
# Convert genes to orthologs using human-trimmed PANTHER table as lookup.
panther_id = self.get_ortholog(gene_id,'inter/panther/panther_human.txt')
gene_to_ortholog_hash[gene_id] = panther_id
# If ortholog is not in the phenotype to ortholog hash, add ortholog to hash.
if panther_id != 'fail' and panther_id not in hpo_phenotype_to_ortholog_hash[phenotype_id]:
hpo_phenotype_to_ortholog_hash[phenotype_id].append(panther_id)
else:
panther_id = gene_to_ortholog_hash[gene_id]
if panther_id != 'fail' and panther_id not in hpo_phenotype_to_ortholog_hash[phenotype_id]:
hpo_phenotype_to_ortholog_hash[phenotype_id].append(panther_id)
if limit is not None and line_counter > limit:
break
# Dump files to disk.
with open(inter1, 'wb') as handle:
pickle.dump(hpo_phenotype_to_gene_hash, handle)
with open(inter2, 'wb') as handle:
pickle.dump(hpo_phenotype_to_ortholog_hash, handle)
with open('inter/hpo/human_gene_to_ortholog_hash.txt', 'wb') as handle:
pickle.dump(gene_to_ortholog_hash, handle)
print('INFO: Done assembling human phenotype to gene/ortholog data.')
print('INFO: '+str(len(hpo_phenotype_to_gene_hash.keys()))+' human phenotypes present.')
return
####### OWLSIM GENOTYPE/GENE TO PHENOTYPE #######
def assemble_nif_zfin_genotype_to_phenotype(self, limit=None):
#TODO: Assuming want to filter out to intrinsic genotypes only?
# Can filter on extrinsic genotype = ''
print('INFO:Assembling zebrafish genotype to phenotype data.')
line_counter = 0
raw = 'raw/zfin/dvp.pr_nif_0000_21427_10'
inter = 'inter/zfin/zebrafish_genotype_phenotype_hash.txt'
zfin_genotype_to_phenotype_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' zebrafish genotype-phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.' )
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(e_uid, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_num, intrinsic_genotype_label, intrinsic_genotype_label_html,
extrinsic_genotype_id, extrinsic_genotype_label, extrinsic_genotype_label_html, phenotype_id,
phenotype_label, phenotype_modifier, implicated_gene_ids, implicated_gene_labels, start_stage_id,
start_stage_zfin_id, start_stage_label, end_stage_id ,end_stage_zfin_id, end_stage_label, stages,
genomic_background_id, genomic_background_num, genomic_background_label,
affected_structure_or_process_1_superterm_id, affected_structure_or_process_1_superterm_name,
affected_structure_or_process_1_subterm_id, affected_structure_or_process_1_subterm_name,
quality_id, quality_label, affected_structure_or_process_2_superterm_id,
affected_structure_or_process_2_superterm_name, affected_structure_or_process_2_subterm_id,
affected_structure_or_process_2_subterm_name, environment_id, environment_label, evidence_code_id,
evidence_code_symbol, evidence_code_label, publication_id, publication_label, publication_url,
taxon_id, taxon_label, v_uid, v_uuid, v_lastmodified) = row
if extrinsic_genotype_id != '':
print('Skipping genotype with extrinsic modifiers: '+effective_genotype_id)
continue
elif extrinsic_genotype_id == '' or extrinsic_genotype_id is None:
if phenotype_id != '' and phenotype_id is not None:
if effective_genotype_id not in zfin_genotype_to_phenotype_hash:
zfin_genotype_to_phenotype_hash[effective_genotype_id] = [phenotype_id]
else:
zfin_genotype_to_phenotype_hash[effective_genotype_id].append(phenotype_id)
if limit is not None and line_counter > limit:
break
with open(inter, 'wb') as handle:
pickle.dump(zfin_genotype_to_phenotype_hash, handle)
print('INFO: Done assembling zebrafish genotype to phenotype data.')
print('INFO: '+str(len(zfin_genotype_to_phenotype_hash))+' zebrafish genotypes present.')
return
def assemble_nif_mgi_genotype_to_phenotype(self, limit=None):
#TODO: Assuming want to filter out to intrinsic genotypes only?
# Can filter on extrinsic genotype = ''
print('INFO:Assembling mouse genotype to phenotype data.')
line_counter = 0
raw = 'raw/mgi/dvp.pr_nif_0000_00096_6'
inter = 'inter/mgi/mouse_genotype_phenotype_hash.txt'
mgi_genotype_to_phenotype_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' mouse genotype-phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.' )
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(annotation_id, effective_genotype_id, effective_genotype_label, effective_genotype_label_html,
intrinsic_genotype_id, intrinsic_genotype_label, intrinsic_genotype_label_html,
genomic_variation_complement_id, genomic_variation_complement_label,
genomic_variation_complement_label_html, implicated_gene_ids, implicated_gene_labels,
implicated_sequence_alteration_ids, implicated_sequence_alteration_labels, genomic_background_id,
genomic_background_label, phenotype_id, phenotype_label, phenotype_description_free_text,
phenotype_modifier, evidence_code_id, evidence_code_symbol, evidence_code_label, environment_id,
environment_label, publication_id, publication_label, publication_url, taxon_id,
taxon_label, e_uid, v_uid, v_uuid, v_lastmodified) = row
if effective_genotype_id not in mgi_genotype_to_phenotype_hash and phenotype_id != '':
mgi_genotype_to_phenotype_hash[effective_genotype_id] = [phenotype_id]
elif phenotype_id != '':
mgi_genotype_to_phenotype_hash[effective_genotype_id].append(phenotype_id)
if limit is not None and line_counter > limit:
break
with open(inter, 'wb') as handle:
pickle.dump(mgi_genotype_to_phenotype_hash, handle)
print('INFO: Done assembling mouse genotype to phenotype data.')
print('INFO: '+str(len(mgi_genotype_to_phenotype_hash))+' mouse genotypes present.')
return
def assemble_nif_hpo_disease_to_gene(self, limit=None):
print('INFO:Assembling human disease to gene data.')
line_counter = 0
failure_counter = 0
raw = 'raw/hpo/dvp.pr_nlx_151835_3'
inter = 'inter/hpo/human_disease_gene_hash.txt'
hpo_disease_to_gene_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' human disease to gene rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.' )
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(e_uid, disease_id, disorder_name, disorder_database_link, gene_id,
gene_num, gene_label, v_uid, v_uuid, v_lastmodified) = row
print(disease_id)
# Convert NCBIGene ID prefix.
gene_id = re.sub('NCBI_gene:', 'NCBIGene:', gene_id)
#print(genes)
if disease_id not in hpo_disease_to_gene_hash:
hpo_disease_to_gene_hash[disease_id] = [gene_id]
#print(hpo_phenotype_to_gene_hash[genotype_id])
else:
hpo_disease_to_gene_hash[disease_id].append(gene_id)
#print(hpo_disease_to_gene_hash[disease_id])
#print(len(hpo_disease_to_gene_hash.keys()))
print('Repeat disease: '+disease_id)
if limit is not None and line_counter > limit:
break
#TODO: Need to filter out phenotypes that don't have any associated genes.
with open(inter, 'wb') as handle:
pickle.dump(hpo_disease_to_gene_hash, handle)
print('INFO: Done assembling human phenotype to gene data.')
print('INFO: '+str(len(hpo_disease_to_gene_hash))+' human phenotypes present.')
return
def assemble_nif_hpo_disease_to_phenotype(self, limit=None):
print('INFO:Assembling human disease to phenotype data.')
line_counter = 0
failure_counter = 0
raw = 'raw/hpo/dvp.pr_nlx_151835_1'
inter = 'inter/hpo/human_disease_phenotype_hash.txt'
hpo_disease_to_phenotype_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' human disease to phenotype rows to process.')
if limit is not None:
print('Only parsing first '+str(limit)+' rows.' )
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
next(filereader,None)
for row in filereader:
line_counter += 1
(e_uid, disorder_id, disorder_database_prefix, disorder_id_num, disorder_database_link, disorder_name,
disorder_qualifier, phenotype_id, phenotype_label, publication_id, evidence_code_id,
evidence_code_symbol, evidence_code_label, onset_id, onset_label, frequency, aspect, aspect_text,
synonyms, v_uid, v_uuid, v_lastmodified) = row
print(disorder_id)
#print(genes)
if disorder_id not in hpo_disease_to_phenotype_hash and phenotype_id != '':
hpo_disease_to_phenotype_hash[disorder_id] = [phenotype_id]
#print(hpo_disease_to_phenotype_hash[disease)id])
elif phenotype_id != '':
if phenotype_id not in hpo_disease_to_phenotype_hash[disorder_id]:
hpo_disease_to_phenotype_hash[disorder_id].append(phenotype_id)
#print(hpo_disease_to_phenotype_hash[disorder_id])
#print(len(hpo_disease_to_phenotype_hash.keys()))
print('Repeat disease: '+disorder_id)
if limit is not None and line_counter > limit:
break
#TODO: Need to filter out phenotypes that don't have any associated genes.
with open(inter, 'wb') as handle:
pickle.dump(hpo_disease_to_phenotype_hash, handle)
print('INFO: Done assembling human disease to phenotype data.')
print('INFO: '+str(len(hpo_disease_to_phenotype_hash))+' human diseases present.')
return
def assemble_nif_mgi_gene_to_phenotype(self, limit=None):
print('INFO:Assembling mouse gene to phenotype data.')
line_counter = 0
failure_counter = 0
raw = 'raw/mgi/dvp.pr_nif_0000_00096_6'
inter = 'inter/mgi/mouse_gene_phenotype_hash.txt'
mgi_gene_to_phenotype_hash = {}
with open(raw, 'r', encoding="iso-8859-1") as csvfile:
filereader = csv.reader(csvfile, delimiter='\t', quotechar='\"')
row_count = sum(1 for row in filereader)
row_count = row_count - 1
print(str(row_count)+' mouse gene to phenotype rows to process.')