| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- use anyhow::Result;
- use minimap2::{Alignment, Mapping, Strand};
- use serde::{Deserialize, Serialize};
- use serde_nested_with::serde_nested;
- use std::num::NonZeroI32;
- use tokio::runtime::Builder;
- 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).unwrap();
- 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.inner)
- }
- #[derive(Serialize, Deserialize, Debug)]
- #[serde(remote = "Strand")]
- enum StrandDef {
- Forward,
- Reverse,
- }
- #[derive(Serialize, Deserialize, Debug)]
- #[serde(remote = "Alignment")]
- struct AlignmentDef {
- pub nm: i32,
- pub cigar: Option<Vec<(u32, u8)>>,
- pub cigar_str: Option<String>,
- pub md: Option<String>,
- pub cs: Option<String>,
- }
- #[serde_nested]
- #[derive(Serialize, Deserialize, Debug)]
- #[serde(remote = "Mapping")]
- pub struct MappingDef {
- query_name: Option<String>,
- query_len: Option<NonZeroI32>,
- query_start: i32,
- query_end: i32,
- #[serde(with = "StrandDef")]
- strand: Strand,
- target_name: Option<String>,
- target_len: i32,
- target_start: i32,
- target_end: i32,
- match_len: i32,
- block_len: i32,
- mapq: u32,
- is_primary: bool,
- is_supplementary: bool,
- #[serde_nested(sub = "Alignment", serde(with = "AlignmentDef"))]
- alignment: Option<Alignment>,
- }
- #[serde_nested]
- #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
- pub struct Mappings {
- #[serde_nested(
- sub = "Mapping",
- serde(
- serialize_with = "MappingDef::serialize",
- deserialize_with = "MappingDef::deserialize"
- )
- )]
- pub inner: Vec<Mapping>,
- }
- pub fn dist_align(url: String) -> impl Fn(String) -> Result<Vec<Mapping>> {
- move |sequence: String| -> Result<Vec<Mapping>> { get_mappings(url.as_str(), sequence) }
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn it_works() {
- let url = "http://localhost:4444/align".to_string();
- let aligner = dist_align(url);
- let mut mappings =
- aligner("CCAAAACACATATTCACGGCAGCCACTCCACCCAGCACCTCaca".to_string()).unwrap();
- let mapping = mappings.pop().unwrap();
- assert_eq!(mapping.target_name.unwrap(), "chr6".to_string());
- }
- }
|