<p>There's an example in the README (<a href="https://github.com/epifluidlab/cragr#example-datasets" rel="nofollow noreferrer">https://github.com/epifluidlab/cragr#example-datasets</a>):</p>
<blockquote>
<p>For you information, below are steps from the BAM file to the
fragment file:</p>
</blockquote>
<pre class="lang-none prettyprint-override"><code>samtools sort -n -o qsorted.bam -@ 4 sample.bam
samtools view -h -f 3 -F 3852 -G 48 --incl-flags 48 qsorted.bam |\
  bamToBed -bedpe -mate1 -i stdin |\
  awk -F'\t' -v OFS=&quot;\t&quot; '{if ($1!=$4) next; if ($9==&quot;+&quot;) {s=$2;e=$6} else {s=$5;e=$3} if (e&gt;s) print $1,s,e,$7,$8,$9}' |\
  sort -k1,1V -k2,2n |\
  bgzip &gt; sample.frag.bed.gz
tabix -p bed sample.frag.bed.gz
</code></pre>

CC BY-SA 4.0
2023-08-02T04:29:38.607Z
2023-08-02T04:29:38.607Z
jared_mamrot
<p>Im trying to follow this pipeline <a href="https://github.com/epifluidlab/cragr" rel="nofollow noreferrer">https://github.com/epifluidlab/cragr</a> on cfDNA WGS. I have bam files from paired end reads aligned with bwa mem.</p>
<p>The workflow requires bgzipped and indexed fragment data files as input.
<a href="https://i.sstatic.net/CJ4Gt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/CJ4Gt.png" alt="enter image description here" /></a></p>
<p>I have been searching how can I obtain BED files from fragments coordinates instead of reads coordinates, and came up with no answer.</p>
<p>Does someone know how can I obtain such bed file?</p>

CC BY-SA 4.0
2023-08-01T19:45:48.847Z
Hi, what do you mean? :) I don't know how to do it. I'm familiar with bamtobed from bedtools but that gives me a bed file based on reads. I need a bed file based on fragments instead :)
Julieta
CC BY-SA 4.0
2023-08-01T21:39:22.193Z
I see so you are looking whether there is a bedtools option for fragment output. Got it, thanks. Would be good to modify the question with the info.
M__
<sam><bed><bedtools><wgs><epigenetics>
obtaining a fragment data file bed from bam
# obtaining a fragment data file bed from bam

(Asked by: username_0 on 2023-08-01)

Im trying to follow this pipeline <https://github.com/epifluidlab/cragr> on cfDNA WGS. I have bam files from paired end reads aligned with bwa mem.

The workflow requires bgzipped and indexed fragment data files as input.
[![enter image description here](https://i.sstatic.net/CJ4Gt.png)](https://i.sstatic.net/CJ4Gt.png)

I have been searching how can I obtain BED files from fragments coordinates instead of reads coordinates, and came up with no answer.

Does someone know how can I obtain such bed file?

**Tags:** sam, bed, bedtools, wgs, epigenetics
> **Comments:**
> **username_0**: Hi, what do you mean? :) I don't know how to do it. I'm familiar with bamtobed from bedtools but that gives me a bed file based on reads. I need a bed file based on fragments instead :)  
> **username_1**: I see so you are looking whether there is a bedtools option for fragment output. Got it, thanks. Would be good to modify the question with the info.  

---

## 1 answers

### Answer by username_2 on 2023-08-02. Score: 2

There's an example in the README (https://github.com/epifluidlab/cragr#example-datasets):

For you information, below are steps from the BAM file to the
fragment file:

samtools sort -n -o qsorted.bam -@ 4 sample.bam
samtools view -h -f 3 -F 3852 -G 48 --incl-flags 48 qsorted.bam |\
  bamToBed -bedpe -mate1 -i stdin |\
  awk -F'\t' -v OFS="\t" '{if ($1!=$4) next; if ($9=="+") {s=$2;e=$6} else {s=$5;e=$3} if (e>s) print $1,s,e,$7,$8,$9}' |\
  sort -k1,1V -k2,2n |\
  bgzip > sample.frag.bed.gz
tabix -p bed sample.frag.bed.gz


---

