<p>I am not sure what you mean by "fasta alignment file". If you mean a multi-sequence alignment (MSA) in the fasta format, you can't get that because SAM keeps pairwise alignments only and doesn't align inserted sequences. Even if you don't care about inserted sequences, a MSA in fasta is far to big to be practical. Alternatively, by "fasta alignment file", you could mean pairwise alignment, but it is still impractical to output every pairwise alignment in a separate fasta file.</p>

<p>There are tools to convert SAM to BLAST-like format if that is what you want. The following shows one of them. I believe there are other tools easier to use, but I forget what are they.</p>

<p>Anyway, to install:</p>

<pre class="lang-bash prettyprint-override"><code>wget https://raw.githubusercontent.com/lh3/minimap2/master/misc/paftools.js
curl -L https://github.com/attractivechaos/k8/releases/download/v0.2.4/k8-0.2.4.tar.bz2 | tar -jxf -
cp k8-0.2.4/k8-`uname -s` k8
</code></pre>

<p>Then you can convert your <code>ALN.sam</code> with:</p>

<pre class="lang-bash prettyprint-override"><code>./k8 paftools.js sam2paf -L ALN.sam | ./k8 paftools.js view -l0 - | less -S
</code></pre>

<p>Or generate a SAM stream from <code>ALN.bam</code> and pipe it with:</p>

<pre class="lang-bash prettyprint-override"><code>samtools view -h ALN.bam | ./k8 paftools.js sam2paf -L - | ./k8 paftools.js view -l0 -
</code></pre>

<p>Here is an example output:</p>

<pre><code>&gt;MT_orang   16499   0   16025   +   MT_human    16569   576 16569   13773   16095   60  tp:A:P  mm:i:2150   gn:i:172    go:i:101
Ref+:        577 GTTTATGTAGCTTACCTCCT---CAAAGCAATACACTGAAAATGTTTAGACGGGCTCACATCACCCCATAAACAAATAGGTTTGGTCCTAGCCTTTCTATTAGCTCTTAGTAAGATTACACATGCAAGCATCCCCGTTCCAGTGAGTTCACCCTCTAAATCACCACGATCAAAAGGAACAAGCATCA
                 ||||||||||||||  | ||   ||||||||| |||||||||||| | ||||||| |||| | |||||||||||||||||||||||||||||||||||||||||||||||| || |||||||||||||||||||||  |||||||| || ||||| || ||||   ||  || |||| |||||||||
Qry+:          1 GTTTATGTAGCTTA--TTCTATCCAAAGCAATGCACTGAAAATGTCTCGACGGGCCCACA-CGCCCCATAAACAAATAGGTTTGGTCCTAGCCTTTCTATTAGCTCTTAGTGAGGTTACACATGCAAGCATCCCCGCCCCAGTGAG-TCGCCCTCCAAGTCACTCTGACTAAGAGGAGCAAGCATCA
//
</code></pre>

<p>You can change <code>-l0</code> to <code>-l80</code> to print alignments in multiple lines.</p>

