Debian Codegraph @

avogadro/core/residue.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "residue.h"
7 #include "molecule.h"
8 #include "residuecolors.h"
9 #include "residuedata.h"
10
11 namespace Avogadro::Core {
12
13 Residue::Residue(std::string& name)
14 : m_residueName(name), m_residueId(0), m_chainId('A'), m_heterogen(false),
15 m_color(0, 0, 0), m_customColorSet(false), m_secondaryStructure(undefined)
16 {
17 }
18
19 Residue::Residue(std::string& name, Index& number)
20 : m_residueName(name), m_residueId(number), m_chainId('A'),
21 m_heterogen(false), m_color(0, 0, 0), m_customColorSet(false),
22 m_secondaryStructure(undefined)
23 {
24 }
25
26 Residue::Residue(std::string& name, Index& number, char& id)
27 : m_residueName(name), m_residueId(number), m_chainId(id), m_heterogen(false),
28 m_color(0, 0, 0), m_customColorSet(false), m_secondaryStructure(undefined)
29 {
30 }
31
32 Residue::Residue(const Residue& other)
33 : m_residueName(other.m_residueName), m_residueId(other.m_residueId),
34 m_chainId(other.m_chainId), m_atomNameMap(other.m_atomNameMap),
35 m_heterogen(other.m_heterogen), m_color(other.m_color),
36 m_customColorSet(other.m_customColorSet),
37 m_secondaryStructure(other.m_secondaryStructure)
38 {
39 }
40
41 Residue& Residue::operator=(Residue other)
42 {
43 m_residueName = other.m_residueName;
44 m_residueId = other.m_residueId;
45 m_chainId = other.m_chainId;
46 m_atomNameMap = other.m_atomNameMap;
47 m_heterogen = other.m_heterogen;
48 m_color = other.m_color;
49 m_customColorSet = other.m_customColorSet;
50 m_secondaryStructure = other.m_secondaryStructure;
51 return *this;
52 }
53
54 void Residue::addResidueAtom(const std::string& name, const Atom& atom)
55 {
56 m_atomNameMap.insert(std::pair<std::string, Atom>(name, atom));
57 }
58
59 std::vector<Atom> Residue::residueAtoms() const
60 {
61 std::vector<Atom> res;
62 for (const auto& it : m_atomNameMap) {
63 res.push_back(it.second);
64 }
65 return res;
66 }
67
68 Atom Residue::atomByName(std::string name) const
69 {
70 Atom empty;
71 auto search = m_atomNameMap.find(name);
72 if (search != m_atomNameMap.end()) {
73 return search->second;
74 }
75
76 return empty;
77 }
78
79 std::string Residue::atomName(const Atom atom) const
80 {
81 for (const auto& it : m_atomNameMap) {
82 if (it.second == atom) {
83 return it.first;
84 }
85 }
86 return "";
87 }
88
89 std::string Residue::atomName(const Index index) const
90 {
91 for (const auto& it : m_atomNameMap) {
92 if (it.second.index() == index) {
93 return it.first;
94 }
95 }
96 return "";
97 }
98
99 void Residue::resolveResidueBonds(Molecule& mol)
100 {
101 std::vector<std::pair<std::string, std::string>> bondSeq;
102 if (residueDict.find(m_residueName) != residueDict.end()) {
103 size_t i = 0;
104 bondSeq = residueDict[m_residueName].residueSingleBonds();
105 for (i = 0; i < bondSeq.size(); ++i) {
106 if (m_atomNameMap.find(bondSeq[i].first) != m_atomNameMap.end() &&
107 m_atomNameMap.find(bondSeq[i].second) != m_atomNameMap.end()) {
108 mol.addBond(m_atomNameMap[bondSeq[i].first],
109 m_atomNameMap[bondSeq[i].second], 1);
110 }
111 }
112 bondSeq = residueDict[m_residueName].residueDoubleBonds();
113 for (i = 0; i < bondSeq.size(); ++i) {
114 if (m_atomNameMap.find(bondSeq[i].first) != m_atomNameMap.end() &&
115 m_atomNameMap.find(bondSeq[i].second) != m_atomNameMap.end()) {
116 mol.addBond(m_atomNameMap[bondSeq[i].first],
117 m_atomNameMap[bondSeq[i].second], 2);
118 }
119 }
120 }
121 }
122
123 int Residue::atomicNumber(std::string name) const
124 {
125 auto search = m_atomNameMap.find(name);
126 if (search != m_atomNameMap.end()) {
127 return search->second.atomicNumber();
128 }
129
130 return 0;
131 }
132
133 void Residue::setColor(const Vector3ub color)
134 {
135 m_customColorSet = true;
136 m_color = color;
137 }
138
139 Vector3ub Residue::color() const
140 {
141 if (m_customColorSet)
142 return m_color;
143
144 // default return a color for the chain
145 int offset = 0;
146 if (m_chainId >= 'A' && m_chainId <= 'Z')
147 offset = m_chainId - 'A';
148 else if (m_chainId >= 'a' && m_chainId <= 'z')
149 offset = m_chainId - 'a';
150 else if (m_chainId >= '0' && m_chainId <= '9')
151 offset = m_chainId - '0' + 15; // starts at 'P'
152
153 return Vector3ub(chain_color[offset]);
154 }
155
156 bool Residue::hasAtomByIndex(Index index) const
157 {
158 for (const auto& atom : residueAtoms()) {
159 if (atom.index() == index) {
160 return true;
161 }
162 }
163 return false;
164 }
165
166 } // namespace Avogadro::Core