| 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 "secondarystructure.h" |
| 7 |
|
| 8 |
#include <cstdlib> |
| 9 |
#include <limits> |
| 10 |
|
| 11 |
#include <avogadro/core/molecule.h> |
| 12 |
#include <avogadro/core/residue.h> |
| 13 |
|
| 14 |
namespace Avogadro::Core { |
| 15 |
|
| 16 |
SecondaryStructureAssigner::SecondaryStructureAssigner(Molecule* mol) |
| 17 |
: m_molecule(mol) |
| 18 |
{ |
| 19 |
} |
| 20 |
|
| 21 |
SecondaryStructureAssigner::~SecondaryStructureAssigner() |
| 22 |
{ |
| 23 |
for (auto* hBond : m_hBonds) |
| 24 |
delete hBond; |
| 25 |
m_hBonds.clear(); |
| 26 |
} |
| 27 |
|
| 28 |
//! Adapted from 3DMol.js parsers.js |
| 29 |
//! https://github.com/3dmol/3Dmol.js/blob/master/3Dmol/parsers.js |
| 30 |
void SecondaryStructureAssigner::assign(Molecule* mol) |
| 31 |
{ |
| 32 |
m_molecule = mol; |
| 33 |
|
| 34 |
// Clear the current secondary structure |
| 35 |
auto residueCount = m_molecule->residues().size(); |
| 36 |
for (auto residue : m_molecule->residues()) |
| 37 |
residue.setSecondaryStructure(Residue::SecondaryStructure::undefined); |
| 38 |
|
| 39 |
// First assign the hydrogen bonds along the backbone |
| 40 |
assignBackboneHydrogenBonds(); |
| 41 |
|
| 42 |
float infinity = std::numeric_limits<float>::max(); |
| 43 |
// Then assign the alpha helix by going through the hBond records |
| 44 |
for (auto* hBond : m_hBonds) { |
| 45 |
if (hBond->distSquared < infinity) { |
| 46 |
// check to see how far apart the residues are |
| 47 |
int separation = std::abs(int(hBond->residue - hBond->residuePair)); |
| 48 |
|
| 49 |
switch (separation) { |
| 50 |
case 3: { // 3-10 |
| 51 |
m_molecule->residue(hBond->residue) |
| 52 |
.setSecondaryStructure(Residue::SecondaryStructure::helix310); |
| 53 |
break; |
| 54 |
} |
| 55 |
case 4: { // alpha |
| 56 |
m_molecule->residue(hBond->residue) |
| 57 |
.setSecondaryStructure(Residue::SecondaryStructure::alphaHelix); |
| 58 |
break; |
| 59 |
} |
| 60 |
case 5: { // pi |
| 61 |
m_molecule->residue(hBond->residue) |
| 62 |
.setSecondaryStructure(Residue::SecondaryStructure::piHelix); |
| 63 |
break; |
| 64 |
} |
| 65 |
default: { |
| 66 |
break; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
// If there are less than 3 residues, don't do anything |
| 73 |
if (residueCount < 3) |
| 74 |
return; |
| 75 |
|
| 76 |
// Plug gaps in the helix |
| 77 |
for (size_t i = 1; i < residueCount - 1; ++i) { |
| 78 |
// check that before and after this residue are in the same chain |
| 79 |
if (m_molecule->residue(i).chainId() != |
| 80 |
m_molecule->residue(i - 1).chainId() || |
| 81 |
m_molecule->residue(i).chainId() != |
| 82 |
m_molecule->residue(i + 1).chainId()) |
| 83 |
continue; |
| 84 |
|
| 85 |
auto current = m_molecule->residue(i).secondaryStructure(); |
| 86 |
auto previous = m_molecule->residue(i - 1).secondaryStructure(); |
| 87 |
auto next = m_molecule->residue(i + 1).secondaryStructure(); |
| 88 |
// is there a gap in a helix (before and after are helix, so this should be) |
| 89 |
if (previous != Residue::SecondaryStructure::undefined && |
| 90 |
previous == next && current != previous) |
| 91 |
m_molecule->residue(i).setSecondaryStructure(previous); |
| 92 |
} |
| 93 |
|
| 94 |
// Then assign the beta sheet - but only if a residue isn't assigned |
| 95 |
for (auto* hBond : m_hBonds) { |
| 96 |
if (hBond->distSquared < infinity) { |
| 97 |
if (m_molecule->residue(hBond->residue).secondaryStructure() == |
| 98 |
Residue::SecondaryStructure::undefined) |
| 99 |
m_molecule->residue(hBond->residue) |
| 100 |
.setSecondaryStructure(Residue::SecondaryStructure::maybeBeta); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Check that sheets bond to other sheets |
| 105 |
for (auto* hBond : m_hBonds) { |
| 106 |
if (hBond->distSquared < infinity) { |
| 107 |
// find the match |
| 108 |
auto current = m_molecule->residue(hBond->residue); |
| 109 |
auto match = m_molecule->residue(hBond->residuePair); |
| 110 |
|
| 111 |
// if we're "maybe" beta see if the match is either beta or "maybe" |
| 112 |
if (current.secondaryStructure() == |
| 113 |
Residue::SecondaryStructure::maybeBeta && |
| 114 |
(match.secondaryStructure() == |
| 115 |
Residue::SecondaryStructure::maybeBeta || |
| 116 |
match.secondaryStructure() == |
| 117 |
Residue::SecondaryStructure::betaSheet)) { |
| 118 |
// we can be sure now |
| 119 |
m_molecule->residue(hBond->residue) |
| 120 |
.setSecondaryStructure(Residue::SecondaryStructure::betaSheet); |
| 121 |
m_molecule->residue(hBond->residuePair) |
| 122 |
.setSecondaryStructure(Residue::SecondaryStructure::betaSheet); |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// Plug gaps in the beta sheet |
| 128 |
for (size_t i = 1; i < residueCount - 1; ++i) { |
| 129 |
// check that before and after this residue are in the same chain |
| 130 |
if (m_molecule->residue(i).chainId() != |
| 131 |
m_molecule->residue(i - 1).chainId() || |
| 132 |
m_molecule->residue(i).chainId() != |
| 133 |
m_molecule->residue(i + 1).chainId()) |
| 134 |
continue; |
| 135 |
|
| 136 |
auto current = m_molecule->residue(i).secondaryStructure(); |
| 137 |
auto previous = m_molecule->residue(i - 1).secondaryStructure(); |
| 138 |
auto next = m_molecule->residue(i + 1).secondaryStructure(); |
| 139 |
// is there a gap in a beta sheet? |
| 140 |
if (previous == Residue::SecondaryStructure::betaSheet && |
| 141 |
previous == next && current != previous) |
| 142 |
m_molecule->residue(i).setSecondaryStructure(previous); |
| 143 |
} |
| 144 |
|
| 145 |
// remove singletons |
| 146 |
for (size_t i = 1; i < residueCount - 1; ++i) { |
| 147 |
// check that before and after this residue are in the same chain |
| 148 |
if (m_molecule->residue(i).chainId() != |
| 149 |
m_molecule->residue(i - 1).chainId() || |
| 150 |
m_molecule->residue(i).chainId() != |
| 151 |
m_molecule->residue(i + 1).chainId()) |
| 152 |
continue; |
| 153 |
|
| 154 |
auto current = m_molecule->residue(i); |
| 155 |
// clear maybeBeta assignments (e.g. short bits) |
| 156 |
if (current.secondaryStructure() == Residue::SecondaryStructure::maybeBeta) |
| 157 |
m_molecule->residue(i).setSecondaryStructure( |
| 158 |
Residue::SecondaryStructure::undefined); |
| 159 |
|
| 160 |
if (current.secondaryStructure() != |
| 161 |
Residue::SecondaryStructure::undefined) { |
| 162 |
// make sure we don't have one lone odd assignment |
| 163 |
if (current.secondaryStructure() != |
| 164 |
m_molecule->residue(i - 1).secondaryStructure() && |
| 165 |
current.secondaryStructure() != |
| 166 |
m_molecule->residue(i + 1).secondaryStructure()) { |
| 167 |
m_molecule->residue(i).setSecondaryStructure( |
| 168 |
Residue::SecondaryStructure::undefined); |
| 169 |
} |
| 170 |
} |
| 171 |
} // end loop over residues (for singletons) |
| 172 |
} |
| 173 |
|
| 174 |
//! Adapted from 3DMol.js parsers.js assignBackboneHBond |
| 175 |
//! https://github.com/3dmol/3Dmol.js/blob/master/3Dmol/parsers.js |
| 176 |
void SecondaryStructureAssigner::assignBackboneHydrogenBonds() |
| 177 |
{ |
| 178 |
if (m_molecule == nullptr) |
| 179 |
return; |
| 180 |
|
| 181 |
const float maxDist = 3.2; // in Angstroms |
| 182 |
const float maxDistSq = maxDist * maxDist; // 10.24 |
| 183 |
|
| 184 |
// delete any previous records |
| 185 |
for (auto* hBond : m_hBonds) |
| 186 |
delete hBond; |
| 187 |
m_hBonds.clear(); |
| 188 |
|
| 189 |
// Loop over the backbone atoms |
| 190 |
// we're just considering N and O (on a peptide) |
| 191 |
unsigned int idx = 0; // track the residue index |
| 192 |
for (const auto& residue : m_molecule->residues()) { |
| 193 |
unsigned int residueId = idx++; |
| 194 |
if (residue.isHeterogen()) |
| 195 |
continue; |
| 196 |
|
| 197 |
auto oxygen = residue.atomByName("O"); |
| 198 |
if (oxygen.isValid()) { |
| 199 |
auto* oRecord = new hBondRecord(); |
| 200 |
oRecord->atom = oxygen.index(); |
| 201 |
oRecord->atomZ = oxygen.position3d()[2]; |
| 202 |
oRecord->distSquared = std::numeric_limits<float>::max(); |
| 203 |
oRecord->residue = residueId; |
| 204 |
oRecord->residuePair = residueId; // just a placeholder |
| 205 |
m_hBonds.push_back(oRecord); |
| 206 |
} |
| 207 |
|
| 208 |
auto nitrogen = residue.atomByName("N"); |
| 209 |
if (nitrogen.isValid()) { |
| 210 |
auto* nRecord = new hBondRecord(); |
| 211 |
nRecord->atom = nitrogen.index(); |
| 212 |
nRecord->atomZ = nitrogen.position3d()[2]; |
| 213 |
nRecord->distSquared = std::numeric_limits<float>::max(); |
| 214 |
nRecord->residue = residueId; |
| 215 |
nRecord->residuePair = residueId; |
| 216 |
m_hBonds.push_back(nRecord); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
if (m_hBonds.size() == 0) |
| 221 |
return; |
| 222 |
|
| 223 |
// sort by z-coordinate |
| 224 |
std::sort(m_hBonds.begin(), m_hBonds.end(), |
| 225 |
// lambda for sorting by z-coordinate [2] |
| 226 |
[](const hBondRecord* a, const hBondRecord* b) { |
| 227 |
return a->atomZ < b->atomZ; |
| 228 |
}); |
| 229 |
|
| 230 |
// now loop through the sorted list (so we can exit quickly) |
| 231 |
int n = m_hBonds.size(); |
| 232 |
for (int i = 0; i < n; ++i) { |
| 233 |
auto* recordI = m_hBonds[i]; |
| 234 |
const Residue& residueI = m_molecule->residue(recordI->residue); |
| 235 |
|
| 236 |
for (int j = i + 1; j < n; ++j) { |
| 237 |
auto* recordJ = m_hBonds[j]; |
| 238 |
const Residue& residueJ = m_molecule->residue(recordJ->residue); |
| 239 |
|
| 240 |
// skip if we're not on the same chain |
| 241 |
if (residueI.chainId() != residueJ.chainId()) |
| 242 |
continue; |
| 243 |
|
| 244 |
if (residueI.chainId() == residueJ.chainId() && |
| 245 |
std::abs(int(residueI.residueId() - residueJ.residueId())) < 3) |
| 246 |
continue; // either the same or too close to each other |
| 247 |
|
| 248 |
// compute the distance between the two atoms |
| 249 |
float zDiff = fabs(m_molecule->atomPosition3d(recordJ->atom)[2] - |
| 250 |
m_molecule->atomPosition3d(recordI->atom)[2]); |
| 251 |
if (zDiff > maxDist) // everything else is too far away |
| 252 |
break; |
| 253 |
|
| 254 |
// x and y, we just skip this atom |
| 255 |
float yDiff = fabs(m_molecule->atomPosition3d(recordJ->atom)[1] - |
| 256 |
m_molecule->atomPosition3d(recordI->atom)[1]); |
| 257 |
if (yDiff > maxDist) |
| 258 |
continue; |
| 259 |
float xDiff = fabs(m_molecule->atomPosition3d(recordJ->atom)[0] - |
| 260 |
m_molecule->atomPosition3d(recordI->atom)[0]); |
| 261 |
if (xDiff > maxDist) |
| 262 |
continue; |
| 263 |
|
| 264 |
// compute the squared distance between the two atoms |
| 265 |
float distSq = xDiff * xDiff + yDiff * yDiff + zDiff * zDiff; |
| 266 |
if (distSq > maxDistSq) |
| 267 |
continue; |
| 268 |
|
| 269 |
// if we get here, we have a potential hydrogen bond |
| 270 |
// select the one with the shortest distance |
| 271 |
if (distSq < recordI->distSquared) { |
| 272 |
recordI->distSquared = distSq; |
| 273 |
recordI->residuePair = recordJ->residue; |
| 274 |
} |
| 275 |
if (distSq < recordJ->distSquared) { |
| 276 |
recordJ->distSquared = distSq; |
| 277 |
recordJ->residuePair = recordI->residue; |
| 278 |
} |
| 279 |
} // end for(j) |
| 280 |
} // end for(i) |
| 281 |
} |
| 282 |
|
| 283 |
} // namespace Avogadro::Core |