bio-grumpy 0.1.14__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/Cargo.lock +60 -14
  2. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/Cargo.toml +2 -1
  3. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/PKG-INFO +6 -1
  4. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/README-python.md +6 -1
  5. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/src/common.rs +25 -0
  6. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/src/difference.rs +27 -13
  7. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/src/lib.rs +1 -0
  8. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/src/vcf.rs +172 -110
  9. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/.github/workflows/release.yaml +0 -0
  10. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/.github/workflows/test.yaml +0 -0
  11. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/.gitignore +0 -0
  12. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/LICENSE +0 -0
  13. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/README.md +0 -0
  14. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/pyproject.toml +0 -0
  15. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/reference/MN908947.3.gb +0 -0
  16. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/reference/NC_000962.3.gbk +0 -0
  17. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/reference/TEST-DNA.gbk +0 -0
  18. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/src/gene.rs +0 -0
  19. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/src/genome.rs +0 -0
  20. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/src/main.rs +0 -0
  21. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/TEST-DNA-4-minor.vcf +0 -0
  22. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/TEST-DNA-4.vcf +0 -0
  23. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/TEST-DNA-misc-indel.vcf +0 -0
  24. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/TEST-DNA.vcf +0 -0
  25. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/dummy.vcf +0 -0
  26. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/minor-populations-revcomp-AD.vcf +0 -0
  27. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/minor-populations-revcomp.vcf +0 -0
  28. {bio_grumpy-0.1.14 → bio_grumpy-0.2.0}/test/revcomp-del-first-pos.vcf +0 -0
@@ -32,6 +32,31 @@ version = "0.3.0"
32
32
  source = "registry+https://github.com/rust-lang/crates.io-index"
33
33
  checksum = "b0fc239e0f6cb375d2402d48afb92f76f5404fd1df208a41930ec81eda078bea"
34
34
 
35
+ [[package]]
36
+ name = "crossbeam-deque"
37
+ version = "0.8.5"
38
+ source = "registry+https://github.com/rust-lang/crates.io-index"
39
+ checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d"
40
+ dependencies = [
41
+ "crossbeam-epoch",
42
+ "crossbeam-utils",
43
+ ]
44
+
45
+ [[package]]
46
+ name = "crossbeam-epoch"
47
+ version = "0.9.18"
48
+ source = "registry+https://github.com/rust-lang/crates.io-index"
49
+ checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
50
+ dependencies = [
51
+ "crossbeam-utils",
52
+ ]
53
+
54
+ [[package]]
55
+ name = "crossbeam-utils"
56
+ version = "0.8.20"
57
+ source = "registry+https://github.com/rust-lang/crates.io-index"
58
+ checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
59
+
35
60
  [[package]]
36
61
  name = "diff"
37
62
  version = "0.1.13"
