<p>You have to take your variables and make them into a single string variable.</p>

<p>for example:</p>

<pre><code>chr_ = "chr1"
start = 3000
end = 3402
as_str = ' '.join([chr_, str(start), str(end)])
# or 
as_str2 = "{} {} {}".format(chr_, start, end)
</code></pre>

<p>Once you have the string built you can use the example in the <a href="https://daler.github.io/pybedtools/autodocs/pybedtools.bedtool.BedTool.html" rel="nofollow noreferrer">bedTool docs</a>.</p>

<pre><code>a = BedTool(as_str, from_string=True)
</code></pre>

CC BY-SA 4.0
2018-05-31T17:58:57.417Z
If you like an answer please accept it so future users know it worked.
Bioathlete
CC BY-SA 4.0
2018-05-31T01:22:43.957Z
2018-05-31T01:29:00.713Z
2018-05-31T01:29:00.713Z
Bioathlete
<p>I am trying to use bedtools and fetch sequences from a whole genome fasta file inside the script I get region coordinates as variables.
For example:</p>

<pre><code>chr_ = "chr1"
start = 3000
end = 3402
</code></pre>

<p>I am not sure how to wrap my function, because the example provided by <a href="https://daler.github.io/pybedtools/autodocs/pybedtools.bedtool.BedTool.sequence.html#pybedtools.bedtool.BedTool.sequence" rel="nofollow noreferrer">BedTool.sequence</a> only uses coordinates in a string form.</p>

<p>Can you please help me to have something like this?</p>

<pre><code>a = pybedtools.BedTool(chr_, start, end, from_variables=True)
</code></pre>

<bedtools><pybedtools>
How to use variables while calling pybedtools
# How to use variables while calling pybedtools

(Asked by: username_0 on 2018-05-30)

I am trying to use bedtools and fetch sequences from a whole genome fasta file inside the script I get region coordinates as variables.
For example:
chr_ = "chr1"
start = 3000
end = 3402

I am not sure how to wrap my function, because the example provided by BedTool.sequence only uses coordinates in a string form.
Can you please help me to have something like this?
a = pybedtools.BedTool(chr_, start, end, from_variables=True)

**Tags:** bedtools, pybedtools

---

## 1 answers

### Answer by username_1 on 2018-05-31. Score: 2 | Accepted answer

You have to take your variables and make them into a single string variable.
for example:
chr_ = "chr1"
start = 3000
end = 3402
as_str = ' '.join([chr_, str(start), str(end)])
# or 
as_str2 = "{} {} {}".format(chr_, start, end)

Once you have the string built you can use the example in the bedTool docs.
a = BedTool(as_str, from_string=True)

> **Comments:**
> **username_1**: If you like an answer please accept it so future users know it worked.  

---

