Newer
Older
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
package uk.ac.sanger.artemis.util;
import uk.ac.sanger.artemis.io.ChadoCanonicalGene;
import uk.ac.sanger.artemis.io.DocumentEntry;
import uk.ac.sanger.artemis.io.PartialSequence;
import uk.ac.sanger.artemis.io.ReadFormatException;
import uk.ac.sanger.artemis.chado.IBatisDAO;
import uk.ac.sanger.artemis.chado.JdbcDAO;
import uk.ac.sanger.artemis.chado.GmodDAO;
import uk.ac.sanger.artemis.chado.ChadoTransaction;
import uk.ac.sanger.artemis.components.database.DatabaseEntrySource;
import org.gmod.schema.sequence.Feature;
import org.gmod.schema.sequence.FeatureProp;
import org.gmod.schema.sequence.FeatureLoc;
import org.gmod.schema.sequence.FeatureRelationship;
import org.gmod.schema.sequence.FeatureSynonym;
import org.gmod.schema.sequence.FeatureCvTerm;
import org.gmod.schema.sequence.FeatureCvTermProp;
import org.gmod.schema.cv.Cv;
import org.gmod.schema.general.DbXRef;
import org.gmod.schema.pub.PubDbXRef;
import org.gmod.schema.pub.Pub;
import java.util.Collections;
import java.util.Comparator;
* Objects of this class are Documents created from a relational database.
*
*/
/** source feature_id */
private String srcFeatureId = "1";
/** database schema */
private String schema = "public";
private static Hashtable cvterms;
private InputStreamProgressListener progress_listener;
/** JDBC DAO */
private JdbcDAO jdbcDAO = null;
/** iBatis DAO */
private IBatisDAO connIB = null;
private String[] types = { "exon", "gene", "CDS", "transcript" };
private List schema_list;
private boolean gene_builder;
// include children in reading from the database
private boolean readChildren = true;
private Hashtable idFeatureStore;
private boolean lazyFeatureLoad = true;
public static String EXONMODEL = "exon-model";
public static String TRANSCRIPT = "mRNA";
/** list of controlled_curation CV names */
private static Vector cvControledCuratioNames;
// controlled vocabulary
/** controlled_curation controlled vocabulary */
public static String RILEY_TAG_CVNAME = "RILEY";
private static org.apache.log4j.Logger logger4j =
org.apache.log4j.Logger.getLogger(DatabaseDocument.class);
*
* Create a new Document from a database.
*
* @param location
* This should be a URL string giving:
* jdbc:postgresql://host:port/datbase_name?user=username
*
*/
public DatabaseDocument(String location, JPasswordField pfield)
if(location.indexOf('=') > -1)
this.schema = location.substring( location.indexOf('=')+ 1);
*
* Create a new Document from a database.
*
* @param location
* This should be a URL string giving:
* @param feature_id
* ID of a feature to be extracted.
*
*/
public DatabaseDocument(String location, JPasswordField pfield,
String srcFeatureId, String schema)
this.srcFeatureId = srcFeatureId;
*
* Create a new Document from a database.
*
* @param location
* This should be a URL string giving:
* jdbc:postgresql://host:port/datbase_name?user=username
* @param srcFeatureId
* ID of a feature to be extracted.
* @param splitGFFEntry
* split into separate entries based on feature types.
* @param progress_listener
* input stream progress listener
*
*/
public DatabaseDocument(String location, JPasswordField pfield,
String srcFeatureId, String schema, boolean splitGFFEntry,
InputStreamProgressListener progress_listener)
{
super(location);
this.srcFeatureId = srcFeatureId;
this.progress_listener = progress_listener;
/**
* Used by the gene builder to read a database entry
* for a single gene.
* @param location
* @param pfield
* @param srcFeatureId
* @param schema
* @param gene_builder
*/
public DatabaseDocument(String location, JPasswordField pfield,
String srcFeatureId, String schema, boolean gene_builder)
{
super(location);
this.pfield = pfield;
this.srcFeatureId = srcFeatureId;
this.schema = schema;
this.gene_builder = gene_builder;
if(System.getProperty("ibatis") != null)
{
iBatis = true;
System.setProperty("chado", location);
}
public DatabaseDocument(String location, JPasswordField pfield,
String srcFeatureId, String schema,
this.srcFeatureId = srcFeatureId;
/**
* Use another DatabaseDocument to make a new document.
* @param originalDocument
* @param srcFeatureId
* @param schema
* @param gene_builder
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
public DatabaseDocument (final DatabaseDocument originalDocument,
final String schema, final Feature geneFeature,
final Range range,
final InputStreamProgressListener progress_listener)
{
this((String)originalDocument.getLocation(),
originalDocument.getPfield(),
"-1", schema, false);
this.progress_listener = progress_listener;
this.range = range;
this.geneFeature = geneFeature;
}
/**
* Use another DatabaseDocument to make a new document.
* @param originalDocument
* @param srcFeatureId
* @param schema
* @param gene_builder
* @param region_grab
* @param progress_listener
*/
public DatabaseDocument (final DatabaseDocument originalDocument,
final String srcFeatureId,
final String schema,
final boolean gene_builder,
final InputStreamProgressListener progress_listener)
{
this((String)originalDocument.getLocation(),
originalDocument.getPfield(),
srcFeatureId, schema, gene_builder);
{
// add username & host to MDC data for logging
try
catch(NullPointerException npe)
{
org.apache.log4j.MDC.put("username",System.getProperty("user.name"));
}
try
{
org.apache.log4j.MDC.put("host",
}
catch(Exception e) {}
}
public void setReadChildren(final boolean readChildren)
{
this.readChildren = readChildren;
/**
* Reset the schema.
* @param location
* @param schema
*/
private void reset(String location, String schema)
{
this.schema = schema;
if(!location.endsWith("="+schema))
{
int index = location.lastIndexOf('=');
setLocation(location.substring(0,index+1) + schema);
if(iBatis && connIB != null)
{
try
{
connIB.close();
}
catch(SQLException e)
{
logger4j.warn(e.getMessage());
}
connIB = null;
}
jdbcDAO = null;
System.setProperty("chado", (String)getLocation());
return new DatabaseDocument( ((String)getLocation()) + name, pfield);
* Return the name of this Document (the last element of the Document
* location).
*/
public String getName()
int ind = ((String) getLocation()).indexOf("?");
String name = ((String) getLocation()).substring(0, ind);
/**
* Set the name of this document.
*/
public void setName(String name)
{
this.name = name;
}
public DatabaseDocument createDatabaseDocument()
return new DatabaseDocument( (String)getLocation(), pfield,
srcFeatureId, schema );
* Return true if and only if the Document refered to by this object exists
* and is readable. Always returns true.
*/
public boolean readable()
* Return true if and only if the Document refered to by this object exists
* and can be written to. Always returns false.
*/
public boolean writable()
* Create a new InputStream object from this Document. The contents of the
* Document can be read from the InputStream.
*
* @exception IOException
* Thrown if the Document can't be read from (for example if it
* doesn't exist).
*/
public InputStream getInputStream() throws IOException
ByteArrayInputStream instream;
if(gff_buff != null)
{
instream = new ByteArrayInputStream(gff_buff.getBytes());
List schemaList = new Vector();
schemaList.add(schema);
ByteBuffer bb = getGeneFeature(srcFeatureId,
schemaList, dao, readChildren);
else if(range != null)
{
//
// Retrieve all features within a range
final Feature srcFeature;
if(geneFeature != null)
{
Collection featureLocs = geneFeature.getFeatureLocsForFeatureId();
Iterator it = featureLocs.iterator();
final FeatureLoc featureLoc = (FeatureLoc)it.next();
int srcfeatureid = featureLoc.getFeatureBySrcFeatureId().getFeatureId();
srcFeature = dao.getFeatureById(srcfeatureid);
this.srcFeatureId = Integer.toString(srcfeatureid);
}
else
{
srcFeature = dao.getFeatureById(Integer.parseInt(srcFeatureId));
}
final ByteBuffer entryBuffer = getFeaturesInRange(srcFeature, range, dao);
getChadoSequence(srcFeature, entryBuffer);
return new ByteArrayInputStream(entryBuffer.getBytes());
}
ByteBuffer entryBuffer = new ByteBuffer();
if(dao instanceof IBatisDAO)
((IBatisDAO) dao).startTransaction();
logger4j.debug("RETRIEVE SOURCE FEATURE FROM: "+getLocation());
Feature srcFeature = getChadoSequence(dao, sequenceBuffer);
gff_buffer = getGff(dao, srcFeature);
}
else
{
for(int i = 0; i < gff_buffer.length; i++)
{
if(gff_buffer[i].size() > 0)
if(dao instanceof IBatisDAO)
((IBatisDAO) dao).commitTransaction();
}
finally
{
if(dao instanceof IBatisDAO)
((IBatisDAO) dao).endTransaction();
catch(RuntimeException re)
{
JOptionPane.showMessageDialog(null, "Problems Reading...\n" +
re.getMessage(),
"Problems Reading From the Database ",
JOptionPane.ERROR_MESSAGE);
re.printStackTrace();
}
JOptionPane.showMessageDialog(null, "Problems Reading...\n" +
sqlExp.getMessage(),
"Problems Reading From the Database ",
sqlExp.printStackTrace();
}
return null;
}
*
* Called (by DatabaseEntrySource) to retrieve all the documents for each
* entry created.
*
*/
public DatabaseDocument[] getGffDocuments(String location, String id,
String schema)
{
if(gff_buffer[i].size() > 0)
nentries++;
}
DatabaseDocument[] new_docs = new DatabaseDocument[nentries];
nentries = 0;
String name;
if(i >= types.length)
name = "other";
else
name = types[i];
new_docs[nentries] = new DatabaseDocument(location, pfield, id, schema,
gff_buffer[i], name);
* Create an array of GFF lines.
* @param dao the data access object
* @param parentFeatureID the parent identifier for the features to
* extract
* @return the <code>ByteBuffer</code> array of GFF lines
private ByteBuffer[] getGff(final GmodDAO dao,
final Feature srcFeature)
featureloc.setFeatureBySrcFeatureId(srcFeature);
final List featList = dao.getFeaturesByLocatedOnFeature(child);
final ByteBuffer[] buffers = new ByteBuffer[types.length + 1];
final Hashtable id_store = new Hashtable(feature_size);
for(int i = 0; i < feature_size; i++)
{
id_store.put(Integer.toString(feat.getFeatureId()), feat);
if(lazyFeatureLoad)
idFeatureStore = id_store;
final Hashtable dbxrefs;
final Hashtable synonym;
final Hashtable featureCvTerms;
final Hashtable featureCvTermDbXRefs;
final Hashtable featureCvTermPubs;
final Hashtable featurePubs;
final List pubDbXRefs;
if(lazyFeatureLoad)
{
dbxrefs = null;
synonym = null;
featureCvTerms = null;
featureCvTermDbXRefs = null;
featureCvTermPubs = null;
featurePubs = null;
pubDbXRefs = null;
}
else
{
dbxrefs= IBatisDAO.mergeDbXRef(
featureCvTerms = getFeatureCvTermsByFeature(dao,
featureCvTermDbXRefs = getFeatureCvTermDbXRef(dao,
featurePubs = getFeaturePubs(dao,
dao.getFeaturePubsBySrcFeature(srcFeature));
String typeName = getCvtermName(type_id, dao, gene_builder);
{
if(types[j].equals(typeName))
this_buff = buffers[j];
}
dbxrefs, synonym, featureCvTerms,
feat.getFeatureLoc(), this_buff, gene_builder);
if( i%10 == 0 || i == feature_size-1)
progress_listener.progressMade("Read from database: " +
* Get a <code>Hashtable</code> of feature_id keys and their corresponding
* feature_synonym
private Hashtable getAllFeatureSynonyms(final List list)
{
Integer featureId;
featureId = new Integer(alias.getFeature().getFeatureId());
if(synonym.containsKey(featureId))
value = (Vector)synonym.get(featureId);
else
value = new Vector();
value.add(alias);
synonym.put(featureId, value);
/**
* Get FeaturePub's (i.e. /literature qualifiers).
* @param dao
private Hashtable getFeaturePubs(final GmodDAO dao,
final List list)
Integer featureId;
List value;
FeaturePub featurePub;
for(int i=0; i<list.size(); i++)
{
featurePub = (FeaturePub)list.get(i);
featureId = new Integer(featurePub.getFeature().getFeatureId());
if(featurePubs.containsKey(featureId))
value = (Vector)featurePubs.get(featureId);
else
value = new Vector();
value.add(featurePub);
featurePubs.put(featureId, value);
}
return featurePubs;
}
/**
*
* @param dao
* @param chadoFeature null if we want them all
* @return
*/
private Hashtable getFeatureCvTermsByFeature(final GmodDAO dao,
{
Hashtable featureCvTerms = new Hashtable();
Integer featureId;
List value;
FeatureCvTerm feature_cvterm;
for(int i=0; i<list.size(); i++)
{
feature_cvterm = (FeatureCvTerm)list.get(i);
featureId = new Integer(feature_cvterm.getFeature().getFeatureId());
if(featureCvTerms.containsKey(featureId))
value = (Vector)featureCvTerms.get(featureId);
else
value = new Vector();
value.add(feature_cvterm);
featureCvTerms.put(featureId, value);
}
return featureCvTerms;
}
/**
*
* @param dao
* @param chadoFeature null if we want all
* @return
*/
private Hashtable getFeatureCvTermDbXRef(final GmodDAO dao, final List list)
Hashtable featureCvTermDbXRefs = new Hashtable(list.size());
for(int i=0; i<list.size(); i++)
{
FeatureCvTermDbXRef featureCvTermDbXRef =
(FeatureCvTermDbXRef)list.get(i);
featureCvTermDbXRefId = new Integer(
featureCvTermDbXRef.getFeatureCvTerm().getFeatureCvTermId());
if(featureCvTermDbXRefs.containsKey(featureCvTermDbXRefId))
value = (Vector)featureCvTermDbXRefs.get(featureCvTermDbXRefId);
else
value = new Vector();
value.add(featureCvTermDbXRef);
featureCvTermDbXRefs.put(featureCvTermDbXRefId, value);
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
{
if(list == null || list.size() == 0)
return null;
Integer featureCvTermId;
List value;
Hashtable featureCvTermPubs = new Hashtable(list.size());
for(int i=0; i<list.size(); i++)
{
FeatureCvTermPub featureCvTermPub =
(FeatureCvTermPub)list.get(i);
featureCvTermId = new Integer(
featureCvTermPub.getFeatureCvTerm().getFeatureCvTermId());
if(featureCvTermPubs.containsKey(featureCvTermId))
value = (Vector)featureCvTermPubs.get(featureCvTermId);
else
value = new Vector();
value.add(featureCvTermPub);
featureCvTermPubs.put(featureCvTermId, value);
}
return featureCvTermPubs;
}
/**
* Retrieve the features in a given range
* @param srcFeature
* @param range
* @param dao
* @return
*/
private ByteBuffer getFeaturesInRange(final Feature srcFeature,
final Range range,
final GmodDAO dao)
{
ByteBuffer buff = new ByteBuffer();
List featuresInRange = dao.getFeaturesByRange(range.getStart()-1,
range.getEnd(), 0, srcFeature, null);
List featureIds = new Vector(featuresInRange.size());
for(int i=0; i<featuresInRange.size(); i++)
{
Feature thisFeature = (Feature)featuresInRange.get(i);
featureIds.add(new Integer(thisFeature.getFeatureId()));
}
FeatureLoc featureLoc = new FeatureLoc();
featureLoc.setFmin(new Integer(range.getStart()));
featureLoc.setFmax(new Integer(range.getEnd()));
srcFeature.setFeatureLoc(featureLoc);
Hashtable dbxrefs = IBatisDAO.mergeDbXRef(
dao.getFeatureDbXRefsBySrcFeature(srcFeature));
Hashtable synonym = getAllFeatureSynonyms(
dao.getFeatureSynonymsBySrcFeature(srcFeature));
Hashtable featureCvTerms = getFeatureCvTermsByFeature(dao,
dao.getFeatureCvTermsBySrcFeature(srcFeature));
Hashtable featureCvTermDbXRefs = getFeatureCvTermDbXRef(dao,
dao.getFeatureCvTermDbXRefBySrcFeature(srcFeature));
Hashtable featureCvTermPubs = getFeatureCvTermPub(dao,
dao.getFeatureCvTermPubBySrcFeature(srcFeature));
Hashtable featurePubs = getFeaturePubs(dao,
dao.getFeaturePubsBySrcFeature(srcFeature));
List pubDbXRefs = dao.getPubDbXRef();
Hashtable id_store = new Hashtable(featuresInRange.size());
// build feature name store
for(int i = 0; i < featuresInRange.size(); i++)
{
Feature chadoFeature = (Feature)featuresInRange.get(i);
String featureId = Integer.toString(chadoFeature.getFeatureId());
}
for(int i=0; i<featuresInRange.size(); i++)
{
Feature chadoFeature = (Feature)featuresInRange.get(i);
id_store.put(Integer.toString(chadoFeature.getFeatureId()), chadoFeature);
chadoToGFF(chadoFeature, srcFeature.getUniqueName(), dbxrefs, synonym, featureCvTerms,
pubDbXRefs, featureCvTermDbXRefs, featureCvTermPubs, featurePubs,
id_store, dao, chadoFeature.getFeatureLoc(), buff, gene_builder);
if( i%10 == 0 || i == featuresInRange.size()-1)
progress_listener.progressMade("Read from database: " +
chadoFeature.getUniqueName());
}
return buff;
}
/**
* Use by the gene editor to retrieve the gene and related
* features
* @param search_gene gene uniquename
* @param schema_search schema list to search
* @param dao data access method
* @return GFF byte buffer
* @throws SQLException
* @throws ReadFormatException
private ByteBuffer getGeneFeature(final String search_gene,
final List schema_search,
GmodDAO dao,
final boolean readChildren)
throws SQLException, ReadFormatException, ConnectException
CvTermThread cvThread = null;
{
cvThread = new CvTermThread(dao);
cvThread.start();
}
boolean singleSchema = true;
final List pg_schemas = dao.getSchema();
Iterator schemasIt = pg_schemas.iterator();
while(schemasIt.hasNext())
{
String thisSchema = (String)schemasIt.next();
if( thisSchema.equalsIgnoreCase(schema) )
{
singleSchema = false;
break;
}
}
if(singleSchema)
logger4j.debug("SINGLE SCHEMA");
else
reset((String)getLocation(), (String)schema_search.get(0));
(Feature)(dao.getFeaturesByUniqueName(search_gene).get(0));
ChadoCanonicalGene chado_gene = new ChadoCanonicalGene();
id_store.put(Integer.toString(chadoFeature.getFeatureId()),
List featurelocs = new Vector(chadoFeature.getFeatureLocsForFeatureId());
FeatureLoc featureloc = (FeatureLoc) featurelocs.get(0);
int src_id = featureloc.getSrcFeatureId();
srcFeatureId = Integer.toString(src_id);
parent = dao.getLazyFeatureNoResiduesById(new Integer(src_id));