Skip to content

Commit

Permalink
deal with wrong molecule inputs, set to None
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelduranfrigola committed Jun 28, 2023
1 parent 9bbc3c1 commit 028dbf2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
3 changes: 3 additions & 0 deletions ersilia/io/readers/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ def resolve_columns(self):
reader = csv.reader(f, delimiter=self._column_delimiter)
N = 0
for i, r in enumerate(reader):
if len(r) == 1:
self.matching = {"input": [0], "key": None}
return self.matching
if i > self.sniff_line_limit:
self.logger.debug("Stopping sniffer for resolving column types")
break
Expand Down
26 changes: 16 additions & 10 deletions ersilia/utils/identifiers/compound.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, local=True):
else:
self.Chem = None
self.unichem = unichem
self.default_type = "smiles"

def _is_smiles(self, text):
if self.Chem is None:
Expand Down Expand Up @@ -53,6 +54,8 @@ def _is_inchikey(text):
return True

def guess_type(self, text):
if text is None:
return self.default_type
if self._is_inchikey(text):
return "inchikey"
if self._is_smiles(text):
Expand Down Expand Up @@ -111,14 +114,17 @@ def encode(self, smiles):
if inchikey is None:
inchikey = self._nci_smiles_to_inchikey(smiles)
else:
mol = self.Chem.MolFromSmiles(smiles)
if mol is None:
raise Exception(
"The SMILES string: %s is not valid or could not be converted to an InChIKey"
% smiles
)
inchi = self.Chem.rdinchi.MolToInchi(mol)[0]
if inchi is None:
raise Exception("Could not obtain InChI")
inchikey = self.Chem.rdinchi.InchiToInchiKey(inchi)
try:
mol = self.Chem.MolFromSmiles(smiles)
if mol is None:
raise Exception(
"The SMILES string: %s is not valid or could not be converted to an InChIKey"
% smiles
)
inchi = self.Chem.rdinchi.MolToInchi(mol)[0]
if inchi is None:
raise Exception("Could not obtain InChI")
inchikey = self.Chem.rdinchi.InchiToInchiKey(inchi)
except:
inchikey = None
return inchikey

0 comments on commit 028dbf2

Please sign in to comment.