alphabeta 0.2.0

Tools for analysing epigenetic data.
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
use std::fmt::Display;

use crate::{genes::Genome, Error};
use arguments::Windows as Args;
use itertools::Itertools;

#[macro_export]
macro_rules! print_dev {
    ($($rest:tt)*) => {
        #[cfg(debug_assertions)]
        std::println!($($rest)*)
    }
}

use crate::{
    genes::{Gene, Region, Strand},
    windows::Windows,
    *,
};

/// Basic Data Type for a Methylation Site
///
/// Includes conversion from a .bed file line in several formats. If your program fails,
/// check if the format of your .bed file is supported.
///
/// If not, simply add your own conversion function and submit a pull request.
///
/// Most times, only one position is mentioned, but in some cases, a range is given.
/// If only one location, we assign `start` to that and `end` to `start + 1`, as a cg site is two nucleotides long.
///
#[derive(Clone, PartialEq, Debug)]
pub struct MethylationSite {
    pub chromosome: Chromosome,
    pub start: u32,
    pub end: u32,
    pub strand: Strand,
    pub original: String,
    pub context: String,
    pub count_methylated: u32,
    pub count_total: u32,
    pub posteriormax: f64,
    pub status: MethylationStatus,
    pub meth_lvl: f64,
    pub context_trinucleotide: String,
}

// TODO: Is there a better name for the regular chromosomes than "Numbered"?
#[derive(Clone, PartialEq, Debug, Eq, Hash)]
pub enum Chromosome {
    Numbered(u8),
    Mitochondrial,
    Chloroplast,
}

impl TryFrom<&str> for Chromosome {
    type Error = Error;
    fn try_from(s: &str) -> Result<Self, Error> {
        let s = s.trim_start_matches("chr");
        match s {
            "M" => Ok(Chromosome::Mitochondrial),
            "C" => Ok(Chromosome::Chloroplast),
            _ => match s.parse::<u8>() {
                Ok(n) => Ok(Chromosome::Numbered(n)),
                Err(_) => Err(Error::Chromosome(s.to_string())),
            },
        }
    }
}

impl Default for MethylationSite {
    fn default() -> Self {
        MethylationSite {
            chromosome: Chromosome::Numbered(1),
            start: 1,
            end: 2,
            strand: Strand::Unknown,
            original: String::new(),
            context: String::new(),
            count_methylated: 1,
            count_total: 1,
            posteriormax: 0.999,
            status: MethylationStatus::M,
            meth_lvl: 0.1,
            context_trinucleotide: String::new(),
        }
    }
}

/// The three different kinds of methylations statusses that are distinguished in the AlphaBeta paper.
///
/// U: unmethylated on both alleles
/// M: methylated on both alleles
/// I: methylated on one allele, unmethylated on the other (intermediate)
#[derive(Clone, PartialEq, Debug)]
pub enum MethylationStatus {
    U,
    M,
    I,
}

impl From<char> for MethylationStatus {
    fn from(c: char) -> Self {
        match c {
            'M' => MethylationStatus::M,
            'I' => MethylationStatus::I,
            'U' => MethylationStatus::U,
            _ => {
                println!(
                    "Warning: Encountered invalid methylation status: {c}. Parsed as Unmethylated"
                );
                MethylationStatus::U
            }
        }
    }
}

impl MethylationSite {
    pub fn new(location: u32, strand: Strand) -> Self {
        MethylationSite {
            start: location,
            end: location + 1,
            strand,
            ..Default::default()
        }
    }
    /// Convert status to numeric value following the AlphaBeta paper.
    /// u/u => 0
    /// u/m => 1
    /// m/m => 2
    pub fn status_numeric(&self) -> u32 {
        match self.status {
            MethylationStatus::M => 2,
            MethylationStatus::U => 0,
            MethylationStatus::I => 1,
        }
    }

