Debian Codegraph @

docs/content/tools/fisher.rst

raw
1 .. _fisher:
2
3 ########
4 *fisher*
5 ########
6
7 Perform fisher's exact test on the number of overlaps/unique intervals between
8 2 files.
9
10 |
11
12 Traditionally, in order to test whether 2 sets of intervals are related
13 spatially, we resort to shuffling the genome and checking the simulated
14 (shuffled) versus the observed. We can do the same analytically for many
15 scenarios using
16 `Fisher's Exact Test`_ .
17
18 This implementation can calculate the number of overlaps and the number
19 of intervals unique to each file and it infers (or accepts) the number
20 that are not present in each file.
21
22 Given a pair of input files `-a` and `-b` in the usual BedTools parlance:
23
24 .. code-block:: bash
25
26 $ cat a.bed
27 chr1 10 20
28 chr1 30 40
29 chr1 51 52
30
31 $ cat b.bed
32 chr1 15 25
33 chr1 51 52
34
35 And a genome of 500 bases:
36
37 .. code-block:: bash
38
39 $ echo -e "chr1\t500" > t.genome
40
41 We may wish to know **if the amount of overlap between the 2 sets of intervals is
42 more than we would expect given their coverage and the size of the genome**. We
43 can do this with ``fisher`` as:
44
45 .. code-block:: bash
46
47 $ bedtools fisher -a a.bed -b b.bed -g t.genome
48 # Number of query intervals: 3
49 # Number of db intervals: 2
50 # Number of overlaps: 2
51 # Number of possible intervals (estimated): 37
52 # phyper(2 - 1, 3, 37 - 3, 2, lower.tail=F)
53 # Contingency Table Of Counts
54 #_________________________________________
55 # | in -b | not in -b |
56 # in -a | 2 | 1 |
57 # not in -a | 0 | 34 |
58 #_________________________________________
59 # p-values for fisher's exact test
60 left right two-tail ratio
61 1 0.0045045 0.0045045 inf
62
63
64 Where we can see the constructed contingency table and the pvalues for left, right
65 and two-tail tests.
66 From here, we can say that given **500 bases** of genome, it is unlikely that we
67 would see **as many** overlaps as we do if the intervals from `a` and `b` were not
68 related.
69
70 .. note::
71
72 the total number of **possible** intervals in the above example was
73 estimated to be 37. This is based on a heuristic that uses the mean sizes of
74 intervals in the `a` and `b` sets and the size of the genome. The reported
75 p-value will depend greatly on this. Below, we show how well the reported
76 value matches with simulations.
77
78 The above had a fairly low p-value (0.0045), but if our genome were only **60 bases**:
79
80 .. code-block:: bash
81
82 $ echo -e "chr1\t60" > t.genome
83 $ bedtools fisher -a a.bed -b b.bed -g t.genome
84 # Number of query intervals: 3
85 # Number of db intervals: 2
86 # Number of overlaps: 2
87 # Number of possible intervals (estimated): 4
88 # phyper(2 - 1, 3, 4 - 3, 2, lower.tail=F)
89 # Contingency Table Of Counts
90 #_________________________________________
91 # | in -b | not in -b |
92 # in -a | 2 | 1 |
93 # not in -a | 0 | 1 |
94 #_________________________________________
95 # p-values for fisher's exact test
96 left right two-tail ratio
97 1 0.5 1 inf
98
99
100 We can see that neither tail is significant. Intuitively, this makes sense;
101 if we randomly place 3 intervals (from `-a`), and 2 (from `-b`) intervals from
102 within 60 bases, it doesn't seem unlikely that we'd see 2 overlaps.
103
104 Note also that since the genome size is much smaller, the number of possible
105 intervals is also decreased.
106
107
108 ==========
109 Evaluation
110 ==========
111
112 The p-value depends on knowing or inferring the total number of possible
113 intervals (to fill in the lower right corner of the contingency table). This
114 inference is not straightforward since we will most likely have variable sized
115 intervals within and between files. Below, we show the correspondence of
116 the p-value reported by `fisher` and one from simulated data.
117
118 .. figure:: ../images/fisher/res-genes.png
119 :alt: plot of p-values of fisher vs. simulated
120 :figclass: align-center
121
122 The comparison of the p-value from 'fisher' to one derived by simulation
123 (see tests/fisher/ for details). The top plot shows the p-value distribution.
124 Since we are most interested in extreme p-values, the middle plot shows
125 -log10(p-value). The bottom plot is the same as the middle except looking at
126 the other tail of the p-value distribution.
127
128 Note that we do see inflation from the fisher test, but we do not see
129 'false-negatives'--that is, bedtools fisher is less likely to miss 'true'
130 candidates, but it will give many candidates for further exploration. As
131 such we recommend validating low p-values from fisher using simulation.
132
133 This evaluation used all known canonical genes on chromosome 1 and repeatedly
134 (1000 times) randomly generated 3000 intervals between 20 and 5250 bases. It
135 then calculated the p-value for each set using fisher and then using shuffled
136 data.
137
138 .. note::
139
140 The ``fisher`` tool requires that your data is pre-sorted by chromosome and
141 then by start position (e.g., ``sort -k1,1 -k2,2n in.bed > in.sorted.bed``
142 for BED files).
143
144 This uses Heng Li's implementation of Fisher's exact test in kfunc.c.
145
146 .. seealso::
147
148 :doc:`../tools/jaccard`
149 :doc:`../tools/reldist`
150 :doc:`../tools/intersect`
151
152
153 ===============================
154 Usage and option summary
155 ===============================
156 **Usage**:
157 ::
158
159 bedtools fisher [OPTIONS] -a <BED/GFF/VCF> -b <BED/GFF/VCF> -g <genome>
160
161
162 =========================== =========================================================================================================================================================
163 Option Description
164 =========================== =========================================================================================================================================================
165 **-a** BED/GFF/VCF file A. Each feature in A is compared to B in search of overlaps. Use "stdin" if passing A with a UNIX pipe.
166 **-b** BED/GFF/VCF file B. Use "stdin" if passing B with a UNIX pipe.
167 **-g** genome file listing chromosome size.
168 **-f** Minimum overlap required as a fraction of A. Default is 1E-9 (i.e. 1bp).
169 **-r** Require that the fraction of overlap be reciprocal for A and B. In other words, if -f is 0.90 and -r is used, this requires that B overlap at least 90% of A and that A also overlaps at least 90% of B.
170 **-s** Force "strandedness". That is, only report hits in B that overlap A on the same strand. By default, overlaps are reported without respect to strand.
171 **-S** Require different strandedness. That is, only report hits in B that overlap A on the _opposite_ strand. By default, overlaps are reported without respect to strand.
172 **-split** Treat "split" BAM (i.e., having an "N" CIGAR operation) or BED12 entries as distinct BED intervals.
173 =========================== =========================================================================================================================================================
174
175 .. _Fisher's Exact Test: http://en.wikipedia.org/wiki/Fisher's_exact_test