<p>Use <a href="http://www.htslib.org/doc/samtools-flagstat.html" rel="nofollow noreferrer"><code>samtools flagstat</code></a> with option <code>-O tsv</code>:</p>
<blockquote>
<p>Using <strong>-O tsv</strong> selects a tab-separated values format that can easily be imported into spreadsheet software. In this format the first column contains the values for QC-passed reads, the second column has the values for QC-failed reads and the third contains the category names.</p>
</blockquote>
<p><strong>SEE ALSO:</strong><br />
<a href="https://www.biostars.org/p/12475/" rel="nofollow noreferrer">What Does Samtools Flagstat Results Mean?</a></p>
<p><strong>INSTALLATION:</strong><br />
Some old versions of <code>samtools</code> do not have option <code>-O tsv</code>. To install a recent versions of <code>samtools</code>, use either:</p>
<p><a href="https://anaconda.org/bioconda/samtools" rel="nofollow noreferrer"><code>conda</code></a> (macOS, Linux) (but see also: <a href="https://www.biostars.org/p/455593/" rel="nofollow noreferrer">Tutorial: installing samtools with conda - overcoming two common errors</a>):</p>
<pre><code>conda create --name samtools samtools
</code></pre>
<p>or <a href="https://formulae.brew.sh/formula/samtools" rel="nofollow noreferrer"><code>brew</code></a> (macOS):</p>
<pre><code>brew install samtools
</code></pre>

CC BY-SA 4.0
2021-01-26T20:26:15.037Z
Thank you, it worked however just in a "default" combination, e.g. samtools flagstat input.sam           If I try  samtools flagstat -O tsv input.sam, it throws me an error  invalid option -- 'O'
Dmitrii Trubetskoy
CC BY-SA 4.0
2021-01-26T20:53:29.533Z
@DmitriiTrubetskoy See updated answer.
Timur Shtatland
CC BY-SA 4.0
2021-01-26T21:16:05.503Z
thanks, however my ``` samtools is already the newest version (1.7-1).``` (info from terminal). Just to be sure, the right syntax will be: ```samtools flagstat -O tsv input.sam``` ?
Dmitrii Trubetskoy
CC BY-SA 4.0
2021-01-26T21:22:01.410Z
@DmitriiTrubetskoy Exactly, and note that `1.7*` is older than `1.11`, which is the most recent version available through both `conda` and `brew` (see links in my answer). I also confirmed that this option is present in `samtools 1.10` that I installed via `brew`. So try updating/installing the new `samtools` version if you really need this option.
Timur Shtatland
CC BY-SA 4.0
2021-01-26T22:29:27.753Z
Thanks a lot. I just wonder how can I roll back to ```1.10```? Is there a way to do it?
Dmitrii Trubetskoy
CC BY-SA 4.0
2021-01-27T01:55:16.210Z
@DmitriiTrubetskoy Don't roll back, because 1.7 is fairly old. I am suggesting *updating* from 1.7 to the most recent version, currently 1.11. Use conda or brew. To install conda (for mac or linux), follow directions here for miniconda: https://docs.conda.io/projects/conda/en/latest/user-guide/install/ . To install brew (for mac), follow the directions here: https://brew.sh/ . Then use the commands in my answer to install samtools (via conda **or** brew). This will install by default the most recent version. You are usually best off with the most recent version. Good luck!
Timur Shtatland
CC BY-SA 4.0
2021-01-27T20:01:17.367Z
Thank you. Now it worked, something happened...however I suppose I must have created some new file that would contain some output information - what kind of file that should be? I cannot see find anything in the same folder where ```input.sam``` file was.
Dmitrii Trubetskoy
CC BY-SA 4.0
2021-01-27T20:04:10.580Z
@DmitriiTrubetskoy It writes into STDOUT a tsv file, 16 rows x 3 cols.
Timur Shtatland
CC BY-SA 4.0
2021-01-28T16:58:22.157Z
Great, now everything's ok. Much appreciated the tips and for being patient.
Dmitrii Trubetskoy
CC BY-SA 4.0
2021-01-26T20:04:50.437Z
2021-01-26T20:53:14.153Z
2021-01-26T20:53:14.153Z
Timur Shtatland
<p>I have 10 <code>.sam</code> files after my <code>bowtie2</code> alignment on ten single-pair sequences. I would like to build a graph based on that output data, however I don't see any other solution but just writing the alignment percentage manually after each iteration.</p>
<p>Is there a tool that can help me to build a <code>.csv</code> file so I can manipulate the output data more easily?</p>

