Skip to content

Commit

Permalink
improve logging for converting custom chem hm to json
Browse files Browse the repository at this point in the history
  • Loading branch information
an-altosian committed Dec 17, 2024
1 parent dd5efc2 commit 958ea7f
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 84 deletions.
18 changes: 12 additions & 6 deletions resources/custom_chemistries.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,42 @@
"geometry": "1{b[16]u[12]x:}2{r[50]x:}",
"version" : "0.1.0",
"expected_ori" : "fw",
"local_pl_path" : null
"local_pl_path" : null,
"remote_pl_path" : "https://umd.box.com/shared/static/851sydyypct2cy4nri98yhelb4a9q3sg.txt"
} ,
"visiumv1" : {
"geometry": "1{b[16]u[12]x:}2{r:}",
"version" : "0.1.0",
"expected_ori" : "fw",
"local_pl_path" : null
"local_pl_path" : null,
"remote_pl_path" : "https://umd.box.com/shared/static/851sydyypct2cy4nri98yhelb4a9q3sg.txt"
} ,
"visiumv4-probe" : {
"geometry": "1{b[16]u[12]x:}2{r[50]x:}",
"version" : "0.1.0",
"expected_ori" : "fw",
"local_pl_path" : null
"local_pl_path" : null,
"remote_pl_path" : "https://umd.box.com/shared/static/i4udjwaceixd1em71fol50c3izl1wbzk.txt"
} ,
"visiumv4" : {
"geometry": "1{b[16]u[12]x:}2{r:}",
"version" : "0.1.0",
"expected_ori" : "fw",
"local_pl_path" : null
"local_pl_path" : null,
"remote_pl_path" : "https://umd.box.com/shared/static/i4udjwaceixd1em71fol50c3izl1wbzk.txt"
} ,
"visiumv5-probe" : {
"geometry": "1{b[16]u[12]x:}2{r[50]x:}",
"version" : "0.1.0",
"expected_ori" : "fw",
"local_pl_path" : null
"local_pl_path" : null,
"remote_pl_path" : "https://umd.box.com/shared/static/518elp0hmb0es0qbhmp2o34evup4qwwr.txt"
} ,
"visiumv5" : {
"geometry": "1{b[16]u[12]x:}2{r:}",
"version" : "0.1.0",
"expected_ori" : "fw",
"local_pl_path" : null
"local_pl_path" : null,
"remote_pl_path" : "https://umd.box.com/shared/static/518elp0hmb0es0qbhmp2o34evup4qwwr.txt"
}
}
4 changes: 2 additions & 2 deletions src/simpleaf_commands/chemistry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ pub fn add_chemistry(af_home_path: PathBuf, add_chem_cmd: Commands) -> Result<()
name: name.clone(),
geometry: geometry.clone(),
expected_ori: Some(ExpectedOri::from_str(&expected_ori)?),
local_pl_path: local_pl_path.clone(),
remote_pl_url: remote_pl_url.clone(),
local_pl_path: local_pl_path,
remote_pl_url: remote_pl_url,
version: None
};

Expand Down
4 changes: 2 additions & 2 deletions src/simpleaf_commands/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::PathBuf;

