Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Updates (#17)
Browse files Browse the repository at this point in the history
* add an image

* fix residue names in PDB creatojn

* some progress on Camera and Transform

* add docs

* add just file note in readme
  • Loading branch information
zachcp authored Jul 31, 2024
1 parent 4575e4d commit 3321fa1
Showing 9 changed files with 132 additions and 40 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -10,6 +10,9 @@ Binary serializaton from `.pse` to `X` using RUST. Where I want `X` to be [mol-v
## Testing

```sh
# build and show docs
just serve

cargo test

# to get all the print statements that are sprinkled around.
Binary file added docs/examples/images/example_pse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion docs/index.qmd
Original file line number Diff line number Diff line change
@@ -4,7 +4,13 @@ title: "pseutils"
---


PSE utils: Conversion of python sessionfiles to Molveiwspec.
PSE utils: Conversion of python sessionfiles to Molviewspec.


## Example PSE

This is a simple example PSE that has a protein object and a selection object.

![](./examples/images/example_pse.png)

{{< mol-json ./examples/example/state.mvsj >}}
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ docs: convert
# quarto
quarto render docs

serve: docs
quarto preview docs

clean:
cargo clean --doc
59 changes: 34 additions & 25 deletions src/molviewspec/nodes.rs
Original file line number Diff line number Diff line change
@@ -249,8 +249,17 @@ impl Node {
pub fn tooltip_from_source() {
unimplemented!()
}
pub fn transform() {
unimplemented!()
pub fn transform(&mut self, params: TransformParams) -> Option<&mut Node> {
if self.kind == KindT::Structure {
let transform_node =
Node::new(KindT::Transform, Some(NodeParams::TransformParams(params)));
self.children
.get_or_insert_with(Vec::new)
.push(transform_node);
self.children.as_mut().unwrap().last_mut()
} else {
None
}
}
pub fn _is_rotation_matrix() {
unimplemented!()
@@ -843,36 +852,23 @@ pub struct TooltipFromSourceParams {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct FocusInlineParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub direction: Option<(f32, f32, f32)>,
pub direction: Option<(f64, f64, f64)>,
#[serde(skip_serializing_if = "Option::is_none")]
pub up: Option<(f32, f32, f32)>,
pub up: Option<(f64, f64, f64)>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct TransformParams {
#[serde(skip_serializing_if = "Option::is_none")]
pub rotation: Option<Vec<f32>>,
pub rotation: Option<Vec<f64>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub translation: Option<(f32, f32, f32)>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct CameraParams {
pub target: (f64, f64, f64),
pub position: (f64, f64, f64),
#[serde(skip_serializing_if = "Option::is_none")]
pub up: Option<(f64, f64, f64)>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CanvasParams {
pub background_color: ColorT,
pub translation: Option<(f64, f64, f64)>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SphereParams {
pub position: (f32, f32, f32),
pub radius: f32,
pub position: (f64, f64, f64),
pub radius: f64,
pub color: ColorT,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
@@ -882,16 +878,29 @@ pub struct SphereParams {

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LineParams {
pub position1: (f32, f32, f32),
pub position2: (f32, f32, f32),
pub radius: f32,
pub position1: (f64, f64, f64),
pub position2: (f64, f64, f64),
pub radius: f64,
pub color: ColorT,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tooltip: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct CameraParams {
pub target: (f64, f64, f64),
pub position: (f64, f64, f64),
#[serde(skip_serializing_if = "Option::is_none")]
pub up: Option<(f64, f64, f64)>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CanvasParams {
pub background_color: ColorT,
}

#[derive(Debug, Serialize, Deserialize, Validate, Clone)]
pub struct DownloadParams {
pub url: String,
44 changes: 41 additions & 3 deletions src/pymolparsing/parsing.rs
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@
//! m_tmpids[m_iter.getAtm()] = m_id;
use crate::molviewspec::nodes::{ComponentExpression, ComponentSelector};
use crate::pymolparsing::colors::{Color, COLOR_SET};
use crate::pymolparsing::representation::{RepBitmask, RepType};
use crate::pymolparsing::representation::RepBitmask;

use itertools::Itertools;
use pdbtbx::{self, Residue, PDB};
@@ -478,8 +478,10 @@ impl PyObjectMolecule {
.collect();

let resv = residue_number as isize;
let res_name = atoms[0].name.clone();
let res_name = atoms[0].resn.clone();
let mut residue = pdbtbx::Residue::new(resv, None, None).expect("Couldn't create residue");

println!("ResidueNames: {}", res_name);
let mut conformer =
pdbtbx::Conformer::new(res_name, None, None).expect("Couldn't create Conformer");

@@ -628,7 +630,6 @@ impl SceneView {
orthoscopic_flag: view[24],
}
}

pub fn to_array(&self) -> [f64; 25] {
let mut array = [0.0; 25];
for i in 0..4 {
@@ -643,6 +644,43 @@ impl SceneView {
array[24] = self.orthoscopic_flag;
array
}
pub fn get_location(&self) -> [f64; 9] {
[
self.rotation_matrix[0][0],
self.rotation_matrix[0][1],
self.rotation_matrix[0][2],
self.rotation_matrix[1][0],
self.rotation_matrix[1][1],
self.rotation_matrix[1][2],
self.rotation_matrix[2][0],
self.rotation_matrix[2][1],
self.rotation_matrix[2][2],
]
}
// pub fn get_translated_position(&self) -> [f64; 3] {
// // this is not right
// // use the 4x4 translation matrix and the position vector
// // to get a translated positions
// let mut translated = [0.0; 3];
// for i in 0..3 {
// translated[i] = self.rotation_matrix[i][0] * self.position[0]
// + self.rotation_matrix[i][1] * self.position[1]
// + self.rotation_matrix[i][2] * self.position[2]
// + self.rotation_matrix[i][3];
// }
// translated
// }
// pub fn get_translated_origin(&self) -> [f64; 3] {
// // this is not right
// let mut translated = [0.0; 3];
// for i in 0..3 {
// translated[i] = self.rotation_matrix[i][0] * self.origin[0]
// + self.rotation_matrix[i][1] * self.origin[1]
// + self.rotation_matrix[i][2] * self.origin[2]
// + self.rotation_matrix[i][3];
// }
// translated
// }
}
impl<'de> Deserialize<'de> for SceneView {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
39 changes: 30 additions & 9 deletions src/pymolparsing/psedata.rs
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
//! other PSE data types which include the folloing:
//!
use crate::molviewspec::nodes::{self as mvsnodes, CameraParams, ColorNamesT, State};
use crate::molviewspec::nodes::{self as mvsnodes, ColorNamesT, State};
use crate::pymolparsing::parsing::{
PyObjectMolecule, PymolSessionObjectData, SceneView, SessionName, SessionSelectorList,
Settings, SettingsEnum,
@@ -154,6 +154,14 @@ impl PSEData {
.collect()
}

pub fn get_location_as_transform(&self) -> mvsnodes::TransformParams {
let location = self.view.get_location();
mvsnodes::TransformParams {
rotation: Some(location.to_vec()),
..Default::default()
}
}

pub fn create_pdb(&self) -> PDB {
// todo: extend this to more than one molecuelo and/or to modify the global scene
let moldata = &self.get_molecule_data();
@@ -186,14 +194,23 @@ impl PSEData {
// write state for loading the PDB files
let mut state = State::new();

// Add Global Data
let pos = self.view.position;
let camparam = CameraParams {
target: (0.0, 0.0, 0.0), // <--- Todo
position: (pos[0], pos[1], pos[2]),
..Default::default() // <--- Todo
};
state.camera(camparam);
// Add Global Camera Data
// let [o1, o2, o3] = self.view.origin;
// let [p1, p2, p3] = self.view.position;
//
// let [o1, o2, o3] = self.view.get_translated_origin();
// let [p1, p2, p3] = self.view.get_translated_position();
//
// let camparam = CameraParams {
// // https://molstar.org/mol-view-spec-docs/camera-settings/
// target: (o1, o2, o3), // <--- Todo
// position: (p1, p2, p3),
// ..Default::default() // <--- Todo
// };
// state.camera(camparam);

// It will be easier to set the focus based on all of the components in the PDB then trying to match pymol exactly
// let focus = FocusInlineParams {};

// Add Molecule Data
for molecule in self.get_molecule_data() {
@@ -218,6 +235,10 @@ impl PSEData {
.expect("defined a valid component")
.representation(mvsnodes::RepresentationTypeT::Cartoon);

// this works great
let transform = self.get_location_as_transform();
structure.transform(transform);

// selections return MVD ComponentExpression
let selection_data = self.get_selection_data()[0];
for selector in selection_data.get_selectors() {
2 changes: 1 addition & 1 deletion src/pymolparsing/representation.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@
use bitflags::bitflags;
use serde::{Deserialize, Serialize};

// First, define an enum for all representation types
/// First, define an enum for all representation types
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RepType {
Cyl,
15 changes: 14 additions & 1 deletion tests/test_pse_parsing.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use pseutils::pymolparsing::colors::{Color, COLOR_SET};
use pseutils::pymolparsing::colors::Color;
use pseutils::pymolparsing::parsing::{CustomValue, SettingsEnum};
use pseutils::pymolparsing::representation::RepBitmask;
use pseutils::PSEData;
@@ -46,8 +46,21 @@ fn test_pdb_00() {
let chains = mols[0].get_chains();
let residues = mols[0].get_residues_by_chain(chains[0].clone());
let residue = mols[0].create_residue(chains[0].clone(), residues[0]);
assert!(residue.name() == Some("VAL"));

// Original
// ATOM 1 N VAL A 1 50.873 32.978 2.387 1.00 27.72 A N
// ATOM 1 N VAL A1 50.873 32.978 2.387 0.00 27.72 N

// Original
// MASTER 365 0 0 5 18 0 0 6 1519 1 0 15
// MASTER 0 0 0 0 0 0 0 6 1519 1 0 0

let chain = mols[0].create_chain(chains[0].clone());

let view = &psedata.view;
println!("{:?}", view);

// Check symmetry code
let (unit, sym) = mols[0].get_unit_cell_symmetry();

0 comments on commit 3321fa1

Please sign in to comment.