Skip to content

Commit 2d9325e

Browse files
author
Will Trimble
committed
Handle missing index fields
1 parent 6961f8e commit 2d9325e

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

scripts/mg-export-metadata.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ def get_project_keys(meta):
4040
keys = set()
4141
for key in meta["data"].keys():
4242
keys.add(key)
43-
keys.remove("project_name")
43+
if "project_name" in keys: keys.remove("project_name")
4444
return(["project_name"] + list(keys))
4545

4646
def get_sample_keys(meta):
4747
keys = set()
4848
for sample in meta["samples"]:
4949
for key in sample["data"].keys():
5050
keys.add(key)
51-
keys.remove("sample_name")
51+
if "sample_name" in keys: keys.remove("sample_name")
5252
return(["sample_name"] + list(keys))
5353

5454
def get_library_keys(meta):
@@ -113,7 +113,7 @@ def main(args):
113113

114114
outfile = PROJECT + "-export.xlsx"
115115
#
116-
k = obj_from_url("http://api.mg-rast.org/metadata/export/{project}?verbosity=full".format(project=PROJECT))
116+
k = obj_from_url("http://api.mg-rast.org/metadata/export/{project}?verbosity=full".format(project=PROJECT), auth=TOKEN)
117117
metadata = k # json.loads(open(infile).read())
118118

119119
workbook = xlsxwriter.Workbook(outfile)
@@ -154,8 +154,11 @@ def main(args):
154154
col += 1
155155
col = 0
156156
row += 1
157+
try:
158+
librarytype = metadata["samples"][0]["libraries"][0]["data"]["investigation_type"]["value"]
159+
except IndexError:
160+
sys.exit("This metadata bundle does not have any libraries")
157161

158-
librarytype = metadata["samples"][0]["libraries"][0]["data"]["investigation_type"]["value"]
159162
worksheet["library"] = workbook.add_worksheet("library "+librarytype)
160163

161164
libkeys = get_library_keys(metadata)

0 commit comments

Comments
 (0)