# How can I extract information from .sam files?

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?

• 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). Jan 30 at 12:49

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.

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

• 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' Jan 26 at 20:26
• @DmitriiTrubetskoy See updated answer. Jan 26 at 20:53
• 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 ? Jan 26 at 21:16
• @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. Jan 26 at 21:22
• Great, now everything's ok. Much appreciated the tips and for being patient. Jan 28 at 16:58