This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How Could I Handle Bed Files In Java?

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

2 answers

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...

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();
    }

Log in to answer this question.