Answers.Body: <p>I think <code>vcf_reader.fetch</code> is what you want.  You have to index the file first though.</p>
  <p>If starting from regular text VCF you can run:</p>
  <pre><code>bgzip example.vcf
  tabix example.vcf.gz
  </code></pre>
  <p>Then the Python code is almost the same as yours:</p>
  <pre><code>import vcf
  vcf_reader = vcf.Reader(filename=&quot;example.vcf.gz&quot;)
  vcf_read = vcf_reader.fetch(&quot;NC_023663.1&quot;, 7803980, 7804000)
  for record in vcf_read:
      print(record)
  </code></pre>
  <p>For this example VCF you get:</p>
  <pre><code>Record(CHROM=NC_023663.1, POS=7803981, REF=G, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803982, REF=T, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803983, REF=A, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803984, REF=A, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803985, REF=G, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803986, REF=G, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803987, REF=G, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803988, REF=C, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803989, REF=A, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803990, REF=T, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803991, REF=A, ALT=[G, &lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803992, REF=G, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803993, REF=A, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803994, REF=C, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803995, REF=T, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803996, REF=A, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803997, REF=A, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803998, REF=C, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7803999, REF=A, ALT=[&lt;*&gt;])
  Record(CHROM=NC_023663.1, POS=7804000, REF=T, ALT=[&lt;*&gt;])
  </code></pre>
  <p>API reference:
  <a href="https://pyvcf.readthedocs.io/en/latest/API.html#vcf.Reader.fetch" rel="nofollow noreferrer">https://pyvcf.readthedocs.io/en/latest/API.html#vcf.Reader.fetch</a></p>
  <p>Note the &quot;zero-based, half-open coordinate system&quot; it describes (which is why 7803980 in the second Python argument becomes POS=7803981 from the VCF but then it stops at POS=7804000).</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-08T20:31:01.133Z
Answers.LastActivityDate: 2020-10-08T20:31:01.133Z
Answers.UserDisplayName: Jesse
Body: <p>I wondered how would I be able to obtain variant based on chrom and location range.</p>
  <p>Optimally, this function would be like:</p>
  <pre><code>import vcf
  vcf_reader = vcf.Reader(filename=some_file_name)
  vcf_read = vcf.variants_between(CHROM, START, END)
  </code></pre>
  <p>Does anyone how to do so?</p>
  
Tags: <vcf><python>
Title: How do I obtain a vcf read from a pyvcf.Reader?
ThreadText: # How do I obtain a vcf read from a pyvcf.Reader?
  
  (Asked by: username_0 on 2020-10-06)
  
  I wondered how would I be able to obtain variant based on chrom and location range.
  
  Optimally, this function would be like:
  
  ```
  import vcf
  vcf_reader = vcf.Reader(filename=some_file_name)
  vcf_read = vcf.variants_between(CHROM, START, END)
  
  ```
  
  Does anyone how to do so?
  
  **Tags:** vcf, python
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-10-08. Score: 1 | Accepted answer
  
  I think `vcf_reader.fetch` is what you want. You have to index the file first though.
  
  If starting from regular text VCF you can run:
  
  ```
  bgzip example.vcf
  tabix example.vcf.gz
  
  ```
  
  Then the Python code is almost the same as yours:
  
  ```
  import vcf
  vcf_reader = vcf.Reader(filename="example.vcf.gz")
  vcf_read = vcf_reader.fetch("NC_023663.1", 7803980, 7804000)
  for record in vcf_read:
      print(record)
  
  ```
  
  For this example VCF you get:
  
  ```
  Record(CHROM=NC_023663.1, POS=7803981, REF=G, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803982, REF=T, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803983, REF=A, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803984, REF=A, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803985, REF=G, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803986, REF=G, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803987, REF=G, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803988, REF=C, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803989, REF=A, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803990, REF=T, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803991, REF=A, ALT=[G, <*>])
  Record(CHROM=NC_023663.1, POS=7803992, REF=G, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803993, REF=A, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803994, REF=C, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803995, REF=T, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803996, REF=A, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803997, REF=A, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803998, REF=C, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7803999, REF=A, ALT=[<*>])
  Record(CHROM=NC_023663.1, POS=7804000, REF=T, ALT=[<*>])
  
  ```
  
  API reference:
  <https://pyvcf.readthedocs.io/en/latest/API.html#vcf.Reader.fetch>
  
  Note the "zero-based, half-open coordinate system" it describes (which is why 7803980 in the second Python argument becomes POS=7803981 from the VCF but then it stops at POS=7804000).
  
  
  ---
  
  