pub fn inspect_simpleaf(version: &str, af_home_path: PathBuf) -> Result<()> {
// Read the JSON contents of the file as an instance of `User`.
let v: Value = inspect_af_home(af_home_path.as_path())?;
let simpleaf_info: Value = inspect_af_home(af_home_path.as_path())?;
// do we have a custom chemistry file
let custom_chem_p = af_home_path.join("custom_chemistries.json");
let chem_info_value = if custom_chem_p.is_file() {
Expand All @@ -35,7 +35,7 @@ pub fn inspect_simpleaf(version: &str, af_home_path: PathBuf) -> Result<()> {

let inspect_v = json!({
"simpleaf_version" : version,
"simpleaf_info" : v,
"simpleaf_info" : simpleaf_info,
"custom_chem_info" : chem_info_value,
"builtin_chemistries" : {
"rna" : rna_chem_list,
Expand Down
188 changes: 114 additions & 74 deletions src/utils/af_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,114 +733,144 @@ pub fn parse_single_custom_chem_from_value(key: &str, value: &Value) -> Result<C
// check if the geometry field exists and is valid
let geometry = obj.get(GEOMETRY_KEY).with_context(|| {
format!(
"Couldn't find the required geometry field for the custom chemistry record for {}: {}.",
"Couldn't find the required {} field for the custom chemistry record for {}: {}.",
GEOMETRY_KEY,
key,
value
)
})?;
// it should be a string
let geometry_str = geometry.as_str().with_context(|| {
format!(
"Couldn't parse the geometry field for the custom chemistry record for {}: {}.",
"Couldn't parse the {} field for the custom chemistry record for {}: {}.",
GEOMETRY_KEY,
key,
geometry
)
})?;
// it should be a valid geometry
extract_geometry(geometry_str).with_context(|| {
format!(
"Found invalid custom chemistry record for {}: {}.",
"Found invalid custom geometry for {}: {}.",
key,
geometry_str
)
})?;

// check if the expected_ori field exists and is valid
let expected_ori = if let Some(eo) = obj.get(EXPECTED_ORI_KEY) {
// if it exists, it should be valid
let expected_ori_str = eo.as_str().with_context(|| {
format!(
"Couldn't parse the expected_ori field for {}: {}.",
key,
eo
)
})?;
// convert it to expected_ori enum
let eo = ExpectedOri::from_str(expected_ori_str).with_context(|| {
format!(
"Found invalid expected_ori for {}: {}",
key,
expected_ori_str
)
})?;
Some(eo)
if eo.is_null() {
None
} else {
// if it exists, it should be string
let expected_ori_str = eo.as_str().with_context(|| {
format!(
"Couldn't parse the {} field for the custom chemistry record for {}: {}",
EXPECTED_ORI_KEY,
key,
eo
)
})?;
// check if the expected_ori exists
if !ExpectedOri::from_str(expected_ori_str).is_ok() {
Err(anyhow!(
"Found invalid {} for {}: {}",
EXPECTED_ORI_KEY,
key,
expected_ori_str
))?;
}
// convert it to expected_ori enum
let eo = ExpectedOri::from_str(expected_ori_str).with_context(|| {
format!(
"Found invalid {} for {}: {}",
EXPECTED_ORI_KEY,
key,
expected_ori_str
)
})?;
Some(eo)
}
} else {
None
};

// check if the version field exists
let version = match obj.get("version") {
Some(v) => {
let prog_ver_string = v
.as_str()
.with_context(|| {
if v.is_null() {
None
} else {
// if it exists, it should be string
let prog_ver_string = v
.as_str()
.with_context(|| {
format!(
"Couldn't parse the version for the custom chemistry {} as a string: {}",
key,
v,
)
})?;

// check if the version is valid
Version::parse(prog_ver_string).with_context(|| {
format!(
"Couldn't parse the version for the custom chemistry {} as a string: {}",
"Found invalid version string for the custom chemistry {}: {}",
key,
v,
prog_ver_string
)
})?;

// check if the version is valid
Version::parse(prog_ver_string).with_context(|| {
format!(
"Found invalid version string for the custom chemistry {}: {}",
key,
prog_ver_string
)
})?;
Some(prog_ver_string.to_string())
Some(prog_ver_string.to_string())
}
}
None => None
};

// check if the local_pl_path field exists and is valid
let local_pl_path = if let Some(lpp) = obj.get(LOCAL_PL_PATH_KEY) {
// if it exists, it should be string
let local_pl_path_str = lpp.as_str().with_context(|| {
format!(
"Couldn't parse the local_pl_path field for {}: {}",
key,
lpp
)
})?;

// check if the local_pl_path exists
if !PathBuf::from(local_pl_path_str).is_file() {
Err(anyhow!(
"Couldn't find the local_pl_path for the custom chemistry record for {}: {}",
key,
local_pl_path_str
))?;
if lpp.is_null() {
None
} else {
// if it exists, it should be string
let local_pl_path_str = lpp.as_str().with_context(|| {
format!(
"Couldn't parse the local_pl_path field for {}: {}",
key,
lpp
)
})?;

// check if the local_pl_path exists
if !PathBuf::from(local_pl_path_str).is_file() {
Err(anyhow!(
"Couldn't find the local_pl_path for the custom chemistry record for {}: {}",
key,
local_pl_path_str
))?;
}

Some(local_pl_path_str.to_string())
}

Some(local_pl_path_str.to_string())
} else {
None
};

// check if the remote_pl_url field exists and is valid
// TODO: should we try to access the remote_pl_url to ensure it is valid?
let remote_pl_url = if let Some(rpu) = obj.get(REMOTE_PL_URL_KEY) {
// if it exists, it should be valid
let remote_pl_url_str = rpu.as_str().with_context(|| {
format!(
"Couldn't parse the remote_pl_url field for {}: {}",
key,
rpu
)
})?;
Some(remote_pl_url_str.to_string())
if rpu.is_null() {
None
} else {
// if it exists, it should be valid
let remote_pl_url_str = rpu.as_str().with_context(|| {
format!(
"Couldn't parse the remote_pl_url field for {}: {}",
key,
rpu
)
})?;
Some(remote_pl_url_str.to_string())
}
} else {
None
};
Expand Down Expand Up @@ -875,18 +905,28 @@ pub fn custom_chem_hm_to_json(custom_chem_hm: &HashMap<String, CustomChemistry>)
let mut value = json!({
GEOMETRY_KEY: v.geometry.clone()
});
if let Some(eo) = &v.expected_ori {
value[EXPECTED_ORI_KEY] = json!(eo.as_str());
}
if let Some(ver) = &v.version {
value[VERSION_KEY] = json!(ver);
}
if let Some(lpp) = &v.local_pl_path {
value[LOCAL_PL_PATH_KEY] = json!(lpp);
}
if let Some(rpu) = &v.remote_pl_url {
value[REMOTE_PL_URL_KEY] = json!(rpu);
}
value[EXPECTED_ORI_KEY] = if let Some(eo) = &v.expected_ori {
json!(eo.as_str())
} else {
info!("`expected_ori` is missing for custom chemistry {}; Set as {}", k, ExpectedOri::Both.as_str());
json!(ExpectedOri::Both.as_str())
};
value[VERSION_KEY] = if let Some(ver) = &v.version {
json!(ver)
} else {
info!("`version` is missing for custom chemistry {}; Set as {}", k, "0.0.1");
json!("0.0.1")
};
value[LOCAL_PL_PATH_KEY] = if let Some(lpp) = &v.local_pl_path {
json!(lpp)
} else {
json!(null)
};
value[REMOTE_PL_URL_KEY] = if let Some(rpu) = &v.remote_pl_url {
json!(rpu)
} else {
json!(null)
};
(k.clone(), value)
})
.collect();
Expand Down

0 comments on commit 958ea7f

Please sign in to comment.