Skip to content

Commit

Permalink
Change variant to ambiguous in code. (#2632)
Browse files Browse the repository at this point in the history
  • Loading branch information
AliaksandrDziarkach authored Nov 14, 2024
1 parent a75a3f7 commit 5e2d3bc
Show file tree
Hide file tree
Showing 21 changed files with 665 additions and 133 deletions.
3 changes: 3 additions & 0 deletions api/c/indigo/src/indigo_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

#include "layout/reaction_layout.h"

#include "molecule/ket_document.h"
#include "molecule/molecule_fingerprint.h"
#include "molecule/molecule_gross_formula.h"
#include "molecule/molecule_ionize.h"
Expand Down Expand Up @@ -188,6 +189,8 @@ class DLLEXPORT IndigoObject
virtual Molecule& getMolecule();
virtual const Molecule& getMolecule() const;

virtual KetDocument& getKetDocument();

virtual BaseReaction& getBaseReaction();
virtual QueryReaction& getQueryReaction();
virtual Reaction& getReaction();
Expand Down
1 change: 1 addition & 0 deletions api/c/indigo/src/indigo_ket_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
***************************************************************************/

#include "indigo_ket_document.h"
#include "molecule/ket_document_json_loader.h"

//
// IndigoKetDocument
Expand Down
2 changes: 1 addition & 1 deletion api/c/indigo/src/indigo_ket_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DLLEXPORT IndigoKetDocument : public IndigoObject

const char* debugInfo() const override;

KetDocument& getKetDocument();
KetDocument& getKetDocument() override;

private:
KetDocument _document;
Expand Down
17 changes: 15 additions & 2 deletions api/c/indigo/src/indigo_molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "molecule/molecule_gross_formula.h"
#include "molecule/molecule_inchi.h"
#include "molecule/molecule_json_loader.h"
#include "molecule/molecule_json_saver.h"
#include "molecule/molecule_mass.h"
#include "molecule/molecule_name_parser.h"
#include "molecule/molecule_savers.h"
Expand Down Expand Up @@ -102,6 +103,11 @@ BaseMolecule& IndigoMolecule::getBaseMolecule()
return mol;
}

KetDocument& IndigoMolecule::getKetDocument()
{
return getBaseMolecule().getKetDocument();
}

