-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenbank.rs
More file actions
539 lines (523 loc) · 21 KB
/
genbank.rs
File metadata and controls
539 lines (523 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use crate::calculate_hash;
use crate::genbank::{process_sequence, EditType, GenBankError};
use crate::models::block_group::{BlockGroup, PathChange};
use crate::models::block_group_edge::{BlockGroupEdge, BlockGroupEdgeData};
use crate::models::collection::Collection;
use crate::models::edge::Edge;
use crate::models::node::{Node, PATH_END_NODE_ID, PATH_START_NODE_ID};
use crate::models::operations::{Operation, OperationInfo};
use crate::models::path::{Path, PathBlock};
use crate::models::sample::Sample;
use crate::models::sequence::Sequence;
use crate::models::strand::Strand;
use crate::operation_management::{end_operation, start_operation};
use crate::progress_bar::{add_saving_operation_bar, get_handler, get_progress_bar};
use gb_io::reader;
use rusqlite::Connection;
use std::io::Read;
use std::str;
pub fn import_genbank<'a, R>(
conn: &Connection,
op_conn: &Connection,
data: R,
collection: impl Into<Option<&'a str>>,
sample: impl Into<Option<&'a str>>,
operation_info: OperationInfo,
) -> Result<Operation, GenBankError>
where
R: Read,
{
let progress_bar = get_handler();
let mut session = start_operation(conn);
let reader = reader::SeqReader::new(data);
let collection = Collection::create(conn, collection.into().unwrap_or_default());
let sample = sample.into();
if let Some(sample_name) = sample {
Sample::get_or_create(conn, sample_name);
}
let _ = progress_bar.println("Parsing GenBank");
let bar = progress_bar.add(get_progress_bar(None));
bar.set_message("Entries parsed");
for result in reader {
match result {
Ok(seq) => {
let locus = process_sequence(seq)?;
let original_seq = locus.original_sequence();
let mut seq_model = Sequence::new().sequence(&original_seq);
if !locus.name.is_empty() {
seq_model = seq_model.name(&locus.name);
}
if let Some(ref mol_type) = locus.molecule_type {
seq_model = seq_model.sequence_type(mol_type);
}
let sequence = seq_model.save(conn);
let wt_node_id = Node::create(
conn,
&sequence.hash,
calculate_hash(&format!(
"{collection}.{contig}:{hash}",
collection = &collection.name,
contig = &locus.name,
hash = sequence.hash
)),
);
let block_group = BlockGroup::create(conn, &collection.name, sample, &locus.name);
let edge_into = Edge::create(
conn,
PATH_START_NODE_ID,
0,
Strand::Forward,
wt_node_id,
0,
Strand::Forward,
);
let edge_out_of = Edge::create(
conn,
wt_node_id,
sequence.length,
Strand::Forward,
PATH_END_NODE_ID,
0,
Strand::Forward,
);
BlockGroupEdge::bulk_create(
conn,
&[
BlockGroupEdgeData {
block_group_id: block_group.id,
edge_id: edge_into.id,
chromosome_index: 0,
phased: 0,
},
BlockGroupEdgeData {
block_group_id: block_group.id,
edge_id: edge_out_of.id,
chromosome_index: 0,
phased: 0,
},
],
);
let path = Path::create(
conn,
&locus.name,
block_group.id,
&[edge_into.id, edge_out_of.id],
);
for edit in locus.changes_to_wt() {
let start = edit.start;
let end = edit.end;
let change = match edit.edit_type {
EditType::Insertion | EditType::Replacement => {
let change_seq = Sequence::new()
.sequence(&edit.new_sequence)
.name(&format!(
"Geneious type: Editing History {edit_type}",
edit_type = edit.edit_type
))
.sequence_type("DNA")
.save(conn);
let change_node = Node::create(
conn,
&change_seq.hash,
calculate_hash(&format!(
"{parent_hash}:{start}-{end}->{new_hash}",
parent_hash = &sequence.hash,
new_hash = &change_seq.hash,
)),
);
PathChange {
block_group_id: block_group.id,
path: path.clone(),
path_accession: None,
start,
end,
block: PathBlock {
id: 0,
node_id: change_node,
block_sequence: edit.new_sequence.clone(),
sequence_start: 0,
sequence_end: change_seq.length,
path_start: start,
path_end: end + change_seq.length,
strand: Strand::Forward,
},
chromosome_index: 0,
phased: 0,
}
}
EditType::Deletion => PathChange {
block_group_id: block_group.id,
path: path.clone(),
path_accession: None,
start,
end,
block: PathBlock {
id: 0,
node_id: wt_node_id,
block_sequence: "".to_string(),
sequence_start: 0,
sequence_end: 0,
path_start: start,
path_end: end,
strand: Strand::Forward,
},
chromosome_index: 0,
phased: 0,
},
};
let tree = path.intervaltree(conn);
BlockGroup::insert_change(conn, &change, &tree);
}
}
Err(e) => return Err(GenBankError::ParseError(format!("Failed to parse {}", e))),
}
bar.inc(1);
}
bar.finish();
let filename = operation_info.file_path.clone();
let bar = add_saving_operation_bar(&progress_bar);
let op = end_operation(
conn,
op_conn,
&mut session,
operation_info,
&format!("Genbank Import of {filename}",),
None,
)
.map_err(GenBankError::OperationError);
bar.finish();
op
}
#[cfg(test)]
mod tests {
use super::*;
use crate::models::file_types::FileTypes;
use crate::models::metadata;
use crate::models::operations::setup_db;
use crate::test_helpers::{get_connection, get_operation_connection, setup_gen_dir};
use noodles::fasta;
use std::collections::HashSet;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
fn get_unmodified_sequence() -> String {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/unmodified.fa");
let mut reader = fasta::io::reader::Builder.build_from_path(path).unwrap();
let mut records = reader.records();
let record = records.next().unwrap().unwrap();
let seq = record.sequence();
str::from_utf8(seq.as_ref()).unwrap().to_string()
}
#[test]
fn test_error_on_invalid_file() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
assert_eq!(
import_genbank(
conn,
op_conn,
BufReader::new("this is not valid".as_bytes()),
None,
None,
OperationInfo {
file_path: "".to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
}
),
Err(GenBankError::ParseError(
"Failed to parse Syntax error: Error Tag while parsing [this is not valid]"
.to_string()
))
)
}
#[test]
fn test_records_operation() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/insertion.gb");
let file = File::open(&path).unwrap();
let operation = import_genbank(
conn,
op_conn,
BufReader::new(file),
None,
None,
OperationInfo {
file_path: path.to_str().unwrap().to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
},
)
.unwrap();
assert_eq!(
Operation::get_by_hash(op_conn, &operation.hash).unwrap(),
operation
);
}
#[test]
fn test_creates_sample() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/insertion.gb");
let file = File::open(&path).unwrap();
let _ = import_genbank(
conn,
op_conn,
BufReader::new(file),
None,
"new-sample",
OperationInfo {
file_path: "".to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
},
);
assert_eq!(
Sample::get_by_name(conn, "new-sample").unwrap().name,
"new-sample"
);
}
#[cfg(test)]
mod geneious_genbanks {
use super::*;
use crate::normalize_string;
#[test]
fn test_parses_insertion() {
setup_gen_dir();
// this file has an insertion from 1426-2220
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/insertion.gb");
let file = File::open(&path).unwrap();
let _ = import_genbank(
conn,
op_conn,
BufReader::new(file),
None,
None,
OperationInfo {
file_path: "".to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
},
);
let f = reader::parse_file(&path).unwrap();
let seq = str::from_utf8(&f[0].seq).unwrap().to_string();
let seqs = BlockGroup::get_all_sequences(conn, 1, false);
assert_eq!(
seqs,
HashSet::from_iter([
seq.clone(),
format!("{}{}", &seq[..1425].to_string(), &seq[2220..].to_string()).to_string()
])
);
}
#[test]
fn test_parses_deletion() {
setup_gen_dir();
// this file has a deletion from 765-766
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/deletion.gb");
let file = File::open(&path).unwrap();
let _ = import_genbank(
conn,
op_conn,
BufReader::new(file),
None,
None,
OperationInfo {
file_path: "".to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
},
);
let f = reader::parse_file(&path).unwrap();
let seq = str::from_utf8(&f[0].seq).unwrap().to_string();
let deleted: String = normalize_string(
"TTACGCCCCGCCCTGCCACTCATCGCAGTACTGTTGTAATT
CATTAAGCATTCTGCCGACATGGAAGCCATCACAAACGGCATGATGAACCTGAATCGCCAGCG
GCATCAGCACCTTGTCGCCTTGCGTATAATATTTGCCCATGGTGAAAACGGGGGCGAAGAAGT
TGTCCATATTGGCCACGTTTAAATCAAAACTGGTGAAACTCACCCAGGGATTGGCTGAGACGA
AAAACATATTCTCAATAAACCCTTTAGGGAAATAGGCCAGGTTTTCACCGTAACACGCCACAT
CTTGCGAATATATGTGTAGAAACTGCCGGAAATCGTCGTGGTATTCACTCCAGAGCGATGAAA
ACGTTTCAGTTTGCTCATGGAAAACGGTGTAACAAGGGTGAACACTATCCCATATCACCAGCT
CACCGTCTTTCATTGCCATACGGAATTCCGGATGAGCATTCATCAGGCGGGCAAGAATGTGAA
TAAAGGCCGGATAAAACTTGTGCTTATTTTTCTTTACGGTCTTTAAAAAGGCCGTAATATCCA
GCTGAACGGTCTGGTTATAGGTACATTGAGCAACTGACTGAAATGCCTCAAAATGTTCTTTAC
GATGCCATTGGGATATATCAACGGTGGTATATCCAGTGATTTTTTTCTCCAT",
);
let seqs = BlockGroup::get_all_sequences(conn, 1, false);
assert_eq!(
seqs,
HashSet::from_iter([
seq.clone(),
format!(
"{}{deleted}{}",
&seq[..765].to_string(),
&seq[765..].to_string()
)
.to_string()
])
);
}
#[test]
fn test_parses_deletion_and_insertion() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/deletion_and_insertion.gb");
let file = File::open(&path).unwrap();
let _ = import_genbank(
conn,
op_conn,
BufReader::new(file),
None,
None,
OperationInfo {
file_path: "".to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
},
);
let f = reader::parse_file(&path).unwrap();
let seq = str::from_utf8(&f[0].seq).unwrap().to_string();
let deleted: String = normalize_string(
"TACGCCCCGCCCTGCCACTCATCGCAGTACTGTTGTAATTC
ATTAAGCATTCTGCCGACATGGAAGCCATCACAAACGGCATGATGAACCTGAATCGCC
AGCGGCATCAGCACCTTGTCGCCTTGCGTATAATATTTGCCCATGGTGAAAACGGGGG
CGAAGAAGTTGTCCATATTGGCCACGTTTAAATCAAAACTGGTGAAACTCACCCAGGG
ATTGGCTGAGACGAAAAACATATTCTCAATAAACCCTTTAGGGAAATAGGCCAGGTTT
TCACCGTAACACGCCACATCTTGCGAATATATGTGTAGAAACTGCCGGAAATCGTCGT
GGTATTCACTCCAGAGCGATGAAAACGTTTCAGTTTGCTCATGGAAAACGGTGTAACA
AGGGTGAACACTATCCCATATCACCAGCTCACCGTCTTTCATTGCCATACGGAATTCC
GGATGAGCATTCATCAGGCGGGCAAGAATGTGAATAAAGGCCGGATAAAACTTGTGCT
TATTTTTCTTTACGGTCTTTAAAAAGGCCGTAATATCCAGCTGAACGGTCTGGTTATA
GGTACATTGAGCAACTGACTGAAATGCCTCAAAATGTTCTTTACGATGCCATTGGGAT
ATATCAACGGTGGTATATCCAGTGATTTTTTTCTC",
);
let seqs = BlockGroup::get_all_sequences(conn, 1, false);
assert_eq!(
seqs,
HashSet::from_iter([
seq.clone(),
format!(
"{}{deleted}{}",
&seq[..766].to_string(),
&seq[1557..].to_string()
)
.to_string()
])
);
}
#[test]
fn test_parses_substitution() {
setup_gen_dir();
// replacing a sequence ends up with the same result as doing a compound delete + insert
// in the above test.
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/substitution.gb");
let file = File::open(&path).unwrap();
let _ = import_genbank(
conn,
op_conn,
BufReader::new(file),
None,
None,
OperationInfo {
file_path: "".to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
},
);
let f = reader::parse_file(&path).unwrap();
let seq = str::from_utf8(&f[0].seq).unwrap().to_string();
let deleted: String = normalize_string(
"TACGCCCCGCCCTGCCACTCATCGCAGTACTGTTGTAATTC
ATTAAGCATTCTGCCGACATGGAAGCCATCACAAACGGCATGATGAACCTGAATCGCC
AGCGGCATCAGCACCTTGTCGCCTTGCGTATAATATTTGCCCATGGTGAAAACGGGGG
CGAAGAAGTTGTCCATATTGGCCACGTTTAAATCAAAACTGGTGAAACTCACCCAGGG
ATTGGCTGAGACGAAAAACATATTCTCAATAAACCCTTTAGGGAAATAGGCCAGGTTT
TCACCGTAACACGCCACATCTTGCGAATATATGTGTAGAAACTGCCGGAAATCGTCGT
GGTATTCACTCCAGAGCGATGAAAACGTTTCAGTTTGCTCATGGAAAACGGTGTAACA
AGGGTGAACACTATCCCATATCACCAGCTCACCGTCTTTCATTGCCATACGGAATTCC
GGATGAGCATTCATCAGGCGGGCAAGAATGTGAATAAAGGCCGGATAAAACTTGTGCT
TATTTTTCTTTACGGTCTTTAAAAAGGCCGTAATATCCAGCTGAACGGTCTGGTTATA
GGTACATTGAGCAACTGACTGAAATGCCTCAAAATGTTCTTTACGATGCCATTGGGAT
ATATCAACGGTGGTATATCCAGTGATTTTTTTCTC",
);
let seqs = BlockGroup::get_all_sequences(conn, 1, false);
assert_eq!(
seqs,
HashSet::from_iter([
seq.clone(),
format!(
"{}{deleted}{}",
&seq[..766].to_string(),
&seq[1557..].to_string()
)
.to_string()
])
);
}
#[test]
fn test_parses_multiple_changes() {
setup_gen_dir();
let conn = &get_connection(None);
let db_uuid = metadata::get_db_uuid(conn);
let op_conn = &get_operation_connection(None);
setup_db(op_conn, &db_uuid);
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("fixtures/geneious_genbank/multiple_insertions_deletions.gb");
let file = File::open(&path).unwrap();
let _ = import_genbank(
conn,
op_conn,
BufReader::new(file),
None,
None,
OperationInfo {
file_path: "".to_string(),
file_type: FileTypes::GenBank,
description: "test".to_string(),
},
);
// there would be 4! sequences so we just check we have the fully changed and unchanged sequence
let f = reader::parse_file(&path).unwrap();
let mod_seq = str::from_utf8(&f[0].seq).unwrap().to_string();
let sequences: HashSet<String> = BlockGroup::get_all_sequences(conn, 1, false)
.iter()
.map(|s| s.to_lowercase())
.collect();
let unchanged_seq = get_unmodified_sequence();
assert!(sequences.contains(&mod_seq));
assert!(sequences.contains(&unchanged_seq));
}
}
}