Comments (6)
Hmm, I'll need to look into this more. But you can accomplish it in a different way, which should be much faster too:
Usually when modifying features you'd write a modifying function and give it to BedTool.each()
. But in this case, since you want to rename using which line you're on, I think it's easiest to use a generator:
def generator():
for i, feature in enumerate(bed):
x.name = "GSM_{0}".format(i+1)
yield x
pybedtools.BedTool(generator()).saveas("renamed.bed")
Keep in mind though if you don't already have a "name" field, you'll need to use pybedtools.featurefuncs.extend_fields(x, 4)
before setting the name in the generator.
from pybedtools.
Hi,
Thank you for your speedy response.
I tried the generator trick you showed me. I think I do not understand it fully. I am attempting to run these on a test file TestD.bed, which I generate thusly
echo 'chr2L\t20\t30\tTestD1\nchr2L\t45\t60\tTestD2\n' > TestD.bed
Here is what I attempted as far as code goes:
import pybedtools
from pybedtools import BedTool
def gen_change_name(bed,GSM):
for i, feature in enumerate(bed):
feature.name = GSM+"_{0}".format(i+1)
yield feature
#BedTool(gen_change_name(bed,GSM)).saveas("temp.bed")
def change_name(bed,fn):
GSM = st.split(fn,'_')[0]
mod4= gen_change_name(bed,GSM)
i=0
for line in mod4:
print line
print bed[i]
bed[i] = line #this produces an error "TypeError: 'BedTool' object does not support item assignment"
i+=1
return bed
I can now generate a generator that will spit out the "corrected" lines, but I cannot pass them to the bed files that are already there. It seems if I call BedTool on my generator, it returns a generator and not a bed file, and I do not get the desired output. How can I get the generator output into a BedTool?
Also, GSM is just a string, nothing more.
If my query has turned into one about generators and requires its own thread I apologize.
Please advise...
from pybedtools.
Hmm, it's difficult to see exactly what you're doing here . . . can you edit your response so that the code is wrapped in triple backticks (``` -- see http://github.github.com/github-flavored-markdown/) with the correct whitespace?
from pybedtools.
Hi,
Sorry about that! Thank you for the very helpful link! I hope my question is more understandable now. Thank you as well for your patience and speedy response.
I tried the generator code you showed me. I think I do not understand it fully. I am attempting to run these on a test file TestD.bed, which I generate thusly
echo 'chr2L\t20\t30\tTestD1\nchr2L\t45\t60\tTestD2\n' > TestD.bed
and produces:
cat TestD.bed
chr2L 20 30 TestD1
chr2L 45 60 TestD2
Here is what I attempted as far as code goes:
import pybedtools
from pybedtools import BedTool
def gen_change_name(bed,GSM):
#GSM is just a string
for i, feature in enumerate(bed):
feature.name = GSM+"_{0}".format(i+1)
yield feature
#BedTool(gen_change_name(bed,GSM)).saveas("temp.bed")
#The above line produces an error
def change_name(bed,fn):
GSM = st.split(fn,'_')[0]
mod4= gen_change_name(bed,GSM)
i=0
for line in mod4:
print line
print bed[i]
bed[i] = line #this produces an error "TypeError: 'BedTool' object does not support item assignment"
i+=1
return bed
I can now generate a generator that will spit out the "corrected" lines, but I cannot pass them to the bed files that are already there. It seems if I call BedTool on my generator, it returns a generator and not a bed file, and I do not get the desired output. How can I get the generator output into a BedTool?
Also, GSM is just a string, nothing more.
If my query has turned into one about generators and requires its own thread I apologize.
Please advise...
from pybedtools.
Here is a self-contained script that creates the test files. Based on how you're getting GSM
, I'm assuming there's some meaningful information in the filenames that you want to get, so I changed the example filenames to reflect that.
You can play around with this test file to help figure out how it works and adapt it to your needs.
import pybedtools
# Create two example files so that this example is self-contained.
pybedtools.BedTool('''
chr2L 20 30 TestD1
chr2L 45 60 TestD2''', from_string=True).saveas('a_test.bed')
pybedtools.BedTool('''
chr3L 500 600 TestB1
chr3L 900 1000 TestB2''', from_string=True).saveas('b_test.bed')
def gen_change_name(fn, GSM):
'''
This generator accepts a filename and a string, and yields
pybedtools.Interval features with names changed according to `GSM` and line
number.
'''
for i, feature in enumerate(pybedtools.BedTool(fn)):
feature.name = GSM + '_{0}'.format(i + 1)
yield feature
def change_name(fn):
'''
This function accepts a filename and creates a new file with changed names.
It returns a BedTool pointing to this new file.
'''
GSM = fn.split('_')[0]
# This is the key: BedTool objects can be created from a generator of
# pybedtools.Interval objects...which is what gen_name_change was designed
# to do.
return pybedtools.BedTool(gen_change_name(fn, GSM))\
.saveas(fn + '.changed')
for fn in ('a_test.bed', 'b_test.bed'):
original = pybedtools.BedTool(fn)
changed = change_name(fn)
print 'original', fn
print original
print 'changed', fn
print changed
from pybedtools.
Hi daler,
Thanks a lot! This works great! I appreciate your time!
from pybedtools.
Related Issues (20)
- pybedtools.bedtool.BedTool.save_seqs leaves open .tmp files
- Support Python 3.10 and 3.11 HOT 1
- "python setup.py bdist_wheel did not run successfully" when pip installing with python v3.11 HOT 8
- to_dataframe() creates 0th row with generic names in nucleotide_content HOT 2
- build failure under python 3.11 HOT 6
- pybedtools intersect error HOT 2
- Cannot create a BedTool object from list of regions that uses np.int64 coordinates
- remove historical py27 support HOT 1
- bedtools intersect reported incorrect interval intersection HOT 3
- Cythonizing files requires `language_level=2` to be set in cythonize() HOT 4
- pybedtools multi_bam_coverage assistance HOT 2
- "fastaFromBed" error HOT 2
- intersect with multiple -b arguments not working with -sorted HOT 1
- Unable to install pybedtools==0.9.1 in Python3.10 HOT 4
- Len modifying the Bedtools after a filter HOT 2
- Has pybedtools considered packaging bedtools? HOT 3
- how to mask gap regions for randomization? HOT 1
- Issue while doing pip install pybedtools HOT 3
- Inconsistent behaviour when using files from `pathlib.PosixPath` with BedTool functions...
- pybedtools.bedtool.Bedtool.sort()
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pybedtools.