-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo_tools.py
638 lines (402 loc) · 17.5 KB
/
geo_tools.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
"""A module that makes downloading and using datasets from the Gene Expression Omnibus (GEO) easy.
Author: Joshua Blanchard, jeblanchard@berkeley.edu
Currently has limited functionality."""
# In[2]:
import pandas as pd
import os
import numpy as np
import re as re
import xmltodict
import ftplib
import tarfile
import pickle
import gzip
# In[3]:
__base_path = "data"
# In[4]:
__hidden_path = ".aidp_files"
# In[5]:
if not os.path.exists(__base_path):
os.mkdir(__base_path)
if not os.path.exists(__hidden_path):
os.mkdir(__hidden_path)
# In[6]:
def __family_path(GSE_family):
"""Exists to build paths to the family's directory.
I downloaded the files using the download() function in conjuction with the extract() function."""
return os.path.join(__base_path, GSE_family + "/")
# In[7]:
def __clean(file_path):
"""Cleans a given .txt file.
Returns a dictionary:
"site": first column
"measurement": second column
"bad_rows": list of all the invalid rows"""
valid_rows = []
not_valid_rows = []
file = open(file_path, 'r')
for line in file:
# checks for only the first two columns
line_match = re.match(r"\S+\t\S+", line)
if line_match:
valid_rows.append(line_match.group(0))
else:
not_valid_rows.append(line)
file.close()
# now let's split our valid_rows list into two lists, one for each column
col_1 = []
col_2 = []
for row in valid_rows:
row_match = re.match(r"(\S+)\t(\S+)", row)
col_1.append(row_match.group(1))
col_2.append(row_match.group(2))
return {"col_1":col_1, "col_2":col_2, "bad_rows":not_valid_rows}
# In[8]:
def __load_file(file_directory):
# clean data file
# convert to a dataframe object and return
"""Given a file name will output a corresponding pandas.DataFrame object."""
try:
clean_dict = __clean(file_directory)
except PermissionError:
print("You likely inputted the path of a directory, not a file.")
# return pd.DataFrame({"site": clean_dict["col_1"], "measurement": clean_dict["col_2"]})
return pd.DataFrame(data= clean_dict["col_2"], index= clean_dict["col_1"], columns= ["measurement"])
# In[9]:
def family_dict(GSE_family):
"""*** DEPRECATED ***
Given a family ID, will output a dictionary. Keys will be the sample IDs, values will be the corresponding
pandas.DataFrame object."""
# let's check if we already have this dictionary saved
if not os.path.exists("./" + __hidden_path):
os.mkdir(__hidden_path)
dict_path = __hidden_path + "/" + GSE_family + "_dict"
if not os.path.exists(dict_path):
family_directory = __family_path(GSE_family)
total_list = os.listdir(family_directory)
valid_files = []
for file_name in total_list:
match = re.match(r"GSM", file_name)
if match:
valid_files.append(file_name)
family_dict = {}
for file_name in valid_files:
file_df = __load_file(os.path.join(family_directory, file_name))
sample_id = re.match(r"GSM\d+", file_name).group(0)
family_dict[sample_id] = file_df
dict_file = open(dict_path, 'wb')
pickle.dump(family_dict, dict_file)
dict_file.close()
else:
dict_file = open(dict_path, 'rb')
family_dict = pickle.load(dict_file)
dict_file.close()
return family_dict
# In[10]:
def __matrix_helper(file_path, use= "series()"):
"""Returns a tuple containing the start line for reading (0) and the number of rows to read (1).
Possible values for use: "series()", "info()"
"""
file = open(file_path, mode= 'r', errors= 'replace')
line_num = 0
if use == "series()":
while True:
line = file.readline()
if "!series_matrix_table_begin" in line:
start_row = line_num
elif "!series_matrix_table_end" in line:
end_row = line_num - 1
elif line == "":
break
line_num += 1
else:
while True:
line = file.readline()
if "!Sample_title" in line:
start_row = line_num
elif "!series_matrix_table_begin" in line:
end_row = line_num - 1
elif line == "":
break
line_num += 1
num_rows = end_row - start_row - 1
return start_row, num_rows
# In[11]:
def __matrix_to_df(file_path, use= "series()", GSE= ""):
"""Returns the pandas.dataframe corresponding to the series_matrix file.
Possible values for use: "series()", "info()"
"""
if use == "series()":
series_path = os.path.join(__hidden_path + "/" + GSE + "/" + "series_df")
if os.path.exists(series_path):
df_file = open(series_path, "rb")
df = pickle.load(df_file)
df_file.close()
else:
start_row, num_rows = __matrix_helper(file_path, use)
df = pd.read_csv(file_path, header= start_row, sep= "\t", low_memory= False, nrows= num_rows)
df.set_index("ID_REF", inplace= True)
GSE_dir = os.path.join(__hidden_path + "/" + GSE)
if not os.path.exists(GSE_dir):
os.mkdir(GSE_dir)
df_file = open(series_path, 'wb')
pickle.dump(df, df_file)
df_file.close()
elif use == "info()":
info_path = os.path.join(__hidden_path + "/" + GSE + "/" + "info_df")
if os.path.exists(info_path):
info_df_file = open(info_path, "rb")
df = pickle.load(info_df_file)
info_df_file.close()
else:
start_row, num_rows = __matrix_helper(file_path, use)
df = pd.read_csv(file_path, header= start_row, sep= "\t", low_memory= False, nrows= num_rows)
df.set_index("!Sample_geo_accession", inplace= True)
df = df.loc["!Sample_characteristics_ch1"]
GSE_dir = os.path.join(__hidden_path + "/" + GSE)
if not os.path.exists(GSE_dir):
os.mkdir(GSE_dir)
df_file = open(info_path, 'wb')
pickle.dump(df, df_file)
df_file.close()
return df
# In[12]:
def info(GSE_family, sample_id, info= "age"):
"""Given a GSE family and the ID of the wanted sample will return the desired information of the sample.
Possible values for the info parameter:
"age": the unit of the outputted age will always be years.
"brca1": the BRCA1 mutation status for GSE57285
"arthritis": the arthritis status for GSE42861
"crohns": the Crohn's disease status for GSE32148"
"werner synd: GSE100825"
"schiz": GSE41169"""
# create dataframe of sample characteristics portion of the series_matrix file
# read desired info from this dataframe. will have to use regex to find the right information
# set up some sort of persistance of this dataframe for faster retrieval of information
# match = re.search(r"(a|A)(g|G)(e|E)", tag)
info_df = __matrix_to_df("./" + __base_path + "/" + GSE_family + "/" + GSE_family + "_series_matrix.txt", use= "info()", GSE= GSE_family)
info_series = info_df.loc[:, sample_id]
if info == "age":
for row in info_series:
match_age = re.search(r"(a|A)(g|G)(e|E)", row)
if match_age:
# let's grab the age
match_age_dig = re.search(r"\d+\.?\d*", row)
if match_age_dig:
age_float = float(match_age_dig.group())
# to account for "newborn" instead of an actual number
else:
age_float = 0.0
# now let's find the unit of age
match_years = re.search(r"(y|Y)(e|E)(a|A)(r|R)(s|S)*", row)
match_months = re.search(r"(m|M)(o|O)(n|N)(t|T)(h|H)(s|S)*", row)
match_days = re.search(r"(d|D)(a|A)(y|Y)(s|S)*", row)
match_hours = re.search(r"(h|H)(o|O)(u|U)(r|R)(s|S)*", row)
# we always return the age in units of years
if match_years:
return age_float
elif match_months:
return age_float / 12
elif match_days:
return age_float / 365
elif match_hours:
return age_float / 8760
else:
# if no unit is given we'll default to years
return age_float
elif info == "brca1":
for row in info_series:
match_status = re.search(r"brca1 mutation status", row)
if match_status:
# let's grab the brca1 status
match_status_dig = re.search(r"(brca1 mutation status: )(\d)", row)
if match_status_dig:
status_int = int(match_status_dig.group(2))
return status_int
elif info == "arthritis":
for row in info_series:
match_arth = re.search(r"disease state", row)
if match_arth:
# let's grab the arthritis status
match_arth_status = re.search(r"(disease state: )(\S+)", row)
if match_arth_status.group(2) == "rheumatoid":
return "rheumatoid arthritis"
else:
return "normal"
elif info == "werner synd":
for row in info_series:
match_wern = re.search(r"disease", row)
if match_wern:
# let's grab the arthritis status
match_wern_status = re.search(r"(disease: )(\S+)", row)
if match_wern_status.group(2) == "Werner Syndrome":
return "Werner Syndrome"
else:
return "normal"
elif info == "schiz":
for row in info_series:
match_schiz = re.search(r"diseasestatus", row)
if match_schiz:
match_schiz_status = re.search(r"(diseasestatus \(1=control, 2=scz patient\): )(\S+)", row)
if match_schiz_status.group(2) == "2":
return "schizophrenia"
else:
return "normal"
else:
for row in info_series:
match_crohns = re.search(r"disease state", row)
if match_crohns:
# let's grab the Crohn's status
match_crohns_status = re.search(r"(disease state: )(\S+)", row)
return match_crohns_status.group(2)
# In[13]:
def __ID_to_int(GSE_ID):
"""Given some GSE ID will return the corresponding integer as an int.
Example:
__ID_to_int("GSE41037") will return 41037."""
match = re.search(r"\d+", GSE_ID)
if not match:
print(GSE_ID)
return int(match.group(0))
# In[14]:
def __sub_directory(GSE_ID):
"""Given a GSE ID will return the corresponding sub-directory."""
gse_int = __ID_to_int(GSE_ID)
if gse_int <= 171:
ret_str = "GSE" + str(gse_int) + "nnn"
else:
first_3_dig = int(str(gse_int)[0:3])
if first_3_dig <= 171:
ret_str = "GSE" + str(first_3_dig) + "nnn"
else:
first_2_dig_str = str(gse_int)[0:2]
ret_str = "GSE" + first_2_dig_str + "nnn"
return ret_str
# In[15]:
def download(GSE_family_list, file_type= "miniml"):
"""Will download family .tgz files to the following directory: "./data/"
Possible values for file_type: "miniml", "series_matrix"
"""
if (type(GSE_family_list) == str):
GSE_family_list = [GSE_family_list]
url = "ftp.ncbi.nlm.nih.gov"
ftp = ftplib.FTP(url)
ftp.login()
if not os.path.exists(__base_path):
os.mkdir(__base_path)
for GSE_family in GSE_family_list:
if file_type == "miniml":
ftp.cwd("/geo/series/" + __sub_directory(GSE_family) + "/" + GSE_family + "/miniml/")
filename = GSE_family + "_family.xml.tgz"
if not (os.path.exists(__base_path + "/" + filename) or os.path.exists(__base_path + "/" + GSE_family)):
local_file = open(__base_path + "/" + filename, 'wb')
ftp.retrbinary('RETR ' + filename, local_file.write, blocksize= 16_384)
local_file.close()
else:
ftp.cwd("/geo/series/" + __sub_directory(GSE_family) + "/" + GSE_family + "/matrix/")
filename = GSE_family + "_series_matrix.txt.gz"
if not (os.path.exists(__base_path + "/" + filename) or os.path.exists(__base_path + "/" + GSE_family + "/" + GSE_family + "_series_matrix.txt")):
local_file = open(__base_path + "/" + filename, 'wb')
ftp.retrbinary('RETR ' + filename, local_file.write, blocksize= 16_384)
local_file.close()
ftp.quit()
# In[16]:
def __check_beta(series_df):
"""Returns True if Beta values are recorded, returns False if M values are recorded."""
# In[17]:
def series(GSE):
"""Returns a pandas.DataFrame representative of the series' data."""
# download(GSE, file_type= "series_matrix")
# __extract()
file_path = os.path.join(__base_path, GSE, GSE + "_series_matrix.txt")
return __matrix_to_df(file_path, GSE= GSE)
# In[18]:
def __xml_path(GSE_family):
"""Exists to build a path to the family's .xml.
I downloaded these families using the download() function in conjuction with the extract() function."""
return os.path.join(__base_path, GSE_family + "/" + GSE_family + "_family.xml")
# In[19]:
def __xml_to_dict(xml_path):
"""Exists to convert a .xml file at xml_path to a dictionary."""
family_file = open(xml_path,'r+b')
family_dict = xmltodict.parse(family_file)
family_file.close()
return family_dict
# In[20]:
def __dict_index(GSE_family, sample_id):
"""Gives the index of where in the dictionary the sample's information is."""
return __sample_indices(GSE_family)[sample_id]
# In[21]:
def __sample_indices(GSE_family):
"""Exists to return the indices of each sample's information within the associated family dictionary. Returns a dictionary
with keys equal to the sample ID ("GSM***") and values equal to the index of that sample's information within the family
dictionary."""
family_dir = __family_path(GSE_family)
file_list = os.listdir(family_dir)
filtered_list = []
for file_name in file_list:
sample_match = re.match(r"GSM", file_name)
if sample_match:
filtered_list.append(file_name)
for i in np.arange(len(filtered_list)):
filtered_list[i] = re.match(r"GSM\d+", filtered_list[i]).group(0)
index_dict = {}
index = 0
for sample_id in filtered_list:
index_dict[sample_id] = index
index += 1
return index_dict
# In[22]:
def __extract():
"""Will extract files from all downloaded family .tgz files to a respective directory: ./data/GSE***/
This will also delete the .tgz and .gz files."""
file_list = os.listdir(__base_path)
tgz_list = []
gz_list = []
for filename in file_list:
match_tgz = re.search(r"\.tgz", filename)
match_gz = re.search(r"\.gz", filename)
if match_tgz:
tgz_list.append(filename)
elif match_gz:
gz_list.append(filename)
flag = False
for filename in tgz_list:
full_path = __base_path + "/" + filename
family_id = re.search(r"GSE\d+", filename).group(0)
file = tarfile.open(full_path)
try:
out_path = "./" + __base_path + "/" + family_id
file.extractall(out_path)
except:
# let's end the content extraction of the file. will fix later.
file.close()
flag = True
print("Something weird happened while extracting from the " + family_id + " compressed file. Ended extraction early for " + family_id + ".")
if not flag:
file.close()
os.remove(full_path)
for filename in gz_list:
full_path = "./" + __base_path + "/" + filename
family_id = re.search(r"GSE\d+", filename).group(0)
compressed_file = open(full_path, 'rb')
compressed_file_contents = compressed_file.read()
compressed_file.close()
split_compressed_bytes = compressed_file_contents.split()
contents_str = ''
for compressed_bytes in split_compressed_bytes:
contents_bytes = gzip.decompress(compressed_bytes)
contents_str.append(contents_bytes.decode(errors= "replace"))
family_dir = os.path.join(__base_path, family_id)
if not os.path.exists(family_dir):
os.mkdir(family_dir)
out_path = "./" + __base_path + "/" + family_id + "/" + family_id + "_series_matrix.txt"
file = open(out_path, mode= 'w', encoding= "utf-8")
file.write(contents_str)
file.close()
if not flag:
file.close()
os.remove(full_path)