Giter Site home page Giter Site logo

Comments (6)

daler avatar daler commented on July 28, 2024

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.

alanlhutchison avatar alanlhutchison commented on July 28, 2024

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.

daler avatar daler commented on July 28, 2024

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.

alanlhutchison avatar alanlhutchison commented on July 28, 2024

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.

daler avatar daler commented on July 28, 2024

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.

alanlhutchison avatar alanlhutchison commented on July 28, 2024

Hi daler,

Thanks a lot! This works great! I appreciate your time!

from pybedtools.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.