-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNHANES_extractor.py
376 lines (351 loc) · 22.9 KB
/
NHANES_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
from bs4 import BeautifulSoup
import urllib
import requests
import os
import re
import csv
import urllib2
import time
start_time=time.time()
if not os.path.exists("codebook"):
os.makedirs("codebook")
if not os.path.exists("sdd"):
os.makedirs("sdd")
code_mappings_url = 'https://raw.githubusercontent.com/tetherless-world/chear-ontology/master/code_mappings.csv'
code_mappings_response = urllib2.urlopen(code_mappings_url)
code_mappings_reader = csv.reader(code_mappings_response)
unit_code_list = []
unit_uri_list = []
unit_label_list = []
for code_row in code_mappings_reader :
unit_code_list.append(code_row[0])
unit_uri_list.append(code_row[1])
unit_label_list.append(code_row[2])
#r2013 = urllib.urlopen('https://wwwn.cdc.gov/Nchs/Nhanes/continousnhanes/default.aspx?BeginYear=2013')
begin_year = 2013
r = requests.get("https://wwwn.cdc.gov/nchs/nhanes/continuousnhanes/default.aspx?BeginYear=" + begin_year.__str__())
data = r.text
soup = BeautifulSoup(data, "lxml")
list_items = soup.find("h3",text="Data, Documentation, Codebooks, SAS Code").findNext("ul").findAll("li")
for item in list_items :
link = "https://wwwn.cdc.gov" + item.findNext("a").get("href")
print "\tPortal: " + link
portal_soup = BeautifulSoup(requests.get(link).text, "lxml")
variable_link = "https://wwwn.cdc.gov/nchs/nhanes/search/" + portal_soup.find("span",id="lblVarlist").findNext("a").get("href")
print "\tVariable List: " + variable_link
#variable_soup = BeautifulSoup(requests.get(variable_link).text, "lxml")
subfolder_name = item.findNext("a").contents[0].replace(" ","_")
if not os.path.exists("codebook/" + subfolder_name) :
os.makedirs("codebook/" + subfolder_name)
if not os.path.exists("sdd/" + subfolder_name) :
os.makedirs("sdd/" + subfolder_name)
doc_rows = portal_soup.find("table",id="GridView1").find("tbody").findAll("tr")
for row in doc_rows:
if not (row.find("a").get("href") is "#"):
doc_link = "https://wwwn.cdc.gov" + row.find("a").get("href")
print "\tDocument: " + doc_link
doc_soup = BeautifulSoup(requests.get(doc_link).text, "lxml")
try:
sdd_fn="_".join(row.find("a").contents[0].split())+ "-SDD.csv"
print "\t\tWriting to sdd/" + subfolder_name + "/" + sdd_fn
sdd = open("sdd/" + subfolder_name + "/" + sdd_fn,"w")
sdd.write("Column,Label,Comment,Note,Target,Attribute,attributeOf,Unit,Time,Entity,Role,Relation,inRelationTo,wasDerivedFrom,wasGeneratedBy,hasPosition\n")
codebook_fn="_".join(row.find("a").contents[0].split())+ "-CB.csv"
print "\t\tWriting to codebook/" + subfolder_name + "/" + codebook_fn
codebook = open("codebook/" + subfolder_name + "/" + codebook_fn,"w")
codebook.write("Column,Variable,Label\n")
entries=doc_soup.find("div", id="Codebook").findAll("div")
for entry in entries :
#try:
# print "\t\t" + entry.find("dl").find("dt",text="Variable Name: ").findNext("dd").contents[0] + " - " + entry.find("dl").find("dt",text="English Text: ").findNext("dd").contents[0]
#except:
# try:
# print "\t\t" + entry.find("dl").find("dt",text="Variable Name: ").findNext("dd").contents[0] + " - " + entry.find("dl").find("dt",text="SAS Label: ").findNext("dd").contents[0]
# except:
# print "\t\t" + entry.find("dl").find("dt",text="Variable Name: ").findNext("dd").contents[0]
# Insert Semantic Data Dictionary Entries
columnVal = entry.find("dl").find("dt",text="Variable Name: ").findNext("dd").contents[0].replace("\t","").replace("\n","").replace("\r","")
labelVal = ""
commentVal = ""
noteVal = ""
targetVal = ""
attributeVal = ""
attributeOfVal = ""
unitVal = ""
timeVal = ""
entityVal = ""
roleVal=""
relationVal = ""
inRelationToVal = ""
wasDerivedFromVal = ""
wasGeneratedByVal = ""
hasPositionVal = ""
try :
labelVal = '"' + entry.find("dl").find("dt",text="SAS Label: ").findNext("dd").contents[0].encode('ascii','ignore').decode('utf-8').replace("\"","'") + '"'
except :
# Don't update labelVal
pass
#labelVal = ""
try :
commentVal = '"' + entry.find("dl").find("dt",text="English Text: ").findNext("dd").contents[0].encode('ascii','ignore').decode('utf-8').replace("\"","'").replace("\t","").replace("\n","").replace("\r","") + '"'
except :
# Don't update commentVal
pass
#commentVal = ""
try :
noteVal = '"' + entry.find("dl").find("dt",text="English Instructions: ").findNext("dd").contents[0].encode('ascii','ignore').decode('utf-8').replace("\"","'").replace("\t","").replace("\n","").replace("\r","") + '"'
except :
# Don't update noteVal
pass
#noteVal = ""
try :
targetVal = '"' + entry.find("dl").find("dt",text="Target: ").findNext("dd").contents[0].replace("\t","").replace("\n","").replace("\r"," ") + '"'
except :
# Don't update targetVal
pass
#targetVal = ""
try :
#if(("age" in labelVal.lower()) or ("age" in commentVal.lower()) or ("age" in noteVal.lower())) :
if("age" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:Age"
if("weight" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="chear:Weight"
if("height" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:Height"
if("race" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:Race"
if("ethnic" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:Ethnicity"
if("circumference" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:Circumference"
if("status" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:StatusDescriptor"
if("education" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="chear:EducationLevel"
if("language" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="chear:Language"
if("income" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="chear:Income"
if("country" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:Country"
if(("#" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("number of" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
attributeVal="sio:Quantity"
if("ratio" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:Ratio"
if("time" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeVal="sio:TimeInterval"
except :
# Don't update attributeVal
pass
#attributeVal = ""
try :
if("child" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeOfVal = "??child"
if("mother" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeOfVal = "??mother"
if("father" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
attributeOfVal = "??father"
except :
# Don't update attributeOfVal
pass
#attributeOfVal = ""
try :
if (("gram" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(g)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000021"
if (("milligram" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(mg)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000022"
if (("microgram" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(ug)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000023"
if (("meter" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(m)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000008"
if (("kilogram" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(kg)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000009"
if(("second" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))or ("(s)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000010"
if (("centimeter" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(cm)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000015"
if (("millimeter" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(mm)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000016"
if(("millisecond" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(ms)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000028"
if(("microsecond" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(us)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000029"
if(("picosecond" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) or ("(ps)" in (labelVal.lower() or commentVal.lower() or noteVal.lower()))) :
unitVal = "obo:UO_0000030"
if("minute" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unitVal = "obo:UO_0000031"
if("hour" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unitVal = "obo:UO_0000032"
if("day" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unitVal = "obo:UO_0000033"
if("week" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unitVal = "obo:UO_0000034"
if("month" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unitVal = "obo:UO_0000035"
if("year" in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unitVal = "obo:UO_0000036"
try:
for unit_label in unit_label_list :
if (unit_label.lower() in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unit_index = unit_label_list.index(unit_label)
unitVal = unit_uri_list[unit_index]
for unit_code in unit_code_list :
unitCode = "(" + unit_code.lower() + ")"
if (unitCode in (labelVal.lower() or commentVal.lower() or noteVal.lower())) :
unit_index = unit_code_list.index(unit_code)
unitVal = unit_uri_list[unit_index]
#print unit_uri_list[unit_index]
except :
print "Somthing went wrong.."
except :
# Don't update unitVal
pass
#unitVal = ""
try :
timeVal = ""
except :
# Don't update timeVal
pass
#timeVal = ""
try :
entityVal = ""
except :
# Don't update entityVal
pass
#entityVal = ""
try :
relationVal = ""
except :
# Don't update relationVal
pass
#relationVal = ""
try :
inRelationToVal = ""
except :
# Don't update inrelationToVal
pass
#inRelationToVal = ""
try :
wasDerivedFromVal = ""
except :
# Don't update wasDerivedFromVal
pass
#wasDerivedFromVal = ""
try :
wasGeneratedByVal = ""
except :
# Don't update wasGeneratedByVal
pass
#wasGeneratedByVal = ""
try :
hasPositionVal = ""
except :
# Don't update hasPositionVal
pass
#hasPositionVal = ""
sdd.write(columnVal + "," + labelVal + "," + commentVal + "," + noteVal + "," + targetVal + "," + attributeVal + "," + attributeOfVal + "," + unitVal + "," + timeVal + "," + entityVal + "," + roleVal + "," + relationVal + "," + inRelationToVal + "," + wasDerivedFromVal + "," + wasGeneratedByVal + "," + hasPositionVal + "\n")
tables = entry.findAll("table")
# Insert Codebook Entries
for table in tables :
table_rows = table.find("tbody").findAll("tr")
for table_row in table_rows :
codebook.write(entry.find("dl").find("dt",text="Variable Name: ").findNext("dd").contents[0].replace(" ","") + "," + table_row.find("td",scope="row").contents[0].encode('ascii','ignore').decode('utf-8') + ',"' + table_row.find("td",scope="row").findNext("td").contents[0].encode('ascii','ignore').decode('utf-8').replace("\"","'") + '"\n')
sdd.close()
codebook.close()
except:
print "\t\tNo codebook found for document"
print ""
print("The extractor took %s seconds to run" % (time.time() - start_time))
# demographics_link = "https://wwwn.cdc.gov" + soup.find("a",text="Demographics").get("href")
# print demographics_link
# demographics_soup = BeautifulSoup(requests.get(demographics_link).text, "lxml")
# #print demographics_soup.prettify()[0:1000]
# demographics_variable_link = "https://wwwn.cdc.gov/nchs/nhanes/search/" + demographics_soup.find("span",id="lblVarlist").findNext("a").get("href")
# print demographics_variable_link
# demographics_variable_soup = BeautifulSoup(requests.get(demographics_variable_link).text, "lxml")
# #print demographics_variable_soup.prettify()[0:1000]
# demographics_doc_rows = demographics_soup.find("table",id="GridView1").find("tbody").findAll("tr")
# for row in demographics_doc_rows:
# if not (row.find("a").get("href") is "#"):
# demographics_doc_link = "https://wwwn.cdc.gov" + row.find("a").get("href")
# print demographics_doc_link
# demographics_doc_soup = BeautifulSoup(requests.get(demographics_doc_link).text, "lxml")
# entries=demographics_doc_soup.find("div", id="Codebook").findAll("div")
# for entry in entries :
# #print "Column: " + entry.find("dl").find("dt",text="Variable Name: ").findNext("dd").contents[0]
# #print "label: " + entry.find("dl").find("dt",text="SAS Label: ").findNext("dd").contents[0]
# tables = entry.findAll("table")
# for table in tables :
# table_rows = table.find("tbody").findAll("tr")
# for table_row in table_rows :
# print entry.find("dl").find("dt",text="Variable Name: ").findNext("dd").contents[0] + "," + table_row.find("td",scope="row").contents[0] + "," + table_row.find("td",scope="row").findNext("td").contents[0]
# dietary_link = "https://wwwn.cdc.gov" + soup.find("a",text="Dietary").get("href")
# print dietary_link
# dietary_soup = BeautifulSoup(requests.get(dietary_link).text, "lxml")
# #print dietary_soup.prettify()[0:1000]
# dietary_variable_link = "https://wwwn.cdc.gov/nchs/nhanes/search/" + dietary_soup.find("span",id="lblVarlist").findNext("a").get("href")
# print dietary_variable_link
# dietary_variable_soup = BeautifulSoup(requests.get(dietary_variable_link).text, "lxml")
# #print dietary_variable_soup.prettify()[0:1000]
# dietary_doc_rows = dietary_soup.find("table",id="GridView1").find("tbody").findAll("tr")
# for row in dietary_doc_rows:
# if not (row.find("a").get("href") is "#"):
# dietary_doc_link = "https://wwwn.cdc.gov" + row.find("a").get("href")
# print dietary_doc_link
# dietary_doc_soup = BeautifulSoup(requests.get(dietary_doc_link).text, "lxml")
# examination_link = "https://wwwn.cdc.gov" + soup.find("a",text="Examination").get("href")
# print examination_link
# examination_soup = BeautifulSoup(requests.get(examination_link).text, "lxml")
# #print examination_soup.prettify()[0:1000]
# examination_variable_link = "https://wwwn.cdc.gov/nchs/nhanes/search/" + examination_soup.find("span",id="lblVarlist").findNext("a").get("href")
# print examination_variable_link
# examination_variable_soup = BeautifulSoup(requests.get(examination_variable_link).text, "lxml")
# #print examination_variable_soup.prettify()[0:1000]
# examination_doc_rows = examination_soup.find("table",id="GridView1").find("tbody").findAll("tr")
# for row in examination_doc_rows:
# if not (row.find("a").get("href") is "#"):
# examination_doc_link = "https://wwwn.cdc.gov" + row.find("a").get("href")
# print examination_doc_link
# examination_doc_soup = BeautifulSoup(requests.get(examination_doc_link).text, "lxml")
# laboratory_link = "https://wwwn.cdc.gov" + soup.find("a",text="Laboratory").get("href")
# print laboratory_link
# laboratory_soup = BeautifulSoup(requests.get(laboratory_link).text, "lxml")
# #print laboratory_soup.prettify()[0:1000]
# laboratory_variable_link = "https://wwwn.cdc.gov/nchs/nhanes/search/" + laboratory_soup.find("span",id="lblVarlist").findNext("a").get("href")
# print laboratory_variable_link
# laboratory_variable_soup = BeautifulSoup(requests.get(laboratory_variable_link).text, "lxml")
# #print laboratory_variable_soup.prettify()[0:1000]
# laboratory_doc_rows = laboratory_soup.find("table",id="GridView1").find("tbody").findAll("tr")
# for row in laboratory_doc_rows:
# if not (row.find("a").get("href") is "#"):
# laboratory_doc_link = "https://wwwn.cdc.gov" + row.find("a").get("href")
# print laboratory_doc_link
# laboratory_doc_soup = BeautifulSoup(requests.get(laboratory_doc_link).text, "lxml")
# questionnaire_link = "https://wwwn.cdc.gov" + soup.find("a",text="Questionnaire").get("href")
# print questionnaire_link
# questionnaire_soup = BeautifulSoup(requests.get(questionnaire_link).text, "lxml")
# #print questionnaire_soup.prettify()[0:1000]
# questionnaire_variable_link = "https://wwwn.cdc.gov/nchs/nhanes/search/" + questionnaire_soup.find("span",id="lblVarlist").findNext("a").get("href")
# print questionnaire_variable_link
# questionnaire_variable_soup = BeautifulSoup(requests.get(questionnaire_variable_link).text, "lxml")
# #print questionnaire_variable_soup.prettify()[0:1000]
# questionnaire_doc_rows = questionnaire_soup.find("table",id="GridView1").find("tbody").findAll("tr")
# for row in questionnaire_doc_rows:
# if not (row.find("a").get("href") is "#"):
# questionnaire_doc_link = "https://wwwn.cdc.gov" + row.find("a").get("href")
# print questionnaire_doc_link
# questionnaire_doc_soup = BeautifulSoup(requests.get(questionnaire_doc_link).text, "lxml")
# limited_access_link= "https://wwwn.cdc.gov" + soup.find("a",text="Limited Access").get("href")
# print limited_access_link
# limited_access_soup = BeautifulSoup(requests.get(limited_access_link).text, "lxml")
# #print limited_access_soup.prettify()[0:1000]
# limited_access_variable_link = "https://wwwn.cdc.gov/nchs/nhanes/search/" + limited_access_soup.find("span",id="lblVarlist").findNext("a").get("href")
# print limited_access_variable_link
# limited_access_variable_soup = BeautifulSoup(requests.get(limited_access_variable_link).text, "lxml")
# #print limited_access_variable_soup.prettify()[0:1000]
# limited_access_doc_rows = limited_access_soup.find("table",id="GridView1").find("tbody").findAll("tr")
# for row in limited_access_doc_rows:
# if not (row.find("a").get("href") is "#"):
# limited_access_doc_link = "https://wwwn.cdc.gov" + row.find("a").get("href")
# print limited_access_doc_link
# limited_access_doc_soup = BeautifulSoup(requests.get(limited_access_doc_link).text, "lxml")