-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_files.py
executable file
·571 lines (459 loc) · 27.4 KB
/
create_files.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
import pandas
import yaml
from collections import OrderedDict
from os.path import join, isdir, isfile
## For data fetching
import psycopg2
import csv
import requests
import ldap
from base64 import b64encode
import httplib2
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
## For data processing
import geojson
import shapely.geometry
import shapely.ops
from shapely.geometry import JOIN_STYLE, Point
import sys # For: csv.field_size_limit(sys.maxsize)
import os # For: os.rename('data.tmp', 'data')
import overpass
with open('settings_nosecrets', 'r') as f:
settings = yaml.load(f, Loader=yaml.BaseLoader)
with open('settings_local', 'r') as f:
settings = {**settings, **yaml.load(f, Loader=yaml.BaseLoader)}
csv_value_separator = "\t"
csv_row_separator = "\n"
csv_list_separator = "; "
def run():
# Determine the paths to the datatable directories.
datatables_path = join('output')
#####################################################################
### Observatory db server
# Try to connect to the database,
# http://initd.org/psycopg/docs/
conn = psycopg2.connect(
host = settings["observatoryDbServerHost"],
port = str(settings["observatoryDbServerPort"]),
sslmode = settings["observatoryDbServerSSL"],
database = settings["observatoryDbServerDatabase"],
user = settings["observatoryDbServerUser"],
password = settings["observatoryDbServerPass"]
)
cur = conn.cursor()
# Get the authoritative list of studies from the Observatory.
study_list_query = "SELECT \"" + settings["observatoryDbStudyField"] + "\" FROM \"" + settings["observatoryDbServerDbSchema"] + "\".\"" + settings["observatoryDbStudiesView"] + "\""
cur.execute(study_list_query)
obsStudies = [row[0] for row in cur.fetchall()]
if len(obsStudies) == 0:
raise ValueError('observatoryDbStudiesView returned zero obsStudies')
for observatoryDbView_index, observatoryDbView in enumerate(settings["observatoryDbViews"]):
datatable = settings["panoptesObsTables"][observatoryDbView_index]
datatable_path = join(datatables_path, datatable)
data_file_path = join(datatable_path, "data")
if not isdir(datatable_path):
os.mkdir(datatable_path)
# Open the data file for writing.
data_file = open(data_file_path, 'w')
# http://initd.org/psycopg/docs/cursor.html#cursor.copy_expert
## Mogrify throws a syntax error when I try to interpolate the params.
# http://initd.org/psycopg/docs/cursor.html#cursor.mogrify
# https://www.postgresql.org/docs/9.5/static/sql-copy.html
## Such queries can be checked first using the psql CLI, e.g.
# COPY (SELECT * FROM observatory."Samples with types") TO STDOUT (FORMAT csv, HEADER TRUE, DELIMITER E'\t');
copy_data_query = "COPY (SELECT * FROM \"" + settings["observatoryDbServerDbSchema"] + "\".\"" + observatoryDbView + "\") TO STDOUT (FORMAT csv, HEADER TRUE, DELIMITER E'\t', QUOTE E'\b', ESCAPE E'\b', NULL '')"''
cur.copy_expert(copy_data_query, data_file)
# Close the data file.
data_file.close()
#Get all countries JSON polygons
geoJSON_for_country = {}
query = 'SELECT "'+settings["panoptesObsCountriesTableCountryField"] + '","' + settings["panoptesObsCountriesTableGeoJsonField"] + \
'" FROM "'+ settings["observatoryDbServerDbSchema"] + '"."' + settings["panoptesObsCountriesTable"] + '"'
cur.execute(query)
result = cur.fetchall()
for (country_id, geoJSON) in result:
geoJSON_for_country[country_id] = geoJSON
# Close the db cursor and connection.
cur.close()
conn.close()
#####################################################################
### Alfresco server (studies)
data = requests.get(settings["alfrescoStudiesURL"], auth=(settings["alfrescoUserId"], settings["alfrescoUserPass"])).json()
alfStudies = data["collaborationNodes"]
if len(alfStudies) == 0:
raise ValueError('fetchAlfrescoStudies returned zero collaborationNodes')
# Collect the studies by name, to facilitate a subsequent parse.
studiesByName = {}
for study in alfStudies:
if study["name"] not in studiesByName:
studiesByName[study["name"]] = study
else:
raise ValueError('Duplicate study name: ', str(study['name']))
#####################################################################
### LDAP server (people)
ldapPeople = fetchLdapPeople(
ldapServer=settings["ldapServerURL"]
, ldapUserDN=settings["ldapUserDN"]
, ldapUserPass=settings["ldapUserPass"]
, ldapPeopleBaseDN=settings["ldapPeopleBaseDN"]
, ldapPeopleFilterString=settings["ldapPeopleFilterString"]
, ldapPeopleFields=settings["ldapPeopleFields"]
)
#####################################################################
### Google Sheets (genes)
gsheets_service = discovery.build(
settings["gsheetsApiServiceName"],
settings["gsheetsApiServiceVersion"],
http=establishGSheetsCredentials(
settings["gsheetsClientSecretPath"],
settings["gsheetsCredentialsPath"],
settings["gsheetsAuthHost"],
settings["gsheetsAuthPort"]
).authorize(httplib2.Http()),
discoveryServiceUrl=settings["gsheetsApiDiscoveryUrl"]
)
for gsheetsId_index, gsheet_id in enumerate(settings["gsheetsIds"]):
datatable = settings["panoptesGsheetsTables"][gsheetsId_index]
datatable_path = join(datatables_path, datatable)
data_file_path = join(datatable_path, "data")
gsheet_range = settings["gsheetsRanges"][gsheetsId_index]
if not isdir(datatable_path):
os.mkdir(datatable_path)
# Read in the data.
gsheet_rows = gsheets_service.spreadsheets().values().get(spreadsheetId=gsheet_id, range=gsheet_range).execute().get('values', [])
# Merge with data fetched from observatoryDb - observatoryDb rows are used and gsheet rows merged in, such that only primary keys from observatoryDb persist
if datatable in settings["panoptesObsTables"]:
print("Merging google sheet for " + datatable)
with open(data_file_path, 'r') as data_file:
exisiting_rows = list(csv.DictReader(data_file, delimiter=csv_value_separator))
#Transform the google sheet rows to dicts
gsheet_columns = gsheet_rows[0]
gsheet_rows = [{key: (row[i] if i < len(row) else '') for i, key in enumerate(gsheet_columns)} for row in gsheet_rows[1:]]
#Find the index column
exisiting_columns = set(exisiting_rows[0].keys())
matching_columns = set(gsheet_columns).intersection(exisiting_columns)
all_columns = set(gsheet_columns).union(exisiting_columns)
if len(matching_columns) != 1:
raise ValueError('When merging a gsheet there should be one and only one column to match on, we got:', str(matching_columns))
index_column = list(matching_columns)[0]
#Index the gsheet data
indexed_gsheet_rows = {row[index_column]:row for row in gsheet_rows}
# Write out the data.
with open(data_file_path, 'w') as data_file:
writer = csv.DictWriter(data_file, all_columns, delimiter=csv_value_separator, lineterminator=csv_row_separator)
writer.writeheader()
for row in exisiting_rows:
row.update(indexed_gsheet_rows.get(row[index_column], {}))
writer.writerow(row)
else:
# Write out the data.
with open(data_file_path, 'w') as data_file:
writer = csv.writer(data_file, delimiter=csv_value_separator, lineterminator=csv_row_separator)
for line in gsheet_rows:
writer.writerow(line)
#####################################################################
### Process the Alfresco and LDAP data
# Determine the paths to the datatable directories.
alf_studies_datatable_dir_path = join(datatables_path, settings["panoptesAlfStudiesTable"])
alf_study_publications_datatable_dir_path = join(datatables_path, settings["panoptesAlfStudyPublicationsTable"])
alf_study_ldap_people_datatable_dir_path = join(datatables_path, settings["panoptesAlfStudyLdapPeopleTable"])
if not isdir(alf_studies_datatable_dir_path):
os.mkdir(alf_studies_datatable_dir_path)
if not isdir(alf_study_publications_datatable_dir_path):
os.mkdir(alf_study_publications_datatable_dir_path)
if not isdir(alf_study_ldap_people_datatable_dir_path):
os.mkdir(alf_study_ldap_people_datatable_dir_path)
# Determine the paths to the data files.
alf_studies_data_file_path = join(alf_studies_datatable_dir_path, "data")
alf_study_publications_data_file_path = join(alf_study_publications_datatable_dir_path, "data")
alf_study_ldap_people_data_file_path = join(alf_study_ldap_people_datatable_dir_path, "data")
# Print a warning if any of the data files already exist.
if isfile(alf_studies_data_file_path):
print("Warning: Overwriting file: " + alf_studies_data_file_path)
if isfile(alf_study_publications_data_file_path):
print("Warning: Overwriting file: " + alf_study_publications_data_file_path)
if isfile(alf_study_ldap_people_data_file_path):
print("Warning: Overwriting file: " + alf_study_ldap_people_data_file_path)
# Open the CSV files for writing.
alf_studies_data_file = open(alf_studies_data_file_path, 'w')
alf_study_publications_data_file = open(alf_study_publications_data_file_path, 'w')
alf_study_ldap_people_data_file = open(alf_study_ldap_people_data_file_path, 'w')
# Append the heading lines.
alf_studies_data_file.write(csv_value_separator.join(["study"] + settings["alfrescoStudiesFields"]) + csv_row_separator)
alf_study_publications_data_file.write(csv_value_separator.join(["study"] + settings["alfrescoStudyPublicationsFields"]) + csv_row_separator)
alf_study_ldap_people_data_file.write(csv_value_separator.join(["study"] + settings["panoptesAlfStudyLdapPeopleFields"]) + csv_row_separator)
webStudies = {}
for study in alfStudies:
if study['name'] not in obsStudies:
continue
if "webStudy" in study:
webStudies[study['name']] = study['webStudy']['name']
print("appending", study['webStudy']['name'])
if study['webStudy']['name'] not in obsStudies:
obsStudies.append(study['webStudy']['name'])
studiesNotProcessed = list(obsStudies)
for study in alfStudies:
alf_study_name = study['name']
study_number = getStudyNumber(study['name'], csv_value_separator)
# Skip this study if the alf_study_name is not in the list of obsStudies.
if alf_study_name not in obsStudies:
continue
if studiesNotProcessed is not None:
studiesNotProcessed.remove(alf_study_name)
# Skip anything with a webStudy.
if "webStudy" in study:
continue
# Compose the study row, which will be appended to the CSV file
# study study_number webTitle description
study_row = [alf_study_name, study_number]
if study["webTitleApproved"] == "false":
print("Warning: webTitle not approved for:" + study['name'])
study_row.append(study["webTitle"].replace(csv_value_separator, ""))
if study["descriptionApproved"] == "false":
print("Warning: description not approved for:" + study['name'])
study_row.append(study["description"].replace(csv_value_separator, ""))
alfStudyLdapPeople = getAlfStudyLdapPeople(
ldapPeople=ldapPeople
, alfStudy=study
, panoptesAlfStudyLdapPeopleGroups=settings["panoptesAlfStudyLdapPeopleGroups"]
)
# Write the related records: people and publications.
writeRelatedRecords(study["publications"], alf_study_name, settings["alfrescoStudyPublicationsFields"], alf_study_publications_data_file, csv_list_separator, csv_row_separator, csv_value_separator)
writeRelatedRecords(alfStudyLdapPeople, alf_study_name, settings["panoptesAlfStudyLdapPeopleFields"], alf_study_ldap_people_data_file, csv_list_separator, csv_row_separator, csv_value_separator)
# Write the study to the CSV file
alf_studies_data_file.write((csv_value_separator.join(study_row).replace("\n", " ").replace("\r", " ") + csv_row_separator).encode('ascii', 'xmlcharrefreplace').decode())
if studiesNotProcessed is not None and len(studiesNotProcessed) > 0:
raise ValueError('These studies were not found', str(studiesNotProcessed))
# Close the CSV files.
alf_studies_data_file.close()
alf_study_publications_data_file.close()
alf_study_ldap_people_data_file.close()
#####################################################################
### Determine the paths to the datatable directories and data files.
# Determine the paths to the datatable directories.
if settings["panoptesObsRegionsTable"]:
obs_regions_datatable_dir_path = join(datatables_path, settings["panoptesObsRegionsTable"])
obs_samples_datatable_dir_path = join(datatables_path, settings["panoptesObsSamplesTable"])
obs_countries_datatable_dir_path = join(datatables_path, settings["panoptesObsCountriesTable"])
obs_locations_datatable_dir_path = join(datatables_path, settings["panoptesObsLocationsTable"])
# Create dirs if the datatable directories are missing.
if settings["panoptesObsRegionsTable"]:
if not isdir(obs_regions_datatable_dir_path):
os.mkdir(obs_regions_datatable_dir_path)
if not isdir(obs_samples_datatable_dir_path):
os.mkdir(obs_samples_datatable_dir_path)
if not isdir(obs_countries_datatable_dir_path):
os.mkdir(obs_countries_datatable_dir_path)
# Determine the paths to the data files.
if settings["panoptesObsRegionsTable"]:
obs_regions_data_file_path = join(obs_regions_datatable_dir_path, "data")
obs_samples_data_file_path = join(obs_samples_datatable_dir_path, "data")
obs_locations_data_file_path = join(obs_locations_datatable_dir_path, "data")
obs_countries_data_file_path = join(obs_countries_datatable_dir_path, "data")
# Raise errors if the data files do not exist.
if settings["panoptesObsRegionsTable"]:
if not isfile(obs_regions_data_file_path):
raise ValueError('panoptesObsRegionsTable data file does not exist at path: ', str(obs_regions_data_file_path))
if not isfile(obs_samples_data_file_path):
raise ValueError('panoptesObsSamplesTable data file does not exist at path: ', str(obs_samples_data_file_path))
if not isfile(obs_countries_data_file_path):
raise ValueError('panoptesObsCountriesTable data file does not exist at path: ', str(obs_countries_data_file_path))
#####################################################################
### Modify the studies table to make studies with a "webStudy" masquerade as that study
with open(obs_samples_data_file_path, 'r') as data_file:
samples = list(csv.DictReader(data_file, delimiter=csv_value_separator))
sample_columns = samples[0].keys()
study_column = settings["panoptesObsSamplesTableStudyField"]
with open(obs_samples_data_file_path, 'w') as data_file:
writer = csv.DictWriter(data_file, sample_columns, delimiter=csv_value_separator, lineterminator=csv_row_separator)
writer.writeheader()
for row in samples:
#Hack around postgres outputing bools as t/f (WTF?)
if 'qc_pass' in row:
row['qc_pass'] = 'True' if row['qc_pass'] == 't' else 'False'
#Rewrite web studies
if row[study_column] in webStudies:
print(row[study_column], webStudies.get(row[study_column], row[study_column]))
row[study_column] = webStudies[row[study_column]]
writer.writerow(row)
samples = pandas.read_csv(obs_samples_data_file_path, delimiter=csv_value_separator)
#PROVINCE AND DISTRICT NOT NEEDED FOR NOW WITH PF6 AS SITES ARE USUALLY LARGE AREAS
# locations = pandas.read_csv(obs_locations_data_file_path, delimiter=csv_value_separator)
# locations.set_index('site_id')
# provinces = []
# for index, row in locations.iterrows():
# print(
# 'Fetching location data from OSM for ' + row['name'] + ' in ' + row['country_id'])
# (province, district) = overpass.admin_levels_for_point(row['lat'], row['lng'])
# provinces.append(province)
# locations['province_id'] = [province['province_id'] for province in provinces]
# provinces = pandas.DataFrame(provinces).drop_duplicates('province_id')
# # Denormalise somethings for convenience
# samples['province_id'] = [locations.loc[s['site_id'], 'province_id'] for index, s in samples.iterrows()]
# samples['country_id'] = [locations.loc[s['site_id'], 'country_id'] for index, s in samples.iterrows()]
#os.mkdir(join(datatables_path, 'provinces'))
#provinces.to_csv(join(datatables_path, 'provinces', 'data'), delimiter=csv_value_separator)
#locations.to_csv(obs_locations_data_file_path, delimiter=csv_value_separator)
markers = pandas.read_csv(
'ftp://ngs.sanger.ac.uk/production/malaria/pfcommunityproject/Pf6/Pf_6_drug_resistance_marker_genotypes.txt',
delimiter='\t')
markers = markers.rename(columns={"Sample": "sample_id"})
markers = markers.set_index('sample_id')
fws = pandas.read_csv(
'ftp://ngs.sanger.ac.uk/production/malaria/pfcommunityproject/Pf6/Pf_6_fws.txt',
delimiter='\t'
)
fws = fws.rename(columns={"Sample": "sample_id"})
fws = fws.set_index('sample_id')
samples = samples.join(markers)
samples = samples.join(fws)
samples.to_csv(obs_samples_data_file_path, index=True, sep=csv_value_separator)
gene_diff = pandas.read_csv('ftp://ngs.sanger.ac.uk/production/malaria/pfcommunityproject/Pf6/Pf_6_genes_data.txt',
delimiter='\t')
if not isdir(join(datatables_path, 'gene_diff')):
os.mkdir(join(datatables_path, 'gene_diff'))
gene_diff.to_csv(join(datatables_path, 'gene_diff', 'data'), sep=csv_value_separator)
#####################################################################
### Generate the region GeoJSON
if settings["panoptesObsRegionsTable"]:
# Collect all of the distinct countries for each distinct region appearing in the obs_samples data.
countries_by_region = {}
sample_points = set()
obs_samples_data_file_reader = csv.DictReader(open(obs_samples_data_file_path, 'r'), delimiter=csv_value_separator, lineterminator=csv_row_separator)
i = 0
countries = {}
for row in obs_samples_data_file_reader:
i += 1
if settings["panoptesObsSamplesTableRegionField"] in row and settings["panoptesObsSamplesTableCountryField"] in row:
region_id = row[settings["panoptesObsSamplesTableRegionField"]]
country_id = row[settings["panoptesObsSamplesTableCountryField"]]
countries.setdefault(country_id, {}).setdefault(region_id, 0)
countries[country_id][region_id] += 1
sample_points.add((float(row[settings["panoptesObsSamplesTableLngField"]]), float(row[settings["panoptesObsSamplesTableLatField"]])))
if region_id in countries_by_region:
countries_by_region[region_id].add(country_id)
else:
countries_by_region[region_id] = {country_id}
else:
raise ValueError('panoptesObsSamplesTableRegionField and panoptesObsSamplesTableCountryField are not in data at line: ', str(i))
#If a country is in more than one region then just keep it in the modal region
for country, region_counts in countries.items():
bad_regions = sorted(region_counts.keys(),key=lambda r: region_counts[r])[:-1]
for region in bad_regions:
countries_by_region[region].remove(country)
# Combine all of the GeoJSON for every country in each region.
geojson_by_region = {}
for region_id in countries_by_region:
region_shape = None
for country_id in countries_by_region[region_id].union(settings["panoptesObsRegionsAdditionalCountries"].get(region_id, [])):
if geoJSON_for_country[country_id] is None or geoJSON_for_country[country_id] == '':
print('Ignoring ' + country_id + ' for polygon as no data')
continue
new_region = shapely.geometry.asShape(geoJSON_for_country[country_id]['geometry'])
if region_shape is None:
region_shape = new_region
else:
region_shape = region_shape.union(new_region)
if region_shape is not None:
region_shape = region_shape.buffer(0.001, join_style=JOIN_STYLE.mitre).buffer(-0.001, join_style=JOIN_STYLE.mitre).simplify(0.1)
#Filter the polygons in the shape that don't have samples in (ie islands without samples)
region_shape = shapely.ops.cascaded_union([poly for poly in region_shape if any(poly.contains(Point(lng, lat)) for (lng, lat) in sample_points)])
geojson_by_region[region_id] = geojson.Feature(geometry=region_shape, properties={})
# Add the geojson to the region data file. (We have to write to a temporary file first.)
with open(obs_regions_data_file_path, 'r') as obs_regions_data_in, open(obs_regions_data_file_path + '.tmp', 'w') as obs_regions_data_out:
obs_regions_data_file_reader = csv.DictReader(obs_regions_data_in, delimiter=csv_value_separator, lineterminator=csv_row_separator)
obs_regions_data_file_fieldnames = obs_regions_data_file_reader.fieldnames + [settings["panoptesObsRegionsTableGeoJsonField"]]
obs_regions_data_file_writer = csv.DictWriter(obs_regions_data_out, fieldnames=obs_regions_data_file_fieldnames, delimiter=csv_value_separator, lineterminator=csv_row_separator, quoting=csv.QUOTE_NONE, escapechar="\\", quotechar="'")
obs_regions_data_file_writer.writeheader()
for obs_regions_data_file_line in obs_regions_data_file_reader:
region_id = obs_regions_data_file_line[settings["panoptesObsRegionsTableRegionField"]]
obs_regions_data_file_line['geojson'] = geojson_by_region[region_id]
obs_regions_data_file_writer.writerow(obs_regions_data_file_line)
# Overwrite the original data file.
os.rename(obs_regions_data_file_path + '.tmp', obs_regions_data_file_path)
###################### Functions
def fetchLdapPeople(ldapServer, ldapUserDN, ldapUserPass, ldapPeopleBaseDN, ldapPeopleFilterString, ldapPeopleFields):
ldapConnection = ldap.initialize(ldapServer)
try:
ldapConnection.bind_s(ldapUserDN, ldapUserPass)
except ldap.INVALID_CREDENTIALS:
raise ValueError('Invalid credentials for ldapServer: ', str(ldapServer))
except ldap.LDAPError as e:
if hasattr(e, 'message') and type(e.message) == dict and e.message.has_key('desc'):
print(e.message['desc'])
else:
print(str(e))
searchResults = ldapConnection.search_s(
ldapPeopleBaseDN
, ldap.SCOPE_SUBTREE
, ldapPeopleFilterString
, [str(x) for x in ldapPeopleFields]
)
ldapPeople = {}
for dn, ldapEntry in searchResults:
if 'malariagenUID' not in ldapEntry:
print(("No malariagenUID in DN: ", dn, ", ldapEntry: ", str(ldapEntry)))
continue
malariagenUID = str(ldapEntry['malariagenUID'][0],"utf-8")
ldapPeople[malariagenUID] = {'dn': dn}
for ldapPeopleField in ldapPeopleFields:
if ldapPeopleField in ldapEntry:
ldapPeople[malariagenUID][ldapPeopleField] = str(ldapEntry[ldapPeopleField][0],"utf-8")
ldapConnection.unbind()
return ldapPeople
def getStudyNumber(study_name, csv_value_separator):
return study_name.split('-')[0].replace(csv_value_separator, "")
def getAlfStudyLdapPeople(ldapPeople, alfStudy, panoptesAlfStudyLdapPeopleGroups):
study_people = {}
alfStudyLdapPeople = []
for group_type in panoptesAlfStudyLdapPeopleGroups:
group = alfStudy["group" + group_type]
for study_person in group:
malariagenUID = study_person['malariagenUID']
if malariagenUID in ldapPeople:
person = ldapPeople[malariagenUID]
if malariagenUID in study_people:
for p in alfStudyLdapPeople:
if p['malariagenUID'] == malariagenUID:
p['class'].append(group_type)
else:
person['class'] = [group_type]
study_people[malariagenUID] = person
alfStudyLdapPeople.append(person)
return alfStudyLdapPeople
def writeRelatedRecords(records, foreign_key_value, fields, file, csv_list_separator, csv_row_separator, csv_value_separator):
for record in records:
record_values = []
record_values.append(foreign_key_value)
for record_field in fields:
record_field_value = ''
if record_field in record:
if isinstance(record[record_field], str):
record_field_value = record[record_field]
elif isinstance(record[record_field], (list, tuple)):
record_field_value = csv_list_separator.join(record[record_field])
record_values.append(record_field_value)
# Write the record_values to the CSV file.
file.write((csv_value_separator.join(record_values).replace("\n", " ").replace("\r", " ") + csv_row_separator).encode('ascii', 'xmlcharrefreplace').decode())
def establishGSheetsCredentials(client_secret_path, credentials_path, auth_host_name, auth_host_port):
store = Storage(credentials_path)
credentials = None
if os.path.isfile(credentials_path):
credentials = store.get()
if credentials is None or credentials.invalid:
flow = client.flow_from_clientsecrets(client_secret_path, 'https://www.googleapis.com/auth/spreadsheets.readonly')
flow.user_agent = 'malobs'
# http://oauth2client.readthedocs.io/en/latest/source/oauth2client.tools.html
# https://docs.python.org/2/library/argparse.html
# https://stackoverflow.com/questions/24890146/how-to-get-google-analytics-credentials-without-gflags-using-run-flow-instea
print("Will now try to authenticate by starting a web server (and opening a web browser) using gsheetsAuthHost " + auth_host_name + " and gsheetsAuthPort: " + str(auth_host_port))
# Note: If this fails, it will try to fall back to using --noauth_local_webserver, which this script is *not* designed to support.
flags = tools.argparser.parse_args(args=['--noauth_local_webserver', '--logging_level', 'DEBUG', '--auth_host_name', auth_host_name, '--auth_host_port', str(auth_host_port)])
# http://oauth2client.readthedocs.io/en/latest/_modules/oauth2client/tools.html
# Note: The ports in this message are hard-coded so misleading: "Failed to start a local webserver listening on either port 8080 or port 8090. Please check your firewall settings and locally running programs that may be blocking or using those ports."
credentials = tools.run_flow(flow, store, flags)
return credentials
run()