001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022package org.biojava.bio.seq.impl;
023
024import java.util.ArrayList;
025import java.util.Collections;
026import java.util.HashSet;
027import java.util.Iterator;
028import java.util.LinkedList;
029import java.util.List;
030import java.util.Set;
031
032import org.biojava.bio.BioException;
033import org.biojava.bio.seq.Feature;
034import org.biojava.bio.seq.FeatureFilter;
035import org.biojava.bio.seq.FeatureHolder;
036import org.biojava.bio.seq.RemoteFeature;
037import org.biojava.bio.seq.Sequence;
038import org.biojava.bio.seq.db.SequenceDB;
039
040/**
041 * A no-frills implementation of a remote feature.
042 *
043 * @author Matthew Pocock
044 * @author Greg Cox
045 * @since 1.2
046 */
047public class SimpleRemoteFeature
048extends SimpleStrandedFeature
049implements RemoteFeature, java.io.Serializable {
050  private List regions;
051  private RemoteFeature.Resolver resolver;
052
053  public List getRegions() {
054    return Collections.unmodifiableList(regions);
055  }
056
057  public RemoteFeature.Resolver getResolver() {
058    return resolver;
059  }
060
061  public Feature getRemoteFeature() throws BioException {
062    return getResolver().resolve(this);
063  }
064
065  public Feature.Template makeTemplate() {
066    RemoteFeature.Template rt = new RemoteFeature.Template();
067    fillTemplate(rt);
068    return rt;
069  }
070
071  protected void fillTemplate(RemoteFeature.Template rt) {
072    super.fillTemplate(rt);
073    rt.resolver = getResolver();
074    rt.regions = new ArrayList(getRegions());
075  }
076
077  public SimpleRemoteFeature(
078    Sequence sourceSeq,
079    FeatureHolder parent,
080    RemoteFeature.Template template
081  ) {
082    super(sourceSeq, parent, template);
083    // System.err.println("Constructing a RemoteFeature.  Strand = " + template.strand);
084    this.regions = new ArrayList(template.regions);
085    this.resolver = template.resolver;
086  }
087
088  public static class DBResolver implements RemoteFeature.Resolver {
089    private SequenceDB seqDB;
090
091    public SequenceDB getSeqDB() {
092      return seqDB;
093    }
094
095    public DBResolver(SequenceDB seqDB) {
096      this.seqDB = seqDB;
097    }
098
099    public Feature resolve(RemoteFeature rFeat) throws BioException {
100      FeatureFilter remoteFilter
101        = new FeatureFilter.ByClass(RemoteFeature.class);
102
103      Set seqs = new HashSet();
104      LinkedList ids = new LinkedList();
105      Set feats = new HashSet();
106
107      Sequence parent = rFeat.getSequence();
108      ids.add(parent);
109
110      while(!ids.isEmpty()) {
111        Sequence seq = (Sequence) ids.removeFirst();
112        seqs.add(seq);
113
114        FeatureHolder remotes = seq.filter(remoteFilter, false);
115        for(Iterator fi = remotes.features(); fi.hasNext(); ) {
116          RemoteFeature rf = (RemoteFeature) fi.next();
117          feats.add(rf);
118          for(Iterator ri = rf.getRegions().iterator(); ri.hasNext(); ) {
119            RemoteFeature.Region r = (RemoteFeature.Region) ri.next();
120            if(r.isRemote()) {
121              // potentialy expensive - should we cache IDs? What about the ID
122              // of this sequence?
123              Sequence rseq = getSeqDB().getSequence(r.getSeqID());
124              if(!ids.contains(rseq) && !seqs.contains(rseq)) {
125                ids.addLast(rseq);
126              }
127            }
128          }
129        }
130      }
131
132      StringBuffer nameBuff = new StringBuffer();
133      {
134        Iterator si = seqs.iterator();
135        Sequence nextSeq = (Sequence) si.next(); // don't need to check hasNext
136        nameBuff.append(nextSeq.getName());
137        while(si.hasNext()) {
138          nextSeq = (Sequence) si.next();
139          nameBuff.append("-");
140          nameBuff.append(nextSeq.getName());
141        }
142      }
143
144
145
146      return null;
147    }
148  }
149}