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

Updated Types for Parsers and Parser Utils #699

Merged
merged 11 commits into from
Jul 13, 2023
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
9 changes: 6 additions & 3 deletions src/parsers/CDJSON.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ParserOptionsSpec } from "./ParserOptionsSpec";

/**
* This parses the ChemDoodle json file format. Although this is registered
* for the json file extension, other chemical json file formats exist that
Expand All @@ -7,14 +9,15 @@
* @param {string} str
* @param {ParserOptionsSpec} options
* @category Parsers
*/
export function CDJSON(str, options) {
*/

export function CDJSON(str: string, options: ParserOptionsSpec) {
var atoms: any[][] & Record<string, any> = [[]];
if (typeof str === "string") {
// Str is usually automatically parsed by JQuery
str = JSON.parse(str);
}
var molecules = str.m;
var molecules = (str as any).m;
var atomsInFile = molecules[0].a; // Assumes there is at least one
var bondsInFile = molecules[0].b; // molecule and ignores any more
// Ignores any shapes
Expand Down
15 changes: 8 additions & 7 deletions src/parsers/CIF.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { ParserOptionsSpec } from './ParserOptionsSpec';
// puts atoms specified in mmCIF fromat in str into atoms

import { assignBonds } from "./utils/assignBonds";
import { computeSecondaryStructure } from "./utils/computeSecondaryStructure";
import { processSymmetries } from "./utils/processSymmetries";
import { conversionMatrix3, Matrix4, Vector3, } from "../WebGL"
import { conversionMatrix3, Matrix3, Matrix4, Vector3, } from "../WebGL"

/**
* Puts atoms specified in mmCIF fromat in str into atoms
*
* @param {string} str
* @param {ParserOptionsSpec} options
* @category Parsers
*/

*/
export function CIF(str: string, options: ParserOptionsSpec = {}) {
var atoms: any[] & Record<string, any> = [];
var noAssembly = !options.doAssembly; // don't assemble by default
Expand All @@ -20,12 +21,12 @@ export function CIF(str: string, options: ParserOptionsSpec = {}) {
options.assignBonds === undefined ? true : options.assignBonds;

//coordinate conversion
var fractionalToCartesian = function (cmat, x, y, z) {
var fractionalToCartesian = function (cmat: Matrix3, x: number, y: number, z: number) {
return new Vector3(x, y, z).applyMatrix3(cmat);
};

// Used to handle quotes correctly
function splitRespectingQuotes(string, separator) {
function splitRespectingQuotes(string: string, separator: string | any[]) {
var sections: any[] = [];
var sectionStart = 0;
var sectionEnd = 0;
Expand Down Expand Up @@ -318,7 +319,7 @@ export function CIF(str: string, options: ParserOptionsSpec = {}) {
modelData[modelData.length - 1].symmetries.push(matrix);
}
}
var parseTerm = function (term) {
var parseTerm = function (term: string) {
var negative = term.match("-");
term = term.replace(/[-xyz]/g, "");
var fractionParts = term.split("/");
Expand All @@ -342,7 +343,7 @@ export function CIF(str: string, options: ParserOptionsSpec = {}) {
/["' ]/g,
""
);
var componentStrings = transform.split(",").map(function (val) {
var componentStrings = transform.split(",").map(function (val: string) {
return val.replace(/-/g, "+-");
});
let matrix = new Matrix4(
Expand Down
20 changes: 10 additions & 10 deletions src/parsers/CUBE.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Vector3, Matrix4 } from "../WebGL";
import { assignBonds } from "./utils/assignBonds";
import { anumToSymbol } from "./utils/anumToSymbol";
import { ParserOptionsSpec } from "./ParserOptionsSpec";

/**
* @param {string}
* str
* @param {ParserOptionsSpec}
* options
* @param {string}
* str
* @param {ParserOptionsSpec}
* options
* @category Parsers

*/
export function CUBE(str, options) {
export function CUBE(str: string, options: ParserOptionsSpec) {
options = options || {};
var atoms: any[][] & Record<string, any> = [[]];
var lines = str.split(/\r?\n/);
Expand All @@ -37,22 +37,22 @@ export function CUBE(str, options) {
// always assume bohr: openbabel source code
// always assume angstrom: http://www.ks.uiuc.edu/Research/vmd/plugins/molfile/cubeplugin.html
// we are going to go with n<0 means angstrom - note this is just the first n
var convFactor = (lineArr[0] > 0) ? 0.529177 : 1;
var convFactor = ((lineArr[0] as any) > 0) ? 0.529177 : 1;
origin.multiplyScalar(convFactor);

var nX = Math.abs(lineArr[0]);
var nX = Math.abs((lineArr[0] as any));
var xVec = new Vector3(parseFloat(lineArr[1]),
parseFloat(lineArr[2]), parseFloat(lineArr[3]))
.multiplyScalar(convFactor);

lineArr = lines[4].replace(/^\s+/, "").replace(/\s+/g, " ").split(" ");
var nY = Math.abs(lineArr[0]);
var nY = Math.abs((lineArr[0] as any));
var yVec = new Vector3(parseFloat(lineArr[1]),
parseFloat(lineArr[2]), parseFloat(lineArr[3]))
.multiplyScalar(convFactor);

lineArr = lines[5].replace(/^\s+/, "").replace(/\s+/g, " ").split(" ");
var nZ = Math.abs(lineArr[0]);
var nZ = Math.abs((lineArr[0] as any));
var zVec = new Vector3(parseFloat(lineArr[1]),
parseFloat(lineArr[2]), parseFloat(lineArr[3]))
.multiplyScalar(convFactor);
Expand Down
18 changes: 9 additions & 9 deletions src/parsers/GRO.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import { atomNameToElem } from "./utils/atomNameToElem";

/**
* Parse a gro file from str and create atoms
*
* @param {string}
* str
* @param {ParserOptionsSpec}
* options*
* @category Parsers
*
* @param {string}
* str
* @param {ParserOptionsSpec}
* options*
* @category Parsers
*/

*/
export function GRO(str /*, options*/) {
export function GRO(str: string /*, options*/) {
var allatoms: any[][] & Record<string, any> = [];
var lines = str.split(/\r?\n|\r/);
while (lines.length > 0) {
Expand Down Expand Up @@ -53,7 +53,7 @@ export function GRO(str /*, options*/) {
var box = last.trim().split(/\s+/);
if (box.length == 3) {
for (var b = 0; b < 3; b++) {
box[b] = parseFloat(box[b]) * 10.0;
(box[b] as any) = parseFloat(box[b]) * 10.0;
}
allatoms.box = box;
}
Expand Down
10 changes: 6 additions & 4 deletions src/parsers/LAMMPSTRJ.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { ParserOptionsSpec } from "./ParserOptionsSpec";
import { assignBonds } from "./utils/assignBonds";

/**
* Parse a lammps trajectory file from str and create atoms
*
* @category Parsers
*/

*/
export function LAMMPSTRJ(str, options) {
export function LAMMPSTRJ(str: string, options: ParserOptionsSpec) {
var atoms: any[] = [];
var dic = {
var dic:any = {
id: "serial",
type: "atom",
element: "elem",
Expand Down Expand Up @@ -43,7 +45,7 @@ export function LAMMPSTRJ(str, options) {
atoms.push([]);
for (let j = offset; j < offset + atomCount; j++) {
var atom: Record<string, any> = {};
var properties = {};
var properties: any = {};
var tokens = lines[j].split(" ");
for (var k = 0; k < tokens.length; k++) {
var prop = dic[types[k]];
Expand Down
19 changes: 11 additions & 8 deletions src/parsers/MMTF.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { base64ToArray } from "../utilities";
import { Matrix4 } from "../WebGL";
import { ParserOptionsSpec } from "./ParserOptionsSpec";
import { computeSecondaryStructure } from "./utils/computeSecondaryStructure";
import { processSymmetries } from "./utils/processSymmetries";

Expand All @@ -8,12 +9,12 @@ interface MMTFobj {
}
declare var MMTF: MMTFobj;

var fromCharCode = function (charCodeArray) {
var fromCharCode = function (charCodeArray: any) {
return String.fromCharCode.apply(null, charCodeArray).replace(/\0/g, '');
};

var convertSS = function (val) {
//convert mmtf code to 3dmol code
var convertSS = function (val: number | boolean) {
// Convert mmtf code to 3dmol code
/*
0: pi helix
1: bend
Expand Down Expand Up @@ -41,11 +42,13 @@ let mmtfHETATMtypes = new Set([
"PEPTIDE-LIKE",
"SACCHARIDE"]);

/** @param bindata - binary UInt8Array buffer or a base64 encoded string
* @param ParserOptionsSpec
* @category Parsers
/**
* @param bindata - binary UInt8Array buffer or a base64 encoded string
* @param ParserOptionsSpec
* @category Parsers
*/
export function MMTFparser(bindata, options) {

export function MMTFparser(bindata: any, options: ParserOptionsSpec) {

var noH = !options.keepH; // suppress hydrogens by default
var selAltLoc = options.altLoc ? options.altLoc : 'A'; //default alternate location to select if present
Expand Down Expand Up @@ -108,7 +111,7 @@ export function MMTFparser(bindata, options) {
}

let chainIsPolymer: boolean[] = [];
mmtfData.entityList.forEach(entity => {
mmtfData.entityList.forEach((entity: { chainIndexList: any[]; type: string; }) => {
entity.chainIndexList.forEach(ch => {
chainIsPolymer[ch] = entity.type == "polymer";
});
Expand Down
12 changes: 7 additions & 5 deletions src/parsers/MOL2.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ParserOptionsSpec } from "./ParserOptionsSpec";

let SYBYLtoElem = {
let SYBYLtoElem:any = {
'C.1': 'C',
'C1': 'C',
'C.2': 'C',
Expand Down Expand Up @@ -50,16 +51,17 @@ let SYBYLtoElem = {
'So2':'S'
};

// parse SYBYL mol2 file from string - assumed to only contain one molecule
// tag
// Parse SYBYL mol2 file from string - assumed to only contain one molecule tag

/**
* @param {string}
* str
* @param {ParserOptionsSpec}
* options
* @category Parsers
*/
export function MOL2(str, options) {
*/

export function MOL2(str: string, options: ParserOptionsSpec) {
var atoms: any[][] & Record<string,any> = [[]];
var noH = false;
if (typeof options.keepH !== "undefined") noH = !options.keepH;
Expand Down
16 changes: 8 additions & 8 deletions src/parsers/PDB.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// parse pdb file from str and create atoms
// if computeStruct is true will always perform secondary structure
// analysis,
// otherwise only do analysis of SHEET/HELIX comments are missing

import { ParserOptionsSpec } from "./ParserOptionsSpec";
import { getSinglePDB } from "./utils/getSinglePDB";

/**
* Parse pdb file from str and create atoms if computeStruct is true will always perform secondary structure analysis,
* otherwise only do analysis of SHEET/HELIX comments are missing
*
* @param {string} str
* @param {ParserOptionsSpec} options - keepH (do not strip hydrogens), noSecondaryStructure,
* assignbonds (default true, calculate implicit bonds)
* (do not compute ss), altLoc (which alternate location to select, if present; '*' to load all)
* @category Parsers
*
*/
export function PDB(str, options) {
*/

export function PDB(str: string, options: ParserOptionsSpec) {
options = options || {};
var atoms: any[] & Record<string, any> = []; //a separate list for each model
var sslookup = {}; //stores SHEET and HELIX info, which is shared across models
atoms.modelData = [];
var lines = str.split(/\r?\n|\r/);
var lines: any = str.split(/\r?\n|\r/);
while (lines.length > 0) {
var pdbinfo = getSinglePDB(lines, options, sslookup);
var modelatoms = pdbinfo[0];
Expand Down
23 changes: 12 additions & 11 deletions src/parsers/PQR.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { ParserOptionsSpec } from "./ParserOptionsSpec";
import { assignPDBBonds } from "./utils/assignPDBBonds";
import { computeSecondaryStructure } from "./utils/computeSecondaryStructure";

/**
adithyaakrishna marked this conversation as resolved.
Show resolved Hide resolved
* Parse a pqr file from str and create atoms. A pqr file is assumed to be a
* whitespace delimited PDB with charge and radius fields.
*
* @param {string}
* str
* @param {ParserOptionsSpec}
* options - noSecondaryStructure (do not compute ss)
* @category Parsers
*/
export function PQR(str, options) {

/**
* Parse a pqr file from str and create atoms. A pqr file is assumed to be a whitespace delimited PDB with charge and radius fields.
*
* @param {string}
* str
* @param {ParserOptionsSpec}
* options - noSecondaryStructure (do not compute ss)
* @category Parsers
*/

export function PQR(str: string, options: ParserOptionsSpec) {
var atoms: any[][] & Record<string, any> = [[]];
var computeStruct = !options.noSecondaryStructure;
atoms.modelData = [{symmetries:[]}];
Expand Down
14 changes: 7 additions & 7 deletions src/parsers/PRMTOP.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @ts-nocheck

/**
adithyaakrishna marked this conversation as resolved.
Show resolved Hide resolved
* Parse a prmtop file from str and create atoms
*
* @param {string}
* str
* @param {ParserOptionsSpec}
* options - noSecondaryStructure (do not compute ss)
*
* @param {string}
* str
* @param {ParserOptionsSpec}
* options - noSecondaryStructure (do not compute ss)
* @category Parsers
*/

*/
export function PRMTOP(str /*, options*/) {
export function PRMTOP(str: string /*, options*/) {
var atoms = [];
var atomIndex;
var count = 0;
Expand Down
Loading