[Biojava-l] Extending Feature?

Thomas Down td2@sanger.ac.uk
Tue, 5 Mar 2002 10:54:33 +0000


On Tue, Mar 05, 2002 at 10:32:35AM +0000, pootle monster wrote:
> 
> I to create a new Feature Object
> 
> I have extended the Feature interface, in the same way as StrandedFeature 
> does (but with my own methods instead)
> 
> This works fine and I can create a template :
> 
> MyFeatureType.Template template = new MyFeatureType.Template();
> template.type = "MyFeature";
> template.source = source;
> template.location = new RangeLocation(start, end);
> template.annotation = Annotation.EMPTY_ANNOTATION;
> template.myValue = 12345;
> 
> Which I can then add to the sequence FeatureHolder using:
> 
> sequence.createFeature(template);
> 
> 
> The problem is that when I then use an itreator:
> 
> Iterator myIterator = sequence.features();
> 
> I get a SimpleFeature cast exception when trying to cast the feature to 
> MyFeatureType.


Hi...

It looks like you've gone the right way about creating a 
new Feature interface.  However, you'll also need to write
a concrete implementation of that MyFeature.  For a simple
in-memory implementation, you should just be able to subclass
off SimpleFeature.

You can then use code like:

  FeatureRealizer realizer = new SimpleFeatureRealizer(FeatureImpl.DEFAULT);
  realizer.addImplementation(MyFeature.Template.class, MyFeatureImpl.class);
  Sequence seq = new SimpleSequence(symbols, name, urn, anno, realizer);

That will then give you a Sequence on which you can create
MyFeatures.

When you try to create an unrecognized Feature type, the
system automatically falls back to the first recognized
superclass of your interface (in this case just Feature),
which explains the ClassCastExceptions you're getting.

I'm thinking about modifying the system to auto-generate
in-memory Feature implementation classes, rather than requiring
them to be written by hand.  This would make it much easier
to add new Feature types at run time.  Would you find this
helpful?

     Thomas.