-
Notifications
You must be signed in to change notification settings - Fork 2
/
UKBscrape.py
230 lines (180 loc) · 8.44 KB
/
UKBscrape.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
It is function to scrape the UKBiobank website:
1) It helps you find intersection between variables available through your
application and (almost) all other UKB variables.
2) It can act as a dictionary, you can pass a variable Field ID and it will
return the variable name and its category (by category I mean *UKbiobank*
pre-defined categories.)
To kick start do the following:
import UKBscrape as ukbs
ukbs.update()
You need to be connected to internet for ukbs.update() to work. Once you
have done it, there is a new sub-directory ~/IDLabCat which contains
scrapped text files from the UKBiobank.
See usage/help for individual functions.
Created on Mon Jan 7 14:32:45 2019
@author: sorooshafyouni
University of Oxford, 2019
"""
################################################################################################
################################################################################################
import re
import numpy as np
import os
import pandas as pd
def update():
"""
REQUIREMENTS:
packages: os
This function uses inner bash script 'UKBscrape.sh' to scrape UKBB website.
Need to be called on regularly bases to be updated with the latest version
of variables.
SA, Ox, 2019
"""
import os
if not os.path.isdir("IDLabCat"):
os.makedirs('IDLabCat')
os.system('sh UKBscrape.sh')
print('==DONE UPDATES==')
return
################################################################################################
################################################################################################
def match(UKBCSVFile = 'ukb25120_Headers.txt',\
Report2 = ''):
"""
ukbs.match(UKBCSVFile = '',Report2 = '')
REQUIREMENTS:
packages: os, re, numpy
INPUTS:
UKBCSVFile: path to a text file which contains *your* UKBiobank headers.
On you linux/Mac terminal do as following to make such file:
$ head -n 1 /path/2/your/UKB/ukbXXXXX.csv > ukbXXXX_Headers.txt
Report2 [optional]: if you need to save the reports, set a path for the results.
This will produce two csv files:
YouDontHave.csv, FieldID, label and category of the variables that you don't have
WasntParsed.csv, the variables that may have missed in parsing but your application has!
SA, Ox, 2019
"""
# 1) Should check the match folder, it creates a dictory a / before the target
# 2) Also, skip if the field doesn't exist in the UKB scrapped.
#######INPUT CHECK ##############
if UKBCSVFile == '':
print('HALTED: Specify the path to your UKBB CSV file...')
return
if not os.path.isdir("IDLabCat"):
update()
print('You requested a match between your current version of UKBB variables at:\n'\
+ str(UKBCSVFile) + ' \n and the UKB available data at:\nhttps://biobank.ctsu.ox.ac.uk/crystal/list.cgi')
################################
# Extract what we have:
#UKBCSVFile = 'ukb25120_Headers.txt';
ScrapedCSVFile = 'IDLabCat/IDs_All.txt';
WeHave = open(UKBCSVFile, 'r').read()
A = WeHave.split(sep=',');
IDRegEx = '^\"([0-9]*).*\"'
IDsWeHave = list()
for i in range(0,np.size(A)):
IDsWeHave.append(re.findall(IDRegEx,A[i])[0])
del IDsWeHave[0] # because the first element is always eid, right?
IDsWeHave = list(set(IDsWeHave))
################################
UKBHave = open(ScrapedCSVFile, 'r').read()
IDsUKBHave = UKBHave.split(sep='\n')
WhatWeDontHave = list(set(IDsUKBHave)-set(IDsWeHave))
WhatUKBDontHave = list(set(IDsWeHave)-set(IDsUKBHave))
if Report2!='':
Report2_dir = os.path.dirname(Report2)
Report2_dir = Report2_dir + '/matchreport/';
dictionary(UKBCSVFile=UKBCSVFile, FieldID = WhatWeDontHave, SaveMe2=Report2_dir + 'YouDontHave.csv')
dictionary(UKBCSVFile=UKBCSVFile, FieldID = WhatUKBDontHave, SaveMe2=Report2_dir + 'WasntParsed.csv')
return WhatWeDontHave,WhatUKBDontHave
################################################################################################
################################################################################################
def dictionary(\
UKBCSVFile = 'ukb25120_Headers.txt',\
FieldID = '',\
SaveMe2 = ''):
"""
ukbs.dictionary(UKBCSVFile = '', FieldID = '' ,SaveMe2 = '')
REQUIREMENTS:
packages: os, numpy, pandas
INPUTS:
UKBCSVFile: path to a text file which contains *your* UKBiobank headers.
On you linux/Mac terminal do as following to make such file:
$ head -n 1 /path/2/your/UKB/ukbXXXXX.csv > ukbXXXX_Headers.txt
FieldID: Field IDs as a list of strings.
For example, labels and categories for field IDs 4056 & 26412 can be found as following:
In [80]: ukbs.dictionary(UKBCSVFile = 'ukb25120_Headers.txt',FieldID=['4056','26412']);
You requested dictionary for 2 data fields.
Label Category
4056 Age stroke diagnosed Medical conditions
26412 Employment score (England) Indices of Multiple Deprivation
SaveMe2 [optional]: path to a directory to save the results as tsv file.
saves the FieldID, labels and categories of passed FieldIDs
SA, Ox, 2019
"""
#######INPUT CHECK ##############
if FieldID=='':
print('HALTED: You have to specify which data field you need...')
return
headPath, tailName = os.path.split(UKBCSVFile)
if headPath != '':
headPath = headPath + '/'
print("I am looking at this directory: " + headPath)
if not os.path.isdir(headPath + "IDLabCat"):
update()
print('You requested dictionary for ' + str(np.size(FieldID)) + ' data fields.')
################################
DataDict = pd.DataFrame(index=FieldID,columns=('Label','Category','WeHave'))
# Can't I just simply use np.loadtxt?!
ScrapedCSVFile = headPath + 'IDLabCat/IDs_All.txt';
UKBHave = open(ScrapedCSVFile, 'r').read()
IDsUKBHave = UKBHave.split(sep='\n')
Path2CatFile = headPath + 'IDLabCat/Cats_All.txt'
CatFile = open(Path2CatFile, 'r').read()
Cats = CatFile.split(sep='\n')
Path2LabelFile = headPath + 'IDLabCat/Labels_All.txt'
LabFile = open(Path2LabelFile, 'r').read()
Labs = LabFile.split(sep='\n')
for i in range(0,np.size(FieldID)):
WeHaveFlag = False
if not (FieldID[i] in IDsUKBHave):
print('*****Field ID: ' + str(FieldID[i]) + ' is missing in the UKBB scrapped files.*****')
Cats_tmp = np.nan
Labs_tmp = np.nan
else:
Idx = IDsUKBHave.index(FieldID[i])
Cats_tmp = Cats[Idx]
Labs_tmp = Labs[Idx]
################################
# Extract what we have:
#UKBCSVFile = 'ukb25120_Headers.txt';
ScrapedCSVFile = headPath + 'IDLabCat/IDs_All.txt';
WeHave = open(UKBCSVFile, 'r').read()
A = WeHave.split(sep=',');
IDRegEx = '^\"([0-9]*).*\"'
IDsWeHave = list()
for ii in range(0,np.size(A)):
IDsWeHave.append(re.findall(IDRegEx,A[ii])[0])
del IDsWeHave[0] # because the first element is always eid, right?
IDsWeHave = list(set(IDsWeHave))
################################
if FieldID[i] in IDsWeHave:
WeHaveFlag = True
DataDict['Category'][i] = Cats_tmp
DataDict['Label'][i] = Labs_tmp
DataDict['WeHave'][i] = WeHaveFlag
print(DataDict)
if SaveMe2!='':
if not os.path.isdir(SaveMe2):
print('Report dictory was created: ' + str(SaveMe2))
SaveMe2_dir = os.path.dirname(SaveMe2)
os.makedirs(SaveMe2_dir,exist_ok=True)
DataDict.to_csv(SaveMe2)
return DataDict