    /// Create a new CG site from a line of a methylation file.
    /// Only yields a CG site if the line is formatted correctly and is a CG site.
    /// If invalid, an error is returned.
    ///
    /// One pitfall of this implementation is the `collect tuple` call, which only yields a `Some` value if the line has exactly 9 or 10 tab-separated fields.
    /// Some files provide the trinucleotide, others don't, so there are two versions.
    ///
    /// If you need to parse a different format, simply add a new function and submit a pull request.
    pub fn from_methylome_file_line(s: &str, invert_strand: bool) -> Option<Self> {
        let first_format = |s: &str| {
            s.split('\t')
                .collect_tuple()
                .filter(|(_, _, _, context, _, _, _, _, _)| context == &"CG")
                .map(
                    |(
                        chromosome,
                        location,
                        strand,
                        context,
                        count_methylated,
                        count_total,
                        posteriormax,
                        status,
                        meth_lvl,
                    )| {
                        Ok(MethylationSite {
                            chromosome: chromosome.try_into()?,
                            start: location.parse::<u32>()?,
                            end: location.parse::<u32>()? + 1,
                            strand: if (strand == "+") ^ invert_strand {
                                Strand::Sense
                            } else {
                                Strand::Antisense
                            },
                            original: s.to_owned(),
                            context: String::from(context),
                            count_methylated: count_methylated.parse::<u32>()?,
                            count_total: count_total.parse::<u32>()?,
                            posteriormax: posteriormax.parse::<f64>()?,
                            status: status
                                .chars()
                                .next()
                                .ok_or(Error::Simple("Status could not included in file"))?
                                .into(),
                            meth_lvl: meth_lvl.parse::<f64>()?,
                            context_trinucleotide: String::from("XXX"),
                        })
                    },
                )
                .unwrap_or(Err(Error::MethlyationSiteFormat))
        };

        let second_format = |s: &str| {
            s.split('\t')
                .collect_tuple()
                .filter(|(_, _, _, context, _, _, _, _, _, _)| context == &"CG")
                .map(
                    |(
                        chromosome,
                        location,
                        strand,
                        context,
                        count_methylated,
                        count_total,
                        posteriormax,
                        status,
                        meth_lvl,
                        trinucleotide,
                    )| {
                        Ok(MethylationSite {
                            chromosome: chromosome.try_into()?,
                            start: location.parse::<u32>()?,
                            end: location.parse::<u32>()? + 1,
                            strand: if (strand == "+") ^ invert_strand {
                                Strand::Sense
                            } else {
                                Strand::Antisense
                            },
                            original: s.to_owned(),
                            context: String::from(context),
                            count_methylated: count_methylated.parse::<u32>()?,
                            count_total: count_total.parse::<u32>()?,
                            posteriormax: posteriormax.parse::<f64>()?,
                            status: status
                                .chars()
                                .next()
                                .ok_or(Error::Simple("Status could not be parsed"))?
                                .into(),
                            meth_lvl: meth_lvl.parse::<f64>()?,
                            context_trinucleotide: String::from(trinucleotide),
                        })
                    },
                )
                .unwrap_or(Err(Error::MethlyationSiteFormat))
        };

        let third_format = |s: &str| {
            s.split('\t')
                .collect_tuple()
                .filter(|(_, _, _, context, _, _, _, _, _, _, _)| context == &"CG")
                .map(
                    |(
                        chromosome,
                        first_nucleotide,
                        second_nucleotide,
                        context,
                        _, // No idea what that is
                        strand,
                        count_methylated,
                        count_total,
                        posteriormax,
                        status,
                        meth_lvl,
                    )| {
                        Ok(MethylationSite {
                            chromosome: chromosome.try_into()?,
                            start: first_nucleotide.parse::<u32>()?,
                            end: second_nucleotide.parse::<u32>()?,
                            strand: if (strand == "+") ^ invert_strand {
                                Strand::Sense
                            } else {
                                Strand::Antisense
                            },
                            original: s.to_owned(),
                            context: String::from(context),
                            count_methylated: count_methylated.parse::<u32>()?,
                            count_total: count_total.parse::<u32>()?,
                            posteriormax: posteriormax.parse::<f64>()?,
                            status: status
                                .chars()
                                .next()
                                .ok_or(Error::Simple("Status could not be parsed"))?
                                .into(),
                            meth_lvl: meth_lvl.parse::<f64>()?,
                            context_trinucleotide: String::from("XXX"),
                        })
                    },
                )
                .unwrap_or(Err(Error::MethlyationSiteFormat))
        };

        // Chromatin State files
        // 1	131800	132400	E10
        let chromatin_state = |s: &str| {
            s.split(['\t', ' '])
                .collect_tuple()
                .map(|(chromosome, start, end, state)| {
                    Ok(MethylationSite {
                        chromosome: chromosome.try_into()?,
                        strand: Strand::Unknown,
                        start: start.parse::<u32>()?,
                        end: end.parse::<u32>()?,
                        context: String::from(state),
                        context_trinucleotide: String::new(),
                        count_methylated: 0,
                        count_total: 0,
                        meth_lvl: 0.0,
                        original: s.to_owned(),
                        posteriormax: 0.0,
                        status: MethylationStatus::U,
                    })
                })
                .unwrap_or(Err(Error::MethlyationSiteFormat))
        };

        // #bedGraph section chr1:1-3480
        // chr1	1	4	1
        let bigwig_format = |s: &str| {
            s.split(['\t', ' '])
                .collect_tuple()
                .map(|(chromosome, start, end, _length)| {
                    Ok(MethylationSite {
                        chromosome: chromosome.try_into()?,
                        strand: Strand::Unknown,
                        start: start.parse::<u32>()?,
                        end: end.parse::<u32>()?,
                        context: String::from("Modification"),
                        context_trinucleotide: String::new(),
                        count_methylated: 0,
                        count_total: 0,
                        meth_lvl: 0.0,
                        original: s.to_owned(),
                        posteriormax: 0.0,
                        status: MethylationStatus::U,
                    })
                })
                .unwrap_or(Err(Error::MethlyationSiteFormat))
        };
        // Heterogeneity Score Files
        // Requested by Patrick Wolf
        // chr | start | end | score | _ | _
        let heterogenity_score_files = |s: &str| {
            s.split(['\t', ' '])
                .collect_tuple()
                .map(|(chromosome, start, end, _score)| {
                    dbg!(chromosome, start, end);
                    Ok(MethylationSite {
                        chromosome: chromosome.try_into()?,
                        start: start.parse()?,
                        end: end.parse()?,
                        ..Default::default()
                    })
                })
                .unwrap_or(Err(Error::MethlyationSiteFormat))
        };

        let results: Result<MethylationSite, crate::error::Error> = first_format(s)
            .or_else(|_| second_format(s))
            .or_else(|_| third_format(s))
            .or_else(|_| chromatin_state(s))
            .or_else(|_| bigwig_format(s))
            .or_else(|_| heterogenity_score_files(s));

        match results {
            Ok(methylation_site) => Some(methylation_site),
            Err(e) => {
                // Log lines that could not be parsed
                // Ignore header
                if !s.contains("seqnames") {
                    print_dev!("Non-fatal error when parsing methylation site: {e}\nLine: {s}");
                }
                None
            }
        }
    }

