Skip to content

Commit bab568f

Browse files
committed
add extract cas to csv script
1 parent 8af06f1 commit bab568f

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

examples/20-extract-cas.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env python
2+
3+
###############################################################################################################
4+
# In this example script, we want to extract the extra field "CAS" from the resources "Chemical Compounds" #
5+
# This can then be used to re-import the compounds in the compounds table and complete them with pubchem data #
6+
# The first step is to export all entries from the Admin panel, as CSV. This is what we will process. #
7+
###############################################################################################################
8+
9+
import elabapi_python
10+
import argparse
11+
import csv
12+
import json
13+
import sys
14+
15+
# use the locally defined client.py module to get the api_client object, fully configured and ready to be used to instantiate api objects
16+
from client import api_client
17+
18+
19+
items_client = elabapi_python.ItemsApi(api_client)
20+
21+
parser = argparse.ArgumentParser(
22+
description="This script extracts the CAS extra field from an eLabFTW CSV export of a resources category and saves it into a new csv file."
23+
)
24+
parser.add_argument(
25+
"csv_path",
26+
type=str,
27+
help="Path to the input CSV file"
28+
)
29+
args = parser.parse_args()
30+
cas_list = []
31+
try:
32+
with open(args.csv_path, mode="r", newline="", encoding="utf-8") as csvfile:
33+
reader = csv.DictReader(csvfile)
34+
if "metadata" not in reader.fieldnames:
35+
print("Error: 'metadata' column not found in CSV.", file=sys.stderr)
36+
sys.exit(1)
37+
38+
for i, row in enumerate(reader, start=1):
39+
raw = row["metadata"]
40+
try:
41+
metadata = json.loads(raw)
42+
except json.JSONDecodeError as e:
43+
print(f"Row {i}: could not parse JSON: {e}", file=sys.stderr)
44+
continue
45+
46+
print(f"Row {i} metadata:", metadata)
47+
extra = metadata.get("extra_fields", {})
48+
cas_entry = extra.get("CAS")
49+
if cas_entry and "value" in cas_entry:
50+
cas_value = cas_entry["value"]
51+
print(f"Row {i} CAS value: {cas_value}")
52+
cas_list.append(cas_value)
53+
else:
54+
print(f"Row {i}: no CAS field found", file=sys.stderr)
55+
56+
except FileNotFoundError:
57+
print(f"Error: file '{args.file_path}' does not exist.", file=sys.stderr)
58+
sys.exit(1)
59+
60+
# Write the collected CAS values to the output CSV
61+
try:
62+
out_path = args.csv_path + "-cas.csv"
63+
with open(out_path, mode="w", newline="", encoding="utf-8") as outcsv:
64+
writer = csv.writer(outcsv)
65+
writer.writerow(["cas"])
66+
for cas in cas_list:
67+
writer.writerow([cas])
68+
print(f"Wrote {len(cas_list)} CAS values to {out_path}")
69+
except IOError as e:
70+
print(f"Error writing to '{out_path}': {e}", file=sys.stderr)
71+
sys.exit(1)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
id,date,title,userid,fullname,body,category,category_title,category_color,status,status_title,status_color,custom_id,elabid,rating,url,metadata,tags
2+
230,2025-05-16,Morphine,1,Toto Le sysadmin,,10,Compounds,0A0A0A,,,,,20250516-2d29600514894de9c2c2843c7121c6932d24ac9c,0,https://elab.local:3148/database.php?mode=view&id=230,"{""extra_fields"": {""CAS"": {""type"": ""text"", ""value"": ""57-27-2"", ""group_id"": null}}}",
3+
231,2025-05-16,EGFR inhibitor,1,Toto Le sysadmin,,10,Compounds,0A0A0A,,,,,20250516-bb9794f2ce0682f1c0635686151329378c7b727b,0,https://elab.local:3148/database.php?mode=view&id=231,"{""extra_fields"": {""CAS"": {""type"": ""text"", ""value"": ""879127-07-8"", ""group_id"": null}}}",
4+
232,2025-05-16,Acetone,1,Toto Le sysadmin,,10,Compounds,0A0A0A,,,,,20250516-3abf450365d179c7e46c9a80ce9069a0d2a22289,0,https://elab.local:3148/database.php?mode=view&id=232,"{""extra_fields"": {""CAS"": {""type"": ""text"", ""value"": ""67-64-1"", ""group_id"": null}}}",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
cas
2+
57-27-2
3+
879127-07-8
4+
67-64-1

0 commit comments

Comments
 (0)