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.program.ssbind;
023
024import java.util.Map;
025
026import org.biojava.bio.BioException;
027import org.biojava.bio.seq.Sequence;
028import org.biojava.bio.seq.db.SequenceDB;
029import org.biojava.bio.seq.db.SequenceDBInstallation;
030import org.biojava.bio.seq.db.SequenceDBLite;
031import org.biojava.bio.seq.impl.ViewSequence;
032
033/**
034 * <code>ViewSequenceFactory</code> is a base class for creating
035 * search handlers which create and cache views on the query and
036 * subject sequences.
037 *
038 * @author Keith James
039 */
040public abstract class ViewSequenceFactory
041{
042    // Supplier of instances of searched databases
043    protected SequenceDBInstallation subjectDBs;
044    // Holder for all query sequences
045    protected SequenceDB querySeqHolder;
046    // Identifier for database
047    protected String databaseID;
048
049    // Cache which holds view(s) of query sequence(s) which have
050    // been instantiated for annotation
051    protected Map queryViewCache;
052    // Cache which holds view(s) of subject sequence(s) which have
053    // been instantiated for annotation
054    protected Map subjectViewCache;
055
056    /**
057     * <code>getQuerySeqHolder</code> returns the database of query
058     * sequences used to retrieve sequences for creation of the
059     * various result objects.
060     *
061     * @return a <code>SequenceDB</code> value.
062     */
063    public SequenceDB getQuerySeqHolder()
064    {
065        return querySeqHolder;
066    }
067
068    /**
069     * <code>setQuerySeqHolder</code> sets the query sequence holder
070     * to a specific database.
071     *
072     * @param querySeqHolder a <code>SequenceDB</code> containing the
073     * query sequence(s).
074     */
075    public void setQuerySeqHolder(SequenceDB querySeqHolder)
076    {
077        this.querySeqHolder = querySeqHolder;
078    }
079
080    /**
081     * <code>getSubjectDBInstallation</code> returns the installation
082     * in which all the databases searched may be
083     * found. <code>SequenceDB</code>s are retrieved for creation of
084     * the various result objects.
085     *
086     * @return a <code>SequenceDBInstallation</code> containing the
087     * subject database(s).
088     */
089    public SequenceDBInstallation getSubjectDBInstallation()
090    {
091        return subjectDBs;
092    }
093
094    /**
095     * <code>setSubjectDBInstallation</code> sets the subject database
096     * holder to a specific installation.
097     *
098     * @param subjectDBs a <code>SequenceDBInstallation</code>
099     * containing the subject database(s)
100     */
101    public void setSubjectDBInstallation(SequenceDBInstallation subjectDBs)
102    {
103        this.subjectDBs = subjectDBs;
104    }
105
106    public void setDatabaseID(String databaseID)
107    {
108        this.databaseID = databaseID;
109    }
110
111    protected Sequence makeQueryViewSequence(String queryID)
112        throws BioException
113    {
114        if (querySeqHolder == null)
115            throw new BioException("Running with null query SequenceDB");
116
117        if (queryViewCache.containsKey(queryID))
118        {
119            return (Sequence) queryViewCache.get(queryID);
120        }
121        else
122        {
123            Sequence query = querySeqHolder.getSequence(queryID);
124
125            // It shouldn't happen, but it can with some implementations
126            // of SequenceDB
127            if (query == null)
128                throw new BioException("Failed to retrieve query sequence "
129                                       + "from holder using ID '"
130                                       + queryID
131                                       + "' (sequence was null)");
132
133            query = new ViewSequence(query);
134            queryViewCache.put(queryID, query);
135
136            return query;
137        }
138    }
139
140    protected Sequence makeSubjectViewSequence(String subjectID)
141        throws BioException
142    {
143        if (subjectDBs == null)
144            throw new BioException("Running with null subject "
145                                   + "SequenceDB installation");
146
147        SequenceDBLite subjectDB = subjectDBs.getSequenceDB(databaseID);
148
149        // It shouldn't happen, but it can with some implementations
150        // of SequenceDBInstallation
151        if (subjectDB == null)
152            throw new BioException("Failed to retrieve database "
153                                   + "from installation using ID '"
154                                   + databaseID
155                                   + "' (database was null)");
156
157        if (subjectViewCache.containsKey(subjectID))
158        {
159            return (Sequence) subjectViewCache.get(subjectID);
160        }
161        else
162        {
163            Sequence subject = subjectDB.getSequence(subjectID);
164
165            // It shouldn't happen, but it can with some implementations
166            // of SequenceDB
167            if (subject == null)
168                throw new BioException("Failed to retrieve subject sequence "
169                                       + "from subjectDB using ID '"
170                                       + subjectID
171                                       + "' (sequence was null)");
172
173            subject = new ViewSequence(subject);
174            subjectViewCache.put(subjectID, subject);
175
176            return subject;
177        }
178    }
179}