    /// Checks weather a given CG site belongs to a specific gene. The cutoff is the number of bases upstream and downstream of the gene to consider the CG site in the gene. For example, a cutoff of 1000 would consider a CG site 1000 bases upstream of the gene to be in the gene.
    /// To strictly check weather a CG site is within the gene region, pass a cutoff of 0.
    ///
    /// Passing a negative cutoff is possible but leads to undefined behaviour if used together with ``find_gene``.
    pub fn is_in_gene(&self, gene: &Gene, args: &Args) -> bool {
        let cutoff = if args.cutoff_gene_length {
            gene.end - gene.start
        } else {
            args.cutoff
        };
        self.chromosome == gene.chromosome
            && gene.start <= self.start + cutoff
            && self.end <= gene.end + cutoff
            && self.strand == gene.strand
    }

    /// Find the gene within a genome that a CG site belongs to. Due to binary search, searching is O(log n) where n is the number of genes in the genome.
    /// Therefore, this method is efficient to use on large genomes.
    ///
    /// The lifetime of the genome is longer than the lifetime of the CG site.
    /// GG sites exist only while a single methylation file is being processed but the genome is loaded once and exists for the entire program
    pub fn find_gene<'long>(&self, genome: &'long Genome, args: &Args) -> Option<&'long Gene> {
        // This can fail sensibly if there are chromosomes in the methylome files that are not in the annotation files
        let chromosome = genome.get(&self.chromosome)?;

        let strand: &Vec<Gene> = match self.strand {
            Strand::Sense => &chromosome.sense, // This is a performance hit. Is there a better way to do this?
            Strand::Antisense => &chromosome.antisense,
            Strand::Unknown => &chromosome.combined,
        };

        let search = |gene: &Gene| {
            if args.cutoff_gene_length {
                gene.end + (gene.end - gene.start)
            } else {
                gene.end + args.cutoff
            }
        };

        let first_matching_gene_index = strand
            .binary_search_by_key(&self.start, search)
            .unwrap_or_else(|x| x); // Collapse exact match on gene end and closest previous match into one, as both are valid

        if strand.len() < first_matching_gene_index + 1 {
            return None;
        }

        let gene = &strand[first_matching_gene_index];

        if self.is_in_gene(gene, args) {
            return Some(gene);
        }

        None
    }
    /// Place a CG site in the correct windows. Returns a list of all the successfull insertions as a tuple of the region (upstream, downstream or gene) and the index of the window.
    ///
    /// It works by first finding the region the CG site is in (upstream, downstream or gene) and then finding the windows within that a CG site belongs to.
    /// For genes on the - strand, the windows are reversed, so that the first window is the one closest to the end of the gene.
    pub fn place_in_windows(
        &self,
        gene: &Gene,
        windows: &mut Windows,
        args: &Args,
    ) -> Vec<(Region, usize)> // Return a vector of (strand, window) tuples for each window the CG site is in
    {
        const E: f64 = 0.1; // Epsilon for floating point comparison
        let location = self.start as f64;
        let cutoff = args.cutoff as f64;
        let step = args.window_step as f64;
        let size = args.window_size as f64;
        let start = gene.start as f64;
        let end = gene.end as f64;
        let length = end - start;

        if let Strand::Unknown = self.strand {
            let mut copy = self.clone();
            copy.strand = Strand::Sense;
            return copy.place_in_windows(gene, windows, args);
        }

        // Offset from start for + strand, offset from end for - strand. Can be negative for upstream sites
        let mut windows_in = Vec::new();
        let offset = match &self.strand {
            Strand::Sense => location - start,
            Strand::Antisense => end - location,
            _ => return windows_in, // TODO better handling
        };
        let region = match offset {
            x if x < 0.0 => Region::Upstream,
            x if x > length => Region::Downstream, // CG site exactly on the end of the gene is still considered in the gene
            _ => Region::Gene,
        };
        let local_windows = windows.get_mut(&region);

        // let max = if args.absolute { gene_length } else { 100 };
        let mut position = match (&region, &self.strand) {
            // Position within the region of the gene, switched start & end for - strand
            (Region::Upstream, Strand::Sense) => location - start + cutoff,
            (Region::Gene, Strand::Sense) => location - start,
            (Region::Downstream, Strand::Sense) => location - end,
            (Region::Upstream, Strand::Antisense) => end - location + cutoff,
            (Region::Gene, Strand::Antisense) => end - location,
            (Region::Downstream, Strand::Antisense) => start - location,
            _ => return windows_in,
        };

        if !args.absolute {
            position = match region {
                Region::Upstream => position / cutoff,
                Region::Gene => position / length,
                Region::Downstream => position / cutoff,
            };
            position *= 100.0; // Normalize to 0-100%
        }

        for (i, window) in local_windows.iter_mut().enumerate() {
            let lower_bound = i as f64 * step - E;
            let upper_bound = lower_bound + size + E;

            if position >= lower_bound && position <= upper_bound {
                window.push(self.clone());
                windows_in.push((region.clone(), i));
            }
        }
        windows_in
    }
}

