Skip to content

Commit

Permalink
#1296 Add "customQuery" option support to ket format (#1297)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliaksandrDziarkach authored Oct 5, 2023
1 parent 8498ae1 commit a333f0f
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 178 deletions.
6 changes: 6 additions & 0 deletions api/tests/integration/ref/formats/ket_with_query.py.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*** KET with query components ***
ket_with_query_components.ket:SUCCEED
*** KET with query properties ***
ket_with_query_properties.ket:SUCCEED
*** KET with custom query ***
ket_with_custom_query.ket:SUCCEED

This file was deleted.

This file was deleted.

45 changes: 45 additions & 0 deletions api/tests/integration/tests/formats/ket_with_query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import difflib
import os
import sys


def find_diff(a, b):
return "\n".join(difflib.unified_diff(a.splitlines(), b.splitlines()))


sys.path.append(
os.path.normpath(
os.path.join(os.path.abspath(__file__), "..", "..", "..", "common")
)
)
from env_indigo import * # noqa

indigo = Indigo()
indigo.setOption("json-saving-pretty", True)

ref_path = joinPathPy("ref/", __file__)


def check_ket_file(name):
filename = os.path.join(ref_path, name)

mol = indigo.loadQueryMoleculeFromFile(filename)
with open(filename, "r") as file:
ket_ref = file.read()
ket = mol.json()
diff = find_diff(ket_ref, ket)
if not diff:
print(name + ":SUCCEED")
else:
print(name + ":FAILED")
print(diff)


print("*** KET with query components ***")
check_ket_file("ket_with_query_components.ket")

print("*** KET with query properties ***")
check_ket_file("ket_with_query_properties.ket")

print("*** KET with custom query ***")
check_ket_file("ket_with_custom_query.ket")
35 changes: 0 additions & 35 deletions api/tests/integration/tests/formats/ket_with_query_components.py

This file was deleted.

35 changes: 0 additions & 35 deletions api/tests/integration/tests/formats/ket_with_query_properties.py

This file was deleted.

123 changes: 123 additions & 0 deletions api/tests/integration/tests/formats/ref/ket_with_custom_query.ket
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"root": {
"nodes": [
{
"$ref": "mol0"
}
]
},
"mol0": {
"type": "molecule",
"atoms": [
{
"label": "C",
"location": [
6.3348493576049809,
-5.550074577331543,
0.0
]
},
{
"label": "C",
"location": [
8.06515121459961,
-5.549589157104492,
0.0
]
},
{
"label": "C",
"location": [
7.2016377449035648,
-5.049966812133789,
0.0
]
},
{
"label": "C",
"location": [
8.06515121459961,
-6.55053186416626,
0.0
]
},
{
"label": "C",
"location": [
6.3348493576049809,
-6.555019855499268,
0.0
]
},
{
"label": "C",
"location": [
7.203820705413818,
-7.050033092498779,
0.0
],
"queryProperties": {
"customQuery": "C&X3,N&X2"
}
}
],
"bonds": [
{
"type": 2,
"atoms": [
2,
0
]
},
{
"type": 2,
"atoms": [
3,
1
]
},
{
"type": 1,
"atoms": [
0,
4
]
},
{
"type": 1,
"atoms": [
1,
2
]
},
{
"type": 2,
"atoms": [
4,
5
]
},
{
"type": 1,
"atoms": [
5,
3
]
}
],
"sgroups": [
{
"type": "MUL",
"atoms": [
0,
1,
2,
3,
4,
5
],
"mul": 1
}
]
}
}
4 changes: 2 additions & 2 deletions core/indigo-core/molecule/molecule_json_saver.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ namespace indigo

bool Int64(int64_t i64)
{
return pretty_json ? _pretty_writer.Uint(i64) : _writer.Uint(i64);
return pretty_json ? _pretty_writer.Uint64(i64) : _writer.Uint64(i64);
}

bool Uint64(uint64_t u64)
{
return pretty_json ? _pretty_writer.Uint(u64) : _writer.Uint(u64);
return pretty_json ? _pretty_writer.Uint64(u64) : _writer.Uint64(u64);
}

bool Double(double d)
Expand Down
13 changes: 9 additions & 4 deletions core/indigo-core/molecule/smiles_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ namespace indigo
bool ignore_bad_valence;
bool ignore_no_chiral_flag{false};

static void readSmartsAtomStr(const std::string& atom_str, std::unique_ptr<QueryMolecule::Atom>& qatom);
static void readSmartsBondStr(const std::string& bond_str, std::unique_ptr<QueryMolecule::Bond>& qbond);

