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
use std::{
fs::{self, File},
io::{BufRead, BufReader, Write},
ops::Deref,
path::{Path, PathBuf},
};
use anyhow::{anyhow, Error};
use petgraph::{algo::astar, prelude::UnGraph};
use crate::{
methylation_site::{MethylationSite, MethylationStatus},
*,
};
use ndarray::{array, Array2, ArrayView, Axis};
#[derive(Clone, Debug)]
struct Node {
id: usize,
file: PathBuf,
name: String,
generation: u32,
meth: bool,
proportion_unmethylated: Option<f64>,
rc_meth_lvl: Option<f64>,
sites: Option<Vec<MethylationSite>>, // Might lead to memory issues if there are too many sites. But it makes lookup really fast.
}
#[derive(Debug)]
struct Edge<'a> {
from: &'a Node,
to: &'a Node,
}
/// A pedigree describes the divergence bewteen any two samples in a population.
/// It's a matrix with four columns, containing the following information:
///
/// t0: The generation of the last common ancestor between two samples (Can be one of the samples if direct heritage).
///
/// t1: The generation of the first sample.
///
/// t2: The generation of the second sample.
///
/// d: The divergence between the two samples.
///
/// The length of the pedigree is the number of possible pairs of samples, for which methlyation data is available => n * (n - 1) / 2
#[derive(Clone, Debug)]
pub struct Pedigree(Array2<f64>);
impl Pedigree {
/// Read a pedigree from a file.
///
/// The file must be a tab-separated file with four columns:
///
/// `t0`: The generation of the last common ancestor between two samples (Can be one of the samples if direct heritage).
///
/// `t1`: The generation of the first sample.
///
/// `t2`: The generation of the second sample.
///
///` d`: The divergence between the two samples.
///
/// The first line of the file is ignored.
/// I chose not to return a result, as this function is meant to statically read a file and therefore it is preferable to panic if the file is not found or parsing errors occur.
pub fn from_file(filename: &str) -> Self {
let mut pedigree = Array2::<f64>::zeros((0, 4));
let file = std::fs::read_to_string(filename).unwrap();
file.split('\n').skip(1).for_each(|line| {
if line.is_empty() {
return;
}
let mut entries = line.split(' ');
let row = [
entries.next().unwrap().parse::<f64>().unwrap(),
entries.next().unwrap().parse::<f64>().unwrap(),
entries.next().unwrap().parse::<f64>().unwrap(),
entries.next().unwrap().parse::<f64>().unwrap(),
];
pedigree.push_row(ArrayView::from(&row)).unwrap();
});
Pedigree(pedigree)
}
pub fn to_file(&self, path: &Path) -> std::io::Result<()> {
println!("Writing pedigree to file: {}", path.display());
let mut file = File::create(path)?;
let mut content = String::new();
content += "time0\ttime1\ttime2\tD.value\n";
for row in self.rows() {
content.push_str(&format!("{}\t{}\t{}\t{}\n", row[0], row[1], row[2], row[3]));
}
file.write_all(content.as_bytes())
}
pub fn build(
nodelist: &Path,
edgelist: &Path,
posterior_max_filter: f64,
) -> Result<(Self, f64), Error> {
let nodes = fs::read_to_string(nodelist)?;
let edges = fs::read_to_string(edgelist)?;
let nodes: Vec<Node> = nodes
.split(['\n', '\r'])
.skip(1)
.enumerate()
.filter_map(|(i, line)| {
let mut entries = line.split([',', '\t', ' ']);
Some(Node {
id: i,
file: PathBuf::from(entries.next()?),
name: String::from(entries.next()?),
generation: entries.next()?.parse::<u32>().ok()?,
meth: entries.next()? == "Y",
proportion_unmethylated: None,
rc_meth_lvl: None,
sites: None,
})
})
.collect();
if nodes.is_empty() {
return Err(anyhow::anyhow!(
"No nodes could be parsed from the nodelist"
));
}
let edges: Vec<Edge> = edges
.split(['\n', '\r'])
.skip(1)
.filter_map(|line| {
let mut entries = line.split(['\t', ' ', ',']);
let from = entries.next()?;
let to = entries.next()?;
Some(Edge {
from: nodes.iter().find(|n| n.name == from)?,
to: nodes.iter().find(|n| n.name == to)?,
})
})
.collect();
let mut nodes: Vec<Node> = nodes.iter().filter(|n| n.meth).cloned().collect();
for node in nodes.iter_mut() {
// let mut file = PathBuf::from(nodelist.parent().unwrap());
// file.push(&node.file);
let f = File::open(&node.file)
.map_err(|_| anyhow!("Could not open node file: {}", node.file.display()))?;
let reader = BufReader::new(f);
let mut sites = Vec::new();
for line in reader.lines() {
let line = line.expect("Could not read line");
let methylation = MethylationSite::from_methylome_file_line(&line, false);
if methylation.is_none() {
continue;
}
let methylation = methylation.unwrap();
sites.push(methylation);
}
let valid_sites: Vec<&MethylationSite> = sites
.iter()
.filter(|s| s.posteriormax >= posterior_max_filter)
.collect();
let p_uu_count = valid_sites
.iter()
.filter(|s| s.status == MethylationStatus::U)
.count();
let p_uu_share = p_uu_count as f64 / valid_sites.len() as f64;
let avg_meth_lvl =
valid_sites.iter().map(|s| s.meth_lvl).sum::<f64>() / valid_sites.len() as f64;
node.proportion_unmethylated = Some(p_uu_share);
node.rc_meth_lvl = Some(avg_meth_lvl);
node.sites = Some(sites);
}
let tmp0uu_meth_lvl = nodes
.iter()
.map(|n| 1.0 - n.rc_meth_lvl.unwrap())
.sum::<f64>()
/ nodes.len() as f64;
// dbg!(
// tmp0uu,
// tmp0uu_meth_lvl,
// (tmp0uu - tmp0uu_meth_lvl).abs() / tmp0uu
// );
let divergence = DMatrix::from(&nodes, posterior_max_filter);
let pedigree = divergence.convert(&nodes, &edges);
Ok((pedigree, tmp0uu_meth_lvl))
}
}
impl Deref for Pedigree {
type Target = Array2<f64>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
#[derive(Debug)]
struct DMatrix(Array2<f64>);
impl DMatrix {
fn from(nodes: &Vec<Node>, posterior_max: f64) -> Self {
let mut divergences = Array2::<f64>::zeros((nodes.len(), nodes.len()));
// Go over all pairs of nodes, excluding self-pairs
for (i, first) in nodes.iter().enumerate() {
for (j, second) in nodes.iter().skip(i + 1).enumerate() {
assert!(first.sites.as_ref().is_some());
assert!(second.sites.as_ref().is_some());
if first.sites.as_ref().unwrap().len() != second.sites.as_ref().unwrap().len() {
dbg!("Lengths do not match, all bets are off");
divergences[[i, j]] = 0.0;
continue;
}
assert_eq!(
first.sites.as_ref().unwrap().len(),
second.sites.as_ref().unwrap().len()
);
let mut divergence = 0;
let mut compared_sites = 0;
// Go over all sites in the first sample
// IMPORTANT: It is assumed that the same sites are included in the datasets and that the sites are sorted by position
for (k, f) in first.sites.as_ref().unwrap().iter().enumerate() {
let s = second
.sites
.as_ref()
.unwrap()
.get(k)
.expect("Partner methylation site must exists");
if f.posteriormax < posterior_max || s.posteriormax < posterior_max {
continue;
}
divergence += f.status_numeric().abs_diff(s.status_numeric());
compared_sites += 1;
}
let divergence = divergence as f64 / (2.0 * compared_sites as f64);
divergences[[i, j]] = divergence;
}
}
DMatrix(divergences)
}
/// Convert graph of divergences to pedigree
fn convert(&self, nodes: &[Node], edges: &[Edge]) -> Pedigree {
let e = edges
.iter()
.map(|e| {
(
e.from.id,
e.to.id,
e.from.generation.abs_diff(e.to.generation) as usize,
// self.0.get((e.from.id, e.to.id)).unwrap(),
)
})
.collect::<Vec<(usize, usize, usize)>>();
let graph = UnGraph::<usize, usize, usize>::from_edges(e);
let mut pedigree = Pedigree(Array2::<f64>::default((0, 4)));
for (i, source) in nodes.iter().enumerate() {
for (j, target) in nodes.iter().skip(i + 1).enumerate() {
if source.id == target.id {
// Explicitly skip self-pairs
continue;
}
let path = astar(
&graph,
source.id.into(),
|finish| finish == target.id.into(),
|e| *e.weight(),
|_| 0,
);
match path {
None => continue,
Some(path) => {
let distance = path.0;
let visited = path.1;
let t0: f64 = visited
.iter()
.map(|n| {
edges
.iter()
.find_map(|e| {
if e.from.id == n.index() {
return Some(e.from.generation);
}
if e.to.id == n.index() {
return Some(e.to.generation);
}
None
})
.unwrap()
})
.min()
.unwrap() as f64;
let t1 = source.generation as f64;
let t2 = target.generation as f64;
let div = self.0.get((i, j)).unwrap().to_owned();
assert_eq!(distance as f64, t1 - t0 + t2 - t0);
pedigree
.0
.push(Axis(0), array![t0, t1, t2, div].view())
.expect("Could not insert row into pedigree");
}
}
}
}
pedigree
}
}
#[cfg(test)]
mod tests {
use crate::assert_close;
use super::*;
#[test]
fn build_pedigree() {
let nodelist = Path::new("./data/nodelist.txt");
let edgelist = Path::new("./data/edgelist.txt");
let pedigree = Pedigree::build(nodelist, edgelist, 0.99).expect("Could not build pedigree");
assert_eq!(pedigree.0.shape(), &[4 * 3 / 2, 4]);
pedigree
.0
.to_file(Path::new("./data/pedigree_generated.txt"))
.unwrap();
// TODO: enable
// assert_close!(pedigree.1, 0.4567024);
}
#[test]
fn wildtype_pedigree() {
let nodelist = Path::new("./data/nodelist.txt");
let edgelist = Path::new("./data/edgelist.txt");
let comparison = Pedigree::from_file(
"./data/desired_output/pedigree-pdata_epimutation_rate_estimation_window_gene_0.txt",
);
let pedigree = Pedigree::build(nodelist, edgelist, 0.99).expect("Could not build pedigree");
// TODO: Enable
// assert_close!(pedigree.1, 0.991008120326199);
let pedigree = pedigree.0;
for (i, j) in pedigree.iter().zip(comparison.iter()) {
// TODO: Enable
// assert_close!(i, j);
}
}
}