| 1 |
% bedtools Tutorial |
| 2 |
% Aaron Quinlan |
| 3 |
|
| 4 |
|
| 5 |
Puzzles to help teach you more bedtools. |
| 6 |
======================================== |
| 7 |
|
| 8 |
1. Create a BED file representing all of the intervals in the genome |
| 9 |
that are NOT exonic and are not Promoters (based on the promoters in the hESC file). |
| 10 |
|
| 11 |
Answer: |
| 12 |
|
| 13 |
grep Promoter hesc.chromHmm.bed > hesc.promoters.bed |
| 14 |
|
| 15 |
cat exons.bed hesc.promoters.bed | sort -k1,1 -k2,2n | exons.and.promoters.bed |
| 16 |
|
| 17 |
bedtools complement -i exons.and.promoters.bed -g genome.txt > notexonsorpromoters.bed |
| 18 |
|
| 19 |
|
| 20 |
2. What is the average distance from GWAS SNPs to the closest exon? (Hint - have a look at the [closest](http://bedtools.readthedocs.org/en/latest/content/tools/closest.html) tool.) |
| 21 |
|
| 22 |
Answer: |
| 23 |
|
| 24 |
bedtools closest -a gwas.bed -b exons.bed -d | head |
| 25 |
chr1 1005805 1005806 rs3934834 chr1 1007125 1007955 NM_001205252_exon_0_0_chr1_1007126_r 0 - 1320 |
| 26 |
chr1 1079197 1079198 rs11260603 chr1 1078118 1079434 NR_038869_exon_2_0_chr1_1078119_f 0 + 0 |
| 27 |
chr1 1247493 1247494 rs12103 chr1 1247397 1247527 NM_001256456_exon_1_0_chr1_1247398_r 0 - 0 |
| 28 |
chr1 1247493 1247494 rs12103 chr1 1247397 1247527 NM_001256460_exon_1_0_chr1_1247398_r 0 - 0 |
| 29 |
chr1 1247493 1247494 rs12103 chr1 1247397 1247527 NM_001256462_exon_1_0_chr1_1247398_r 0 - 0 |
| 30 |
chr1 1247493 1247494 rs12103 chr1 1247397 1247527 NM_001256463_exon_1_0_chr1_1247398_r 0 - 0 |
| 31 |
chr1 1247493 1247494 rs12103 chr1 1247397 1247527 NM_017871_exon_1_0_chr1_1247398_r 0 - 0 |
| 32 |
chr1 2069171 2069172 rs425277 chr1 2066700 2066786 NM_001033581_exon_1_0_chr1_2066701_f 0 + 2386 |
| 33 |
chr1 2069171 2069172 rs425277 chr1 2066700 2066786 NM_001033582_exon_1_0_chr1_2066701_f 0 + 2386 |
| 34 |
chr1 2069171 2069172 rs425277 chr1 2066700 2066786 NM_001242874_exon_1_0_chr1_2066701_f 0 + 2386 |
| 35 |
|
| 36 |
bedtools closest -a gwas.bed -b exons.bed -d \ |
| 37 |
| awk '{ sum += $11 } END { if (NR > 0) print sum / NR }' |
| 38 |
46713.1 |
| 39 |
|
| 40 |
3. Count how many exons occur in each 500kb interval ("window") in the human genome. (Hint - have a look at the `makewindows` tool.) |
| 41 |
|
| 42 |
Answer: |
| 43 |
|
| 44 |
bedtools makewindows -g genome.txt -w 500000 > genome.windows.bed |
| 45 |
bedtools intersect -a genome.windows.bed -b exons.bed -c > genome.windows.exoncount.bedg |
| 46 |
|
| 47 |
or... |
| 48 |
|
| 49 |
bedtools makewindows -g genome.txt -w 500000 \ |
| 50 |
| bedtools intersect -a - -b exons.bed -c \ |
| 51 |
> genome.windows.exoncount.bedg |
| 52 |
|
| 53 |
4. Are there any exons that are completely overlapped by an enhancer? If so, how many? |
| 54 |
|
| 55 |
Answer: |
| 56 |
|
| 57 |
bedtools intersect -a exons.bed \ |
| 58 |
-b <(grep Enhancer hesc.chromHmm.bed) \ |
| 59 |
-wa -wb -f 1.0 \ |
| 60 |
| head |
| 61 |
chr1 948846 948956 NM_005101_exon_0_0_chr1_948847_f 0 + chr1 948337 949337 4_Strong_Enhancer |
| 62 |
chr1 1051439 1051736 NM_017891_exon_9_0_chr1_1051440_r 0 - chr1 1051337 1051737 6_Weak_Enhancer |
| 63 |
chr1 1109285 1109306 NM_001130045_exon_0_0_chr1_1109286_f 0 + chr1 1108537 1109537 6_Weak_Enhancer |
| 64 |
chr1 1109803 1109869 NM_001130045_exon_2_0_chr1_1109804_f 0 + chr1 1109737 1109937 6_Weak_Enhancer |
| 65 |
chr1 1219357 1219470 NM_001130413_exon_4_0_chr1_1219358_f 0 + chr1 1219137 1220137 7_Weak_Enhancer |
| 66 |
chr1 1219357 1219470 NR_037668_exon_4_0_chr1_1219358_f 0 + chr1 1219137 1220137 7_Weak_Enhancer |
| 67 |
chr1 1229202 1229313 NM_030649_exon_1_0_chr1_1229203_r 0 - chr1 1228937 1229937 6_Weak_Enhancer |
| 68 |
chr1 1229469 1229579 NM_030649_exon_2_0_chr1_1229470_r 0 - chr1 1228937 1229937 6_Weak_Enhancer |
| 69 |
chr1 1234724 1234736 NM_030649_exon_14_0_chr1_1234725_r 0 - chr1 1234137 1234937 7_Weak_Enhancer |
| 70 |
chr1 1245060 1245231 NM_153339_exon_4_0_chr1_1245061_f 0 + chr1 1244937 1245337 4_Strong_Enhancer |
| 71 |
|
| 72 |
bedtools intersect -a exons.bed \ |
| 73 |
-b <(grep Enhancer hesc.chromHmm.bed) \ |
| 74 |
-wa -wb -f 1.0 -u \ |
| 75 |
| wc -l |
| 76 |
13746 |
| 77 |
|
| 78 |
|
| 79 |
5. What fraction of the GWAS SNPs are exonic? |
| 80 |
|
| 81 |
Answer (Any idea why we need -u?): |
| 82 |
|
| 83 |
wc -l gwas.bed |
| 84 |
17680 gwas.bed |
| 85 |
|
| 86 |
bedtools intersect -a gwas.bed -b exons.bed -u | wc -l |
| 87 |
1625 |
| 88 |
|
| 89 |
echo "foo" | awk '{print 1625/17680}' |
| 90 |
0.0919118 |
| 91 |
|
| 92 |
6. What fraction of the GWAS SNPs are lie in either enhancers or promoters in the hESC data we have? |
| 93 |
|
| 94 |
Answer (Any idea why we need -u?): |
| 95 |
|
| 96 |
bedtools intersect -a gwas.bed -b <(egrep "Enhancer|Promoter" hesc.chromHmm.bed) -u \ |
| 97 |
| wc -l |
| 98 |
1285 |
| 99 |
|
| 100 |
echo "foo" | awk '{print 1285/17680}' |
| 101 |
0.072681 |
| 102 |
|
| 103 |
7. Create intervals representing the canonical 2bp splice sites on either side of each exon (don't worry about excluding splice sites at the first or last exon). (Hint - have a look at the [flank](http://bedtools.readthedocs.org/en/latest/content/tools/flank.html) tool.) |
| 104 |
|
| 105 |
Answer: |
| 106 |
|
| 107 |
bedtools flank -l 2 -r 2 -i exons.bed -g genome.txt > splice-sites.bed |
| 108 |
|
| 109 |
Or: |
| 110 |
|
| 111 |
bedtools slop -b 2 -i exons.bed -g genome.txt > exons.plus2.bed |
| 112 |
bedtools subtract -a exons.plus2.bed -b exons.bed > splice-sites.bed |
| 113 |
|
| 114 |
|
| 115 |
8. What is the Jaccard statistic between CpG and hESC enhancers? Compare that to the Jaccard statistic between CpG and hESC promoters. Does the result make sense? (Hint - you will need `grep`). |
| 116 |
|
| 117 |
Answer: |
| 118 |
|
| 119 |
bedtools jaccard -a cpg.bed -b <(grep Enhancer hesc.chromHmm.bed) |
| 120 |
intersection union-intersection jaccard n_intersections |
| 121 |
1148180 132977386 0.0086344 4969 |
| 122 |
|
| 123 |
bedtools jaccard -a cpg.bed -b <(grep Promoter hesc.chromHmm.bed) |
| 124 |
intersection union-intersection jaccard n_intersections |
| 125 |
15661111 53551816 0.292448 20402 |
| 126 |
|
| 127 |
|
| 128 |
9. What would you expect the Jaccard statistic to look like if promoters were randomly distributed throughout the genome? (Hint - you will need the [shuffle](http://bedtools.readthedocs.org/en/latest/content/tools/shuffle.html) tool.) |
| 129 |
|
| 130 |
Answer: |
| 131 |
|
| 132 |
bedtools shuffle -i <(grep Promoter hesc.chromHmm.bed) -g genome.txt \ |
| 133 |
| sort -k1,1 -k2,2n \ |
| 134 |
> promoters.shuffled.bed |
| 135 |
|
| 136 |
bedtools jaccard -a cpg.bed -b promoters.shuffled.bed |
| 137 |
intersection union-intersection jaccard n_intersections |
| 138 |
294071 68556207 0.00428949 78 |
| 139 |
|
| 140 |
10. Which hESC ChromHMM state (e.g., 11_Weak_Txn, 10_Txn_Elongation) represents the most number of base pairs in the genome? (Hint: you will need to use `awk` or `perl` here, as well as the [groupby](http://bedtools.readthedocs.org/en/latest/content/tools/groupby.html) tool.) |