Skip to content

Commit

Permalink
File prop for loading filestrings
Browse files Browse the repository at this point in the history
  • Loading branch information
samirelanduk committed Mar 3, 2022
1 parent bded233 commit 5e16c1e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ You can pre-load an official PDB structure by supplying its 4 letter ID as a pro

You can also pre-load a structure by passing in the URL of the file - as long as it is a file type that Molstar supports.

#### `file`

Thirdly, you can provide a file object, which should have a `filestring` property and a `type` property (`"pdb"`, `"mmcif"` etc. - anything that molstar itself natively supports).

#### `dimensions`

By default the molstar plugin will take up the full screen. If you want it to act as just a normal div, you can provide a `[width, height]` array instead.
Expand Down
34 changes: 21 additions & 13 deletions src/Molstar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "molstar/build/viewer/molstar.css";

const Molstar = props => {

const { useInterface, pdbId, url, dimensions, className, showControls } = props;
const { useInterface, pdbId, url, file, dimensions, className, showControls } = props;
const parentRef = useRef(null);
const canvasRef = useRef(null);
const plugin = useRef(null);
Expand All @@ -29,25 +29,33 @@ const Molstar = props => {
plugin.current.initViewer(canvasRef.current, parentRef.current);
await plugin.current.init();
}
await loadStructure(pdbId, url, plugin.current);
await loadStructure(pdbId, url, file, plugin.current);
return () => plugin.current = null;
}, [])

useEffect(async () => {
await loadStructure(pdbId, url, plugin.current);
}, [pdbId, url])
await loadStructure(pdbId, url, file, plugin.current);
}, [pdbId, url, file])

const loadStructure = async (pdbId, url, plugin) => {
const loadStructure = async (pdbId, url, file, plugin) => {
if (plugin) {
plugin.clear();
const structureUrl = url ? url : pdbId ? `https://files.rcsb.org/view/${pdbId}.cif` : null;
if (!structureUrl) return;
const data = await plugin.builders.data.download(
{ url: structureUrl }, {state: {isGhost: true}}
);
const extension = structureUrl.split(".").pop().replace("cif", "mmcif");
const traj = await plugin.builders.structure.parseTrajectory(data, extension);
await plugin.builders.structure.hierarchy.applyPreset(traj, "default");
if (file) {
const data = await plugin.builders.data.rawData({
data: file.filestring /* string or number[] */,
});
const traj = await plugin.builders.structure.parseTrajectory(data, file.type);
await plugin.builders.structure.hierarchy.applyPreset(traj, "default");
} else {
const structureUrl = url ? url : pdbId ? `https://files.rcsb.org/view/${pdbId}.cif` : null;
if (!structureUrl) return;
const data = await plugin.builders.data.download(
{ url: structureUrl }, {state: {isGhost: true}}
);
const extension = structureUrl.split(".").pop().replace("cif", "mmcif");
const traj = await plugin.builders.structure.parseTrajectory(data, extension);
await plugin.builders.structure.hierarchy.applyPreset(traj, "default");
}
}
}

Expand Down

0 comments on commit 5e16c1e

Please sign in to comment.