CC BY-SA 4.0
2021-01-30T12:49:47.930Z
You should *never* have SAM files: they’re not useful, *not even as intermediate files!* You should *always* directly generate BAM files. They’re smaller, more efficient, and allow more features (e.g. indexing for even faster access and statistics). For example, you would usually call bowtie2 as follows: `bowtie2 ‹args› | samtools sort --output-fmt-option level=0 | samtools view -b -o sorted.bam` (this immediately sorts the BAM file, which requires a lot of RAM; if this isn’t suitable for you, omit the intermediate step; but you normally want to have sorted BAMs).
Konrad Rudolph
<samtools><sam><bowtie2>
How can I extract information from .sam files?
# How can I extract information from .sam files?

(Asked by: username_0 on 2021-01-26)

I have 10 `.sam` files after my `bowtie2` alignment on ten single-pair sequences. I would like to build a graph based on that output data, however I don't see any other solution but just writing the alignment percentage manually after each iteration.

Is there a tool that can help me to build a `.csv` file so I can manipulate the output data more easily?

**Tags:** samtools, sam, bowtie2
> **Comments:**
> **username_1**: You should *never* have SAM files: they’re not useful, *not even as intermediate files!* You should *always* directly generate BAM files. They’re smaller, more efficient, and allow more features (e.g. indexing for even faster access and statistics). For example, you would usually call bowtie2 as follows: `bowtie2 ‹args› | samtools sort --output-fmt-option level=0 | samtools view -b -o sorted.bam` (this immediately sorts the BAM file, which requires a lot of RAM; if this isn’t suitable for you, omit the intermediate step; but you normally want to have sorted BAMs).  

---

## 1 answers

### Answer by username_2 on 2021-01-26. Score: 2 | Accepted answer

Use samtools flagstat with option -O tsv:

Using -O tsv selects a tab-separated values format that can easily be imported into spreadsheet software. In this format the first column contains the values for QC-passed reads, the second column has the values for QC-failed reads and the third contains the category names.

SEE ALSO:
What Does Samtools Flagstat Results Mean?
INSTALLATION:
Some old versions of samtools do not have option -O tsv. To install a recent versions of samtools, use either:
conda (macOS, Linux) (but see also: Tutorial: installing samtools with conda - overcoming two common errors):
conda create --name samtools samtools

or brew (macOS):
brew install samtools

> **Comments:**
> **username_0**: Thank you, it worked however just in a "default" combination, e.g. samtools flagstat input.sam           If I try  samtools flagstat -O tsv input.sam, it throws me an error  invalid option -- 'O'  
> **username_2**: @DmitriiTrubetskoy See updated answer.  
> **username_0**: thanks, however my ``` samtools is already the newest version (1.7-1).``` (info from terminal). Just to be sure, the right syntax will be: ```samtools flagstat -O tsv input.sam``` ?  
> **username_2**: @DmitriiTrubetskoy Exactly, and note that `1.7*` is older than `1.11`, which is the most recent version available through both `conda` and `brew` (see links in my answer). I also confirmed that this option is present in `samtools 1.10` that I installed via `brew`. So try updating/installing the new `samtools` version if you really need this option.  
> **username_0**: Thanks a lot. I just wonder how can I roll back to ```1.10```? Is there a way to do it?  
> **username_2**: @DmitriiTrubetskoy Don't roll back, because 1.7 is fairly old. I am suggesting *updating* from 1.7 to the most recent version, currently 1.11. Use conda or brew. To install conda (for mac or linux), follow directions here for miniconda: https://docs.conda.io/projects/conda/en/latest/user-guide/install/ . To install brew (for mac), follow the directions here: https://brew.sh/ . Then use the commands in my answer to install samtools (via conda **or** brew). This will install by default the most recent version. You are usually best off with the most recent version. Good luck!  
> **username_0**: Thank you. Now it worked, something happened...however I suppose I must have created some new file that would contain some output information - what kind of file that should be? I cannot see find anything in the same folder where ```input.sam``` file was.  
> **username_2**: @DmitriiTrubetskoy It writes into STDOUT a tsv file, 16 rows x 3 cols.  
> **username_0**: Great, now everything's ok. Much appreciated the tips and for being patient.  

---