CC BY-SA 4.0
2019-02-17T15:18:57.530Z
I meant "aligned fasta format" which I think is MSA. Beside general curiosity, my immediate need for the format is as the input file for gubbins.
Ahmed Abdullah
CC BY-SA 4.0
2019-02-17T15:54:23.303Z
gubbins seems to be a program for the fiel of phylogenetics. So the input file must be an alignment file between your species and not the alignments of the reads for one sample.
finswimmer
CC BY-SA 4.0
2019-02-17T19:10:58.500Z
@AhmedAbdullah you should probably ask a new question, explaining your final objective. Going from reads aligned to a genome (bam) to a multiple sequence alignment you want to use for phylogenetic analysis makes no sense. If you explain what you are trying to achieve in a new question, we should be able to help.
terdon
CC BY-SA 4.0
2019-02-18T09:10:40.307Z
@terdon I am trying whether this works or not. I have 48 SAM files corresponding to 48 samples. I converted them to 48 ordered BAM file. and the BAM fiels to a single vcf file using : bcftools mpileup  -d 1000 -Bf R.fa *.bam  | bcftools call --skip-variants indels --multiallelic-caller --variants-only --samples-file samples.txt -O z -o aln.vcf.gz. vcf-kit can generate aligned fasta for only SNPs from this vcf file (vk phylo fast <vcf fiie>. The problem is alignment is only of SNPs, I was wondering whether one can generate full alignment from the (SAM files or the subsequent vcf file)
Ahmed Abdullah
CC BY-SA 4.0
2019-02-18T09:13:13.590Z
@AhmedAbdullah you are almost certainly approaching the problem in the wrong way. Please ask a new question explaining exactly what you need. What you're describing here makes no sense.
terdon
CC BY-SA 4.0
2021-12-16T04:51:17.430Z
cool...did not know that paftools can print out ref vs query like this. another tool that can do similar is https://github.com/mlafave/sam2pairwise
Colin D
CC BY-SA 4.0
2019-02-17T15:04:41.580Z
2019-02-17T15:04:41.580Z
user172818
<p>InIGV you can visuals the alignments of individual reads to the reference as contained in the .bam file. I guess what you want to is get that information into a file containing all the (paired) read sequences aligned to a particular region, with padding for the missing data. That way you could inspect all SNPs or sequencing errors, check possible haplotypes across a few SNPs, use these for phylogenetic analysis etc.
I am also looking for such tool and best I could find so far is an <a href="https://support.bioconductor.org/p/64748/" rel="nofollow noreferrer">answer on BioConductor</a>. It refers to some code in R that can be used for this purpose:  <code>genomicalignments::stackStringsFromBam()</code>.</p>

CC BY-SA 4.0
2019-10-05T08:37:09.873Z
2019-10-08T15:41:11.857Z
2019-10-08T15:41:11.857Z
Tgrandis
<p>You can dump aligned portions of reads from SAM/BAM file into a padded FASTA file with <a href="https://github.com/virus-evolution/gofasta" rel="nofollow noreferrer">https://github.com/virus-evolution/gofasta</a> . Beware it discards INSertions in the sample reads so the output multiFASTA files is &quot;cleansed&quot; from them. DELetions in the sample reads are retained (represented by dashes in the multiFASTA file).</p>

CC BY-SA 4.0
2024-10-31T04:32:59.777Z
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).
Community
CC BY-SA 4.0
2024-10-30T16:47:27.703Z
2024-11-01T10:03:59.593Z
2024-11-01T10:03:59.593Z
Martin Mokrejs
<p>MSA fasta file can be generated from SAM,
but it is almost impractical to generate this HUGE file.
If you still insist to generate the file, then please check this.</p>

<p><a href="https://sourceforge.net/projects/sam2fasta/files/" rel="nofollow noreferrer">https://sourceforge.net/projects/sam2fasta/files/</a></p>

CC BY-SA 4.0
2019-10-16T23:21:23.177Z
2019-10-16T23:21:23.177Z
Chang Park
<p>I am not talking about consensus sequence, I know how to get consensus sequence using mpileup in samtools/bcftools. As I understand , SAM/BAM files are basically sequence alignment format so it's  natural to expect a straightforward way of conversion between them. Sadly I have yet to find one. Vcf-kit allows for conversion between vcf file and fasta alignment, but the fasta alignment contains only the SNPs. I need the whole alignment.  Does samtools/bcftools provide option for such conversion? If not, is there any other tool?</p>

<phylogenetics><samtools><assembly><bcftools>
How to get fasta alignment file from SAM/BAM file?
# How to get fasta alignment file from SAM/BAM file?

(Asked by: username_0 on 2019-02-17)

I am not talking about consensus sequence, I know how to get consensus sequence using mpileup in samtools/bcftools. As I understand , SAM/BAM files are basically sequence alignment format so it's natural to expect a straightforward way of conversion between them. Sadly I have yet to find one. Vcf-kit allows for conversion between vcf file and fasta alignment, but the fasta alignment contains only the SNPs. I need the whole alignment. Does samtools/bcftools provide option for such conversion? If not, is there any other tool?

**Tags:** phylogenetics, samtools, assembly, bcftools

---

## 4 answers

### Answer by username_1 on 2019-02-17. Score: 8

I am not sure what you mean by "fasta alignment file". If you mean a multi-sequence alignment (MSA) in the fasta format, you can't get that because SAM keeps pairwise alignments only and doesn't align inserted sequences. Even if you don't care about inserted sequences, a MSA in fasta is far to big to be practical. Alternatively, by "fasta alignment file", you could mean pairwise alignment, but it is still impractical to output every pairwise alignment in a separate fasta file.
There are tools to convert SAM to BLAST-like format if that is what you want. The following shows one of them. I believe there are other tools easier to use, but I forget what are they.
Anyway, to install:
wget https://raw.githubusercontent.com/lh3/minimap2/master/misc/paftools.js
curl -L https://github.com/attractivechaos/k8/releases/download/v0.2.4/k8-0.2.4.tar.bz2 | tar -jxf -
cp k8-0.2.4/k8-`uname -s` k8

