Answers.Body: <p>I had the same problem. I'm pretty sure you can calculate it yourself using the following:</p>
  <ol>
  <li><p>The length of your sequence (seq_len)</p>
  </li>
  <li><p>The value from the <code>&lt;Hsp_query-from&gt;</code> tag from the hit (<code>&lt;Hsp_query-from&gt;</code>)</p>
  </li>
  <li><p>The value from the <code>&lt;Hsp_query-to&gt;</code> tag from the hit (<code>&lt;Hsp_query-to&gt;</code>)</p>
  </li>
  </ol>
  <p>It can be calculated by:</p>
  <pre><code>query_cover = (&lt;Hsp_query-to&gt; - &lt;Hsp_query-from&gt; + 1) / seq_len * 100
  </code></pre>
  <p>Please note that I did not get this from any documentation. I figured it out by staring at the xml output returned from the Biopython <code>NCBIWWW.qblast()</code> function. This solution seems to match with the results I get by blasting the same sequence on the interactive web portal.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-07-09T18:57:40.437Z
Answers.LastActivityDate: 2021-07-10T18:02:26.130Z
Answers.LastEditDate: 2021-07-10T18:02:26.130Z
Answers.UserDisplayName: Dennis
Answers.Body: <pre class="lang-py prettyprint-override"><code>record = NCBIXML.read(result_handle_xml)
  query_len = record.query_length
  for alignment in record.alignments:
      _ = alignment.hsps.__next__()
      query_cover = (_.query_end - _.query_start + 1) / query_len * 100
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-07T08:57:40.390Z
Answers.LastActivityDate: 2022-04-07T08:57:40.390Z
Answers.UserDisplayName: Vovin
Body: <p>I need to extract (or to calculate precisely) the query coverage from Blast output. I am using NCBIWWW.qblast to query Blast in XML format.</p>
  
  <p>Is it possible to know the query coverage parsing the XML output? If yes, how?</p>
  
  <pre><code>result_handle_xml = NCBIWWW.qblast("blastp","pdb",my_query.seq,format_type='XML')
  </code></pre>
  
  <p><a href="https://www.ncbi.nlm.nih.gov/books/NBK279684/" rel="nofollow noreferrer">here</a> it suggest to use '-6 qcovs', but only for BLAST command line version and only for tabular format</p>
  
  <p>This is the fasta file that I open:</p>
  
  <pre><code>&gt;sp|P02489|CRYAA_HUMAN Alpha-crystallin A chain OS=Homo sapiens 
  GN=CRYAA PE=1 SV=2
  MDVTIQHPWFKRTLGPFYPSRLFDQFFGEGLFEYDLLPFLSSTISPYYRQSLFRTVLDSG
  ISEVRSDRDKFVIFLDVKHFSPEDLTVKVQDDFVEIHGKHNERQDDHGYISREFHRRYRL
  PSNVDQSALSCSLSADGMLTFCGPKIQTGLDATHAERAIPVSREEKPTSAPSS
  </code></pre>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-28T10:30:22.387Z
Comments.Text: Please [edit] your question and include an example of the file you are trying to parse. We can't really help you parse data you don't show.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-28T16:55:57.070Z
Comments.Text: I see you edited, but you still haven't shown us the file you are trying to parse. Is the information even there? Please add an example so we know what you're working with.
Comments.UserDisplayName: terdon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-12-28T21:19:42.693Z
Comments.Text: According to [this](https://www.biostars.org/p/180510), the answer is 'no'.
Comments.UserDisplayName: heathobrien
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2023-09-02T13:30:49.460Z
Comments.Text: @AndreaSpinelli please consider the answers provided and whether these answered your question. If so please remember to "accept" an answer, it helps with site stats
Comments.UserDisplayName: M__
Tags: <python><blast><parsing>
Title: Biopython - extracting query coverage from XML Blast output
ThreadText: # Biopython - extracting query coverage from XML Blast output
  
  (Asked by: username_0 on 2017-12-28)
  
  I need to extract (or to calculate precisely) the query coverage from Blast output. I am using NCBIWWW.qblast to query Blast in XML format.
  
  Is it possible to know the query coverage parsing the XML output? If yes, how?
  
  ```
  result_handle_xml = NCBIWWW.qblast("blastp","pdb",my_query.seq,format_type='XML')
  
  ```
  
  [here](https://www.ncbi.nlm.nih.gov/books/NBK279684/) it suggest to use '-6 qcovs', but only for BLAST command line version and only for tabular format
  
  This is the fasta file that I open:
  
  ```
  >sp|P02489|CRYAA_HUMAN Alpha-crystallin A chain OS=Homo sapiens 
  GN=CRYAA PE=1 SV=2
  MDVTIQHPWFKRTLGPFYPSRLFDQFFGEGLFEYDLLPFLSSTISPYYRQSLFRTVLDSG
  ISEVRSDRDKFVIFLDVKHFSPEDLTVKVQDDFVEIHGKHNERQDDHGYISREFHRRYRL
  PSNVDQSALSCSLSADGMLTFCGPKIQTGLDATHAERAIPVSREEKPTSAPSS
  
  ```
  
  **Tags:** python, blast, parsing
  
  > **Comments:**
  > **username_1**: Please [edit] your question and include an example of the file you are trying to parse. We can't really help you parse data you don't show.  
  > **username_1**: I see you edited, but you still haven't shown us the file you are trying to parse. Is the information even there? Please add an example so we know what you're working with.  
  > **username_2**: According to [this](https://www.biostars.org/p/180510), the answer is 'no'.  
  > **username_3**: @username_0 please consider the answers provided and whether these answered your question. If so please remember to "accept" an answer, it helps with site stats  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_4 on 2021-07-09. Score: 2
  
  I had the same problem. I'm pretty sure you can calculate it yourself using the following:
  
  1. The length of your sequence (seq\_len)
  2. The value from the `<Hsp_query-from>` tag from the hit (`<Hsp_query-from>`)
  3. The value from the `<Hsp_query-to>` tag from the hit (`<Hsp_query-to>`)
  
  It can be calculated by:
  
  ```
  query_cover = (<Hsp_query-to> - <Hsp_query-from> + 1) / seq_len * 100
  
  ```
  
  Please note that I did not get this from any documentation. I figured it out by staring at the xml output returned from the Biopython `NCBIWWW.qblast()` function. This solution seems to match with the results I get by blasting the same sequence on the interactive web portal.
  
  
  ---
  
  ### Answer by username_5 on 2022-04-07. Score: 1
  
  ```py
  record = NCBIXML.read(result_handle_xml)
  query_len = record.query_length
  for alignment in record.alignments:
      _ = alignment.hsps.__next__()
      query_cover = (_.query_end - _.query_start + 1) / query_len * 100
  
  ```
  
  
  ---
  
  