const char* IndigoMolecule::getName()
{
if (mol.name.ptr() == 0)
Expand Down Expand Up @@ -549,9 +555,16 @@ CEXPORT int indigoLoadKetDocument(int source)
INDIGO_BEGIN
{
IndigoObject& obj = self.getObject(source);
auto& scanner = IndigoScanner::get(obj);
std::string json_str;
scanner.readAll(json_str);
if (IndigoBaseMolecule::is(obj))
{
json_str = indigoJson(source);
}
else
{
auto& scanner = IndigoScanner::get(obj);
scanner.readAll(json_str);
}
std::unique_ptr<IndigoKetDocument> docptr = std::make_unique<IndigoKetDocument>();
KetDocumentJsonLoader loader{};
loader.parseJson(json_str, docptr->get());
Expand Down
2 changes: 2 additions & 0 deletions api/c/indigo/src/indigo_molecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class DLLEXPORT IndigoMolecule : public IndigoBaseMolecule
Molecule& getMolecule() override;
const Molecule& getMolecule() const override;

KetDocument& getKetDocument() override;

const char* getName() override;

static IndigoMolecule* cloneFrom(IndigoObject& obj);
Expand Down
5 changes: 5 additions & 0 deletions api/c/indigo/src/indigo_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ QueryReaction& IndigoObject::getQueryReaction()
throw IndigoError("%s is not a query reaction", debugInfo());
}

KetDocument& IndigoObject::getKetDocument()
{
throw IndigoError("%s is not a ket document", debugInfo());
}

IndigoObject* IndigoObject::next()
{
throw IndigoError("%s is not iterable", debugInfo());
Expand Down
1 change: 1 addition & 0 deletions api/c/tests/unit/tests/formats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <gtest/gtest.h>

#include "molecule/ket_document.h"
#include <molecule/molecule_mass.h>

#include <indigo.h>
Expand Down
6 changes: 6 additions & 0 deletions core/indigo-core/molecule/base_molecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace indigo
class Molecule;
class QueryMolecule;
class MetaDataStorage;
class KetDocument;

class DLLEXPORT BaseMolecule : public Graph
{
Expand Down Expand Up @@ -550,6 +551,8 @@ namespace indigo
void setAlias(int atom_idx, const char* alias);
void removeAlias(int atom_idx);

KetDocument& getKetDocument();

DECL_ERROR;

protected:
Expand Down Expand Up @@ -620,6 +623,9 @@ namespace indigo

RedBlackObjMap<int, Array<char>> aliases;
RedBlackObjMap<int, PropertiesMap> _properties;

std::optional<std::unique_ptr<KetDocument>> _document;
int _document_revision;
};

} // namespace indigo
Expand Down
42 changes: 26 additions & 16 deletions core/indigo-core/molecule/ket_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ namespace indigo
MonomerTemplate& addMonomerTemplate(const std::string& id, const std::string& monomer_class, IdtAlias idt_alias, bool unresolved = false);
void addMonomerTemplate(const MonomerTemplate& monomer_template);

KetVariantMonomerTemplate& addVariantMonomerTemplate(const std::string& subtype, const std::string& id, const std::string& name, IdtAlias idt_alias,
std::vector<KetVariantMonomerOption>& options);
std::unique_ptr<KetBaseMonomer>& addVariantMonomer(const std::string& alias, const std::string& template_id);
std::unique_ptr<KetBaseMonomer>& addVariantMonomer(const std::string& id, const std::string& alias, const std::string& template_id);
std::unique_ptr<KetBaseMonomer>& addVariantMonomer(const std::string& id, const std::string& alias, const std::string& template_id,
const std::string& ref);
KetAmbiguousMonomerTemplate& addAmbiguousMonomerTemplate(const std::string& subtype, const std::string& id, const std::string& name, IdtAlias idt_alias,
std::vector<KetAmbiguousMonomerOption>& options);
std::unique_ptr<KetBaseMonomer>& addAmbiguousMonomer(const std::string& alias, const std::string& template_id);
std::unique_ptr<KetBaseMonomer>& addAmbiguousMonomer(const std::string& id, const std::string& alias, const std::string& template_id);
std::unique_ptr<KetBaseMonomer>& addAmbiguousMonomer(const std::string& id, const std::string& alias, const std::string& template_id,
const std::string& ref);

using molecules_map = std::map<std::string, KetMolecule>;
using templates_map = std::map<std::string, MonomerTemplate>;
using monomers_map = std::map<std::string, std::unique_ptr<KetBaseMonomer>>;
using variant_templates_map = std::map<std::string, KetVariantMonomerTemplate>;
using ambiguous_templates_map = std::map<std::string, KetAmbiguousMonomerTemplate>;

inline const molecules_map& molecules() const
{
Expand Down Expand Up @@ -111,32 +111,42 @@ namespace indigo
return _connections;
};

// Return list of connections between simple polimers
// i.e. non-backbone and not sugar-base connections
// connections that create a cycle place here too
// parseSimplePolymers should be called to fill this list
const std::vector<KetConnection> nonSequenceConnections() const
{
return _non_sequence_connections;
};

int original_format;

const variant_templates_map& variantTemplates() const
const ambiguous_templates_map& ambiguousTemplates() const
{
return _variant_templates;
return _ambiguous_templates;
};

const std::vector<std::string>& variantTemplatesIds() const
const std::vector<std::string>& ambiguousTemplatesIds() const
{
return _variant_templates_ids;
return _ambiguous_templates_ids;
};

BaseMolecule& getBaseMolecule();

bool hasVariantMonomerTemplate(const std::string& id) const
bool hasAmbiguousMonomerTemplate(const std::string& id) const
{
return _variant_templates.find(id) != _variant_templates.end();
return _ambiguous_templates.find(id) != _ambiguous_templates.end();
};

void processVariantMonomerTemplates();
void processAmbiguousMonomerTemplates();

// Parse list of monomer and ist of connections stored in document and return list of simple polimers in term of HELM
// Each simple polymer represented as list of monomer IDs.
// Monomers connected from m[i] R2 to m[i+1] R1 for peptides
// For RNA/DNA monomer placed in order Sugar-Base-Phosphate-Sugar... with standard connections
// Each CHEM returned as separate simple polymer
// Also store non-standard or creating cycle connections in nonSequenceConnections list
void parseSimplePolymers(std::vector<std::deque<std::string>>& sequences, bool for_idt = false);

MonomerClass getMonomerClass(const KetBaseMonomer& monomer) const;
Expand Down Expand Up @@ -190,8 +200,8 @@ namespace indigo
std::map<std::string, std::string> _monomer_ref_to_id;
templates_map _templates;
std::vector<std::string> _templates_ids;
variant_templates_map _variant_templates;
std::vector<std::string> _variant_templates_ids;
ambiguous_templates_map _ambiguous_templates;
std::vector<std::string> _ambiguous_templates_ids;
std::vector<KetConnection> _connections;
std::vector<KetConnection> _non_sequence_connections;
std::map<std::string, KetBaseMonomerTemplate::TemplateType> _template_id_to_type;
Expand Down
8 changes: 4 additions & 4 deletions core/indigo-core/molecule/ket_document_json_saver.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ namespace indigo
class MonomerTemplate;
class KetMolecule;
class KetMonomer;
class KetVariantMonomer;
class KetVariantMonomerTemplate;
class KetAmbiguousMonomer;
class KetAmbiguousMonomerTemplate;
class KetDocument;

class DLLEXPORT KetDocumentJsonSaver
Expand All @@ -61,8 +61,8 @@ namespace indigo
protected:
static void saveMolecule(JsonWriter& writer, const std::string& ref, const KetMolecule& molecule);
static void saveMonomer(JsonWriter& writer, const KetMonomer& monomer);
static void saveVariantMonomer(JsonWriter& writer, const KetVariantMonomer& monomer);
static void saveVariantMonomerTemplate(JsonWriter& writer, const KetVariantMonomerTemplate& monomer_template);
static void saveVariantMonomer(JsonWriter& writer, const KetAmbiguousMonomer& monomer);
static void saveVariantMonomerTemplate(JsonWriter& writer, const KetAmbiguousMonomerTemplate& monomer_template);

DECL_ERROR;

Expand Down
23 changes: 12 additions & 11 deletions core/indigo-core/molecule/ket_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,7 @@ namespace indigo

const TYPE connType() const;

// connection type could be "single" or "hydrogen"
inline const std::string connectionType() const
{
return _connection_type;
Expand Down Expand Up @@ -836,11 +837,11 @@ namespace indigo
std::optional<std::string> _id;
};

class DLLEXPORT KetVariantMonomerOption : public KetObjWithProps
class DLLEXPORT KetAmbiguousMonomerOption : public KetObjWithProps
{
public:
DECL_ERROR;
KetVariantMonomerOption(const std::string& templateId) : _templateId(templateId){};
KetAmbiguousMonomerOption(const std::string& templateId) : _templateId(templateId){};

const std::string& templateId() const
{
Expand Down Expand Up @@ -881,7 +882,7 @@ namespace indigo
enum class TemplateType
{
MonomerTemplate,
VariantMonomerTemplate
AmbiguousMonomerTemplate
};

KetBaseMonomerTemplate(TemplateType template_type, const std::string& id, MonomerClass monomer_class, IdtAlias idt_alias)
Expand Down Expand Up @@ -950,16 +951,16 @@ namespace indigo
IdtAlias _idt_alias;
};

class DLLEXPORT KetVariantMonomerTemplate : public KetBaseMonomerTemplate
class DLLEXPORT KetAmbiguousMonomerTemplate : public KetBaseMonomerTemplate
{
public:
DECL_ERROR;

inline static std::string ref_prefix = "ambiguousMonomerTemplate-";

KetVariantMonomerTemplate(const std::string& subtype, const std::string& id, const std::string& alias, IdtAlias idt_alias,
const std::vector<KetVariantMonomerOption>& options)
: KetBaseMonomerTemplate(TemplateType::VariantMonomerTemplate, id, MonomerClass::Unknown, idt_alias), _subtype(subtype), _alias(alias),
KetAmbiguousMonomerTemplate(const std::string& subtype, const std::string& id, const std::string& alias, IdtAlias idt_alias,
const std::vector<KetAmbiguousMonomerOption>& options)
: KetBaseMonomerTemplate(TemplateType::AmbiguousMonomerTemplate, id, MonomerClass::Unknown, idt_alias), _subtype(subtype), _alias(alias),
_options(options){};

const std::string& subtype() const
Expand All @@ -972,25 +973,25 @@ namespace indigo
return _alias;
};

const std::vector<KetVariantMonomerOption>& options() const
const std::vector<KetAmbiguousMonomerOption>& options() const
{
return _options;
};

private:
std::string _subtype;
std::string _alias;
std::vector<KetVariantMonomerOption> _options;
std::vector<KetAmbiguousMonomerOption> _options;
};

class DLLEXPORT KetVariantMonomer : public KetBaseMonomer
class DLLEXPORT KetAmbiguousMonomer : public KetBaseMonomer
{
public:
DECL_ERROR;

inline static std::string ref_prefix = "ambiguousMonomer-";

KetVariantMonomer(const std::string& id, const std::string& alias, const std::string& template_id)
KetAmbiguousMonomer(const std::string& id, const std::string& alias, const std::string& template_id)
: KetBaseMonomer(MonomerType::AmbiguousMonomer, id, alias, template_id)
{
_ref = ref_prefix + _id;
Expand Down
16 changes: 8 additions & 8 deletions core/indigo-core/molecule/sequence_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ namespace indigo
void addMonomer(KetDocument& mol, const std::string& monomer, SeqType seq_type, bool mixed = false);
void addAminoAcid(KetDocument& document, const std::string& monomer, bool mixed = false);
void addNucleotideTemplates(KetDocument& document, const std::string& base_alias, const std::string& sugar_alias, const std::string& phosphate_alias,
bool variant = false);
bool ambiguous = false);
void addNucleotide(KetDocument& document, const std::string& base_alias, const std::string& sugar_alias, const std::string& phosphate_alias,
bool phosphate_at_left = true, bool variant = false);
bool phosphate_at_left = true, bool ambiguous = false);

int addTemplateAtom(BaseMolecule& mol, const char* alias, const char* monomer_class, int seq_id);
void addTemplateBond(BaseMolecule& mol, int left_idx, int right_idx, bool branch = false);
Expand All @@ -108,11 +108,11 @@ namespace indigo

static void check_monomer_place(std::string& idt_alias, IdtModification mon_mod, IdtModification alias_mod, bool has_prev_mon);

using variant_template_opts = std::pair<bool, std::vector<std::pair<std::string, std::optional<float>>>>;
using MonomerInfo = std::tuple<std::string, std::string, std::string, variant_template_opts>;
using ambiguous_template_opts = std::pair<bool, std::vector<std::pair<std::string, std::optional<float>>>>;
using MonomerInfo = std::tuple<std::string, std::string, std::string, ambiguous_template_opts>;

const std::string checkAddVariantMonomerTemplate(KetDocument& document, const std::string& alias, MonomerClass monomer_class,
variant_template_opts& options);
const std::string checkAddAmbiguousMonomerTemplate(KetDocument& document, const std::string& alias, MonomerClass monomer_class,
ambiguous_template_opts& options);
size_t addKetMonomer(KetDocument& document, MonomerInfo info, MonomerClass monomer_class, const Vec3f& pos);
int readCount(std::string& count, Scanner& _scanner);

Expand All @@ -132,8 +132,8 @@ namespace indigo
MonomerTemplateLibrary& _library;
std::map<std::string, std::string> _alias_to_id;
std::map<std::string, std::string> _var_alias_to_id;
int _unknown_variants_count;
std::map<variant_template_opts, std::string> _opts_to_template_id;
int _unknown_ambiguous_count;
std::map<ambiguous_template_opts, std::string> _opts_to_template_id;
};

} // namespace indigo
Expand Down
Loading

0 comments on commit 5e2d3bc

Please sign in to comment.