-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgff.rs
More file actions
186 lines (169 loc) · 6.79 KB
/
gff.rs
File metadata and controls
186 lines (169 loc) · 6.79 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
use crate::models::block_group::BlockGroup;
use crate::models::path::{Annotation, Path};
use crate::models::sample::Sample;
use noodles::core::Position;
use noodles::gff;
use rusqlite::Connection;
use std::collections::HashMap;
use std::fs::File;
use std::io;
use std::io::BufReader;
pub fn propagate_gff(
conn: &Connection,
collection_name: &str,
from_sample_name: Option<&str>,
to_sample_name: &str,
gff_input_filename: &str,
gff_output_filename: &str,
) -> io::Result<()> {
let mut reader = File::open(gff_input_filename)
.map(BufReader::new)
.map(gff::io::Reader::new)?;
let output_file = File::create(gff_output_filename).unwrap();
let mut writer = gff::io::Writer::new(output_file);
let source_block_groups = Sample::get_block_groups(conn, collection_name, from_sample_name);
let target_block_groups = Sample::get_block_groups(conn, collection_name, Some(to_sample_name));
let source_paths_by_bg_name = source_block_groups
.iter()
.map(|bg| (bg.name.clone(), BlockGroup::get_current_path(conn, bg.id)))
.collect::<HashMap<String, Path>>();
let target_paths_by_bg_name = target_block_groups
.iter()
.map(|bg| (bg.name.clone(), BlockGroup::get_current_path(conn, bg.id)))
.collect::<HashMap<String, Path>>();
let mut path_mappings_by_bg_name = HashMap::new();
for (name, target_path) in target_paths_by_bg_name.iter() {
let source_path = source_paths_by_bg_name.get(name).unwrap();
let mapping = source_path.get_mapping_tree(conn, target_path);
path_mappings_by_bg_name.insert(name, mapping);
}
let sequence_lengths_by_path_name = target_paths_by_bg_name
.iter()
.map(|(name, path)| (name.clone(), path.sequence(conn).len() as i64))
.collect::<HashMap<String, i64>>();
for result in reader.record_bufs() {
let record = result?;
let path_name = record.reference_sequence_name().to_string();
let annotation = Annotation {
name: "".to_string(),
start: record.start().get() as i64,
end: record.end().get() as i64,
};
let mapping_tree = path_mappings_by_bg_name.get(&path_name).unwrap();
let sequence_length = sequence_lengths_by_path_name.get(&path_name).unwrap();
let propagated_annotation =
Path::propagate_annotation(annotation, mapping_tree, *sequence_length).unwrap();
let score = record.score();
let phase = record.phase();
let mut updated_record_builder = gff::RecordBuf::builder()
.set_reference_sequence_name(path_name)
.set_source(record.source().to_string())
.set_type(record.ty().to_string())
.set_start(
Position::new(propagated_annotation.start.try_into().unwrap())
.expect("Could not convert start ({start}) to usize for propagation"),
)
.set_end(
Position::new(propagated_annotation.end.try_into().unwrap())
.expect("Could not convert end ({end}) to usize for propagation"),
)
.set_strand(record.strand())
.set_attributes(record.attributes().clone());
if let Some(score) = score {
updated_record_builder = updated_record_builder.set_score(score);
}
if let Some(phase) = phase {
updated_record_builder = updated_record_builder.set_phase(phase);
}
writer.write_record(&updated_record_builder.build())?;
}
Ok(())
}
#[cfg(test)]
mod tests {
// Note this useful idiom: importing names from outer (for mod tests) scope.
use super::*;
use crate::imports::fasta::import_fasta;
use crate::models::metadata;
use crate::models::operations::setup_db;
use crate::test_helpers::{get_connection, get_operation_connection, setup_gen_dir};
use crate::updates::fasta::update_with_fasta;
use std::path::PathBuf;
use tempfile::tempdir;
#[test]
fn test_simple_propagate() {
setup_gen_dir();
let mut fasta_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fasta_path.push("fixtures/simple.fa");
let mut fasta_update_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
fasta_update_path.push("fixtures/aa.fa");
let mut gff_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
gff_path.push("fixtures/simple.gff");
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);
import_fasta(
&fasta_path.to_str().unwrap().to_string(),
"test",
None,
false,
&conn,
op_conn,
)
.unwrap();
let _ = update_with_fasta(
&conn,
op_conn,
"test",
None,
"child sample",
"m123",
15,
25,
fasta_update_path.to_str().unwrap(),
);
let temp_dir = tempdir().expect("Couldn't get handle to temp directory");
let mut output_path = PathBuf::from(temp_dir.path());
output_path.push("output.gff");
let _ = propagate_gff(
&conn,
"test",
None,
"child sample",
gff_path.to_str().unwrap(),
output_path.to_str().unwrap(),
);
let reader = File::open(output_path.to_str().unwrap())
.map(BufReader::new)
.map(gff::io::Reader::new);
for (i, result) in reader
.expect("Could not read output file!")
.record_bufs()
.enumerate()
{
let record = result.unwrap();
assert_eq!(record.reference_sequence_name(), "m123");
if i == 0 {
assert_eq!(record.reference_sequence_name(), "m123");
assert_eq!(record.source(), "gen-test");
assert_eq!(record.ty(), "Region");
// Full region annotation
// Original sequence is on the full region, [0, 34) range (length 34)
// The edit replaces [15, 25) with a 2 bp sequence
// New sequence length is 34 - 8 = 26
assert_eq!(record.start().get(), 1);
assert_eq!(record.end().get(), 26);
} else {
assert_eq!(record.reference_sequence_name(), "m123");
assert_eq!(record.source(), "gen-test");
assert_eq!(record.ty(), "Gene");
// Gene annotation, was on [5, 20)
// Replaced [15, 25) with a 2 bp sequence
// New gene annotation is [5, 15)
assert_eq!(record.start().get(), 5);
assert_eq!(record.end().get(), 15);
}
}
}
}