<p>The <a href="https://www.ncbi.nlm.nih.gov/books/NBK279697/" rel="nofollow noreferrer"><code>blast_formatter</code> command</a> allows you to produce output in any of the formats supported by BLAST. However, it requires that you run <code>blastx</code> with <code>--outfmt 11</code> (ASN.1 format). I'm not sure there are any tools that will do the conversion from the full default BLAST report. This could potentially be scripted, but would be complex enough that you're probably better off re-running BLAST.</p>

CC BY-SA 3.0
2017-09-06T18:31:24.607Z
2017-09-06T18:31:24.607Z
Daniel Standage
<p>Biopython has a parser for Blast's default text format (which I assume is the one you have). Just use the <code>blast-text</code> string to specify this format in <code>SearchIO</code>'s <code>parse</code> method.</p>

<pre><code>from Bio import SearchIO
results = SearchIO.parse('myfile.txt', 'blast-text')
for entry in results:
    # extract the information you want
    query = entry.id
    for hit in entry.hits:
        subject = hit.id
        for hsp in hit.hsps:
            identity = hsp.ident_num *100 / entry.seq_len
            aln_len = hsp.aln.get_alignment_length()
            mismatches = aln_len - hsp.ident_num # not sure about this
            gaps = hsp.gap_num
            q_start = hsp.query_start + 1
            q_end = hsp.query_end
            s_start = hsp.hit_start + 1
            s_end = hsp.hit_end
            e_value = hsp.evalue
            bit_score = int(hsp.bitscore)
            # print in outfmt 6 format
            print('\t'.join([str(x) for x in [query,
                                      subject,
                                      identity,
                                      aln_len,
                                      mismatches,
                                      gaps,
                                      q_start,
                                      q_end,
                                      s_start,
                                      s_end,
                                      e_value,
                                      bit_score]]))
</code></pre>

<p>Have a look at <a href="http://biopython.org/DIST/docs/api/Bio.SearchIO.BlastIO-module.html" rel="nofollow noreferrer">Biopython's docs</a> for more information on how to parse Blast results.</p>

CC BY-SA 3.0
2017-09-08T12:12:38.123Z
This is a very versatile approach, thanks.
bedeabc
CC BY-SA 3.0
2017-09-13T18:53:39.533Z
@andresito, please remember to accept the answer that helped you the most
mgalardini
CC BY-SA 3.0
2017-09-07T08:47:00.067Z
2017-09-07T08:47:00.067Z
mgalardini
<p>I have run a blastx of metagenomic databases (raw illumina reads) using the nr database. Unfortunately, I forgot to add the <code>--outfmt 6</code> argument to the code and got the traditional output.  </p>

<p>Could I parse this output into <code>--outfmt 6</code>?</p>

CC BY-SA 3.0
2017-09-06T18:32:41.433Z
By "metagenomic databases" do you mean collections of reads or assembled contigs? BLAST searches of metagenomic reads vs nr is a very bad idea. Much better tools exist for classifying, etc.
Daniel Standage
CC BY-SA 3.0
2017-09-06T20:24:17.457Z
@ Daniel Standage  I refer to a collection of metagenomic reads. Could you suggest any other classification method for shotgun metagenomic reads?
andresito
CC BY-SA 3.0
2017-09-06T23:41:05.557Z
Sure! Although you should take my recommendations with a grain of salt, since this isn't really my area of expertise. But tools like [Kraken](https://doi.org/10.1186/gb-2014-15-3-r46) and [Kaiju](https://doi.org/doi:10.1038/ncomms11257) seem to be pretty popular.
Daniel Standage
CC BY-SA 3.0
2017-09-07T09:53:01.840Z
I think the standard BLAST output is a moving target so the exact parsing will depend on the version of BLAST you use; anyway I would recommend re-running with the `--outfmt 6` flag unless you don't have the computational resources. If it's too slow you can use DIAMOND as a replacement for BLAST with very similar commands
Chris_Rands
<python><blast><metagenome><parsing>
Transform traditional blast output to `--outfmt 6`
# Transform traditional blast output to `--outfmt 6`

(Asked by: username_0 on 2017-09-06)

I have run a blastx of metagenomic databases (raw illumina reads) using the nr database. Unfortunately, I forgot to add the `--outfmt 6` argument to the code and got the traditional output.

Could I parse this output into `--outfmt 6`?

**Tags:** python, blast, metagenome, parsing
> **Comments:**
> **username_1**: By "metagenomic databases" do you mean collections of reads or assembled contigs? BLAST searches of metagenomic reads vs nr is a very bad idea. Much better tools exist for classifying, etc.  
> **username_0**: @ Daniel Standage  I refer to a collection of metagenomic reads. Could you suggest any other classification method for shotgun metagenomic reads?  
> **username_1**: Sure! Although you should take my recommendations with a grain of salt, since this isn't really my area of expertise. But tools like [Kraken](https://doi.org/10.1186/gb-2014-15-3-r46) and [Kaiju](https://doi.org/doi:10.1038/ncomms11257) seem to be pretty popular.  
> **username_2**: I think the standard BLAST output is a moving target so the exact parsing will depend on the version of BLAST you use; anyway I would recommend re-running with the `--outfmt 6` flag unless you don't have the computational resources. If it's too slow you can use DIAMOND as a replacement for BLAST with very similar commands  

---

## 2 answers

### Answer by username_3 on 2017-09-07. Score: 4 | Accepted answer

Biopython has a parser for Blast's default text format (which I assume is the one you have). Just use the blast-text string to specify this format in SearchIO's parse method.
from Bio import SearchIO
results = SearchIO.parse('myfile.txt', 'blast-text')
for entry in results:
    # extract the information you want
    query = entry.id
    for hit in entry.hits:
        subject = hit.id
        for hsp in hit.hsps:
            identity = hsp.ident_num *100 / entry.seq_len
            aln_len = hsp.aln.get_alignment_length()
            mismatches = aln_len - hsp.ident_num # not sure about this
            gaps = hsp.gap_num
            q_start = hsp.query_start + 1
            q_end = hsp.query_end
            s_start = hsp.hit_start + 1
            s_end = hsp.hit_end
            e_value = hsp.evalue
            bit_score = int(hsp.bitscore)
            # print in outfmt 6 format
            print('\t'.join([str(x) for x in [query,
                                      subject,
                                      identity,
                                      aln_len,
                                      mismatches,
                                      gaps,
                                      q_start,
                                      q_end,
                                      s_start,
                                      s_end,
                                      e_value,
                                      bit_score]]))

Have a look at Biopython's docs for more information on how to parse Blast results.

> **Comments:**
> **username_4**: This is a very versatile approach, thanks.  
> **username_3**: @andresito, please remember to accept the answer that helped you the most  

---

### Answer by username_1 on 2017-09-06. Score: 3

The [`blast_formatter` command](https://www.ncbi.nlm.nih.gov/books/NBK279697/) allows you to produce output in any of the formats supported by BLAST. However, it requires that you run `blastx` with `--outfmt 11` (ASN.1 format). I'm not sure there are any tools that will do the conversion from the full default BLAST report. This could potentially be scripted, but would be complex enough that you're probably better off re-running BLAST.


---

