RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
Pairlist.h
Go to the documentation of this file.
1//
2//
3// Copyright (C) 2020 Schrödinger, LLC
4//
5// @@ All Rights Reserved @@
6// This file is part of the RDKit.
7// The contents are covered by the terms of the BSD license
8// which is included in the file license.txt, found at the root
9// of the RDKit source tree.
10//
11#pragma once
12
13#include <cstdint>
14#include <sstream>
15#include <string>
16#include <vector>
17
18#include "../Descriptor.h"
19
20namespace RDKit {
21namespace CIPLabeler {
22
23/**
24 * Implementation of a descriptor list that allows descriptors to be added and
25 * ignored.
26 *
27 * @see Descriptor
28 */
29class PairList {
30 public:
31 static Descriptor ref(Descriptor descriptor) {
32 switch (descriptor) {
33 case Descriptor::R:
34 case Descriptor::M:
36 return Descriptor::R;
37 case Descriptor::S:
38 case Descriptor::P:
40 return Descriptor::S;
41 default:
42 return Descriptor::NONE;
43 }
44 }
45
46 PairList() = default;
47
49
50 /**
51 * Creates a new list from a provided head and tail. The head and tail
52 * ignored descriptors are first transferred and then their descriptors. In
53 * either list, descriptors that are ignored by the other will be not be
54 * added to the new instance.
55 *
56 * @param head the head of the list (prefix)
57 * @param tail the tail of the list (suffix)
58 */
59 PairList(const PairList &head, const PairList &tail) {
60 // add descriptors to the new instance (ignored descriptors not added)
61 addAll(head.d_descriptors);
62 addAll(tail.d_descriptors);
63 }
64
66 if (d_descriptors.empty()) {
67 throw std::runtime_error("Cannot get a reference from an empty PairList");
68 }
69 return ref(d_descriptors[0]);
70 }
71
72 /**
73 * Adds a descriptor to the descriptor list. If the provided descriptor is
74 * present in the ignore set the descriptor will not be added.
75 *
76 * @param descriptor the descriptor to add.
77 * @return whether the descriptor was added to the list
78 */
79
80 bool add(Descriptor descriptor) {
81 switch (descriptor) {
82 case Descriptor::R:
83 case Descriptor::S:
84 case Descriptor::M:
85 case Descriptor::P:
88 d_descriptors.push_back(ref(descriptor));
89 return true;
90 default:
91 return false;
92 }
93 }
94
95 /**
96 * Adds multiple descriptors to the descriptor list. If the descriptor is
97 * present in the ignore set it will not be added to the list.
98 *
99 * @param descriptors a collection of descriptors to be added
100 */
101 template <typename T>
102 void addAll(const T &descriptors) {
103 for (const auto &descriptor : descriptors) {
104 add(descriptor);
105 }
106 }
107
108 int compareTo(const PairList &that) const {
109 if (d_descriptors.size() != that.d_descriptors.size()) {
110 throw std::runtime_error("Descriptor lists should be the same length!");
111 }
112 if (d_descriptors.empty()) {
113 return 0;
114 }
115 Descriptor thisRef = d_descriptors[0];
116 Descriptor thatRef = that.d_descriptors[0];
117 for (auto i = 1u; i < d_descriptors.size(); ++i) {
118 if (thisRef == d_descriptors[i] && thatRef != that.d_descriptors[i]) {
119 return +1;
120 }
121 if (thisRef != d_descriptors[i] && thatRef == that.d_descriptors[i]) {
122 return -1;
123 }
124 }
125 return 0;
126 }
127
128 bool operator<(const PairList &that) const { return compareTo(that) == -1; }
129
130 std::string toString() const {
131 // handles cases that would break the toString method
132 if (d_descriptors.empty() || d_descriptors[0] == Descriptor::NONE) {
133 return "";
134 }
135
136 std::stringstream ss;
137 auto basis = d_descriptors[0];
138 ss << to_string(basis) << ':';
139
140 basis = ref(basis);
141
142 // build like (l) / unlike (u) descriptor pairing
143 for (auto it = d_descriptors.begin() + 1; it != d_descriptors.end(); ++it) {
144 ss << (basis == ref(*it) ? "l" : "u");
145 }
146
147 return ss.str();
148 }
149
150 private:
151 std::vector<Descriptor> d_descriptors;
152};
153
154} // namespace CIPLabeler
155} // namespace RDKit
bool operator<(const PairList &that) const
Definition Pairlist.h:128
std::string toString() const
Definition Pairlist.h:130
PairList(Descriptor ref)
Definition Pairlist.h:48
static Descriptor ref(Descriptor descriptor)
Definition Pairlist.h:31
int compareTo(const PairList &that) const
Definition Pairlist.h:108
bool add(Descriptor descriptor)
Definition Pairlist.h:80
PairList(const PairList &head, const PairList &tail)
Definition Pairlist.h:59
void addAll(const T &descriptors)
Definition Pairlist.h:102
Descriptor getRefDescriptor() const
Definition Pairlist.h:65
static std::string to_string(const Descriptor &desc)
Definition Descriptor.h:54
Std stuff.