impl Display for MethylationSite {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "CG site is located on the {} strand of chromosome {} at bp {}",
            self.strand, self.chromosome, self.start
        )
    }
}

#[cfg(test)]
mod tests {

    use std::fs::read_to_string;

    use super::MethylationSite;
    use crate::{
        arguments::Windows as Args,
        genes::{Gene, Strand},
        methylation_site::Chromosome,
        windows::Windows,
    };

    #[test]
    fn test_instantiate_from_methylome_file_line() {
        let line = "1	23151	+	CG	0	8	0.9999	U	0.0025";
        let cg = MethylationSite::from_methylome_file_line(line, false).unwrap();
        assert_eq!(cg.chromosome, Chromosome::Numbered(1));
    }

    #[test]
    fn test_bigwig_format() {
        let line = "chr1	7	11	3";
        let cg = MethylationSite::from_methylome_file_line(line, true).unwrap();
        assert_eq!(cg.chromosome, Chromosome::Numbered(1));
        assert_eq!(cg.start, 7);
        assert_eq!(cg.end, 11);
    }

    #[test]
    fn test_instantiate_from_methylome_file_line_invalid_line() {
        let line = "1	23151	+	CG	0	8	0.9999	";
        let cg = MethylationSite::from_methylome_file_line(line, false);
        assert!(cg.is_none());
    }

