<p>I have the following list of intervals</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>Chr</th>
<th>start</th>
<th>end</th>
</tr>
</thead>
<tbody>
<tr>
<td>chr1</td>
<td>0</td>
<td>5</td>
</tr>
<tr>
<td>chr1</td>
<td>5</td>
<td>10</td>
</tr>
<tr>
<td>chr1</td>
<td>10</td>
<td>15</td>
</tr>
<tr>
<td>chr1</td>
<td>15</td>
<td>20</td>
</tr>
<tr>
<td>chr1</td>
<td>20</td>
<td>25</td>
</tr>
<tr>
<td>chr1</td>
<td>25</td>
<td>30</td>
</tr>
</tbody>
</table>
</div>
<p>and the following CpGs I'd like to intersect with the intervals and aggregate the last two columns using sum.</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>chr</th>
<th>start</th>
<th>end</th>
<th>end -1</th>
<th>coverage</th>
</tr>
</thead>
<tbody>
<tr>
<td>chr1</td>
<td>1</td>
<td>3</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<td>chr1</td>
<td>4</td>
<td>6</td>
<td>5</td>
<td>1</td>
</tr>
<tr>
<td>chr1</td>
<td>7</td>
<td>9</td>
<td>8</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>10</td>
<td>12</td>
<td>11</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>13</td>
<td>15</td>
<td>14</td>
<td>1</td>
</tr>
<tr>
<td>chr1</td>
<td>16</td>
<td>18</td>
<td>17</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>19</td>
<td>21</td>
<td>20</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>22</td>
<td>24</td>
<td>23</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>25</td>
<td>27</td>
<td>26</td>
<td>1</td>
</tr>
</tbody>
</table>
</div>
<p>I ran <code>bedtools map -a cgis.bed -b s.bed -c 4 -o sum</code>, but when a CpG falls
across two regions, bedtools counts it in both. Here the 5 - 10 intervals should have only the second row of the CpG table: a sum of 2. However, since the second row spans 5, the 4 - 6 interval falls into both the 0 - 5 and the 5 - 10.</p>
<div class="s-table-container">
<table class="s-table">
<thead>
<tr>
<th>chr</th>
<th>start</th>
<th>end</th>
<th>bedtools sum coverage</th>
<th>correct sum coverage</th>
</tr>
</thead>
<tbody>
<tr>
<td>chr1</td>
<td>0</td>
<td>5</td>
<td>2</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>5</td>
<td>10</td>
<td>3</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>10</td>
<td>15</td>
<td>3</td>
<td>3</td>
</tr>
<tr>
<td>chr1</td>
<td>15</td>
<td>20</td>
<td>4</td>
<td>4</td>
</tr>
<tr>
<td>chr1</td>
<td>20</td>
<td>25</td>
<td>4</td>
<td>2</td>
</tr>
<tr>
<td>chr1</td>
<td>25</td>
<td>30</td>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
</div>
<p>Can I use bedtools to place it in the lower interval only and not both? Intersects should be start inclusive, but end exclusive. This does work as I'd like if I reduce every end column element in the second CpG table by one.</p>

CC BY-SA 4.0
2023-08-03T10:16:31.977Z
What question are you trying to answer exactly? The second row of your CpG table really does overlap with two rows of your first table.
bricoletc
CC BY-SA 4.0
2023-08-26T17:20:48.413Z
@bricoletc I want to pick the first of the overlapping regions, not both. If there are two the second one should not be counted. I think its a question of end-inclusive vs not. If every `end` in the CpGs table is reduced by one, I get the correct answer. I was wondering if its possible to get this correct answer without editing the input CpG file.
basesorbytes
<bedtools><interval>
`bedtools`: Intersecting/map into only one interval when there are multiple matches?
# `bedtools`: Intersecting/map into only one interval when there are multiple matches?

(Asked by: username_0 on 2023-07-28)

I have the following list of intervals

| Chr | start | end |
| --- | --- | --- |
| chr1 | 0 | 5 |
| chr1 | 5 | 10 |
| chr1 | 10 | 15 |
| chr1 | 15 | 20 |
| chr1 | 20 | 25 |
| chr1 | 25 | 30 |

and the following CpGs I'd like to intersect with the intervals and aggregate the last two columns using sum.

| chr | start | end | end -1 | coverage |
| --- | --- | --- | --- | --- |
| chr1 | 1 | 3 | 2 | 1 |
| chr1 | 4 | 6 | 5 | 1 |
| chr1 | 7 | 9 | 8 | 2 |
| chr1 | 10 | 12 | 11 | 2 |
| chr1 | 13 | 15 | 14 | 1 |
| chr1 | 16 | 18 | 17 | 2 |
| chr1 | 19 | 21 | 20 | 2 |
| chr1 | 22 | 24 | 23 | 2 |
| chr1 | 25 | 27 | 26 | 1 |

I ran `bedtools map -a cgis.bed -b s.bed -c 4 -o sum`, but when a CpG falls
across two regions, bedtools counts it in both. Here the 5 - 10 intervals should have only the second row of the CpG table: a sum of 2. However, since the second row spans 5, the 4 - 6 interval falls into both the 0 - 5 and the 5 - 10.

| chr | start | end | bedtools sum coverage | correct sum coverage |
| --- | --- | --- | --- | --- |
| chr1 | 0 | 5 | 2 | 2 |
| chr1 | 5 | 10 | 3 | 2 |
| chr1 | 10 | 15 | 3 | 3 |
| chr1 | 15 | 20 | 4 | 4 |
| chr1 | 20 | 25 | 4 | 2 |
| chr1 | 25 | 30 | 1 | 1 |

Can I use bedtools to place it in the lower interval only and not both? Intersects should be start inclusive, but end exclusive. This does work as I'd like if I reduce every end column element in the second CpG table by one.

**Tags:** bedtools, interval
> **Comments:**
> **username_1**: What question are you trying to answer exactly? The second row of your CpG table really does overlap with two rows of your first table.  
> **username_0**: @bricoletc I want to pick the first of the overlapping regions, not both. If there are two the second one should not be counted. I think its a question of end-inclusive vs not. If every `end` in the CpGs table is reduced by one, I get the correct answer. I was wondering if its possible to get this correct answer without editing the input CpG file.  

---

## 0 answers

