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
use biofile::bed::Bed;
use math::{
    interval::I64Interval,
    iter::{
        AggregateOp, BinnedIntervalIter, CommonRefinementZip, CommonRefinementZipped,
        ConcatenatedIter, IntoBinnedIntervalIter, UnionZip,
    },
    partition::integer_interval_map::IntegerIntervalMap,
    set::traits::Finite,
    stats::correlation::weighted_correlation,
};
use std::collections::HashSet;

/// The weight is the reciprocal of the interval size so as to produce the mean of the values in
/// the interval. Each bin is considered a single entity of "weight" 1 when taking correlations.
macro_rules! binned_extractor {
    () => {
        |(interval, v)| {
            (
                v[0].unwrap_or(0.),
                v[1].unwrap_or(0.),
                1. / interval.size() as f64,
            )
        }
    };
}

/// The weight is the size of the interval since each value has to be multiplied by the number of
/// elements in the interval as the proper weighting in the correlation calculation.
macro_rules! non_binned_extractor {
    () => {
        |(interval, v)| {
            (
                v[0].unwrap_or(0.),
                v[1].unwrap_or(0.),
                interval.size() as f64,
            )
        }
    };
}

pub type ChromCorrelations = Vec<(String, Vec<f64>)>;
pub type OverallCorrelations = Vec<f64>;

pub fn compute_track_correlations(
    first_track_filepath: &str,
    second_track_filepath: &str,
    bin_sizes: Vec<i64>,
    target_chroms: HashSet<String>,
) -> Result<(ChromCorrelations, OverallCorrelations), String> {
    let bed_a = Bed::new(&first_track_filepath);
    let bed_b = Bed::new(&second_track_filepath);

    eprintln!(
        "=> Constructing chrom interval map for {}",
        first_track_filepath
    );
    let chrom_interval_map_a = match bed_a.get_chrom_to_interval_to_val::<f64, _>() {
        Ok(bed) => bed,
        Err(why) => {
            return Err(format!(
                "failed to get chrom interval map for {}: {}",
                first_track_filepath, why
            ))
        }
    };

    eprintln!(
        "=> Constructing chrom interval map for {}",
        second_track_filepath
    );
    let chrom_interval_map_b = match bed_b.get_chrom_to_interval_to_val::<f64, _>() {
        Ok(bed) => bed,
        Err(why) => {
            return Err(format!(
                "failed to get chrom interval map for {}: {}",
                second_track_filepath, why
            ))
        }
    };

    let empty_interval_map = IntegerIntervalMap::new();
    let get_target_interval_maps = || {
        chrom_interval_map_a
            .union_zip(&chrom_interval_map_b)
            .into_iter()
            .filter_map(|(chrom, map_list)| {
                if target_chroms.contains(&chrom) {
                    let interval_map_a = map_list[0].unwrap_or_else(|| &empty_interval_map);
                    let interval_map_b = map_list[1].unwrap_or_else(|| &empty_interval_map);
                    Some((chrom, interval_map_a, interval_map_b))
                } else {
                    None
                }
            })
    };

    let chrom_correlations: Vec<(String, Vec<f64>)> = get_target_interval_maps()
        .map(|(chrom, map_a, map_b)| {
            eprintln!("=> Computing correlations for {}", chrom);
            let correlations = bin_sizes
                .iter()
                .map(|&s| match s {
                    0 => {
                        let vec: Vec<(I64Interval, Vec<Option<f64>>)> =
                            a_common_refine_b(map_a, map_b).collect();
                        weighted_correlation(|| vec.iter(), non_binned_extractor!())
                    }
                    non_zero => {
                        let vec: Vec<(I64Interval, Vec<Option<f64>>)> =
                            a_bin_b(map_a, map_b, non_zero).collect();
                        weighted_correlation(|| vec.iter(), binned_extractor!())
                    }
                })
                .collect();
            (chrom, correlations)
        })
        .collect();

    eprintln!("=> Computing overall correlations");
    let overall_correlations: Vec<f64> = bin_sizes
        .iter()
        .map(|&s| match s {
            0 => weighted_correlation(
                || {
                    ConcatenatedIter::from_iters(
                        get_target_interval_maps()
                            .map(|(_, map_a, map_b)| a_common_refine_b(map_a, map_b))
                            .collect(),
                    )
                },
                non_binned_extractor!(),
            ),
            non_zero => weighted_correlation(
                || {
                    ConcatenatedIter::from_iters(
                        get_target_interval_maps()
                            .map(|(_, map_a, map_b)| a_bin_b(map_a, map_b, non_zero))
                            .collect(),
                    )
                },
                binned_extractor!(),
            ),
        })
        .collect();
    Ok((chrom_correlations, overall_correlations))
}

type Boundary = i64;
type Value = f64;
type BtreeMapIter<'a> = std::collections::btree_map::Iter<'a, I64Interval, f64>;
type BinnedIter<'a> =
    BinnedIntervalIter<std::collections::btree_map::Iter<'a, I64Interval, f64>, f64>;

fn a_common_refine_b<'a>(
    map_a: &'a IntegerIntervalMap<f64>,
    map_b: &'a IntegerIntervalMap<f64>,
) -> CommonRefinementZipped<
    Boundary,
    BtreeMapIter<'a>,
    <BtreeMapIter<'a> as Iterator>::Item,
    I64Interval,
    Value,
> {
    map_a.iter().common_refinement_zip(map_b.iter())
}

fn a_bin_b<'a>(
    map_a: &'a IntegerIntervalMap<f64>,
    map_b: &'a IntegerIntervalMap<f64>,
    bin_size: i64,
) -> CommonRefinementZipped<
    Boundary,
    BinnedIter<'a>,
    <BinnedIter<'a> as Iterator>::Item,
    I64Interval,
    Value,
> {
    map_a
        .iter()
        .into_binned_interval_iter(
            bin_size,
            AggregateOp::Sum,
            Box::new(|item| (*item.0, *item.1)),
        )
        .common_refinement_zip(map_b.iter().into_binned_interval_iter(
            bin_size,
            AggregateOp::Sum,
            Box::new(|item| (*item.0, *item.1)),
        ))
}