File size: 9,233 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | import { IAtom } from "@/UI/Navigation/TreeView/TreeInterfaces";
import { TreeNode } from "@/TreeNodes/TreeNode/TreeNode";
import { TreeNodeList } from "@/TreeNodes/TreeNodeList/TreeNodeList";
import { GLModel } from "@/UI/Panels/Viewer/GLModelType";
import {
standardProteinResidues,
solventSel,
} from "../Types/ComponentSelections";
import { makeEasyParser } from "../ParseMolModels/EasyParser";
import { twoLetterElems } from "../NameVars";
import { IFileInfo } from "@/FileSystem/Types";
const chainOptions = [
"A",
"B",
"C",
"D",
"E",
"F",
"G",
"H",
"I",
"J",
"K",
"L",
"M",
"N",
"O",
"P",
"Q",
"R",
"S",
"T",
"U",
"V",
"W",
"X",
"Y",
"Z",
];
/**
* Aligns a string to the right.
*
* @param {string} str The string to align.
* @param {number} width The width to align to.
* @returns {string} The aligned string.
*/
function _rjust(str: string, width: number): string {
while (str.length < width) {
str = " " + str;
}
str = str.substring(str.length - width);
return str;
}
/**
* Aligns a string to the left.
*
* @param {string} str The string to align.
* @param {number} width The width to align to.
* @returns {string} The aligned string.
*/
function _ljust(str: string, width: number): string {
while (str.length < width) {
str += " ";
}
str = str.substring(0, width);
return str;
}
/**
* Gets an atom name suitable for use in a PDB line.
*
* @param {string} atomName The atom name.
* @param {string} [element] The element, optional.
* @returns {string} The PDB atom name.
*/
function _alignAtomName(atomName: string, element?: string): string {
// Inspired by
// https://github.com/MDAnalysis/mdanalysis/blob/f542aa485983f8d3dd250b36a886061f696c3e97/package/MDAnalysis/coordinates/PDB.py#L997
if (atomName === "") {
return "";
}
// Note this also converts atom names that take up the whole four
// characters.
if (atomName.length >= 4) {
return atomName.substring(0, 4);
}
if (atomName.length === 1) {
return ` ${atomName} `;
}
if (element) {
// Assume first two letters are the element
if (element.length === 2) {
return _ljust(atomName, 4);
}
// Assume first letter is the element
if (element.length === 1) {
return " " + _ljust(atomName, 3);
}
} else if (
twoLetterElems.indexOf(atomName.substring(0, 2).toUpperCase()) !== -1
) {
// Element is unknown.
return _ljust(atomName, 4);
}
return _rjust(atomName, 4);
}
/**
* Create a single line of PDB text.
*
* @param {boolean} isProt Whether the line is for a protein.
* @param {IAtom} atom The atom to create the line for.
* @returns {string} The PDB line.
*/
function _createPDBLine(isProt: boolean, atom: IAtom): string {
// Add defaults to atom
atom.serial = atom.serial === undefined ? 0 : atom.serial;
atom.atom = atom.atom === undefined ? "C" : atom.atom;
atom.elem = atom.elem === undefined ? "C" : atom.elem;
atom.altLoc = atom.altLoc === undefined ? " " : atom.altLoc;
atom.resn = atom.resn === undefined ? "MOL" : atom.resn;
atom.chain = atom.chain === undefined ? "A" : atom.chain;
atom.resi = atom.resi === undefined ? 1 : atom.resi;
atom.x = atom.x === undefined ? 0 : atom.x;
atom.y = atom.y === undefined ? 0 : atom.y;
atom.z = atom.z === undefined ? 0 : atom.z;
atom.b = atom.b === undefined ? 0 : atom.b;
let pdbLine = _ljust(isProt ? "ATOM" : "HETATM", 6);
pdbLine += _rjust((atom.serial as number).toString(), 5);
pdbLine += " ";
pdbLine += _alignAtomName(atom.atom as string, atom.elem);
pdbLine += atom.altLoc; // altloc
pdbLine += _rjust(atom.resn, 3);
pdbLine += _rjust(atom.chain, 2);
pdbLine += _rjust(atom.resi.toString(), 4);
pdbLine += " "; // atom.ins?
pdbLine += _rjust((atom.x as number).toFixed(3), 11);
pdbLine += _rjust((atom.y as number).toFixed(3), 8);
pdbLine += _rjust((atom.z as number).toFixed(3), 8);
pdbLine += _rjust("1.00", 6); // occupancy
pdbLine += _rjust((atom.b ? (atom.b as number) : 0).toFixed(2), 6);
pdbLine += _rjust(" ", 10); // Segment identifier is obsolete
pdbLine += _rjust(atom.elem?.toUpperCase() as string, 2);
return pdbLine;
}
/**
* Gets a list of atoms from GLModel or atom list.
*
* @param {GLModel|IAtom[]} mol The GLModel or atom list.
* @returns {IAtom[]} The list of atoms.
*/
function _getAtomsOfModel(mol: GLModel | IAtom[] | IFileInfo): IAtom[] {
return makeEasyParser(mol).atoms;
}
/**
* Merges multiple molecules into one, reindexing the atoms, making sure chains
* are unique, etc.
*
* @param {GLModel[]|IAtom[][]} mols The molecules to merge.
* @returns {IAtom[]} The list of atoms.
*/
function _mergeMols(mols: GLModel[] | IAtom[][] | IFileInfo[]): IAtom[] {
let curIdx = 0;
let curSerial = 1;
const chainsAvailable: Set<string> = new Set(chainOptions);
const allAtoms: IAtom[] = [];
for (const mol of mols) {
// Get the atomselection of the model.
const atoms = _getAtomsOfModel(mol);
// Get the chain to use.
let curChain: string;
if (atoms[0] && atoms[0].chain && chainsAvailable.has(atoms[0].chain)) {
// The current chain is one that's availble. So use that.
curChain = atoms[0].chain;
} else {
// Current chain has already been used (by different molecule). So
// pick another one from the chainsAvailable set.
curChain = chainsAvailable.values().next().value;
}
chainsAvailable.delete(curChain);
const firstIndex = curIdx;
for (const atom of atoms) {
const atomCopy: IAtom = { ...atom };
atomCopy.chain = curChain;
atomCopy.serial = curSerial;
atomCopy.index = curIdx;
// atomCopy.bondOrder = [];
atomCopy.bonds = atomCopy.bonds?.map((idx) => idx + firstIndex);
curSerial += 1;
curIdx += 1;
allAtoms.push(atomCopy);
}
}
return allAtoms;
}
/**
* Given a list of mol containers, convert them to PDB format. Prefer the
* TreeNodeList.toFileInfos() to this one. Do not call this function directly.
*
* @param {TreeNodeList} treeNodeList The list of mol containers.
* @param {boolean} [merge=false] Whether to merge the models into a
* single PDB string.
* @returns {string[]} The PDB strings.
*/
export function _convertTreeNodeListToPDB(
treeNodeList: TreeNodeList,
merge = false
): string[] {
let mols = treeNodeList.filters
.keepModels()
.map((treeNode: TreeNode) => treeNode.model as GLModel | IAtom[] | IFileInfo[]);
// .filhter((mol) => Array.isArray(mol) && mol.length > 0);
// mol is 3dmoljs molecule object.
if (mols.length === 0) {
return [""];
}
const pdbTxts: string[] = [];
const conectTxts: string[] = [];
if (merge) {
// Note that this converts mols from array to models (GLModel), to
// arrays of atoms. But should still work.
mols = [_mergeMols(mols)];
}
// let curSerial = 1;
for (const mol of mols) {
const atoms: IAtom[] = _getAtomsOfModel(mol);
const atomsToConect: IAtom[] = [];
const pdbLines: string[] = [];
for (const atom of atoms) {
// See https://www.cgl.ucsf.edu/chimera/docs/UsersGuide/tutorials/pdbintro.html
// Note that bonds are not implemented given new EasyParser
// replacement for GLModel.
const isProt = standardProteinResidues.indexOf(atom.resn) !== -1;
const pdbLine = _createPDBLine(isProt, atom);
if (
!isProt &&
(atom.bonds as number[]).length > 0 &&
solventSel.resn.indexOf(atom.resn) === -1
) {
atomsToConect.push(atom);
}
// Note: SSBOND not implemented
pdbLines.push(pdbLine);
}
pdbTxts.push(pdbLines.join("\n"));
const conects: string[] = [];
for (const atom of atomsToConect) {
let conect = "CONECT";
conect += _rjust((atom.serial as number).toString(), 5);
for (const bondedAtomIdx of atom.bonds as number[]) {
if (atoms[bondedAtomIdx] === undefined) {
continue;
}
const bondedAtomSerial = atoms[bondedAtomIdx].serial as number;
conect += _rjust(bondedAtomSerial.toString(), 5);
}
conects.push(conect);
}
conectTxts.push(conects.join("\n"));
}
if (!merge) {
// Zip the pdb and conect files together.
return pdbTxts.map((pdb, i) => {
return pdb + "\n" + conectTxts[i];
});
}
// Merge the PDB files all together.
return [pdbTxts.join("\nTER\n") + "\n" + conectTxts.join("\n")];
}
|