protected:
enum
{
Expand Down Expand Up @@ -178,14 +181,16 @@ namespace indigo
void _handleCurlyBrace(_AtomDesc& atom, bool& inside_polymer);
void _handlePolymerRepetition(int i);

void _readAtom(Array<char>& atom_str, bool first_in_brackets, _AtomDesc& atom, std::unique_ptr<QueryMolecule::Atom>& qatom);
static void _readAtom(Array<char>& atom_str, bool first_in_brackets, _AtomDesc& atom, std::unique_ptr<QueryMolecule::Atom>& qatom,
bool smarts_mode = false, bool inside_rsmiles = false);

bool _readAtomLogic(Array<char>& atom_str, bool first_in_brackets, _AtomDesc& atom, std::unique_ptr<QueryMolecule::Atom>& qatom);
static bool _readAtomLogic(Array<char>& atom_str, bool first_in_brackets, _AtomDesc& atom, std::unique_ptr<QueryMolecule::Atom>& qatom,
bool smarts_mode = false, bool inside_rsmiles = false);

int _parseCurly(Array<char>& curly, int& repetitions);

void _readBond(Array<char>& bond_str, _BondDesc& bond, std::unique_ptr<QueryMolecule::Bond>& qbond);
void _readBondSub(Array<char>& bond_str, _BondDesc& bond, std::unique_ptr<QueryMolecule::Bond>& qbond);
static void _readBond(Array<char>& bond_str, _BondDesc& bond, std::unique_ptr<QueryMolecule::Bond>& qbond, bool smarts_mode);
static void _readBondSub(Array<char>& bond_str, _BondDesc& bond, std::unique_ptr<QueryMolecule::Bond>& qbond, bool smarts_mode);
void _readRGroupOccurrenceRanges(const char* str, Array<int>& ranges);

private:
Expand Down
7 changes: 5 additions & 2 deletions core/indigo-core/molecule/smiles_saver.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ namespace indigo

const Array<int>& getSavedCisTransParities();

static std::string writeSmartsAtomStr(QueryMolecule::Atom* atom);
static std::string writeSmartsBondStr(QueryMolecule::Bond* bond);

protected:
void _saveMolecule();

Expand Down Expand Up @@ -110,8 +113,8 @@ namespace indigo
void _writeAtom(int idx, bool aromatic, bool lowercase, int chirality) const;
void _writeChirality(int chirality) const;
void _writeCharge(int charge) const;
void _writeSmartsAtom(int idx, QueryMolecule::Atom* atom, int chirality, int depth, bool has_or_parent, bool has_not_parent) const;
void _writeSmartsBond(int idx, QueryMolecule::Bond* bond, bool has_or_parent) const;
static void _writeSmartsAtom(Output& output, QueryMolecule::Atom* atom, int aam, int chirality, int depth, bool has_or_parent, bool has_not_parent);
static void _writeSmartsBond(Output& output, QueryMolecule::Bond* bond, bool has_or_parent);
void _markCisTrans();
void _banSlashes();
int _calcBondDirection(int idx, int vprev);
Expand Down
12 changes: 10 additions & 2 deletions core/indigo-core/molecule/src/molecule_json_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "molecule/molecule.h"
#include "molecule/molecule_sgroups.h"
#include "molecule/query_molecule.h"
#include "molecule/smiles_loader.h"

using namespace rapidjson;
using namespace indigo;
Expand Down Expand Up @@ -524,7 +525,10 @@ void MoleculeJsonLoader::parseAtoms(const rapidjson::Value& atoms, BaseMolecule&
auto qProps = a["queryProperties"].GetObject();
if (qProps.HasMember("customQuery"))
{
// Read custom query
std::string customQuery = qProps["customQuery"].GetString();
std::unique_ptr<QueryMolecule::Atom> atom = make_unique<QueryMolecule::Atom>();
SmilesLoader::readSmartsAtomStr(customQuery, atom);
_pqmol->resetAtom(atom_idx, atom.release());
}
else
{
Expand Down Expand Up @@ -676,7 +680,9 @@ void MoleculeJsonLoader::parseBonds(const rapidjson::Value& bonds, BaseMolecule&
if (b.HasMember("customQuery"))
{
std::string customQuery = b["customQuery"].GetString();
// 2do process custom query
std::unique_ptr<QueryMolecule::Bond> bond = make_unique<QueryMolecule::Bond>();
SmilesLoader::readSmartsBondStr(customQuery, bond);
_pqmol->resetBond(bond_idx, bond.release());
}

if (b.HasMember("cip"))
Expand Down Expand Up @@ -865,6 +871,8 @@ void MoleculeJsonLoader::parseSGroups(const rapidjson::Value& sgroups, BaseMolec
_pqmol->components[atom_idx] = components_count;
}
}
else
throw Error("queryProperties is allowed only for queries");
continue;
}
int sg_type = SGroup::getType(sg_type_str.c_str());
Expand Down
Loading

0 comments on commit a333f0f

Please sign in to comment.