Skip to content

Commit

Permalink
enable conversion of .cha to nexus;
Browse files Browse the repository at this point in the history
rely on metadata in HSDS database,
i.e. attributes in 'annotation_sample' and 'annotation_study' groups
  • Loading branch information
vedina committed Oct 14, 2023
1 parent d726d79 commit c49f23e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ git submodule update --remote
git commit -am "Pull the latest commit for the submodules"
git push
```

### Run interminal
```
uvicorn app.main:app --reload
```
4 changes: 2 additions & 2 deletions app/api/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def get_request(request: Request = Depends()):
return request


@router.post("/dataset/convert") # Use router.post instead of app.post
@router.post("/dataset/convert")
async def convert(request: Request,
background_tasks: BackgroundTasks
):
Expand All @@ -46,7 +46,7 @@ async def convert(request: Request,
result=f"{base_url}dataset/{task_id}",
errorCause=None
)
print(substances)
#print(substances)
tasks_db[task.id] = task
background_tasks.add_task(upload_service.convert_to_nexus,substances,task,base_url)
return task
Expand Down
45 changes: 33 additions & 12 deletions app/services/upload_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
import ramanchada2 as rc2
from fastapi import HTTPException
import traceback
import h5py

from ..config.app_config import initialize_dirs

config, UPLOAD_DIR, NEXUS_DIR, TEMPLATE_DIR = initialize_dirs()


async def process(task,dataset_type,file,jsonconfig,expandconfig,base_url):

try:
Expand All @@ -30,7 +32,7 @@ async def process(task,dataset_type,file,jsonconfig,expandconfig,base_url):
task.result=f"{base_url}dataset/{task.id}?format={ext}",

if dataset_type == "raman_spectrum":
parse_spectrum_files(task,base_url,file_path,jsonconfig)
parse_spectrum_files(task,base_url,file_path,jsonconfig)
elif dataset_type == "ambit_json":
task.error = "not supported yet"
task.status = "Error"
Expand Down Expand Up @@ -58,22 +60,41 @@ async def process(task,dataset_type,file,jsonconfig,expandconfig,base_url):
task.status = "Error"
task.completed=int(time.time() * 1000)

def extract_cha_metadata(file_path,json_meta={}):
with h5py.File(file_path) as dataset:
for tag in ["sample","sample_provider"]:
try:
json_meta[tag] = dataset["annotation_sample"].attrs[tag]
except:
json_meta[tag] = None
for tag in ["sample_provider","provider","wavelength","investigation","laser_power","optical_path","laser_power_percent"]:
try:
json_meta[tag] = dataset["annotation_study"].attrs[tag]
except:
json_meta[tag] = "DEFAULT"
return json_meta


def parse_spectrum_files(task,base_url,file_path,jsonconfig):
#instrument,wavelength,provider,investigation,sample,sample_provider,prefix):
spe = rc2.spectrum.from_local_file(file_path)
json_data = {"instrument" : "DEFAULT", "wavelength" : "DEFAULT", "provider" : "DEFAULT",
"investigation" : "DEFAULT", "sample" : "DEFAULT", "sample_provider" : "DEFAULT",
"prefix" : "NONE"}
if jsonconfig is None:
task.status="Warning"
task.error = "Missing jsonconfig"
else:
json_file_path = os.path.join(UPLOAD_DIR, f"{task.id}_config.json")
with open(json_file_path, "wb") as f:
shutil.copyfileobj(jsonconfig.file, f)
with open(json_file_path, "r") as f:
json_data = json.load(f)
"prefix" : "NONE"}
if file_path.endswith(".cha"):
spe = rc2.spectrum.from_chada(file_path)
json_data = extract_cha_metadata(file_path, json_data )
print(json_data)
else:
spe = rc2.spectrum.from_local_file(file_path)
if jsonconfig is None:
task.status="Warning"
task.error = "Missing jsonconfig"
else:
json_file_path = os.path.join(UPLOAD_DIR, f"{task.id}_config.json")
with open(json_file_path, "wb") as f:
shutil.copyfileobj(jsonconfig.file, f)
with open(json_file_path, "r") as f:
json_data = json.load(f)
sample=json_data["sample"]
papp = spe2ambit(spe.x,spe.y,spe.meta,
instrument = json_data["instrument"],
Expand Down

0 comments on commit c49f23e

Please sign in to comment.