chiral_db_grpc_client_rust/
lib.rs

1use tonic; 
2
3type AsyncError = Box<dyn std::error::Error>;
4
5pub mod chiral_db_proto {
6    tonic::include_proto!("chiraldb");
7}
8
9pub struct Client {
10    channel: chiral_db_proto::chiral_db_client::ChiralDbClient<tonic::transport::Channel>
11}
12
13impl Client {
14    pub async fn new(host: &String, port: &String) -> Result<Self, AsyncError> {
15        let channel = chiral_db_proto::chiral_db_client::ChiralDbClient::connect(format!("{host}:{port}")).await?; 
16        Ok(Self { channel })
17    }
18
19    pub async fn get_description(&mut self) -> Result<chiral_db_proto::ReplyDescription, AsyncError> {
20        let response = self.channel.get_description(tonic::Request::new(chiral_db_proto::RequestDescription {})).await?;
21        Ok(response.into_inner())
22    }
23
24    pub async fn query_similarity(&mut self, doc_name: &String, smiles: &String, cutoff: f32) -> Result<chiral_db_proto::ReplySimilarity, AsyncError> {
25        let mol = Some(chiral_db_proto::Molecule { smiles: smiles.clone() });
26        let request = tonic::Request::new(chiral_db_proto::RequestSimilarity { doc_name: doc_name.clone(), mol, cutoff });
27        let response = self.channel.query_similarity(request).await?;
28        Ok(response.into_inner())
29    }
30
31    pub async fn query_substructure(&mut self, doc_name: &String, smarts: &String) -> Result<chiral_db_proto::ReplySubstructure, AsyncError> {
32        let frag = Some(chiral_db_proto::Fragment { smarts: smarts.clone() });
33        let request = tonic::Request::new(chiral_db_proto::RequestSubstructure { doc_name: doc_name.clone(), frag });
34        let response = self.channel.query_substructure(request).await?;
35        Ok(response.into_inner())
36    }
37}
38
39#[cfg(test)]
40mod test_client {
41    use super::*;
42
43    async fn create_test_client() -> Result<Client, AsyncError> {
44        let host = String::from("http://demo.chiral.one");
45        let port = String::from("10000");
46        return Client::new(&host, &port).await
47    }
48
49    #[tokio::test]
50    async fn test_desc() -> Result<(), AsyncError> {
51        let mut client = create_test_client().await?;
52        let response_desc = client.get_description().await?;
53        assert!(response_desc.desc.contains("ChEMBL"));
54        Ok(())
55    }
56
57    #[tokio::test]
58    async fn test_similarity() -> Result<(), AsyncError> {
59        let mut client = create_test_client().await?;
60        let response_sim = client.query_similarity(&String::from("ChEMBL"), &String::from("Cc1cc(NC(=O)c2cc(Cl)cc(Cl)c2O)ccc1Sc1nc2ccccc2s1"), 0.25).await?; 
61        assert_eq!(response_sim.results.keys().count(), 33);
62        Ok(())
63    }
64
65    #[tokio::test]
66    async fn test_substructure() -> Result<(), AsyncError> {
67        let mut client = create_test_client().await?;
68        let response_sub = client.query_substructure(&String::from("ChEMBL"), &String::from("Sc1nc2ccccc2s1")).await?; 
69        assert_eq!(response_sub.results.keys().count(), 2);
70        assert!(response_sub.results.contains_key("CHEMBL6685"));
71        assert!(response_sub.results.contains_key("CHEMBL263810"));
72        Ok(())
73    }
74}