-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdata_extractor.py
872 lines (794 loc) · 29.8 KB
/
data_extractor.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
#!/usr/bin/python !/usr/bin/env python
# -*- coding: utf-8 -*
# Functions to extract knowledge from medical text. Everything related to
# extraction needed for the knowledge base. Also, some wrappers for SemRep,
# MetaMap and Reverb. Contains some enrichment routines for utilizing UTS
# services.
import json
import subprocess
import urllib2
import pymongo
import numpy as np
from nltk.tokenize import sent_tokenize
from config import settings
from pymetamap import MetaMap
from utilities import time_log, get_concept_from_cui, get_concept_from_source
from itertools import product
from multiprocessing import cpu_count, Pool
from unidecode import unidecode
def metamap_wrapper(text):
"""
Function-wrapper for metamap binary. Extracts concepts
found in text.
!!!! REMEMBER TO START THE METAMAP TAGGER AND
WordSense DISAMBIGUATION SERVER !!!!
Input:
- text: str,
a piece of text or sentence
Output:
- a dictionary with key sents and values
a list of the concepts found
"""
# Tokenize into sentences
sents = sent_tokenize(text)
# Load Metamap Instance
mm = MetaMap.get_instance(settings['load']['path']['metamap'])
concepts, errors = mm.extract_concepts(sents, range(len(sents)))
# Keep the sentence ids
ids = np.array([int(concept[0]) for concept in concepts])
sentences = []
for i in xrange(len(sents)):
tmp = {'sent_id': i+1, 'entities': [], 'relations': []}
# Wanted concepts according to sentence
wanted = np.where(ids == i)[0].tolist()
for w_ind in wanted:
w_conc = concepts[w_ind]
if hasattr(w_conc, 'cui'):
tmp_conc = {'label': w_conc.preferred_name, 'cui': w_conc.cui,
'sem_types': w_conc.semtypes, 'score': w_conc.score}
tmp['entities'].append(tmp_conc)
sentences.append(tmp)
if errors:
time_log('Errors with extracting concepts!')
time_log(errors)
return {'sents': sentences, 'sent_text':text}
def runProcess(exe, working_dir):
"""
Function that opens a command line and runs a command.
Captures the output and returns.
Input:
- exe: str,
string of the command to be run. ! REMEMBER TO ESCAPE CHARS!
- working_dir: str,
directory where the cmd should be executed
Output:
- lines: list,
list of strings generated from the command
"""
p = subprocess.Popen(exe, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=working_dir, shell=True)
lines = p.stdout.readlines()
return lines
def stopw_removal(inp, stop):
"""
Stopwords removal in line of text.
Input:
- inp: str,
string of the text input
- stop: list,
list of stop-words to be removed
"""
# Final string to be returned
final = ''
for w in inp.lower().split():
if w not in stop:
final += w + ' '
# Remove last whitespace that was added ' '
final = final[:-1]
return final
def create_text_batches(text, N=5000, buffer_ = 100):
"""
Function that takes a long string and split it into
batches of approximately length N. The actual length
of each batch differs, as each batch end in the next
dot found in the string after the N chars.
Input:
- text: str,
piece of text to clean
- N: int,
split into strings of 5000 characters each
Output:
- chunks: list,
list containing the string parts
"""
M = len(text)
chunks_num = M // N
if M % N != 0:
chunks_num += 1
chunks = []
end_ind = 0
start_ind = 0
i = 0
while i < chunks_num:
start_ind = end_ind
prob_text = text[start_ind + N: start_ind + N + buffer_]
if '.' in prob_text:
end_ind = start_ind + N + prob_text.index('.')+1
else:
end_ind = start_ind + N
chunks.append(text[start_ind:end_ind])
i += 1
chunks = [ch for ch in chunks if ch]
return chunks
def reverb_wrapper(text, stop=None):
"""
Function-wrapper for ReVerb binary. Extracts relations
found in text.
Input:
- text: str,
a piece of text or sentence
- stop: list,
list of stopwords to remove from the relations
Output:
- total: list,
list of lists. Each inner list contains one relation in the form
[subject, predicate, object]
"""
total = []
for sent in sent_tokenize(text):
cmd = 'echo "' + sent + '"' "| ./reverb -q | tr '\t' '\n' | cat -n"
reverb_dir = settings['load']['path']['reverb']
result = runProcess(cmd, reverb_dir)
# Extract relations from reverb output
result = result[-3:]
result = [row.split('\t')[1].strip('\n') for row in result]
# Remove common stopwords from relations
if stop:
result = [stopw_removal(res, stop) for res in result]
total.append(result)
# Remove empty relations
total = [t for t in total if t]
return total
def cui_to_uri(api_key, cui):
"""
Function to map from cui to uri if possible. Uses biontology portal
Input:
- api_key: str,
api usage key change it in setting.yaml
- cui: str,
cui of the entity we wish to map the uri
Output:
- the uri found in string format or None
"""
REST_URL = "http://data.bioontology.org"
annotations = get_json_with_api(api_key, REST_URL + "/search?include_properties=true&q=" + urllib2.quote(cui))
try:
return annotations['collection'][0]['@id']
except Exception, e:
time_log(Exception)
time_log(e)
return None
def get_json_with_api(api_key, url):
"""
Helper funtion to retrieve a json from a url through urlib2
Input:
- api_key: str,
api usage key change it in setting.yaml
- url: str,
url to curl
Output:
- json-style dictionary with the curl results
"""
opener = urllib2.build_opener()
opener.addheaders = [('Authorization', 'apikey token=' + api_key)]
return json.loads(opener.open(url).read())
def threshold_concepts(concepts, hard_num=3, score=None):
"""
Thresholding concepts from metamap to keep only the most probable ones.
Currently supporting thresholding on the first-N (hard_num) or based on
the concept score.
Input:
- concepts: list,
list of Metamap Class concepts
- hard_num: int,
the first-N concepts to keep, if this thresholidng is selected
- score: float,
lowest accepted concept score, if this thresholidng is selected
"""
if hard_num:
if hard_num >= len(concepts):
return concepts
elif hard_num < len(concepts):
return concepts[:hard_num]
elif score:
return [c for c in concepts if c.score > score]
else:
return concepts
def get_name_concept(concept):
"""
Get name from the metamap concept. Tries different variations and
returns the name found.
Input:
- concept: Metamap class concept, as generated from mmap_extract
for example
Output:
- name: str,
the name found for this concept
"""
name = ''
if hasattr(concept, 'preferred_name'):
name = concept.preferred_name
elif hasattr(concept, 'long_form') and hasattr(concept, 'short_form'):
name = concept.long_form + '|' + concept.short_form
elif hasattr(concept, 'long_form'):
name = concept.long_form
elif hasattr(concept, 'short_form'):
name = concept.short_form
else:
name = 'NO NAME IN CONCEPT'
return name
def metamap_ents(x):
"""
Function to get entities in usable form.
Exctracts metamap concepts first, thresholds them and
tries to extract names and uris for the concepts to be
more usable.
Input:
- x: str,
sentence to extract entities
Output:
- ents: list,
list of entities found. Each entity is a dictionary with
fields id (no. found in sentence), name if retrieved, cui if
available and uri if found
"""
# API KEY to biontology mapping from cui to uri
API_KEY = settings['apis']['biont']
concepts = mmap_extract(x)
concepts = threshold_concepts(concepts)
ents = []
for i, concept in enumerate(concepts):
ent = {}
ent['ent_id'] = i
ent['name'] = get_name_concept(concept)
if hasattr(concept, 'cui'):
ent['cui'] = concept.cui
ent['uri'] = cui_to_uri(API_KEY, ent['cui'])
else:
ent['cui'] = None
ent['uri'] = None
ents.append(ent)
return ents
def extract_entities(text, json_={}):
"""
Extract entities from a given text using metamap and
generate a json, preserving infro regarding the sentence
of each entity that was found. For the time being, we preserve
both concepts and the entities related to them
Input:
- text: str,
a piece of text or sentence
- json_: dic,
sometimes the json to be returned is given to us to be enriched
Defaults to an empty json_
Output:
- json_: dic,
json with fields text, sents, concepts and entities
containg the final results
"""
json_['text'] = text
# Tokenize the text
sents = sent_tokenize(text)
json_['sents'] = [{'sent_id': i, 'sent_text': sent} for i, sent in enumerate(sents)]
json_['concepts'], _ = mmap_extract(text)
json_['entities'] = {}
for i, sent in enumerate(json_['sents']):
ents = metamap_ents(sent)
json_['entities'][sent['sent_id']] = ents
return json_
def extract_metamap(json_, key):
"""
Task function to parse and extract concepts from json_ style dic, using
the MetaMap binary.
Input:
- json_ : dic,
json-style dictionary generated from the Parse object related
to the specific type of input
- key : str,
string denoting the type of medical text to read from. Used to
find the correct paragraph in the settings.yaml file.
Output:
- json_ : dic,
the previous json-style dictionary enriched with medical concepts
"""
# outerfield for the documents in json
docfield = settings['out']['json']['itemfield']
# textfield to read text from
textfield = settings['out']['json']['json_text_field']
N = len(json_[docfield])
for i, doc in enumerate(json_[docfield]):
text = clean_text(doc[textfield])
if len(text) > 5000:
chunks = create_text_batches(text)
results = {'text': text, 'sents': []}
sent_id = 0
for chunk in chunks:
tmp = metamap_wrapper(chunk)
for sent in tmp['sents']:
sent['sent_id'] = sent_id
sent_id += 1
results['sents'].append(sent)
else:
results = metamap_wrapper(text)
json_[docfield][i].update(results)
proc = int(i/float(N)*100)
if proc % 10 == 0 and proc > 0:
time_log('We are at %d/%d documents -- %0.2f %%' % (i, N, proc))
return json_
def enrich_with_triples(results, subject, pred='MENTIONED_IN'):
"""
Enrich with rdf triples a json dictionary in the form of:
entity-URI -- MENTIONED_IN -- 'Text 'Title'. Only entities with
uri's are considered.
Input:
- results: dic,
json-style dictionary genereated from the extract_entities function
- subject: str,
the name of the text document in which the entities are mentioned
- pred: str,
the predicate to be used as a link between the uri and the title
Output:
- results: dic,
the same dictionary with one more
"""
triples = []
for sent_key, ents in results['entities'].iteritems():
for ent in ents:
if ent['uri']:
triples.append({'subj': ent['uri'], 'pred': pred, 'obj': subject})
results['triples'] = triples
return results
def force_to_unicode(text):
"If text is unicode, it is returned as is. If it's str, convert it to Unicode using UTF-8 encoding"
return text if isinstance(text, unicode) else text.decode('utf8', 'ignore')
def toAscii_wrapper(text):
"""
Function wrapper for Lexical Tool toAscii:
https://lexsrv3.nlm.nih.gov/LexSysGroup/Projects/lvg/current/docs/userDoc/tools/toAscii.html
Converts input to ascii ready for SemRep
Input:
- text: str,
a piece of text or sentence'
Output:
- text: str,
the same text with changes
"""
text = clean_text(text)
#text = repr(text)
cmd = 'echo "' + text + '" | ./toAscii'
toAscii_dir = settings['load']['path']['toAscii']
lines = runProcess(cmd, toAscii_dir)
return lines[0]
def semrep_wrapper(text):
"""
Function wrapper for SemRep binary. It is called with flags
-F only and changing this will cause this parsing to fail, cause
the resulting lines won't have the same structure.
Input:
- text: str,
a piece of text or sentence
Output:
- results: dic,
jston-style dictionary with fields text and sents. Each
sentence has entities and relations found in it. Each entity and
each relation has attributes denoted in the corresponding
mappings dictionary.
"""
# Exec the binary
# THIS SHOULD FIX ENCODING PROBLEMS???
text = clean_text(text)
utf8 = force_to_unicode(text)
text = unidecode(utf8)
# text = toAscii_wrapper(text)
# THIS IS NEEDED FOR ANY ARTIFACTS!
text = repr(text)
cmd = "echo " + text + " | ./semrep.v1.7 -L 2015 -Z 2015AA -F"
#print cmd
semrep_dir = settings['load']['path']['semrep']
lines = runProcess(cmd, semrep_dir)
#print(lines)
# mapping of line elements to fields
mappings = {
"text": {
"sent_id": 4,
"sent_text": 6
},
"entity": {
'cuid': 6,
'label': 7,
'sem_types': 8,
'score': 15
},
"relation": {
'subject__cui': 8,
'subject__label': 9,
'subject__sem_types': 10,
'subject__sem_type': 11,
'subject__score': 18,
'predicate__type': 21,
'predicate': 22,
'negation': 23,
'object__cui': 28,
'object__label': 29,
'object__sem_types': 30,
'object__sem_type': 31,
'object__score': 38,
}
}
results = {'sents': [], 'text': text}
for line in lines:
# If Sentence
if line.startswith('SE'):
##### DEPRECATED AS IN CLEAN TEXT WE REMOVE TABS FROM TEXT #######
# Temporary workaround to remove read |-delimited semrep output
# Without mixing up tabs contained in the text
# line = line.replace('\|', '!@#$')
# elements = line.split('|')
# elements = [el.replace('!@#$', '\|') for el in elements]
######################### DEPRECATED ###########################
elements = line.split('|')
# New sentence that was processed
if elements[5] == 'text':
tmp = {"entities": [], "relations": []}
for key, ind in mappings['text'].iteritems():
tmp[key] = elements[ind]
results['sents'].append(tmp)
# A line containing entity info
if elements[5] == 'entity':
tmp = {}
for key, ind in mappings['entity'].iteritems():
if key == 'sem_types':
tmp[key] = elements[ind].split(',')
tmp[key] = elements[ind]
results['sents'][-1]['entities'].append(tmp)
# A line containing relation info
if elements[5] == 'relation':
tmp = {}
for key, ind in mappings['relation'].iteritems():
if 'sem_types' in key:
tmp[key] = elements[ind].split(',')
else:
tmp[key] = elements[ind]
results['sents'][-1]['relations'].append(tmp)
return results
def clean_text(text):
"""
Escape specific characters for command line call of SemRep. This
could be updated in the future to more sophisticated transformations.
Input:
- text: str,
piece of text to clean
Output:
- text: str,
the same text with cmd escaped parenthesis and removing '
"""
replace_chars = [('(', ' '), (')', ' '), ("'", ' '), ('\n', " "), ('\t', ' '), (';', " "),
("}", " "), ("{", " "), ("|", " "), ("&", " "), ("/", ' ')]
for unw_pair in replace_chars:
text = text.replace(unw_pair[0], unw_pair[1])
text = ' '.join(text.split())
return text
def extract_semrep(json_, key):
"""
Task function to parse and extract concepts from json_ style dic, using
the SemRep binary.
Input:
- json_ : dic,
json-style dictionary generated from the Parse object related
to the specific type of input
- key : str,
string denoting the type of medical text to read from. Used to
find the correct paragraph in the settings.yaml file.
Output:
- json_ : dic,
the previous json-style dictionary enriched with medical concepts
"""
# outerfield for the documents in json
if key == 'mongo':
key = 'json'
docfield = settings['out']['json']['itemfield']
# textfield to read text from
textfield = settings['out']['json']['json_text_field']
N = len(json_[docfield])
for i, doc in enumerate(json_[docfield]):
print doc['id']
text = doc[textfield]
if len(text) > 5000:
chunks = create_text_batches(text)
results = {'text': text, 'sents': []}
sent_id = 0
c = 0
for chunk in chunks:
c += 1
tmp = semrep_wrapper(chunk)
for sent in tmp['sents']:
sent['sent_id'] = sent_id
sent_id += 1
results['sents'].append(sent)
else:
results = semrep_wrapper(text)
json_[docfield][i].update(results)
proc = int(i/float(N)*100)
if proc % 10 == 0 and proc > 0:
time_log('We are at %d/%d documents -- %0.2f %%' % (i, N, proc))
return json_
def extract_semrep_parallel(json_, key):
"""
Task function to parse and extract concepts from json_ style dic, using
the SemRep binary. It uses multiprocessing for efficiency.
Input:
- json_ : dic,
json-style dictionary generated from the Parse object related
to the specific type of input
- key : str,
string denoting the type of medical text to read from. Used to
find the correct paragraph in the settings.yaml file.
Output:
- json_ : dic,
the previous json-style dictionary enriched with medical concepts
"""
# outerfield for the documents in json
docfield = settings['out']['json']['itemfield']
N = len(json_[docfield])
try:
N_THREADS = int(settings['num_cores'])
except:
N_THREADS = cpu_count()
batches = chunk_document_collection(json_[docfield], N_THREADS)
len_col = " | ".join([str(len(b)) for b in batches])
time_log('Will break the collection into batches of: %s documents!' % len_col)
batches = [{docfield: batch} for batch in batches]
data = zip(batches, [key for batch in batches])
pool = Pool(N_THREADS, maxtasksperchild=1)
res = pool.map(semrep_parallel_worker, data)
pool.close()
pool.join()
del pool
tmp = {docfield: []}
for batch_res in res:
tmp[docfield].extend(batch_res[docfield])
for i, sub_doc in enumerate(json_[docfield]):
for sub_doc_new in tmp[docfield]:
if sub_doc_new['id'] == sub_doc['id']:
json_[docfield][i].update(sub_doc_new)
break
time_log('Completed multiprocessing extraction!')
return json_
def chunk_document_collection(seq, num):
"""
Helper function to break a collection of N = len(seq) documents
to num batches.
Input:
- seq: list,
a list of documents
- num: int,
number of batches to be broken into. This will usually be
equal to the number of cores available
Output:
- out: list,
a list of lists. Each sublist contains the batch-collection
of documents to be used.
"""
avg = len(seq) / float(num)
out = []
last = 0.0
while last < len(seq):
out.append(seq[int(last):int(last + avg)])
last += avg
return out
def semrep_parallel_worker((json_, key)):
"""
Just a worker interface for the different SemRep
executions.
Input:
- json_ : dic,
json-style dictionary generated from the Parse object related
to the specific type of input
- key : str,
string denoting the type of medical text to read from. Used to
find the correct paragraph in the settings.yaml file.
Output:
- res : dic,
the previous json-style dictionary enriched with medical concepts
"""
res = extract_semrep(json_, key)
return res
def get_concepts_from_edges_parallel(json_, key):
"""
Same work as the get_concepts_from_edges_paralle. It uses multiprocessing
for efficiency.
Input:
- json: dict,
json-style dictionary with a field containing
relations
- key : str,
string denoting the type of medical text to read from. Used to
find the correct paragraph in the settings.yaml file.
Output:
- json: dict,
the updated json-style dictionary where the relations
in the list have been updated and each subject-object has been
mapped to the according
"""
outfield = settings['load'][key]['itemfield']
N = len(json_[outfield])
try:
N_THREADS = int(settings['num_cores'])
except:
N_THREADS = cpu_count()
batches = chunk_document_collection(json_[outfield], N_THREADS)
len_col = " | ".join([str(len(b)) for b in batches])
time_log('Will break the edges into batches of: %s documents!' % len_col)
batches = [{outfield: batch} for batch in batches]
data = zip(batches, [key for batch in batches])
pool = Pool(N_THREADS, maxtasksperchild=1)
res = pool.map(edges_parallel_worker, data)
pool.close()
pool.join()
del pool
json_ = {outfield: []}
for batch_res in res:
json_[outfield].extend(batch_res[outfield])
time_log('Completed multiprocessing extraction!')
return json_
def edges_parallel_worker((json_, key)):
"""
Just a worker interface for the parallel enrichment
executions.
Input:
- json_ : dic,
json-style dictionary generated from the Parse object related
to the specific type of input
- key : str,
string denoting the type of medical text to read from. Used to
find the correct paragraph in the settings.yaml file.
Output:
- res : dic,
expected outcome of get_concepts_from_edges
"""
res = get_concepts_from_edges(json_, key)
return res
def get_concepts_from_edges(json_, key):
"""
Get concept-specific info related to an entity from a list
containing relations. Each subject-object in the relations
list is expressed in a another data source(MESH, DRUGBANK etc)
and their unique identifier is provided. Also, articles and new
kinde of sub-obj are handled.
Input:
- json: dict,
json-style dictionary with a field containing
relations
- key : str,
string denoting the type of medical text to read from. Used to
find the correct paragraph in the settings.yaml file.
Output:
- json: dict,
the updated json-style dictionary where the relations
in the list have been updated and each subject-object has been
mapped to the according
"""
# docfield containing list of elements containing the relations
outfield = settings['load'][key]['itemfield']
# field containing the type of the node for the subject
sub_type = settings['load'][key]['sub_type']
# field containing the source of the node for the subject
sub_source = settings['load'][key]['sub_source']
# field containing the type of the node for the object
obj_type = settings['load'][key]['obj_type']
# field containing the source of the node for the object
obj_source = settings['load'][key]['obj_source']
new_relations = []
uri = settings['load']['mongo']['uri']
db_name = settings['load']['mongo']['db']
collection_name = settings['load']['mongo']['cache_collection']
client = pymongo.MongoClient(uri)
db = client[db_name]
collection = db[collection_name]
cur = collection.find({})
cache = {}
for item in cur:
cache[item['key']] = item['value']
N = len(json_[outfield])
for ii, triple in enumerate(json_[outfield]):
print triple
try:
if sub_source == 'UMLS':
if not(triple['s'] in cache):
ent = get_concept_from_cui(triple['s'])
cache[triple['s']] = ent
collection.insert_one({'key':triple['s'],'value':ent})
print 'INSERTED in UMLS %s' % triple['s']
else:
ent = cache[triple['s']]
if (type(ent['sem_types']) == list and len(ent['sem_types']) > 1):
sem_types = ';'.join(ent['sem_types'])
elif (',' in ent['sem_types']):
sem_types = ';'.join(ent['sem_types'].split(','))
else:
sem_types = ent['sem_types']
triple_subj = [{'id:ID': ent['cuid'],
'label': ent['label'],
'sem_types:string[]': sem_types}]
elif (sub_source == 'PMC') or (sub_source == 'TEXT') or (sub_source == 'None'):
triple_subj = [{'id:ID': triple['s']}]
else:
if not(triple['s'] in cache):
ents = get_concept_from_source(triple['s'], sub_source)
cache[triple['s']] = ents
collection.insert_one({'key':triple['s'],'value':ents})
print 'INSERTED in other %s' % triple['s']
else:
ents = cache[triple['s']]
triple_subj = []
for ent in ents:
if (type(ent['sem_types']) == list and len(ent['sem_types']) > 1):
sem_types = ';'.join(ent['sem_types'])
elif (',' in ent['sem_types']):
sem_types = ';'.join(ent['sem_types'].split(','))
else:
sem_types = ent['sem_types']
triple_subj.append({'id:ID': ent['cuid'],
'label': ent['label'],
'sem_types:string[]': sem_types})
if obj_source == 'UMLS':
if not(triple['o'] in cache):
ent = get_concept_from_cui(triple['o'])
cache[triple['o']] = ent
collection.insert_one({'key':triple['o'],'value':ent})
print 'INSERTED in UMLS %s' % triple['o']
else:
ent = cache[triple['o']]
if (type(ent['sem_types']) == list and len(ent['sem_types']) > 1):
sem_types = ';'.join(ent['sem_types'])
elif (',' in ent['sem_types']):
sem_types = ';'.join(ent['sem_types'].split(','))
else:
sem_types = ent['sem_types']
triple_obj = [{'id:ID': ent['cuid'],
'label': ent['label'],
'sem_types:string[]': sem_types}]
elif (obj_source == 'PMC') or (obj_source == 'TEXT') or (obj_source == 'None'):
triple_obj = [{'id:ID': triple['o']}]
else:
if not(triple['o'] in cache):
ents = get_concept_from_source(triple['o'], obj_source)
cache[triple['o']] = ents
collection.insert_one({'key':triple['o'],'value':ents})
print 'INSERTED in other %s' % triple['o']
else:
ents = cache[triple['o']]
triple_obj = []
for ent in ents:
if (type(ent['sem_types']) == list and len(ent['sem_types']) > 1):
sem_types = ';'.join(ent['sem_types'])
elif (',' in ent['sem_types']):
sem_types = ';'.join(ent['sem_types'].split(','))
else:
sem_types = ent['sem_types']
triple_obj.append({'id:ID': ent['cuid'],
'label': ent['label'],
'sem_types:string[]': sem_types})
combs = product(triple_subj, triple_obj)
for comb in combs:
new_relations.append({'s':comb[0], 'p':triple['p'], 'o':comb[1]})
except Exception, e:
time_log(e)
time_log('S: %s | P: %s | O: %s' % (triple['s'],triple['p'],triple['o']))
time_log('Skipped the above edge! Probably due to concept-fetching errors!')
proc = int(ii/float(N)*100)
if proc % 10 == 0 and proc > 0:
time_log('We are at %d/%d edges transformed -- %0.2f %%' % (ii, N, proc))
# if ii % 100 == 0 and ii > 9:
# time_log("Edges Transformation Process: %d -- %0.2f %%" % (ii, 100*ii/float(len(json_[outfield]))))
json_[outfield] = new_relations
return json_