Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description = "A brand-new library to interact with the VAMDC infrastructure"
authors = [
{name = "Carlo Maria Zwölf", email = "carlo-maria.zwolf@obspm.fr"},
{name = "Nicolas Moreau"},
{name = "Yaye Awa Ba},
{name = "Yaye Awa Ba"},
]
readme = "README.md"
requires-python = ">=3.11"
Expand Down
50 changes: 33 additions & 17 deletions spectral/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urllib.request
from datetime import datetime
from io import StringIO
from urllib.parse import urlencode

import numpy as np
from enum import Enum
Expand Down Expand Up @@ -360,20 +361,25 @@ def getSpeciesWithSearchCriteria(text_search = None, stoichiometric_formula = No
"""
_, urlSpeciesEndpoint = _getEndpoints()

urlSuffix = "?"
urlSuffix = (urlSuffix+"text_search="+text_search+"&") if text_search is not None else urlSuffix
urlSuffix = (urlSuffix+"stoichiometric_formula="+stoichiometric_formula+"&") if stoichiometric_formula is not None else urlSuffix
urlSuffix = (urlSuffix+"ivo_identifier="+ivo_identifier+"&") if ivo_identifier is not None else urlSuffix
urlSuffix = (urlSuffix+"inchikey="+inchikey+"&") if inchikey is not None else urlSuffix
urlSuffix = (urlSuffix+"name="+name+"&") if name is not None else urlSuffix
urlSuffix = (urlSuffix+"structural_formula="+structural_formula+"&") if structural_formula is not None else urlSuffix
params = {}
if text_search is not None:
params["text_search"] = text_search
if stoichiometric_formula is not None:
params["stoichiometric_formula"] = stoichiometric_formula
if ivo_identifier is not None:
params["ivo_identifier"] = ivo_identifier
if inchikey is not None:
params["inchikey"] = inchikey
if name is not None:
params["name"] = name
if structural_formula is not None:
params["structural_formula"] = structural_formula

if type == "molecule" or type == "atom":
urlSuffix = urlSuffix + "type="+type + "&"

params["type"] = type

# testing the difference mass_max-mass_min to be positive
maxMinDifference = mass_max-mass_min if (mass_max is not None and mass_min is not None) else 0
maxMinDifference = mass_max - mass_min if (mass_max is not None and mass_min is not None) else 0
if maxMinDifference < 0:
raise Exception("The difference (mass_max - mass_min) must be positive")

Expand All @@ -382,15 +388,19 @@ def getSpeciesWithSearchCriteria(text_search = None, stoichiometric_formula = No
if maxMinDifference < 0:
raise Exception("The difference (charge_max - charge_min) must be positive")

urlSuffix = (urlSuffix+"mass_max="+str(mass_max)+"&") if mass_max is not None else urlSuffix
urlSuffix = (urlSuffix+"mass_min="+str(mass_min)+"&") if mass_min is not None else urlSuffix

urlSuffix = (urlSuffix+"charge_max="+str(charge_max)+"&") if charge_max is not None else urlSuffix
urlSuffix = (urlSuffix+"charge_min="+str(charge_min)+"&") if charge_min is not None else urlSuffix
if mass_max is not None:
params["mass_max"] = str(mass_max)
if mass_min is not None:
params["mass_min"] = str(mass_min)
if charge_max is not None:
params["charge_max"] = str(charge_max)
if charge_min is not None:
params["charge_min"] = str(charge_min)

fullUrl = urlSpeciesEndpoint + urlSuffix
query_string = urlencode(params)
fullUrl = f"{urlSpeciesEndpoint}?{query_string}" if query_string else urlSpeciesEndpoint

species_df , node_df = _getChemicalInfoFromEnpoint(fullUrl)
species_df, node_df = _getChemicalInfoFromEnpoint(fullUrl)

return species_df, node_df

Expand Down Expand Up @@ -461,10 +471,16 @@ def _getChemicalInfoFromEnpoint(specificSpeciesEndpoint):
Returns:
AllSpeciesDF (dataframe): a Pandas dataframe containing the chemical information obtained while resolving the URL in the specificSpeciesEndpoint variable.
df_nodes (dataframe): a Pandas dataframe containing the information regarding the Nodes.
(None, None): if nothing found, is cleaner than getting some Exception from pandas when trying to create a dataframe from an empty json.
"""
response = urllib.request.urlopen(specificSpeciesEndpoint)
data = json.loads(response.read())

# Simply nothing found
if not data or len(data.items()) == 0:
#print("No chemical information has been found in the Species database.")
return None, None

# wrapping the results into a unique Pandas Dataframe (called AllSpeciesDF)
dataFrames = []
AllSpeciesDF = None
Expand Down