Then you can convert your ALN.sam with:
./k8 paftools.js sam2paf -L ALN.sam | ./k8 paftools.js view -l0 - | less -S

Or generate a SAM stream from ALN.bam and pipe it with:
samtools view -h ALN.bam | ./k8 paftools.js sam2paf -L - | ./k8 paftools.js view -l0 -

Here is an example output:
>MT_orang   16499   0   16025   +   MT_human    16569   576 16569   13773   16095   60  tp:A:P  mm:i:2150   gn:i:172    go:i:101
Ref+:        577 GTTTATGTAGCTTACCTCCT---CAAAGCAATACACTGAAAATGTTTAGACGGGCTCACATCACCCCATAAACAAATAGGTTTGGTCCTAGCCTTTCTATTAGCTCTTAGTAAGATTACACATGCAAGCATCCCCGTTCCAGTGAGTTCACCCTCTAAATCACCACGATCAAAAGGAACAAGCATCA
                 ||||||||||||||  | ||   ||||||||| |||||||||||| | ||||||| |||| | |||||||||||||||||||||||||||||||||||||||||||||||| || |||||||||||||||||||||  |||||||| || ||||| || ||||   ||  || |||| |||||||||
Qry+:          1 GTTTATGTAGCTTA--TTCTATCCAAAGCAATGCACTGAAAATGTCTCGACGGGCCCACA-CGCCCCATAAACAAATAGGTTTGGTCCTAGCCTTTCTATTAGCTCTTAGTGAGGTTACACATGCAAGCATCCCCGCCCCAGTGAG-TCGCCCTCCAAGTCACTCTGACTAAGAGGAGCAAGCATCA
//

You can change -l0 to -l80 to print alignments in multiple lines.

> **Comments:**
> **username_0**: I meant "aligned fasta format" which I think is MSA. Beside general curiosity, my immediate need for the format is as the input file for gubbins.  
> **username_2**: gubbins seems to be a program for the fiel of phylogenetics. So the input file must be an alignment file between your species and not the alignments of the reads for one sample.  
> **username_3**: @AhmedAbdullah you should probably ask a new question, explaining your final objective. Going from reads aligned to a genome (bam) to a multiple sequence alignment you want to use for phylogenetic analysis makes no sense. If you explain what you are trying to achieve in a new question, we should be able to help.  
> **username_0**: @terdon I am trying whether this works or not. I have 48 SAM files corresponding to 48 samples. I converted them to 48 ordered BAM file. and the BAM fiels to a single vcf file using : bcftools mpileup  -d 1000 -Bf R.fa *.bam  | bcftools call --skip-variants indels --multiallelic-caller --variants-only --samples-file samples.txt -O z -o aln.vcf.gz. vcf-kit can generate aligned fasta for only SNPs from this vcf file (vk phylo fast <vcf fiie>. The problem is alignment is only of SNPs, I was wondering whether one can generate full alignment from the (SAM files or the subsequent vcf file)  
> **username_3**: @AhmedAbdullah you are almost certainly approaching the problem in the wrong way. Please ask a new question explaining exactly what you need. What you're describing here makes no sense.  
> **username_4**: cool...did not know that paftools can print out ref vs query like this. another tool that can do similar is https://github.com/mlafave/sam2pairwise  

---

### Answer by username_5 on 2019-10-05. Score: 0

InIGV you can visuals the alignments of individual reads to the reference as contained in the .bam file. I guess what you want to is get that information into a file containing all the (paired) read sequences aligned to a particular region, with padding for the missing data. That way you could inspect all SNPs or sequencing errors, check possible haplotypes across a few SNPs, use these for phylogenetic analysis etc.
I am also looking for such tool and best I could find so far is an [answer on BioConductor](https://support.bioconductor.org/p/64748/). It refers to some code in R that can be used for this purpose: `genomicalignments::stackStringsFromBam()`.


---

### Answer by username_6 on 2024-10-30. Score: 0

You can dump aligned portions of reads from SAM/BAM file into a padded FASTA file with <https://github.com/virus-evolution/gofasta> . Beware it discards INSertions in the sample reads so the output multiFASTA files is "cleansed" from them. DELetions in the sample reads are retained (represented by dashes in the multiFASTA file).

> **Comments:**
> **username_7**: Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer).  

---

### Answer by username_8 on 2019-10-16. Score: 0

MSA fasta file can be generated from SAM,
but it is almost impractical to generate this HUGE file.
If you still insist to generate the file, then please check this.

<https://sourceforge.net/projects/sam2fasta/files/>


---

