Answers.Body: <p>The paf format does not have the CIGAR string and base quality information that are typically present in a BAM/SAM file. While it is in principle possible to write a paf2sam converter, it's going to be very difficult to regenerate the CIGAR string with all the indel information (and maybe it's probably impossible to do so) that you'll need in a BAM/SAM file. If your motivation is to get coverage information, it might make more sense to convert the paf file into a bed file, before using a tool like bedtools to convert it into a coverage track.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-08-14T20:15:57.890Z
Answers.Text: note that PAF files can have a "cg:" tag that is a cigar string. it is an optional output from minimap2 for example (there is also cs: tag that is a different encoding of the sequence differences, but cg may be more common)
Answers.UserDisplayName: Colin D
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-06T23:42:56.633Z
Answers.LastActivityDate: 2020-03-06T23:42:56.633Z
Answers.UserDisplayName: KT8
Body: <p>I have a bunch of <a href="https://github.com/lh3/miniasm/blob/master/PAF.md" rel="nofollow noreferrer">PAF files</a> resulting from the alignments of fastq files on a reference genome with <a href="https://lh3.github.io/minimap2/minimap2.html" rel="nofollow noreferrer">minimap2</a>. I would like to convert them into SAM/BAM format so I can use <code>samtools mpileup</code> to recover the read coverage at filtered positions</p>
  
  <p>So far I've looked at <code>paftools.js</code> and found <code>sam2paf</code> but no <code>paf2sam</code></p>
  
  <p>I am open to suggestions but I'd like to avoid mapping the reads again. </p>
  
  <p><strong>Edit</strong></p>
  
  <p>I have opened an issue on <a href="https://github.com/lh3/minimap2/issues/493" rel="nofollow noreferrer">minimap2's github</a></p>
  
  <p>Answer from Heng Li :</p>
  
  <blockquote>
    <p>You need to rerun alignment. No tools exist to convert paf to sam.</p>
  
  <pre><code>why there is no paf2sam in paftools.js
  </code></pre>
    
    <p>It is technically possible, but someone needs to write the code.</p>
  </blockquote>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T12:38:39.910Z
Comments.Text: I'm not sure the question is answerable in its current format, there doesn't appear to be enough background information and this often results in substancial misunderstanding between OP and the reply. Lets see what the other think.
Comments.UserDisplayName: M__
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T13:17:50.637Z
Comments.Text: Thanks for the feedback. So, as far I as know mpileup can take an alignment file and a list of genomic positions (eg BED) and return statistics for each positions, such as the read coverage. mpileup doesn't work with PAF file so I am looking to convert PAF format to SAM/BAM format. If there are alternatives to mpileup, I am open to the suggestions. Is this better ?
Comments.UserDisplayName: Paul Endymion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-09T17:39:56.213Z
Comments.Text: It's really weird that they provide `sam2paf` but not `paf2sam`. Could it be that the conversion in that direction isn't possible? Perhaps `paf` is missing information that would be essential for sam?
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-10T08:17:27.333Z
Comments.Text: @terdon I am not a specialist but it's stated that `CIGAR` can take different format depending on the `input/output options`. It might also be not written in the PAF in some case. Is that CIGAR essential to sam ?
Comments.UserDisplayName: Paul Endymion
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-10-11T03:02:05.460Z
Comments.Text: I would take Heng Li's advice.  `minimap2` is fast. Just add the `-a` option when you re-run, which outputs the alignment in `sam` instead of `paf`
Comments.UserDisplayName: user3479780
Tags: <file-formats><format-conversion><sam><minimap2>
Title: Convert PAF format to SAM/BAM format
ThreadText: # Convert PAF format to SAM/BAM format
  
  (Asked by: username_0 on 2019-10-09)
  
  I have a bunch of [PAF files](https://github.com/lh3/miniasm/blob/master/PAF.md) resulting from the alignments of fastq files on a reference genome with [minimap2](https://lh3.github.io/minimap2/minimap2.html). I would like to convert them into SAM/BAM format so I can use `samtools mpileup` to recover the read coverage at filtered positions
  
  So far I've looked at `paftools.js` and found `sam2paf` but no `paf2sam`
  
  I am open to suggestions but I'd like to avoid mapping the reads again.
  
  **Edit**
  
  I have opened an issue on [minimap2's github](https://github.com/lh3/minimap2/issues/493)
  
  Answer from Heng Li :
  
  > You need to rerun alignment. No tools exist to convert paf to sam.
  >
  > ```
  > why there is no paf2sam in paftools.js
  >
  > ```
  >
  > It is technically possible, but someone needs to write the code.
  
  **Tags:** file-formats, format-conversion, sam, minimap2
  
  > **Comments:**
  > **username_1**: I'm not sure the question is answerable in its current format, there doesn't appear to be enough background information and this often results in substancial misunderstanding between OP and the reply. Lets see what the other think.  
  > **username_0**: Thanks for the feedback. So, as far I as know mpileup can take an alignment file and a list of genomic positions (eg BED) and return statistics for each positions, such as the read coverage. mpileup doesn't work with PAF file so I am looking to convert PAF format to SAM/BAM format. If there are alternatives to mpileup, I am open to the suggestions. Is this better ?  
  > **username_2**: It's really weird that they provide `sam2paf` but not `paf2sam`. Could it be that the conversion in that direction isn't possible? Perhaps `paf` is missing information that would be essential for sam?  
  > **username_0**: @username_2 I am not a specialist but it's stated that `CIGAR` can take different format depending on the `input/output options`. It might also be not written in the PAF in some case. Is that CIGAR essential to sam ?  
  > **username_3**: I would take Heng Li's advice.  `minimap2` is fast. Just add the `-a` option when you re-run, which outputs the alignment in `sam` instead of `paf`  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_4 on 2020-03-06. Score: 2
  
  The paf format does not have the CIGAR string and base quality information that are typically present in a BAM/SAM file. While it is in principle possible to write a paf2sam converter, it's going to be very difficult to regenerate the CIGAR string with all the indel information (and maybe it's probably impossible to do so) that you'll need in a BAM/SAM file. If your motivation is to get coverage information, it might make more sense to convert the paf file into a bed file, before using a tool like bedtools to convert it into a coverage track.
  
  > **Comments:**
  > **username_5**: note that PAF files can have a "cg:" tag that is a cigar string. it is an optional output from minimap2 for example (there is also cs: tag that is a different encoding of the sequence differences, but cg may be more common)  
  
  ---
  
  