-
Notifications
You must be signed in to change notification settings - Fork 0
/
ummzgbif_functionv3.py
351 lines (315 loc) · 17.2 KB
/
ummzgbif_functionv3.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
# Z:\UMMZ>python ummzgbif_functionv3.py UMMZ-TEST > log.txt
'''
Emails for ummz
All of UMMZ:
Divisions:
'''
"""
Example:
Folder name: ummz-mammals-164676
"institutioncode": "ummz","collectioncode": "mammals","catalognumber": "164676"
"""
import os
from sys import argv
#import idigbio
import datetime
import glob
import fnmatch
import re
#from pygbif import occurrences as occ
import shutil
from pandas import read_excel
import json
import requests
import csv
now = datetime.datetime.now()
def parsefolder(fn):
if fn.startswith(('ummz-', 'UMMZ-')):
initSplit = fn.split("_")
tripleSplit = initSplit[0]
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
return ic, cc, cn
else:
print("Incorrect folder name! %s" %fn)
return
def getgbif(ic, cc, cn):
"""
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())
#print(data)
#print("line 80: %s " % key_list['results'])
#for item in key_list['results']:
#key = print(item['key'])
#htmltext = key_list.json
#print(htmltext)
#key_list = occ.search(institutionCode=ic,collectionCode=cc,catalogNumber=cn)
if key_list['results'] == []:
print("No GBIF record for %s contact Scott Martin" % fname)
ummzdict['ic'] = "error"
ummzdict['cc'] = "error"
ummzdict['cn'] = "error"
return ummzdict
else:
gbif_baseurl = 'https://api.gbif.org/v1/'
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'])
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)
return ummzdict
def getmediagroup(up, fn, ic, cc, cn, uid):
uf = ""
filepath = ""
#ummzfiles = os.listdir(fn) # really need to zip these folders
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:
#print(type(folders))
if folders.startswith(("Raw", "Recon")):
mgfolder = os.path.join(mgpath, folders)
#print("mgfolder= %s" % mgfolder)
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), 'zip', mgfolder)
zfiles = shutil.make_archive(mgfolder + "-" + str(uid), 'tar', mgfolder)
except OSError:
pass
#zfilesplit = zfiles.split('/') # for Mac/Linux
zfilesplit = zfiles.split('\\') # for Windows
zsplit = zfilesplit[len(zfilesplit)-1]
uf = uf + (" - %s\n" % (zsplit))
filepath = filepath + (" - /deepbluedata-prep/UMMZ/%s/%s/%s\n" % (up, fn, zsplit))
#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]))
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]))
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]))
return mgName, uf, filepath
def xtekdata(mgName):
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/%s/' % (ummzpath,fname, mgroup)
xtekpath = os.listdir(xpath)
#print('mgfolder= %s' % (mgroup))
if "WholeBody" in mgroup:
mdDict['part'] = "WholeBody"
else:
mdDict['part'] = "Skull"
#mdDict['part'] = roiName
#xteckVol = 1
#if line.startswith("InputFolderName"):
match = re.search(r'\b\d{4}-\d\d?-\d\d?\b', fname)
ummzdict['scandate'] = datetime.datetime.strptime(match.group(), '%Y-%m-%d').date()
#print(filedate)
mdDict['scandate'] = ummzdict['scandate']
#shortMGroup = re.sub(r'-\b\d{4}-\d\d?-\d\d?\b', '', mgroup)
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['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 "Recon" in mgroup:
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"
if "Raw" in mgroup:
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"
if "WholeBody" in mgroup:
roiName = "WholeBody"
else:
roiName = "Skull"
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):
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" : "Division of Birds", "fish" : "sj139222d" , "herps" : "05741r77z", "insects": "Division of Insects" , "mammals" : "nv935298c", "mollusks" : "Division of Mollusks"}
collID = collDict.get(gbifl1, "")
ytop = ("---\n:user:\n :visibility: restricted\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))
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:\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"
ypartof = " :part_of:\n - 'part of'\n"
ycreator = " :creator:\n - 'University of Michigan Museum of Zoology'\n"
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:\n - https://creativecommons.org/licenses/by-nc-sa/4.0/ \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))
yfilename = (" :filenames:\n%s" % (uf)) #pull from directory
yfiles = (" :files:\n%s" % (filepath)) #pull from directory
file_name = '%s/%s.yml' % (ummzpath, 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.close()
return
### Start here!!
#ummzpath = input("UMMZ folder path: ")
ummzpath = argv[1] #For example UMMZ-02-22-219
#print(ummzpath)
#get folder name from the system to parse into query fields
folderName = [dI for dI in os.listdir(ummzpath) if os.path.isdir(os.path.join(ummzpath,dI))]
#error handling for folder names (ex: not including catalognumber)
msMetadata = dict()
msFilePath = ummzpath + '/' + "DBD_file_manifest.csv"
with open(msFilePath, 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 folderName:
#print(fname)
ummzdict = dict()
gbiflist = parsefolder(fname)
if gbiflist == None:
continue
else:
# Get data from GBIF
#morphosourcemd(ummzpath, fname)
ummzdict['ic'] = gbiflist[0]
ummzdict['cc'] = gbiflist[1]
ummzdict['cn'] = gbiflist[2]
#print(ummzdict['ic'], ummzdict['cc'], ummzdict['cn'])
getgbif(ummzdict['ic'], ummzdict['cc'], ummzdict['cn'])
#print(ummzdict)
if ummzdict['ic'] == "error":
continue
mgName, uf, filepath = getmediagroup(ummzpath, fname, ummzdict['ic'], ummzdict['cc'], ummzdict['cn'], ummzdict['yuuid']) # Get media groups zip media group folders
desc, msMetadata, roiName = xtekdata(mgName) # Get values from xtekVolume
#create file with MorphoSource metadata for each work
writer.writerows(msMetadata)
createyml(fname, uf, filepath, gbiflist[1], roiName)
csv_file.close()
#create a file manifest for MorphoSource of all datasets in batch