瀏覽代碼

ProgressBar

Thomas 2 年之前
父節點
當前提交
aafedaf502
共有 3 個文件被更改,包括 159 次插入9 次删除
  1. 132 0
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 25 8
      src/main.rs

+ 132 - 0
Cargo.lock

@@ -31,11 +31,25 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
+[[package]]
+name = "console"
+version = "0.15.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8"
+dependencies = [
+ "encode_unicode",
+ "lazy_static",
+ "libc",
+ "unicode-width",
+ "windows-sys",
+]
+
 [[package]]
 name = "corr"
 version = "0.1.0"
 dependencies = [
  "hashbrown 0.14.0",
+ "indicatif",
  "ndarray",
  "ndarray-stats",
  "rayon",
@@ -90,6 +104,12 @@ version = "1.8.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
 
+[[package]]
+name = "encode_unicode"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
+
 [[package]]
 name = "getrandom"
 version = "0.2.10"
@@ -137,6 +157,28 @@ dependencies = [
  "hashbrown 0.12.3",
 ]
 
+[[package]]
+name = "indicatif"
+version = "0.17.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ff8cc23a7393a397ed1d7f56e6365cba772aba9f9912ab968b03043c395d057"
+dependencies = [
+ "console",
+ "instant",
+ "number_prefix",
+ "portable-atomic",
+ "unicode-width",
+]
+
+[[package]]
+name = "instant"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
+dependencies = [
+ "cfg-if",
+]
+
 [[package]]
 name = "itertools"
 version = "0.10.5"
@@ -146,6 +188,12 @@ dependencies = [
  "either",
 ]
 
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
 [[package]]
 name = "libc"
 version = "0.2.146"
@@ -246,12 +294,24 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "number_prefix"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3"
+
 [[package]]
 name = "once_cell"
 version = "1.18.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
 
+[[package]]
+name = "portable-atomic"
+version = "1.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "767eb9f07d4a5ebcb39bbf2d452058a93c011373abf6832e24194a1c3f004794"
+
 [[package]]
 name = "ppv-lite86"
 version = "0.2.17"
@@ -322,6 +382,12 @@ version = "1.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
 
+[[package]]
+name = "unicode-width"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
+
 [[package]]
 name = "version_check"
 version = "0.9.4"
@@ -333,3 +399,69 @@ name = "wasi"
 version = "0.11.0+wasi-snapshot-preview1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
+
+[[package]]
+name = "windows-sys"
+version = "0.45.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0"
+dependencies = [
+ "windows-targets",
+]
+
+[[package]]
+name = "windows-targets"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071"
+dependencies = [
+ "windows_aarch64_gnullvm",
+ "windows_aarch64_msvc",
+ "windows_i686_gnu",
+ "windows_i686_msvc",
+ "windows_x86_64_gnu",
+ "windows_x86_64_gnullvm",
+ "windows_x86_64_msvc",
+]
+
+[[package]]
+name = "windows_aarch64_gnullvm"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8"
+
+[[package]]
+name = "windows_aarch64_msvc"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43"
+
+[[package]]
+name = "windows_i686_gnu"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f"
+
+[[package]]
+name = "windows_i686_msvc"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060"
+
+[[package]]
+name = "windows_x86_64_gnu"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36"
+
+[[package]]
+name = "windows_x86_64_gnullvm"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3"
+
+[[package]]
+name = "windows_x86_64_msvc"
+version = "0.42.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0"

+ 2 - 1
Cargo.toml

@@ -10,4 +10,5 @@ edition = "2021"
 ndarray = "0.15.6"
 ndarray-stats = "0.5.1"
 rayon = "1.7.0"
-hashbrown = {version = "0.14.0", features = ["rayon"]}
+hashbrown = {version = "0.14.0", features = ["rayon"]}
+indicatif = "0.17.5"

+ 25 - 8
src/main.rs

@@ -10,6 +10,8 @@ use ndarray::Array2;
 use ndarray_stats::CorrelationExt;
 use std::io::Write;
 
+use indicatif::{ProgressBar, ProgressStyle};
+
 fn main() -> Result<(), Box<dyn Error>> {
     let filename = "/Turbine-pool/LAL-T_ChIP/data/all_sub.tsv";
     let enhancer_prefix = "EH38E";
@@ -39,15 +41,19 @@ fn main() -> Result<(), Box<dyn Error>> {
             } else {
                 rna_set.insert(key, values);
             }
-
-            // if enhancer_set.len() > 2 {
-            //     break;
-            // }
         }
     }
     println!("{} enhancers", enhancer_set.len());
     println!("{} rnas", rna_set.len());
 
+
+    // Set the progress bar
+    let bar = ProgressBar::new(enhancer_set.len().try_into().unwrap());
+    bar.set_style(
+        ProgressStyle::default_bar()
+            .template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta}) {msg}")?
+    );
+
     // let best_corr: Arc<Mutex<HashMap<String, (f32, Vec<String>)>>> = Arc::new(Mutex::new(HashMap::new()));
 
     let enhancers_to_rnas: Arc<Mutex<HashMap<String, HashMap<String, f32>>>> = Arc::new(Mutex::new(HashMap::new()));
@@ -55,27 +61,30 @@ fn main() -> Result<(), Box<dyn Error>> {
     enhancer_set.par_iter().for_each(|(a_k, a_v)| {
 
         let correlations: Arc<Mutex<HashMap<String, f32>>> = Arc::new(Mutex::new(HashMap::new()));
-        println!("correlation for {}", a_k);
+        
+        bar.set_message(format!("correlation for {}...", a_k));
         
         rna_set.par_iter().for_each(|(b_k, b_v)| {
 
+            // Convert into 2darray
             let rand_vec = vec![a_v, b_v];
             let ncols = rand_vec.first().map_or(0, |row| row.len());
             let mut nrows = 0;
             let mut data = Vec::new();
-
             for i in 0..rand_vec.len() {
                 data.extend_from_slice(&rand_vec[i]);
                 nrows += 1;
             }
-
             let a: ndarray::ArrayBase<ndarray::OwnedRepr<_>, ndarray::Dim<[usize; 2]>> = Array2::from_shape_vec((nrows, ncols), data).unwrap();
 
+            // Compute the Pearson correlation and insert the result into hashmap
             let corr = a.pearson_correlation().unwrap();
             let mut correlations_guard = correlations.lock().unwrap();
             correlations_guard.insert(b_k.clone(), corr.clone().into_raw_vec().into_iter().reduce(f32::min).unwrap());
 
+            // Increment progress bar
         });
+        bar.inc(1);
 
         let correlations_guard = correlations.lock().unwrap();
         
@@ -95,14 +104,20 @@ fn main() -> Result<(), Box<dyn Error>> {
             
         // }
     });
+    bar.finish();
 
     let enhancers_to_rnas_guard = enhancers_to_rnas.lock().unwrap();
     println!("{:?} enhancers results to write...", enhancers_to_rnas_guard.len());
 
+    let bar = ProgressBar::new(enhancers_to_rnas_guard.len().try_into().unwrap());
+    bar.set_style(
+        ProgressStyle::default_bar()
+            .template("[{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} ({eta}) {msg}")?
+    );
+    bar.set_message("Writing output.tsv");
     let mut rna_keys: Vec<&String> = rna_set.keys().collect();
     rna_keys.sort();
 
-
     let mut file = File::create("output.tsv")?;
 
     // Write the header row
@@ -127,7 +142,9 @@ fn main() -> Result<(), Box<dyn Error>> {
             }
         }
         writeln!(file)?;
+        bar.inc(1);
     }
+    bar.finish();
 
     Ok(())
 }