Skip to content

Commit

Permalink
Merge pull request #761 from prajwalkulkarni/refactor-ts-files
Browse files Browse the repository at this point in the history
Refactor remaining files in parser/utils
  • Loading branch information
dkoes committed Jan 26, 2024
2 parents 8c3e9d4 + ca7ecbd commit 6f0a345
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 158 deletions.
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

0 comments on commit 6f0a345

Please sign in to comment.