@@ -88,12 +113,13 @@ dependencies = [
88
113
 
89
114
  [[package]]
90
115
  name = "grumpy"
91
- version = "0.1.14"
116
+ version = "0.2.0"
92
117
  dependencies = [
93
118
  "gb-io",
94
119
  "ordered-float",
95
120
  "pretty_assertions",
96
121
  "pyo3",
122
+ "rayon",
97
123
  "string_cache",
98
124
  "vcf",
99
125
  ]
@@ -365,7 +391,7 @@ dependencies = [
365
391
  "proc-macro2",
366
392
  "pyo3-macros-backend",
367
393
  "quote",
368
- "syn 2.0.75",
394
+ "syn 2.0.76",
369
395
  ]
370
396
 
371
397
  [[package]]
@@ -378,14 +404,14 @@ dependencies = [
378
404
  "proc-macro2",
379
405
  "pyo3-build-config",
380
406
  "quote",
381
- "syn 2.0.75",
407
+ "syn 2.0.76",
382
408
  ]
383
409
 
384
410
  [[package]]
385
411
  name = "quote"
386
- version = "1.0.36"
412
+ version = "1.0.37"
387
413
  source = "registry+https://github.com/rust-lang/crates.io-index"
388
- checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
414
+ checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
389
415
  dependencies = [
390
416
  "proc-macro2",
391
417
  ]
@@ -420,6 +446,26 @@ dependencies = [
420
446
  "getrandom",
421
447
  ]
422
448
 
449
+ [[package]]
450
+ name = "rayon"
451
+ version = "1.10.0"
452
+ source = "registry+https://github.com/rust-lang/crates.io-index"
453
+ checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
454
+ dependencies = [
455
+ "either",
456
+ "rayon-core",
457
+ ]
458
+
459
+ [[package]]
460
+ name = "rayon-core"
461
+ version = "1.12.1"
462
+ source = "registry+https://github.com/rust-lang/crates.io-index"
463
+ checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
464
+ dependencies = [
465
+ "crossbeam-deque",
466
+ "crossbeam-utils",
467
+ ]
468
+
423
469
  [[package]]
424
470
  name = "redox_syscall"
425
471
  version = "0.5.3"
@@ -443,9 +489,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
443
489
 
444
490
  [[package]]
445
491
  name = "serde"
446
- version = "1.0.208"
492
+ version = "1.0.209"
447
493
  source = "registry+https://github.com/rust-lang/crates.io-index"
448
- checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2"
494
+ checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09"
449
495
  dependencies = [
450
496
  "serde_derive",
451
497
  ]
@@ -461,13 +507,13 @@ dependencies = [
461
507
 
462
508
  [[package]]
463
509
  name = "serde_derive"
464
- version = "1.0.208"
510
+ version = "1.0.209"
465
511
  source = "registry+https://github.com/rust-lang/crates.io-index"
466
- checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf"
512
+ checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
467
513
  dependencies = [
468
514
  "proc-macro2",
469
515
  "quote",
470
- "syn 2.0.75",
516
+ "syn 2.0.76",
471
517
  ]
472
518
 
473
519
  [[package]]
@@ -521,9 +567,9 @@ dependencies = [
521
567
 
522
568
  [[package]]
523
569
  name = "syn"
524
- version = "2.0.75"
570
+ version = "2.0.76"
525
571
  source = "registry+https://github.com/rust-lang/crates.io-index"
526
- checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9"
572
+ checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
527
573
  dependencies = [
528
574
  "proc-macro2",
529
575
  "quote",
@@ -565,7 +611,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
565
611
  dependencies = [
566
612
  "proc-macro2",
567
613
  "quote",
568
- "syn 2.0.75",
614
+ "syn 2.0.76",
569
615
  ]
570
616
 
571
617
  [[package]]
@@ -703,5 +749,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
703
749
  dependencies = [
704
750
  "proc-macro2",
705
751
  "quote",
706
- "syn 2.0.75",
752
+ "syn 2.0.76",
707
753
  ]
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "grumpy"
3
- version = "0.1.14"
3
+ version = "0.2.0"
4
4
  edition = "2021"
5
5
  description = "Genetic analysis in Rust."
6
6
  license-file = "LICENSE"
@@ -15,6 +15,7 @@ string_cache = "0.8.7"
15
15
  vcf = "0.6.1"
16
16
  pyo3 = { version = "0.22.1", features = ["extension-module"] }
17
17
  pretty_assertions = "1.4.0"
18
+ rayon = "1.10.0"
18
19
 
19
20
  [profile.dev]
20
21
  opt-level = 3
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: bio-grumpy
3
- Version: 0.1.14
3
+ Version: 0.2.0
4
4
  Classifier: Development Status :: 2 - Pre-Alpha
5
5
  Classifier: Programming Language :: Rust
6
6
  Classifier: Programming Language :: Python :: Implementation :: CPython
@@ -57,3 +57,8 @@ for gene_name in sample.genes_with_mutations:
57
57
  for mutation in gene_diff.minor_mutations:
58
58
  print(mutation.mutation)
59
59
  ```
60
+
61
+ ## Threads
62
+ By default, VCF parsing will run with the same number of threads as CPU cores. This can give significant speed improvements for complex VCF files
63
+
64
+ To force single threading (or other number of threads), run `grumpy.thread_setup(<thread count>)`. Note that should be run before **any** other function in this library, and should not be called more than once! Not following this will cause errors!
@@ -42,4 +42,9 @@ for gene_name in sample.genes_with_mutations:
42
42
  # And minor alleles
43
43
  for mutation in gene_diff.minor_mutations:
44
44
  print(mutation.mutation)
45
- ```
45
+ ```
46
+
47
+ ## Threads
48
+ By default, VCF parsing will run with the same number of threads as CPU cores. This can give significant speed improvements for complex VCF files
49
+
50
+ To force single threading (or other number of threads), run `grumpy.thread_setup(<thread count>)`. Note that should be run before **any** other function in this library, and should not be called more than once! Not following this will cause errors!
@@ -165,3 +165,28 @@ pub struct Alt {
165
165
  /// Evidence associated with this call
166
166
  pub evidence: Evidence,
167
167
  }
168
+
169
+ #[cfg(not(tarpaulin_include))]
170
+ #[pyfunction]
171
+ #[pyo3(signature = (num_threads=None))]
172
+ /// Set up the number of threads to use for parallel operations
173
+ /// If None, use the default number of threads (usually the number of cores)
174
+ ///
175
+ /// Only call this function to use less cores than the default. Note that
176
+ /// it should be called before **any** other function in the program, and
177
+ /// should not be called more than once.
178
+ ///
179
+ /// # Arguments
180
+ /// * `num_threads` - Number of threads to use for parallel operations
181
+ pub fn thread_setup(num_threads: Option<usize>) {
182
+ let result = match num_threads {
183
+ Some(num) => rayon::ThreadPoolBuilder::new()
184
+ .num_threads(num)
185
+ .build_global(),
186
+ None => rayon::ThreadPoolBuilder::new().build_global(),
187
+ };
188
+ match result {
189
+ Ok(_) => (),
190
+ Err(e) => panic!("Error setting up threads: {}", e),
191
+ }
192
+ }
@@ -248,13 +248,15 @@ impl GenomeDifference {
248
248
 
249
249
  if alt.evidence.is_minor {
250
250
  // Append coverage to the variant
251
- if minor_type == MinorType::COV {
251
+ if minor_type == MinorType::COV && alt.evidence.cov.is_some() {
252
252
  garc = garc + ":" + &alt.evidence.cov.unwrap().to_string();
253
- }
254
- if minor_type == MinorType::FRS {
253
+ } else if minor_type == MinorType::FRS && alt.evidence.frs.is_some() {
255
254
  garc = garc
256
255
  + ":"
257
256
  + &trim_float_string(format!("{:.3}", alt.evidence.frs.unwrap()));
257
+ } else {
258
+ println!("Missing evidence for minor allele: {}, skipping!", garc);
259
+ continue;
258
260
  }
259
261
  }
260
262
 
@@ -656,18 +658,26 @@ impl GeneDifference {
656
658
  indel_nucleotides = Some(e.base.clone());
657
659
  evidence = vec![e.evidence.clone()];
658
660
  }
659
- if minor_type == MinorType::COV {
661
+ if minor_type == MinorType::COV && e.evidence.cov.is_some()
662
+ {
660
663
  _mutation = _mutation
661
664
  + ":"
662
665
  + &e.evidence.cov.unwrap().to_string();
663
- }
664
- if minor_type == MinorType::FRS {
666
+ } else if minor_type == MinorType::FRS
667
+ && e.evidence.frs.is_some()
668
+ {
665
669
  _mutation = _mutation
666
670
  + ":"
667
671
  + &trim_float_string(format!(
668
672
  "{:.3}",
669
673
  e.evidence.frs.unwrap()
670
674
  ));
675
+ } else {
676
+ println!(
677
+ "Missing evidence for minor allele: {}, skipping!",
678
+ _mutation
679
+ );
680
+ continue;
671
681
  }
672
682
  minor_mutations.push(Mutation {
673
683
  mutation: _mutation.clone(),
@@ -740,10 +750,10 @@ impl GeneDifference {
740
750
  for e in e.iter() {
741
751
  minor_evidence.push(e.clone());
742
752
  }
743
- if cov.unwrap() > minor_cov {
753
+ if cov.is_some() && cov.unwrap() > minor_cov {
744
754
  minor_cov = cov.unwrap();
745
755
  }
746
- if frs.unwrap() > minor_frs {
756
+ if frs.is_some() && frs.unwrap() > minor_frs {
747
757
  minor_frs = frs.unwrap();
748
758
  }
749
759
  }
@@ -837,11 +847,11 @@ impl GeneDifference {
837
847
 
838
848
  if alt.evidence.is_minor {
839
849
  // Append coverage to the variant
840
- if minor_type == MinorType::COV {
850
+ if minor_type == MinorType::COV && alt.evidence.cov.is_some() {
841
851
  mutation =
842
852
  mutation + ":" + &alt.evidence.cov.unwrap().to_string();
843
- }
844
- if minor_type == MinorType::FRS {
853
+ } else if minor_type == MinorType::FRS && alt.evidence.frs.is_some()
854
+ {
845
855
  mutation = mutation
846
856
  + ":"
847
857
  + &trim_float_string(format!(
@@ -850,10 +860,14 @@ impl GeneDifference {
850
860
  ));
851
861
  }
852
862
  if alt.alt_type == AltType::DEL {
853
- if alt.evidence.cov.unwrap() > minor_deleted_cov {
863
+ if alt.evidence.cov.is_some()
864
+ && alt.evidence.cov.unwrap() > minor_deleted_cov
865
+ {
854
866
  minor_deleted_cov = alt.evidence.cov.unwrap();
855
867
  }
856
- if alt.evidence.frs.unwrap() > minor_deleted_frs {
868
+ if alt.evidence.frs.is_some()
869
+ && alt.evidence.frs.unwrap() > minor_deleted_frs
870
+ {
857
871
  minor_deleted_frs = alt.evidence.frs.unwrap();
858
872
  }
859
873
  }
@@ -66,6 +66,7 @@ fn grumpy(m: &Bound<'_, PyModule>) -> PyResult<()> {
66
66
  m.add_class::<difference::GeneDifference>()?;
67
67
 
68
68
  m.add_function(wrap_pyfunction!(genome::mutate, m)?)?;
69
+ m.add_function(wrap_pyfunction!(common::thread_setup, m)?)?;
69
70
 
70
71
  Ok(())
71
72
  }
@@ -1,13 +1,26 @@
1
1
  //! Module for handling VCF files
2
2
  use pyo3::prelude::*;
3
+ use rayon::prelude::*;
3
4
 
4
5
  use std::collections::{hash_map, HashMap};
5
6
  use std::fs::File;
6
7
  use std::io::BufReader;
7
8
  use std::string::String;
8
- use vcf::VCFReader;
9
+ use vcf::{VCFReader, VCFRecord};
9
10
 
10
11
  use crate::common::{AltType, Evidence, VCFRow};
12
+
13
+ #[pyclass]
14
+ /// Dummy struct for wrapping VCFRecord
15
+ ///
16
+ /// Required to make a valid pyclass to use as a function argument
17
+ #[derive(Clone)]
18
+ pub struct VCFRecordToParse {
19
+ pub record: VCFRecord,
20
+ pub min_dp: i32,
21
+ pub required_fields: Vec<String>,
22
+ }
23
+
11
24
  #[pyclass]
12
25
  #[derive(Clone, Debug)]
13
26
  /// Struct to hold the information from a VCF file
@@ -45,103 +58,36 @@ impl VCFFile {
45
58
 
46
59
  let mut record = reader.empty_record();
47
60
  let mut more_records = reader.next_record(&mut record);
61
+ let mut reader_records = Vec::new();
48
62
 
49
- let required_fields = ["GT"];
50
- let mut records: Vec<VCFRow> = Vec::new();
63
+ let required_fields = vec!["GT".to_string()];
64
+ // Read data into memory
65
+ while matches!(more_records, Ok(true)) {
66
+ reader_records.push(record.clone());
67
+ more_records = reader.next_record(&mut record);
68
+ }
51
69
 
70
+ // Parse records multithreaded
71
+ let parsed = reader_records
72
+ .par_iter()
73
+ .map(|record| {
74
+ VCFFile::parse_record(VCFRecordToParse {
75
+ record: record.clone(),
76
+ min_dp,
77
+ required_fields: required_fields.clone(),
78
+ })
79
+ })
80
+ .collect::<Vec<(VCFRow, Vec<Evidence>, Vec<Evidence>)>>();
52
81
  // For ease of access, we'll store the calls in a hashmap indexed on genome index
53
82
  let mut calls_map: HashMap<i64, Vec<Evidence>> = HashMap::new();
54
83
  let mut minor_calls_map: HashMap<i64, Vec<Evidence>> = HashMap::new();
55
- while matches!(more_records, Ok(true)) {
56
- // Parse fields of the record to pull out data
57
- // For whatever reason non of these fields are strings, so we need to convert them
58
- let mut alts = Vec::new(); // One string per alt
59
- for alt in record.alternative.iter() {
60
- alts.push(String::from_utf8_lossy(alt).to_string().to_lowercase());
61
- }
62
- let mut filters = Vec::new(); // String per filter
63
- for filter in record.filter.iter() {
64
- for f in String::from_utf8_lossy(filter).split(";") {
65
- filters.push(f.to_string());
66
- }
67
- }
68
-
69
- // Oddities of how this VCF library works...
70
- // Format is a vector of bytes, but we need to convert it to a vector of strings
71
- // Each of these strings corresponds to an item in the genotypes vector
72
- let mut format: Vec<String> = Vec::new();
73
- for f in record.format.iter() {
74
- format.push(String::from_utf8_lossy(f).to_string());
75
- }
76
-
77
- let mut idx = 0;
78
- let mut fields: HashMap<String, Vec<String>> = HashMap::new();
79
- for s in record.genotype.iter() {
80
- let mut item: Vec<Vec<String>> = Vec::new();
81
- for i in s.iter() {
82
- let mut value: Vec<String> = Vec::new();
83
- for j in i.iter() {
84
- value.push(String::from_utf8_lossy(j).to_string());
85
- }
86
- item.push(value.clone());
87
- fields.insert(format[idx].clone(), value.clone());
88
- idx += 1;
89
- }
90
- }
91
-
92
- // Validate that this record has the required fields
93
- for field in required_fields.iter() {
94
- if !format.contains(&field.to_string()) {
95
- panic!("Required field {} not found in record", field);
96
- }
97
- }
98
-
99
- // Enforce that this record has a COV field
100
- if !fields.contains_key("COV") {
101
- let mut _cov_tag = "";
102
- let mut cov = Vec::new();
103
- if fields.contains_key("AD") {
104
- _cov_tag = "AD";
105
- } else if fields.contains_key("RO") && fields.contains_key("AO") {
106
- // Annoying edge case where no single COV field exists but can be constructed
107
- _cov_tag = "RO";
108
- } else {
109
- panic!("No COV tag found in record");
110
- }
111
- if _cov_tag != "RO" {
112
- for c in fields.get(_cov_tag).unwrap() {
113
- cov.push(c.to_string());
114
- }
115
- } else {
116
- let ro = fields.get("RO").unwrap()[0].clone();
117
- let ao = fields.get("AO").unwrap()[0].clone();
118
- cov.push(ro.to_string());
119
- cov.push(ao.to_string());
120
- }
121
-
122
- fields.insert("COV".to_string(), cov);
123
- }
124
-
125
- // Check if this record has passed the filters
126
- let mut passed = false;
127
- if filters == vec!["PASS"] {
128
- passed = true;
129
- }
130
-
131
- records.push(VCFRow {
132
- position: record.position as i64,
133
- reference: String::from_utf8_lossy(&record.reference)
134
- .to_string()
135
- .to_lowercase(),
136
- alternative: alts.clone(),
137
- filter: filters.clone(),
138
- fields: fields.clone(),
139
- is_filter_pass: passed,
140
- });
141
-
142
- let (record_calls, mut record_minor_calls) =
143
- VCFFile::parse_record_for_calls(records[records.len() - 1].clone(), min_dp);
84
+ let mut records = Vec::new();
144
85
 
86
+ // Fetch data
87
+ for (record, record_calls, _record_minor_calls) in parsed.iter() {
88
+ let passed = record.is_filter_pass;
89
+ let mut record_minor_calls = _record_minor_calls.clone();
90
+ records.push(record.clone());
145
91
  for call in record_calls.iter() {
146
92
  let mut added = false;
147
93
  if call.call_type == AltType::NULL {
@@ -157,7 +103,7 @@ impl VCFFile {
157
103
  "NO_DATA".to_string(),
158
104
  ];
159
105
  let mut all_allowed = true;
160
- for f in filters.iter() {
106
+ for f in record.filter.iter() {
161
107
  if !allowed_filters.contains(f) {
162
108
  all_allowed = false;
163
109
  break;
@@ -202,8 +148,29 @@ impl VCFFile {
202
148
  record_minor_calls.push(c);
203
149
  }
204
150
  }
205
- // Add minor calls if the filter is passed or ignored, or specifcally just the MIN_FRS has failed
206
- if ignore_filter || passed || filters.contains(&"MIN_FRS".to_string()) {
151
+
152
+ // Add minor calls if the filter is passed or ignored, or if fails lie within allowed filters
153
+ let mut valid_filters = passed || ignore_filter;
154
+ if !valid_filters {
155
+ // Not passed filter and not ignored, so check if all filters are in allowed filters
156
+ let allowed_filters = [
157
+ "MIN_FRS".to_string(),
158
+ "MIN_DP".to_string(),
159
+ "MIN_GCP".to_string(),
160
+ ];
161
+ // Auto-ignore if no filters are present (i.e filter = "." which is fail)
162
+ let mut all_allowed = !record.filter.is_empty();
163
+ for f in record.filter.iter() {
164
+ if !allowed_filters.contains(f) {
165
+ all_allowed = false;
166
+ break;
167
+ }
168
+ }
169
+ if all_allowed {
170
+ valid_filters = true;
171
+ }
172
+ }
173
+ if valid_filters {
207
174
  for call in record_minor_calls.iter() {
208
175
  if let hash_map::Entry::Vacant(e) = minor_calls_map.entry(call.genome_index) {
209
176
  e.insert(vec![call.clone()]);
@@ -215,21 +182,6 @@ impl VCFFile {
215
182
  }
216
183
  }
217
184
  }
218
-
219
- // if record.position == 1474466{
220
- // println!("{:?}\t{:?}\t{:?}\t{:?}\t{:?}", record.position, String::from_utf8_lossy(&record.reference), alts, filters, fields);
221
- // for call in record_calls.iter(){
222
- // println!("{:?}\n", call);
223
- // }
224
- // println!("--");
225
- // for call in record_minor_calls.iter(){
226
- // println!("{:?}\n", call);
227
- // }
228
- // println!("\n\n");
229
- // }
230
-
231
- // Get the next record
232
- more_records = reader.next_record(&mut record);
233
185
  }
234
186
 
235
187
  // I hate implict returns, but appease clippy
@@ -241,6 +193,116 @@ impl VCFFile {
241
193
  }
242
194
  }
243
195
 
196
+ #[staticmethod]
197
+ fn parse_record(rec: VCFRecordToParse) -> (VCFRow, Vec<Evidence>, Vec<Evidence>) {
198
+ let record = rec.record;
199
+ let min_dp = rec.min_dp;
200
+ let required_fields = rec.required_fields;
201
+
202
+ // Parse fields of the record to pull out data
203
+ // For whatever reason non of these fields are strings, so we need to convert them
204
+ let mut alts = Vec::new(); // One string per alt
205
+ for alt in record.alternative.iter() {
206
+ alts.push(String::from_utf8_lossy(alt).to_string().to_lowercase());
207
+ }
208
+ let mut filters = Vec::new(); // String per filter
209
+ for filter in record.filter.iter() {
210
+ for f in String::from_utf8_lossy(filter).split(";") {
211
+ filters.push(f.to_string());
212
+ }
213
+ }
214
+
215
+ // Oddities of how this VCF library works...
216
+ // Format is a vector of bytes, but we need to convert it to a vector of strings
217
+ // Each of these strings corresponds to an item in the genotypes vector
218
+ let mut format: Vec<String> = Vec::new();
219
+ for f in record.format.iter() {
220
+ format.push(String::from_utf8_lossy(f).to_string());
221
+ }
222
+
223
+ let mut idx = 0;
224
+ let mut fields: HashMap<String, Vec<String>> = HashMap::new();
225
+ for s in record.genotype.iter() {
226
+ let mut item: Vec<Vec<String>> = Vec::new();
227
+ for i in s.iter() {
228
+ let mut value: Vec<String> = Vec::new();
229
+ for j in i.iter() {
230
+ value.push(String::from_utf8_lossy(j).to_string());
231
+ }
232
+ item.push(value.clone());
233
+ fields.insert(format[idx].clone(), value.clone());
234
+ idx += 1;
235
+ }
236
+ }
237
+
238
+ // Validate that this record has the required fields
239
+ for field in required_fields.iter() {
240
+ if !format.contains(&field.to_string()) {
241
+ panic!("Required field {} not found in record", field);
242
+ }
243
+ }
244
+
245
+ // Enforce that this record has a COV field
246
+ if !fields.contains_key("COV") {
247
+ let mut _cov_tag = "";
248
+ let mut cov = Vec::new();
249
+ if fields.contains_key("AD") {
250
+ _cov_tag = "AD";
251
+ } else if fields.contains_key("RO") && fields.contains_key("AO") {
252
+ // Annoying edge case where no single COV field exists but can be constructed
253
+ _cov_tag = "RO";
254
+ } else {
255
+ panic!("No COV tag found in record");
256
+ }
257
+ if _cov_tag != "RO" {
258
+ for c in fields.get(_cov_tag).unwrap() {
259
+ cov.push(c.to_string());
260
+ }
261
+ } else {
262
+ let ro = fields.get("RO").unwrap()[0].clone();
263
+ let ao = fields.get("AO").unwrap()[0].clone();
264
+ cov.push(ro.to_string());
265
+ cov.push(ao.to_string());
266
+ }
267
+
268
+ fields.insert("COV".to_string(), cov);
269
+ }
270
+
271
+ // Check if this record has passed the filters
272
+ let mut passed = false;
273
+ if filters == vec!["PASS"] {
274
+ passed = true;
275
+ }
276
+
277
+ let row = VCFRow {
278
+ position: record.position as i64,
279
+ reference: String::from_utf8_lossy(&record.reference)
280
+ .to_string()
281
+ .to_lowercase(),
282
+ alternative: alts.clone(),
283
+ filter: filters.clone(),
284
+ fields: fields.clone(),
285
+ is_filter_pass: passed,
286
+ };
287
+
288
+ let (record_calls, record_minor_calls) =
289
+ VCFFile::parse_record_for_calls(row.clone(), min_dp);
290
+
291
+ // if record.position == 1474466{
292
+ // println!("{:?}\t{:?}\t{:?}\t{:?}\t{:?}", record.position, String::from_utf8_lossy(&record.reference), alts, filters, fields);
293
+ // for call in record_calls.iter(){
294
+ // println!("{:?}\n", call);
295
+ // }
296
+ // println!("--");
297
+ // for call in record_minor_calls.iter(){
298
+ // println!("{:?}\n", call);
299
+ // }
300
+ // println!("\n\n");
301
+ // }
302
+
303
+ (row, record_calls, record_minor_calls)
304
+ }
305
+
244
306
  #[staticmethod]
245
307
  /// Parse a record from a VCF file to get the calls
246
308
  ///
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes