Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor remaining files in parser/utils #761

Merged
merged 2 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/parsers/utils/assignBonds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,28 @@ const OFFSETS = [
];
const MAX_BOND_LENGTH = 4.95; // (largest bond length, Cs) 2.25 * 2 * 1.1 (fudge factor)

export function assignBonds(atoms: string | any[]) {
export function assignBonds(atoms: AtomSpec[]) {
// Assign bonds - yuck, can't count on connect records

for (let i = 0, n = atoms.length; i < n; i++) {
// Don't reindex if atoms are already indexed
if (!atoms[i].index) atoms[i].index = i;
}

const grid = {};
const grid: {
x: {
y: {
z: AtomSpec[];
};
};
} = {
x: {
y: {
z: [],
},
},
};

for (let index = 0; index < atoms.length; index++) {
const atom = atoms[index];
const x = Math.floor(atom.x / MAX_BOND_LENGTH);
Expand Down Expand Up @@ -76,7 +89,7 @@ export function assignBonds(atoms: string | any[]) {
}
}
}
};
}

for (let xg in grid) {
const x = parseInt(xg);
Expand Down
41 changes: 19 additions & 22 deletions src/parsers/utils/assignPDBBonds.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
// This is optimized for proteins where it is assumed connected atoms are on the same or next residue

import { AtomSpec } from "specs";
import { areConnected } from "./areConnected";
import { assignBonds } from "./assignBonds";
import { standardResidues } from "./standardResidues";


/**
/**
* @param {AtomSpec[]}
* atomsarray
*/
*/

export function assignPDBBonds(atomsarray: string | any[]) {
export function assignPDBBonds(atomsarray: AtomSpec[]) {
// assign bonds - yuck, can't count on connect records
var protatoms: any[] = [];
var hetatoms: any[] = [];
var i: number, n: number;
for (i = 0, n = atomsarray.length; i < n; i++) {
var atom: any = atomsarray[i];
const protatoms: Array<AtomSpec> = [];
const hetatoms: Array<AtomSpec> = [];
for (let i = 0, n = atomsarray.length; i < n; i++) {
const atom = atomsarray[i];
atom.index = i;
if (atom.hetflag || !standardResidues.has(atom.resn)) hetatoms.push(atom);
else protatoms.push(atom);
Expand All @@ -25,18 +24,18 @@ export function assignPDBBonds(atomsarray: string | any[]) {
assignBonds(hetatoms);

// sort by resid
protatoms.sort(function (a: any, b: any) {
if (a.chain != b.chain) return a.chain < b.chain ? -1 : 1;
protatoms.sort(function (a, b) {
if (a.chain !== b.chain) return a.chain < b.chain ? -1 : 1;
return a.resi - b.resi;
});

// for identifying connected residues
var currentResi = -1;
var reschain = -1;
var lastResConnected: boolean;
let currentResi = -1;
let reschain = -1;
let lastResConnected: boolean;

for (i = 0, n = protatoms.length; i < n; i++) {
var ai = protatoms[i];
for (let i = 0, n = protatoms.length; i < n; i++) {
const ai = protatoms[i];

if (ai.resi !== currentResi) {
currentResi = ai.resi;
Expand All @@ -47,12 +46,10 @@ export function assignPDBBonds(atomsarray: string | any[]) {

ai.reschain = reschain;

for (var j = i + 1; j < protatoms.length; j++) {
var aj = protatoms[j];
if (aj.chain != ai.chain) break;
if (aj.resi - ai.resi > 1)
// can't be connected
break;
for (let j = i + 1; j < protatoms.length; j++) {
const aj = protatoms[j];
if (aj.chain !== ai.chain || aj.resi - ai.resi > 1) break;

if (areConnected(ai, aj)) {
if (ai.bonds.indexOf(aj.index) === -1) {
// only add if not already there
Expand Down
Loading
Loading