-
Notifications
You must be signed in to change notification settings - Fork 0
/
nested_ummzgbif_v4.py
462 lines (398 loc) · 23.5 KB
/
nested_ummzgbif_v4.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
# nohup python3 ./scripts/nested_ummzgbif_v4.py Transfer-12-11-2020/ > ./scripts/nested_log.txt &
# assumes folder hierarchy of:
# /deepbluedata-prep/upload/UMMZ/Transfer-12-11-2020/week-12-14/datafiles
'''
Emails for ummz
All of UMMZ:
Divisions:
'''
from pathlib import Path
import os
from sys import argv
# import idigbio
import datetime
import glob
import fnmatch
import re
# from pygbif import occurrences as occ
import shutil
# import pandas
import json
import requests
import csv
"""
Example:
Folder name: ummz-mammals-164676
"institutioncode": "ummz","collectioncode": "mammals","catalognumber": "164676"
"""
now = datetime.datetime.now()
def parsefolder(fn):
if fn.startswith(('ummz-', 'UMMZ-')):
initSplit = fn.split("_")
tripleSplit = initSplit[0]
catCheck = re.split("-|_ ", initSplit[1])
# print(catCheck)
fsplit = tripleSplit.split("-")
if len(fsplit) <= 2:
print("Missing catalog number, contact UMMZ")
return
else:
ic = fsplit[0] # institutioncode
cc = fsplit[1] # collectioncode --> use this to get contact email
cn = fsplit[2] # catalognumber
# print(catCheck)
return ic, cc, cn, catCheck
else:
print("Incorrect folder name! %s" % fn)
return
def getgbif(ic, cc, cn, catCheck):
"""
Using the GBIF library, fetch UMMZ records
"""
# https://www.gbif.org/occurrence/search?catalog_number=246790&collection_code=herps&institution_code=ummz
# https://www.gbif.org/occurrence/1987105097
gbif_baseurl = 'https://api.gbif.org/v1/'
# url = gbif_baseurl + 'occurrence/search?' + 'catalog_number=' + cn + '&collection_code=' + cc + '&institution_code=' + ic
# print(url)
results = requests.get(
gbif_baseurl + 'occurrence/search?' + 'catalog_number=' + cn + '&collection_code=' + cc + '&institution_code=' + ic)
# https://api.gbif.org/v1/occurrence/search?catalog_number=124092&collection_code=mammals&institution_code=ummz
key_list = json.loads(results.content.decode())
catSciNameCheck = re.split('-|_', catCheck[0])
# print(catSciNameCheck[0])
if key_list['results'] == []:
print("No GBIF record for %s contact UMMZ" % fname)
ummzdict['ic'] = "error"
ummzdict['cc'] = "error"
ummzdict['cn'] = "error"
return ummzdict
else:
gbif_baseurl = 'https://api.gbif.org/v1/'
# need to get the GBIF Key from above to use in the Fragment URL below:
for item in key_list['results']:
# print(item['key'])
# key = print(item['key'])
url = gbif_baseurl + 'occurrence/' + str(item['key']) + '/fragment'
# print(url)
record_results = requests.get(gbif_baseurl + 'occurrence/' + str(item['key']) + '/fragment')
record_list = json.loads(record_results.content.decode())
# print(record_list)
# print(record_list['scientificName'])
if catSciNameCheck[0].lower() not in record_list["scientificName"].lower():
print("Catalog number %s not correct points to %s instead" % (
ummzdict['cn'], record_list["scientificName"]))
ummzdict['cn'] = "error"
continue
else:
ummzdict['yuuid'] = item["key"]
ummzdict['sciName'] = record_list["scientificName"]
ummzdict['keyWords'] = (
record_list["kingdom"], record_list["phylum"], record_list["class"], record_list["order"],
record_list["family"], record_list["scientificName"], ummzdict['yuuid'])
# print(ummzdict)
# print(ummzdict)
return ummzdict
def getmediagroup(sp, up, fn, ic, cc, cn, uid):
print(up, fn, ic, cc, cn, uid)
uf = ""
filepath = ""
mgpath = '%s/%s/' % (up, fn)
mgName = [dI for dI in os.listdir(mgpath) if os.path.isdir(os.path.join(mgpath, dI))]
for folders in mgName:
if folders.startswith(("Raw", "Recon")):
mgfolder = os.path.join(mgpath, folders)
# Get tifs and ply files for previews
plyfile = [f for f in os.listdir(mgfolder) if f.endswith('.ply')]
if len(plyfile) != 0:
uf = uf + (" - %s\n" % (plyfile[0]))
# filepath = filepath + (" - /deepbluedata-prep/UMMZ/%s/%s\n" % (mgfolder, plyfile[0]))
filepath = filepath + (" - %s/%s/%s\n" % (sp, mgfolder, plyfile[0]))
if "Skull" in folders:
img_files = [f for f in os.listdir(mgfolder) if f.endswith('.tif')]
uf = uf + (" - %s\n" % (img_files[0]))
# filepath = filepath + (" - /deepbluedata-prep/UMMZ/%s/%s\n" % (mgfolder, img_files[0]))
filepath = filepath + (" - %s/%s/%s\n" % (sp, mgfolder, img_files[0]))
if "WholeBody" in folders:
img_files = [f for f in os.listdir(mgfolder) if f.endswith('.tif')]
fbimg = int(len(img_files) / 2)
uf = uf + (" - %s\n" % (img_files[fbimg]))
# filepath = filepath + (" - /deepbluedata-prep/UMMZ/%s/%s\n" % (mgfolder, img_files[fbimg]))
filepath = filepath + (" - %s/%s/%s\n" % (sp, mgfolder, img_files[fbimg]))
#zfolders = "%s" % folders
try:
# Modify zip name to include Darwin core triple and GBIF occurrence ID
# shutil.make_archive(name_of_zip, 'zip', folder_to_be_zipped) mgfolder + ic + "-" + cc + "-" + cn + "-" + uid
zfiles = shutil.make_archive(mgfolder + "-" + str(uid), 'tar', mgfolder)
except OSError:
print("Error: " + mgfolder)
pass
zfilesplit = zfiles.split('/') # for Mac/Linux
# zfilesplit = zfiles.split('\\') # for Windows
zsplit = zfilesplit[len(zfilesplit) - 1]
uf = uf + (" - %s\n" % (zsplit))
if "Raw" in zsplit:
rawZsplit = zsplit
if "Recon" in zsplit:
reconZsplit = zsplit
# filepath = filepath + (" - /deepbluedata-globus/upload/UMMZ/%s/%s/%s\n" % (up, fn, zsplit))
filepath = filepath + (" - %s/%s/%s/%s\n" % (sp, up, fn, zsplit))
return mgName, uf, filepath, rawZsplit, reconZsplit
def xtekdata(mgName, rawZsplit, reconZsplit):
# print(mgName, rawZsplit, reconZsplit)
mgdesc = list()
msMetadata = list()
for mgroup in mgName:
# print(mgroup)
mdDict = dict()
# msMetadata['mdDict']['name'] = mgroup
# mdDict['part'] = mgroup
if mgroup == 'surface_model':
continue
else:
xpath = '%s/%s/' % (fname, mgroup)
xtekpath = os.listdir(xpath)
# print('mgfolder= %s' % (mgroup))
if "WholeBody" in mgroup:
mdDict['part'] = "WholeBody"
else:
mdDict['part'] = "Skull"
match = re.search(r'\b\d{4}-\d\d?-\d\d?\b', fname.name)
ummzdict['scandate'] = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
# print(filedate)
mdDict['scandate'] = ummzdict['scandate']
for xtek in xtekpath:
# xteckVol = 1
if xtek.endswith(("_01.xtekct", ".xtekVolume")):
xtekfolder = os.path.join(xpath, xtek)
xtekfile = open(xtekfolder, "r")
# print(xtekfile)
for line in xtekfile:
if re.match("VoxelSizeX=", line):
voxRes = re.split('=', line)[-1]
# print(voxres)
if re.match("VoxelsX=", line):
voxX = re.split('=', line)[-1]
mdDict['x_spacing'] = voxX.rstrip('\r\n')
# print(voxX)
if re.match("VoxelsY=", line):
voxY = re.split('=', line)[-1]
mdDict['y_spacing'] = voxY.rstrip('\r\n')
# print(voxY)
if re.match("VoxelsZ=", line):
voxZ = re.split('=', line)[-1]
mdDict['z_spacing'] = voxZ.rstrip('\r\n')
# print(voxZ)
if re.match("Projections=", line):
proj = re.split('=', line)[-1]
# print(proj)
'''
if "Recon" in mgroup:
numtif = voxZ.rstrip('\r\n')
dataType = "Reconstructed -"
if "Raw" in mgroup:
numtif = proj.rstrip('\r\n')
dataType = "Raw -"
'''
mdDict['media_type'] = "CT Image Series"
mdDict['occurrence_id'] = "urn:catalog:" + ummzdict['ic'] + ":" + ummzdict['cc'] + ":" + ummzdict[
'cn']
mdDict["institution_code"] = ummzdict['ic']
mdDict["collection_code"] = ummzdict['cc']
mdDict["catalog_number"] = ummzdict['cn']
mdDict["device_model"] = "XT H225ST"
mdDict["device_manufacturer"] = "Nikon"
mdDict["device_modality"] = "Micro/Nano X-Ray Computed Tomography"
mdDict["device_description"] = ""
mdDict["device_organization_name"] = "University of Michigan Museum of Zoology"
# mdDict["x_spacing"] = voxX
# mdDict["y_spacing"] = voxY
# mdDict["z_spacing"] = voxZ
mdDict["unit"] = "mm"
if "Raw" in mgroup:
# print("RAW Group")
numtif = proj.rstrip('\r\n')
mdDict["number_of_images_in_set"] = proj.rstrip('\r\n')
mdDict["processing_activity_type"] = "Raw"
mdDict["media_ct_series_type"] = "Projections"
mdDict["file_name"] = rawZsplit
# mdDict["parent_file_name"] = RAW filename is parent of Recon file
if "Recon" in mgroup:
# print("RECON Group")
numtif = mdDict['z_spacing'].rstrip('\r\n')
mdDict["media_ct_number_of_images_in_set"] = mdDict['z_spacing'].rstrip('\r\n')
mdDict["processing_activity_type"] = "Reconstructed"
mdDict["media_ct_series_type"] = "Reconstructed Image Stack"
mdDict["file_name"] = reconZsplit
mdDict["parent_file_name"] = rawZsplit
if "WholeBody" in mgroup:
roiName = "WholeBody"
else:
roiName = "Skull"
# License specified below in rights.
# mdDict["license"] = "https://creativecommons.org/licenses/by-nc-sa/4.0/"
# ydesc = (" :description:\n - 'Scan of specimen %s:%s:%s (%s) - %s . Dataset includes %s TIF images (each %s x %s x 1 voxel at %s mm resolution, derived from %s scan projections), xtek and vgi files for volume reconstruction.'\n" % (ummzdict['ic'], ummzdict['cc'], ummzdict['cn'], ummzdict['sciName'], shortMGroup, numtif, voxX.rstrip('\r\n'), voxY.rstrip('\r\n'), voxres.rstrip('\r\n'), proj.rstrip('\r\n'))) #from xtek
ydesc = (
"Scan of specimen %s:%s:%s (%s) - %s. %s Dataset includes %s TIF images (each %s x %s x 1 voxel at %s mm resolution, derived from %s scan projections), xtek and vgi files for volume reconstruction." % (
ummzdict['ic'], ummzdict['cc'], ummzdict['cn'], ummzdict['sciName'], mdDict['part'],
mdDict["processing_activity_type"], numtif, mdDict['x_spacing'], mdDict['y_spacing'],
voxRes.rstrip('\r\n'), proj.rstrip('\r\n'))) # from xtek
# print(ydesc)
mgdesc.append(ydesc)
break
# else:
# xteckVol = 0
# continue
# if xteckVol == 0:
# print("No *.xtekVolume file found in %s!" % xpath)
# print(mdDict)
msMetadata.append(mdDict)
ummzdict['desc'] = mgdesc
return ummzdict['desc'], msMetadata, roiName
def createyml(fname, uf, filepath, gbifl1, roiName):
# print("fname: " + fname)
emailDict = {"birds": "[email protected]", "fish": "[email protected]",
"herps": "[email protected]", "insects": "[email protected]",
"mammals": "[email protected]", "mollusks": "[email protected]"}
ownauth = emailDict.get(gbifl1, "")
collDict = {"birds": "z603qx752", "fish": "sj139222d", "herps": "xk81jk388",
"insects": "Division of Insects", "mammals": "nv935298c", "mollusks": "Division of Mollusks"}
collID = collDict.get(gbifl1, "")
# ytop = "---\n:user:\n :visibility: open\n :email: [email protected]\n :ingester: '[email protected]'\n :source: DBDv2\n :works:\n :depositor: [email protected]\n"
ytop = (
"---\n:user:\n :visibility: open\n :state: active\n :email: '%s'\n :ingester: '[email protected]'\n :source: DBDv2\n :mode: build\n :works:\n :depositor: '[email protected]'\n" % (
ownauth))
yownauth = (" :owner: '%s'\n :authoremail: '%s'\n" % (ownauth, ownauth))
if "dice" in fname:
ytitle = (" :title: \n - 'Computed tomography voxel dataset for %s:%s:%s-%s-%s-DiceCT' \n" % (
ummzdict['ic'], ummzdict['cc'], ummzdict['cn'], ummzdict['sciName'], roiName))
else:
ytitle = (" :title: \n - 'Computed tomography voxel dataset for %s:%s:%s-%s-%s' \n" % (
ummzdict['ic'], ummzdict['cc'], ummzdict['cn'], ummzdict['sciName'], roiName))
ydate = (" :date_uploaded:\n - '%s'\n" % (now.year))
yrefby = (
" :referenced_by:\n - 'For more information on the original UMMZ specimen, see: https://www.gbif.org/occurrence/%s'\n" % (
ummzdict['yuuid'])) # build URL from iDigBio uuid\n - ''\n"
# ymethod = " :methodology: 'This dataset was created at the University of Michigan Museum of Zoology using a procedure involving computed tomography (CT) hardware. After retrieving the specimen from the museum''s archives, staff secured the specimen in the Nikon XT H 225 ST and initiated the scanning process, which included capturing projections by rotating the specimen. The device''s associated software CT-Pro-3D and the projections were then used to reconstruct a set of TIFF images, with each corresponding to a slice of the three-dimensional object (one voxel in height). In addition, the software created a .xtek volume file (included here), which contains details about the scanning environment, projections, and reconstructions.'\n"
if "dice" not in fname:
ymethod = " :methodology:\n - 'This dataset was created at the University of Michigan Museum of Zoology using a procedure involving computed tomography (CT) hardware. After retrieving the specimen from the museum''s archives, staff secured the specimen in the Nikon XT H 225 ST and initiated the scanning process, which included capturing projections by rotating the specimen. The device''s associated software CT-Pro-3D and the projections were then used to reconstruct a set of TIFF images, with each corresponding to a slice of the three-dimensional object (one voxel in height). In addition, the software created a .xtek volume file (included here), which contains details about the scanning environment, projections, and reconstructions.'\n"
else:
ymethod = " :methodology:\n - 'This dataset was created at the University of Michigan Museum of Zoology using a procedure involving computed tomography (CT) hardware. After retrieving the specimen from the museum''s archives, staff secured the specimen in the Nikon XT H 225 ST and initiated the scanning process, which included capturing projections by rotating the specimen. The device''s associated software CT-Pro-3D and the projections were then used to reconstruct a set of TIFF images, with each corresponding to a slice of the three-dimensional object (one voxel in height). In addition, the software created a .xtek volume file (included here), which contains details about the scanning environment, projections, and reconstructions.'\n - '“DiceCT” refers to “diffusible iodine contrast enhanced computed tomography”. DiceCT scans are conducted to visualize the soft tissue anatomy of the scanned specimen. To do this each specimen is stained in 1.25% Lugol’s iodine (1L = 25 g KI + 12.5 I2 + 1L deionized H20) for 1-8 weeks, size dependent. Prior to staining each specimen is stepped down from 70% ETOH to 50% ETOH to 25% ETOH. Following scanning the specimen is then stepped up back to 70% ETOH to destain and remove the Lugol’s iodine from the specimen.'\n"
ypartof = " :part_of:\n - 'part of'\n"
ycreator = " :creator:\n - 'University of Michigan Museum of Zoology'\n"
if "dice" in fname:
ykw = (" :keyword:\n - %s\n - 'computed tomography'\n - 'X-ray'\n - '3D'\n" % (
'\n - '.join("'{0}'".format(w) for w in ummzdict['keyWords'])) + "\n - 'DiceCT'\n")
else:
ykw = (" :keyword:\n - %s\n - 'computed tomography'\n - 'X-ray'\n - '3D'\n" % (
'\n - '.join("'{0}'".format(w) for w in ummzdict['keyWords'])))
yrights = (" :rights_license: 'Other'\n :rights_license_other: 'Terms of use as of date of deposit (February 2024) included in data file. See https://myumi.ch/x75jA for current terms, as these may have changed.' \n")
ydatecov = (" :date_coverage:\n - '%s'\n" % (ummzdict['scandate'])) # from xtek
ysubject = (" :subject_discipline:\n - 'Science'\n")
# ybib = (" :bibliographic_citation:\n - 'For more information on the original UMMZ specimen, see: https://www.gbif.org/occurrence/%s'\n" % (ummzdict['yuuid'])) #build URL from iDigBio uuid
ydesclist = (" :description:\n - %s\n" % ('\n - '.join("'{0}'".format(w) for w in ummzdict['desc'])))
ylang = (" :language:\n - 'English'\n")
ycurnote = (" :curation_notes_admin:\n - 'UMMZ Batch Ingest'\n")
ydoi = (" :doi: 'mint_now'\n")
ycoll = (" :in_collections:\n - %s \n" % (collID))
# UMMZ usage agreement file
yUAFilename = (" - Digital_Data_Usage_Agreement_TC-2023.08.17.pdf") # pull from directory
yUAFiles = (
" - /deepbluedata-prep/UMMZ/mammals/scripts/Digital_Data_Usage_Agreement_TC-2023.08.17.pdf") # pull from directory
if "dice" in fname:
yfilename = (" :filenames:\n%s\n%s\n - DBD_UMMZ_DiceCT_README_File.txt\n" % (
uf, yUAFilename)) # pull from directory
yfiles = (" :files:\n%s\n%s\n - /deepbluedata-prep/UMMZ/mammals/scripts/DBD_UMMZ_DiceCT_README_File.txt\n" % (
filepath, yUAFiles)) # pull from directory
else:
yfilename = (" :filenames:\n%s\n%s\n - DBD_UMMZ_CT_README_File.txt\n" % (
uf, yUAFilename)) # pull from directory
yfiles = (" :files:\n%s\n%s\n - /deepbluedata-prep/UMMZ/mammals/scripts/DBD_UMMZ_CT_README_File.txt\n" % (
filepath, yUAFiles)) # pull from directory
file_name = '%s/%s.yml' % (datafolder, fname)
f = open(file_name, 'w') # open file in write mode
f.write(ytop)
f.write(ycoll)
f.write(yownauth)
f.write(ycreator)
f.write(ytitle)
# f.write(ydate)
f.write(yrefby)
f.write(ymethod)
f.write(ykw)
# for desc in ummzdict['desc']:
f.write(ydesclist)
f.write(yrights)
f.write(ydatecov)
f.write(ysubject)
# f.write(ybib)
f.write(ylang)
f.write(ycurnote)
f.write(ydoi)
f.write(yfilename)
f.write(yfiles)
# f.write(yUAFilename)
# f.write(yUAFiles)
f.close()
return
### Start here!!
# get folder name from the system to parse into query fields
# python3 ./scripts/nested_ummzgbif_v4.py Transfer-12-11-2020/ > ./scripts/nested_log.txt
from pathlib import Path
topDir = Path(argv[1])
# print(topDir)
serverpath = '/deepbluedata-prep/UMMZ/mammals'
for datafolder in topDir.iterdir():
if datafolder.is_dir():
datafolderPath = str(datafolder)
msMetadata = dict()
# msFilePath = nestDir + '/' + "DBD_file_manifest.csv"
with open(str(datafolder) + '/' + "DBD_file_manifest.csv", mode='w', encoding='utf8',
newline='') as csv_file:
fieldnames = ["file_name", "ms_id", "media_type", "ingestable", "parent_file_name",
"parent_ms_id", "parent_identifier", "occurrence_id", "institution_code",
"collection_code", "catalog_number", "scandate", "device_model",
"device_manufacturer", "device_modality", "device_description",
"device_organization_name", "x_spacing", "y_spacing", "z_spacing", "unit",
"number_of_images_in_set", "processing_activity_type", "creator", "part",
"license", "media_ct_series_type", "media_ct_number_of_images_in_set"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
writer.writeheader()
for fname in datafolder.iterdir():
if fname.is_dir():
#print(fname.name)
ummzdict = dict()
gbiflist = parsefolder(fname.name)
# print(gbiflist)
if gbiflist == None:
continue
else:
# Get data from GBIF
# morphosourcemd(nestDir, fname)
ummzdict['ic'] = gbiflist[0]
ummzdict['cc'] = gbiflist[1]
ummzdict['cn'] = gbiflist[2]
catCheck = gbiflist[3]
# print(catCheck)
# print(ummzdict['ic'], ummzdict['cc'], ummzdict['cn'])
getgbif(ummzdict['ic'], ummzdict['cc'], ummzdict['cn'], catCheck)
# print(ummzdict)
if ummzdict['ic'] == "error" or ummzdict['cn'] == "error":
continue
else:
# print(ummzdict)
mgName, uf, filepath, rawZsplit, reconZsplit = getmediagroup(serverpath, str(datafolder), fname.name, ummzdict['ic'], ummzdict['cc'], ummzdict['cn'], ummzdict['yuuid']) # Get media groups zip media group folders
# print(mgName, uf, filepath, rawZsplit, reconZsplit)
desc, msMetadata, roiName = xtekdata(mgName, rawZsplit,
reconZsplit) # Get values from xtekVolume
# create file with MorphoSource metadata for each work
writer.writerows(msMetadata)
createyml(fname.name, uf, filepath, gbiflist[1], roiName)
csv_file.close()
# create a file manifest for MorphoSource of all datasets in batch