How Could I Handle Bed Files In Java?
2
3
Entering edit mode
12.9 years ago
Pascal ★ 1.5k

Hi.

Could you recommend me a Java API to handle BED files?

For now I simply need to know if a given locus is in one of the features described by a BED file.

Thanks

bed java • 3.8k views
ADD COMMENT
6
Entering edit mode
12.9 years ago

you could use the tabix interface for java (requires the picard library) to quickly find the records in a given range:

http://samtools.svn.sourceforge.net/viewvc/samtools/trunk/tabix/TabixReader.java?revision=990&view=markup

then

  • you can just parse the lines using a simple Sting.'split'
  • use some already existing java classes: ( search google for "BedRecord filetype:java" )
  • or you can add your layer of abstraction by declaring a bunch of java classes :-)

    public interface BedRecord { public String getChromosome(); (...) }

and

public abstract class AbstractBedRecord implements BedRecord
    {
    @Override
    public abstract String getChromosome();
    (...)
    }

and

public abstract class DefaultBedRecord extends AbstractBedRecord
    {
    @Override
    public String getChromosome() { ... }
    (...)
    }

and

public interface BedRecordFactory
    {
    public BedRecord parse(String line);
    (...)
    }

and

public class DefaultBedRecordFactory implements BedRecordFactory
    {
    @Override
    public BedRecord parse(String line) { ... }
    (...)
    }

you know, all those java things :-) ....

or you could put your records in a database, using a bin index

etc...

ADD COMMENT
1
Entering edit mode
9.5 years ago
kulvait ▴ 270

See my code. I am not sure if this is intended to work this way but you can play with that at least.

import htsjdk.tribble.bed.BEDCodec;
import htsjdk.tribble.bed.BEDFeature;
import htsjdk.tribble.bed.BEDCodec.StartOffset;
import htsjdk.tribble.readers.AsciiLineReader;
import htsjdk.tribble.readers.LineIteratorImpl;

    List<BEDFeature> allFeatures;
    int x;
    HashMap<String, List<BEDFeature>> chrToBF;
    public AmpliconDesign(File ampliconsBed) throws IOException
    {
        chrToBF = new HashMap<String, List<BEDFeature>>();
        allFeatures = new ArrayList<BEDFeature>();
        InputStream is = new java.io.FileInputStream(ampliconsBed);
        AsciiLineReader lr = new AsciiLineReader(is);
        LineIteratorImpl li = new LineIteratorImpl(lr);
        BEDCodec bc = new BEDCodec(StartOffset.ZERO);
        while(!bc.isDone(li))
        {
            BEDFeature bf = bc.decode(li);
            if(bf != null)
            {
                allFeatures.add(bf);
                if(chrToBF.containsKey(bf.getChr()))
                {
                    chrToBF.get(bf.getChr()).add(bf);
                }else
                {
                    List<BEDFeature> a = new ArrayList<BEDFeature>();
                    a.add(bf);
                    chrToBF.put(bf.getChr(), a);
                }
            }
        }
        this.x = allFeatures.size();
    }
ADD COMMENT

Login before adding your answer.

Traffic: 2634 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6