Thomas 1 жил өмнө
parent
commit
5267b82f5f
3 өөрчлөгдсөн 46 нэмэгдсэн , 14 устгасан
  1. 7 0
      Cargo.lock
  2. 2 0
      Cargo.toml
  3. 37 14
      src/lib.rs

+ 7 - 0
Cargo.lock

@@ -30,6 +30,7 @@ dependencies = [
 name = "aligner_client"
 version = "0.1.0"
 dependencies = [
+ "anyhow",
  "minimap2",
  "reqwest",
  "serde",
@@ -38,6 +39,12 @@ dependencies = [
  "tokio",
 ]
 
+[[package]]
+name = "anyhow"
+version = "1.0.81"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0952808a6c2afd1aa8947271f3a60f1a6763c7b912d210184c5149b5cf147247"
+
 [[package]]
 name = "autocfg"
 version = "1.1.0"

+ 2 - 0
Cargo.toml

@@ -12,3 +12,5 @@ minimap2 = { git = "https://github.com/jguhlin/minimap2-rs", features = ["htslib
 serde = { version = "1.0.197", features = ["derive"] }
 serde_nested_with = "0.2.5"
 serde_json = "1.0.114"
+anyhow = "^1.0.75"
+

+ 37 - 14
src/lib.rs

@@ -1,17 +1,37 @@
-use std::num::NonZeroI32;
-use minimap2::{Alignment, Strand, Mapping};
-use serde::{Serialize, Deserialize};
+use minimap2::{Alignment, Mapping, Strand};
+use serde::{Deserialize, Serialize};
 use serde_nested_with::serde_nested;
+use std::num::NonZeroI32;
+use tokio::runtime::Builder;
+use anyhow::Result;
 
-pub async fn get_mappings(url: &str, sequence: String) -> Result<Mappings, Box<dyn std::error::Error>> {
+pub fn get_mappings(
+    url: &str,
+    sequence: String,
+) -> Result<Vec<Mapping>> {
+    let runtime = Builder::new_multi_thread()
+        .worker_threads(1)
+        .enable_all()
+        .build()?;
+    let res = runtime.block_on(get_mappings_async(url.to_string(), sequence))?;
+    // let handle = runtime.spawn(get_mappings_async(url.to_string(), sequence));
+    // let res = runtime.block_on(handle)??;
+
+    Ok(res)
+}
+
+pub async fn get_mappings_async(
+    url: String,
+    sequence: String,
+) -> Result<Vec<Mapping>> {
     let client = reqwest::Client::new();
-    let url = reqwest::Url::parse(url)?;
+    let url = reqwest::Url::parse(&url).unwrap();
 
-    let response = client.post(url).body(sequence).send().await?;
-    let response_body = response.text().await?;
-    let mappings: Mappings = serde_json::from_str(&response_body)?;
+    let response = client.post(url).body(sequence).send().await.unwrap();
+    let response_body = response.text().await.unwrap();
+    let mappings: Mappings = serde_json::from_str(&response_body).unwrap();
 
-    Ok(mappings)
+    Ok(mappings.inner)
 }
 
 #[derive(Serialize, Deserialize, Debug)]
@@ -67,15 +87,18 @@ pub struct Mappings {
     pub inner: Vec<Mapping>,
 }
 
-
 #[cfg(test)]
 mod tests {
     use super::*;
 
-    #[tokio::test]
-    async fn it_works() {
-        let mut mappings = get_mappings("http://localhost:4444/align", "ACCAAAACACATATTCACGGCAGCCACTCCACCCAGCACCTCaca".to_string()).await.unwrap();
-        let mapping = mappings.inner.pop().unwrap();
+    #[test]
+    fn it_works() {
+        let mut mappings = get_mappings(
+            "http://localhost:4444/align",
+            "ACCAAAACACATATTCACGGCAGCCACTCCACCCAGCACCTCaca".to_string(),
+        )
+        .unwrap();
+        let mapping = mappings.pop().unwrap();
         assert_eq!(mapping.target_name.unwrap(), "chr6".to_string());
     }
 }