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

CIF improvement #129

Merged
merged 6 commits into from
Aug 31, 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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ cd -
export PYO3_CROSS_INCLUDE_DIR="HACK"
export PYO3_CROSS_LIB_DIR="HACK"
rm -rf .pyodide-xbuildenv
pyodide build --build-dependencies -o dist_pyodide
pyodide build -o dist_pyodide
cp dist_pyodide/*.whl test_wasm/wheels/
cd test_wasm
python3 -m http.server
Expand Down
81 changes: 81 additions & 0 deletions moleculekit/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,68 @@ def __str__(self):
return repr(self.value)


_originalResname = {
"ARG": "ARG",
"AR0": "ARG",
"HIS": "HIS",
"HID": "HIS",
"HIE": "HIS",
"HIP": "HIS",
"HSD": "HIS",
"HSE": "HIS",
"HSP": "HIS",
"LYS": "LYS",
"LSN": "LYS",
"LYN": "LYS",
"ASP": "ASP",
"ASH": "ASP",
"GLU": "GLU",
"GLH": "GLU",
"SER": "SER",
"THR": "THR",
"ASN": "ASN",
"GLN": "GLN",
"CYS": "CYS",
"CYM": "CYS",
"CYX": "CYS",
"SEC": "SEC",
"GLY": "GLY",
"PRO": "PRO",
"ALA": "ALA",
"VAL": "VAL",
"ILE": "ILE",
"LEU": "LEU",
"MET": "MET",
"PHE": "PHE",
"TYR": "TYR",
"TRP": "TRP",
"G": "G",
"G5": "G",
"G3": "G",
"C": "C",
"C5": "C",
"C3": "C",
"U": "U",
"U5": "U",
"U3": "U",
"A": "A",
"A5": "A",
"A3": "A",
"T": "T",
"DG": "G",
"DG5": "G",
"DG3": "G",
"DC": "C",
"DC5": "C",
"DC3": "C",
"DA": "A",
"DA5": "A",
"DA3": "A",
"DT": "T",
"DT5": "T",
"DT3": "T",
}

_residueNameTable = {
"ARG": "R",
"AR0": "R",
Expand Down Expand Up @@ -2238,6 +2300,25 @@ def getNeighbors(self, idx):
def _ix(self, resid, name):
return np.where((self.name == name) & (self.resid == resid))[0][0]

def toOpenFFMolecule(self):
import tempfile
from moleculekit.smallmol.smallmol import SmallMol
from openff.toolkit.topology import Molecule as OFFMolecule

with tempfile.TemporaryDirectory() as tmpdir:
molc = self.copy()
# for i in range(molc.numAtoms):
# molc.name[i] = molc.name[i] + str(i)
pdbfile = os.path.join(tmpdir, "mol.pdb")
smiles = SmallMol(molc, fixHs=False).toSMILES(
explicitHs=True, kekulizeSmile=True
)
molc.write(pdbfile)
offmol = OFFMolecule.from_pdb_and_smiles(pdbfile, smiles=smiles)
assert np.array_equal(molc.name, [x.name for x in offmol.atoms])

return offmol


class UniqueAtomID:
_fields = ("name", "altloc", "resname", "chain", "resid", "insertion", "segid")
Expand Down
26 changes: 25 additions & 1 deletion moleculekit/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,7 +1933,7 @@ def CIFread(filename, frame=None, topoloc=None, zerowarning=True):
dataObj = myDataList[0]

def fixDefault(val, dtype):
if val == "?":
if val in ("?", "."):
if dtype == float or dtype == int:
val = 0
if dtype == str:
Expand Down Expand Up @@ -2336,7 +2336,31 @@ def sdf_generator(sdffile):
sdfstr = ""


# This is a hack to fix the URL fetching of MMTF in pyodide
def get_raw_data_from_url(pdb_id, reduced=False):
""" " Get the msgpack unpacked data given a PDB id.

:param pdb_id: the input PDB id
:return the unpacked data (a dict)"""
from mmtf.api.default_api import get_url, ungzip_data, _unpack, urllib2

url = get_url(pdb_id, reduced)
request = urllib2.Request(url)
request.add_header("Accept-encoding", "gzip")
response = urllib2.urlopen(request)
if response.info().get("Content-Encoding") == "gzip":
data = ungzip_data(response.read())
else:
# Fixed here from original data = response.read()
data = response
return _unpack(data)


def MMTFread(filename, frame=None, topoloc=None, validateElements=True):
from mmtf.api import default_api

# Monkey-patch the function to fix bug in data = response.read() which should not have read()
default_api.get_raw_data_from_url = get_raw_data_from_url
from mmtf import fetch, parse_gzip, parse

if len(filename) == 4 and not os.path.isfile(filename):
Expand Down
Loading
Loading