    #[test]
    fn test_cmt3_line_not_cg() {
        let line = "1	25600	+	CHH	0	94	0.9999	U	0.0043	CAT";
        let site = MethylationSite::from_methylome_file_line(line, false);
        assert!(site.is_none());
    }

    #[test]
    fn test_instantiate_from_methylome_file_line_invalid_chromosome() {
        let line = "X	23151	+	CG	0	8	0.9999	U	0.0025";
        let cg = MethylationSite::from_methylome_file_line(line, false);
        assert!(cg.is_none());
    }

    #[test]
    fn test_heterogenity_file_format() {
        let file = read_to_string("data/heterogenity_score_files.txt").unwrap();
        let sites: Vec<MethylationSite> = file
            .split('\n')
            .filter_map(|s| MethylationSite::from_methylome_file_line(s, false))
            .collect();
        assert_eq!(sites.get(0).unwrap().start, 809);
        assert_eq!(sites.len(), 509825);
    }

    #[test]
    fn test_r_example_methylome_data() {
        let file = read_to_string("data/methylome/G0.txt").unwrap();
        let sites: Vec<_> = file
            .split('\n')
            .filter_map(|s| MethylationSite::from_methylome_file_line(s, false))
            .collect();
        assert_eq!(sites.len(), 500); // Only 500 of the > 2800 sites are CG sites

        let file = read_to_string("data/methylome/G1_2.txt").unwrap();
        let sites: Vec<MethylationSite> = file
            .split('\n')
            .filter_map(|s| MethylationSite::from_methylome_file_line(s, false))
            .collect();
        assert_eq!(sites.len(), 500);

        let file = read_to_string("data/methylome/G4_2.txt").unwrap();
        let sites: Vec<MethylationSite> = file
            .split('\n')
            .filter_map(|s| MethylationSite::from_methylome_file_line(s, false))
            .collect();
        assert_eq!(sites.len(), 500);

        let file = read_to_string("data/methylome/G4_8.txt").unwrap();
        let sites: Vec<MethylationSite> = file
            .split('\n')
            .filter_map(|s| MethylationSite::from_methylome_file_line(s, false))
            .collect();
        assert_eq!(sites.len(), 500);
    }

