Thomas 1 year ago
parent
commit
5e682207c0
3 changed files with 131 additions and 0 deletions
  1. 85 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 45 0
      src/lib.rs

+ 85 - 0
Cargo.lock

@@ -23,6 +23,27 @@ version = "2.6.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
 
+[[package]]
+name = "buffer-redux"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4c9f8ddd22e0a12391d1e7ada69ec3b0da1914f1cec39c5cf977143c5b2854f5"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "bytecount"
+version = "0.6.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5ce89b21cab1437276d2650d57e971f9d548a2d9037cc231abdc0562b97498ce"
+
+[[package]]
+name = "cc"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eaff6f8ce506b9773fa786672d63fc7a191ffea1be33f72bbd4aeacefca9ffc8"
+
 [[package]]
 name = "cfg-if"
 version = "1.0.0"
@@ -153,6 +174,18 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "libz-sys"
+version = "1.1.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c15da26e5af7e25c90b37a2d75cdbf940cf4a55316de9d84c679c9b8bfabf82e"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
 [[package]]
 name = "log"
 version = "0.4.22"
@@ -165,6 +198,39 @@ version = "2.7.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
 
+[[package]]
+name = "minimap2"
+version = "0.1.18+minimap2.2.28"
+source = "git+https://github.com/jguhlin/minimap2-rs#b0023633ede58a176cb4adeea2f468938d97bb53"
+dependencies = [
+ "libc",
+ "minimap2-sys",
+ "needletail",
+ "simdutf8",
+]
+
+[[package]]
+name = "minimap2-sys"
+version = "0.1.19+minimap2.2.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7b27cbfa7368abd87de720ae844d9aad28452fa5b415d8df629497b23c030ad6"
+dependencies = [
+ "cc",
+ "libz-sys",
+ "pkg-config",
+]
+
+[[package]]
+name = "needletail"
+version = "0.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "db05a5ab397f64070d8c998fa0fbb84e484b81f95752af317dac183a82d9295d"
+dependencies = [
+ "buffer-redux",
+ "bytecount",
+ "memchr",
+]
+
 [[package]]
 name = "option-ext"
 version = "0.2.0"
@@ -179,11 +245,18 @@ dependencies = [
  "confy",
  "env_logger",
  "log",
+ "minimap2",
  "serde",
  "serde_json",
  "thiserror",
 ]
 
+[[package]]
+name = "pkg-config"
+version = "0.3.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
+
 [[package]]
 name = "proc-macro2"
 version = "1.0.86"
@@ -288,6 +361,12 @@ dependencies = [
  "serde",
 ]
 
+[[package]]
+name = "simdutf8"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f27f6278552951f1f2b8cf9da965d10969b2efdea95a6ec47987ab46edfe263a"
+
 [[package]]
 name = "syn"
 version = "2.0.70"
@@ -368,6 +447,12 @@ version = "1.0.12"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
 
+[[package]]
+name = "vcpkg"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
+
 [[package]]
 name = "wasi"
 version = "0.11.0+wasi-snapshot-preview1"

+ 1 - 0
Cargo.toml

@@ -11,3 +11,4 @@ anyhow = "^1.0.75"
 serde_json = "1.0.120"
 serde = { version = "1.0.204", features = ["derive"] }
 confy = "0.6.1"
+minimap2 = { git = "https://github.com/jguhlin/minimap2-rs" }

+ 45 - 0
src/lib.rs

@@ -3,12 +3,14 @@ mod config;
 use std::{
     fs::{self, File},
     io::Write,
+    num::NonZeroI32,
     process::{Command, Stdio},
 };
 
 use anyhow::{Context, Ok};
 use config::Config;
 use log::info;
+use minimap2::{Mapping, Strand};
 use serde::Serialize;
 
 // cat /home/prom/Documents/Programmes/desc_seq_lib/data_test/419b7353-bc8c-4ffe-8ad6-0bbfac8c0cfa.fasta | blastn -db hs1_simple_chr.fa -outfmt 6
@@ -194,6 +196,40 @@ pub fn best_blastn(fa_path: &str) -> anyhow::Result<Vec<BlastResult>> {
     Ok(results)
 }
 
+pub fn blast(fa_path: &str) -> anyhow::Result<Vec<Mapping>> {
+    Ok(best_blastn(fa_path)?.iter().map(Mapping::from).collect())
+}
+
+impl From<&BlastResult> for Mapping {
+    fn from(br: &BlastResult) -> Self {
+        let query_len = NonZeroI32::new(br.q_end as i32 - br.q_start as i32 + 1);
+        let (strand, target_start, target_end) = if br.s_start < br.s_end {
+            (Strand::Forward, br.s_start as i32, br.s_end as i32)
+        } else {
+            (Strand::Reverse, br.s_end as i32, br.s_start as i32)
+        };
+        let mapq = (br.bit_score.min(60.0) as u32).min(60);
+
+        Mapping {
+            query_name: Some(br.query_id.clone()),
+            query_len,
+            query_start: br.q_start as i32,
+            query_end: br.q_end as i32,
+            strand,
+            target_name: Some(br.subject_id.clone()),
+            target_len: (target_end - target_start + 1).abs() as i32,
+            target_start,
+            target_end,
+            match_len: br.alignment_length as i32,
+            block_len: br.alignment_length as i32,
+            mapq,
+            is_primary: true,
+            is_supplementary: false,
+            alignment: None,
+        }
+    }
+}
+
 #[cfg(test)]
 mod tests {
     use super::*;
@@ -213,4 +249,13 @@ mod tests {
 
         Ok(())
     }
+
+    #[test]
+    fn blast() -> anyhow::Result<()> {
+        let fa = "test_data/419b7353-bc8c-4ffe-8ad6-0bbfac8c0cfa.fasta";
+
+        let res = crate::blast(fa)?;
+        println!("{res:#?}");
+        Ok(())
+    }
 }