23
23
}}
24
24
"""
25
25
26
+ subconcepts_only_query_tmp = """
27
+ SELECT ?inst_full WHERE {{
28
+ <http://linkedlifedata.com/resource/umls/id/{}> <http://www.w3.org/2004/02/skos/core#narrower> ?inst_full .
29
+ }}
30
+ """
31
+
26
32
# mimir query template
27
33
mimir_query_temp = """
28
34
{{Mention sparql = "{}"}}
@@ -76,7 +82,8 @@ def generate_prospector_query(concept_id, sparql_only=None):
76
82
77
83
# query to get all instances of a concept
78
84
def query_instances (concept_id ):
79
- q = generate_prospector_query (concept_id , sparql_only = True )
85
+ # q = generate_prospector_query(concept_id, sparql_only=True)
86
+ q = subconcepts_only_query_tmp .format (concept_id )
80
87
# print q
81
88
ret = query_result (q )
82
89
return [r ['inst_full' ]['value' ] for r in ret ]
@@ -89,9 +96,10 @@ def get_all_instances(save_file):
89
96
if concepts [c ] == '' :
90
97
continue
91
98
insts = query_instances (concepts [c ])
99
+ insts = [concepts [c ]] + insts
92
100
print u'{}\t {}\t {}\t {}' .format (c , concepts [c ], len (insts ), json .dumps (insts ))
93
101
for cid in insts :
94
- concpet2subconcepts_csv += u'"{}", "{}" \n ' .format (c , cid [cid .rfind ('/' )+ 1 :])
102
+ concpet2subconcepts_csv += u'{}, {} \n ' .format (c , cid [cid .rfind ('/' )+ 1 :])
95
103
if save_file is not None :
96
104
utils .save_string (concpet2subconcepts_csv , save_file )
97
105
@@ -110,9 +118,26 @@ def generate_all_queries():
110
118
utils .save_json_array (concept2queries , './resources/mimir_queries.json' )
111
119
112
120
121
+ def get_concept_label (concept_id ):
122
+ query2 = """
123
+ select ?label where {{
124
+ <http://linkedlifedata.com/resource/umls/id/{}> <http://www.w3.org/2008/05/skos-xl#prefLabel> ?labelObj .
125
+ ?labelObj <http://www.w3.org/2008/05/skos-xl#literalForm> ?label .
126
+ FILTER ( lang(?label) = "en" )
127
+ }}
128
+ """ .format (concept_id )
129
+ ret = query_result (query2 )
130
+ labels = [r ['label' ]['value' ] for r in ret ]
131
+
132
+ if len (labels ) > 0 :
133
+ return labels [0 ]
134
+ else :
135
+ return None
136
+
113
137
def main ():
114
138
# generate_all_queries()
115
- get_all_instances ('./resources/all_insts.csv' )
139
+ # get_all_instances('./resources/all_insts.csv')
140
+ print get_concept_label ('C0018799' )
116
141
117
142
if __name__ == "__main__" :
118
143
main ()
0 commit comments