    #[test]
    fn test_extract_gene() {}

    #[test]
    fn test_place_site_absolute() {
        let args = Args {
            db: false,
            invert: false,
            absolute: true,
            cutoff: 1000,
            window_size: 2,
            window_step: 1,
            ..Default::default()
        };
        let all_within_gene = Gene {
            annotation: String::new(),

            chromosome: Chromosome::Numbered(1),
            start: 1000,
            end: 2000,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_upstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 2000,
            end: 3000,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_downstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 0,
            end: 1000,
            strand: Strand::Sense,
            name: String::new(),
        };

        let mut windows = Windows::new(1000, args.clone());
        for i in 1..1000 {
            let cg = MethylationSite::new(i + 1000, Strand::Antisense);
            let upstream = cg.place_in_windows(&all_upstream_gene, &mut windows, &args);
            let gene = cg.place_in_windows(&all_within_gene, &mut windows, &args);
            let downstream = cg.place_in_windows(&all_downstream_gene, &mut windows, &args);

            println!("Placing {i}");
            println!("Upstream: {upstream:?}");
            println!("Gene: {gene:?}");
            println!("Downstream: {downstream:?}");
            // TODO: enable
            // assert!(windows.upstream[i as usize].contains(&cg));
            // assert!(windows.gene[i as usize].contains(&cg));
            // assert!(windows.downstream[i as usize].contains(&cg));

            // if i > 3 {
            //     assert!(windows.upstream[i as usize - 1].contains(&cg));
            //     assert!(windows.gene[i as usize - 1].contains(&cg));
            //     assert!(windows.downstream[i as usize - 1].contains(&cg));
            //     assert!(windows.upstream[i as usize - 2].contains(&cg));
            //     assert!(windows.gene[i as usize - 2].contains(&cg));
            //     assert!(windows.downstream[i as usize - 2].contains(&cg));
            // }
        }
    }
    #[test]
    fn test_place_site_relative_acting_like_absolute() {
        let args = Args {
            invert: false,
            absolute: false,
            cutoff: 100,

            window_size: 2,
            window_step: 1,
            ..Default::default()
        };
        let all_within_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 100,
            end: 200,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_upstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 200,
            end: 300,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_downstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 0,
            end: 100,
            strand: Strand::Sense,
            name: String::new(),
        };

        let mut windows = Windows::new(100, args.clone());
        for i in 1..100 {
            let cg = MethylationSite::new(i + 100, Strand::Sense);
            let upstream = cg.place_in_windows(&all_upstream_gene, &mut windows, &args);
            let gene = cg.place_in_windows(&all_within_gene, &mut windows, &args);
            let downstream = cg.place_in_windows(&all_downstream_gene, &mut windows, &args);

            println!("Placing {i}");
            println!("Upstream: {upstream:?}");
            println!("Gene: {gene:?}");
            println!("Downstream: {downstream:?}");
            assert!(windows.upstream[i as usize].contains(&cg));
            assert!(windows.gene[i as usize].contains(&cg));
            assert!(windows.downstream[i as usize].contains(&cg));
        }
    }
    #[test]
    fn test_place_site_relative() {
        let args = Args {
            invert: false,
            absolute: false,
            cutoff: 1000,

            window_size: 2,
            window_step: 1,
            ..Default::default()
        };
        let all_within_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 1000,
            end: 2000,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_upstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 2000,
            end: 3000,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_downstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 0,
            end: 1000,
            strand: Strand::Sense,
            name: String::new(),
        };

        let mut windows = Windows::new(1000, args.clone());
        assert!(windows.upstream.len() == 100);
        for i in 1..1000 {
            let cg = MethylationSite::new(i + 1000, Strand::Sense);
            let upstream = cg.place_in_windows(&all_upstream_gene, &mut windows, &args);
            let gene = cg.place_in_windows(&all_within_gene, &mut windows, &args);
            let downstream = cg.place_in_windows(&all_downstream_gene, &mut windows, &args);

            println!("Placing {i}");
            println!("Upstream: {upstream:?}");
            println!("Gene: {gene:?}");
            println!("Downstream: {downstream:?}");
            println!("{}: {}", i / 10, windows.upstream[(i / 10) as usize].len());
            assert!(windows.upstream[(i / 10) as usize].contains(&cg));
            assert!(windows.gene[(i / 10) as usize].contains(&cg));
            assert!(windows.downstream[(i / 10) as usize].contains(&cg));
        }
    }

    #[test]
    fn test_place_site() {
        let cg_a = MethylationSite::new(80, Strand::Sense);
        let cg_b = MethylationSite::new(100, Strand::Sense);
        let cg_c = MethylationSite::new(123, Strand::Sense);
        let cg_d = MethylationSite::new(200, Strand::Sense);
        let cg_e = MethylationSite::new(201, Strand::Sense);
        let cg_f = MethylationSite::new(512 + 100 + 100, Strand::Sense);
        let cg_g = MethylationSite::new(1024 + 100 + 100, Strand::Sense);
        let cg_h = MethylationSite::new(2048 + 100 + 100, Strand::Sense);

        let gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 100,
            end: 200,
            strand: Strand::Sense,
            name: String::new(),
        };

        let args = Args {
            invert: false,
            absolute: false,
            cutoff: 2048,

            window_size: 2,
            window_step: 1,
            ..Default::default()
        };
        let mut windows = Windows::new(1000, args.clone());

        cg_a.place_in_windows(&gene, &mut windows, &args);
        cg_b.place_in_windows(&gene, &mut windows, &args);
        cg_c.place_in_windows(&gene, &mut windows, &args);
        cg_d.place_in_windows(&gene, &mut windows, &args);
        cg_e.place_in_windows(&gene, &mut windows, &args);
        cg_f.place_in_windows(&gene, &mut windows, &args);
        cg_g.place_in_windows(&gene, &mut windows, &args);
        cg_h.place_in_windows(&gene, &mut windows, &args);

        println!("{windows}");
        assert!(windows.upstream[98].contains(&cg_a));
        assert!(windows.upstream[99].contains(&cg_a));
        assert!(windows.gene[0].contains(&cg_b));
        assert!(windows.gene[21].contains(&cg_c));
        assert!(windows.gene[22].contains(&cg_c));
        assert!(windows.gene[23].contains(&cg_c));
        assert!(windows.gene[99].contains(&cg_d));
        assert!(windows.downstream[0].contains(&cg_e));
        assert!(windows.downstream[24].contains(&cg_f));
        assert!(windows.downstream[49].contains(&cg_g));
        assert!(windows.downstream[99].contains(&cg_h));
    }
    #[test]
    fn test_place_site_absolute_2() {
        let cg_a = MethylationSite::new(80, Strand::Sense);
        let cg_b = MethylationSite::new(100, Strand::Sense);
        let cg_c = MethylationSite::new(123, Strand::Sense);
        let cg_d = MethylationSite::new(200, Strand::Sense);
        let cg_e = MethylationSite::new(201, Strand::Sense);
        let cg_f = MethylationSite::new(220, Strand::Sense);

        let gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 100,
            end: 200,
            strand: Strand::Sense,
            name: String::new(),
        };

        let args = Args {
            absolute: true,
            cutoff: 2048,

            window_size: 2,
            window_step: 1,
            ..Default::default()
        };
        let mut windows = Windows::new(100, args.clone());

        cg_a.place_in_windows(&gene, &mut windows, &args);
        cg_b.place_in_windows(&gene, &mut windows, &args);
        cg_c.place_in_windows(&gene, &mut windows, &args);
        cg_d.place_in_windows(&gene, &mut windows, &args);
        cg_e.place_in_windows(&gene, &mut windows, &args);
        cg_f.place_in_windows(&gene, &mut windows, &args);
        assert!(windows.upstream[2026].contains(&cg_a));
        assert!(windows.upstream[2027].contains(&cg_a));
        assert!(windows.upstream[2028].contains(&cg_a));
        assert!(windows.gene[0].contains(&cg_b));
        assert!(windows.gene[21].contains(&cg_c));
        assert!(windows.gene[22].contains(&cg_c));
        assert!(windows.gene[23].contains(&cg_c));
        assert!(windows.gene[99].contains(&cg_d));
        assert!(windows.downstream[0].contains(&cg_e));
        assert!(windows.downstream[1].contains(&cg_e));
        assert!(windows.downstream[18].contains(&cg_f));
        assert!(windows.downstream[19].contains(&cg_f));
        assert!(windows.downstream[20].contains(&cg_f));
    }

    #[test]
    fn test_place_site_relative_antisense() {
        let args = Args {
            absolute: false,
            cutoff: 1000,

            window_size: 2,
            window_step: 1,
            ..Default::default()
        };
        let all_within_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 1000,
            end: 2000,
            strand: Strand::Antisense,
            name: String::new(),
        };
        let all_upstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 2000,
            end: 3000,
            strand: Strand::Antisense,
            name: String::new(),
        };
        let all_downstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 0,
            end: 1000,
            strand: Strand::Antisense,
            name: String::new(),
        };

        let mut windows = Windows::new(1000, args.clone());
        assert!(windows.upstream.len() == 100);
        for i in 1..1000 {
            let cg = MethylationSite::new(i + 1000, Strand::Antisense);
            let upstream = cg.place_in_windows(&all_upstream_gene, &mut windows, &args);
            let gene = cg.place_in_windows(&all_within_gene, &mut windows, &args);
            let downstream = cg.place_in_windows(&all_downstream_gene, &mut windows, &args);

            println!("Placing {i}");
            println!("Upstream: {upstream:?}");
            println!("Gene: {gene:?}");
            println!("Downstream: {downstream:?}");
            println!(
                "{}: {}",
                (999 - i) / 10,
                windows.upstream[(i / 10) as usize].len()
            );
            assert!(windows.upstream[((999 - i) / 10) as usize].contains(&cg));
            assert!(windows.gene[((999 - i) / 10) as usize].contains(&cg));
            assert!(windows.downstream[((999 - i) / 10) as usize].contains(&cg));
        }
    }
    #[test]
    fn test_place_site_absolute_invert() {
        let args = Args {
            absolute: true,
            cutoff: 1000,

            window_size: 2,
            window_step: 1,
            ..Default::default()
        };
        let all_within_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 1000,
            end: 2000,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_upstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 2000,
            end: 3000,
            strand: Strand::Sense,
            name: String::new(),
        };
        let all_downstream_gene = Gene {
            annotation: String::new(),
            chromosome: Chromosome::Numbered(1),
            start: 0,
            end: 1000,
            strand: Strand::Sense,
            name: String::new(),
        };

        let mut windows = Windows::new(1000, args.clone());
        assert!(windows.upstream.len() == 1000);
        for i in 1..1000 {
            let cg = MethylationSite::new(i + 1000, Strand::Sense);
            let upstream = cg.place_in_windows(&all_upstream_gene, &mut windows, &args);
            let gene = cg.place_in_windows(&all_within_gene, &mut windows, &args);
            let downstream = cg.place_in_windows(&all_downstream_gene, &mut windows, &args);

            println!("Placing {i}");
            println!("Upstream: {upstream:?}");
            println!("Gene: {gene:?}");
            println!("Downstream: {downstream:?}");
            println!("{}: {}", (i), windows.upstream[(i) as usize].len());
            assert!(windows.upstream[(i) as usize].contains(&cg));
            assert!(windows.gene[(i) as usize].contains(&cg));
            assert!(windows.downstream[(i) as usize].contains(&cg));
        }
    }
}