Newer
Older
/**
* Get the CDS FeatureLoc's associated with a give protein
* @param peptideName
* @return
*/
public List getCdsFeatureLocsByPeptideName(final String peptideName)
Collection frs = peptideFeature.getFeatureRelationshipsForSubjectId();
Iterator it = frs.iterator();
Feature transcriptFeature = null;
while(it.hasNext())
{
FeatureRelationship fr = (FeatureRelationship)it.next();
if(fr.getCvTerm().getName().equalsIgnoreCase("derives_from"))
{
transcriptFeature = fr.getFeatureByObjectId();
logger4j.debug("TRANSCRIPT :: "+transcriptFeature.getUniqueName());
break;
}
}
if(transcriptFeature == null)
return null;
return getCdsFeatureLocsByTranscriptName(transcriptFeature.getUniqueName());
* Get the CDS FeatureLoc's associated with a given transcript
* @param transcriptName
* @return
*/
public List getCdsFeatureLocsByTranscriptName(final String transcriptName)
{
Feature transcriptFeature = getFeatureByUniquename(transcriptName);
if(transcriptFeature == null)
Collection frs = transcriptFeature.getFeatureRelationshipsForObjectId();
Iterator it = frs.iterator();
while(it.hasNext())
{
FeatureRelationship fr = (FeatureRelationship)it.next();
org.gmod.schema.sequence.Feature child = fr.getFeatureBySubjectId();
if(child.getCvTerm().getName().equals("exon") ||
child.getCvTerm().getName().equals("pseudogenic_exon"))
Collection featureLocs = child.getFeatureLocsForFeatureId();
Iterator it2 = featureLocs.iterator();
while(it2.hasNext())
{
FeatureLoc featureLoc = (FeatureLoc) it2.next();
cdsFeatureLocs.add(featureLoc);
}
Collections.sort(cdsFeatureLocs, new LocationComarator());
/**
* Get the sequence for a feature.
* @return the resulting buffer
*/
public PartialSequence getChadoSequence(final String uniqueName)
{
Feature feature = getDAOOnly().getResiduesByUniqueName(uniqueName);
char[] c = getChars(feature.getResidues());
PartialSequence ps = new PartialSequence(c, feature.getSeqLen(),
feature.getFeatureLoc().getFmin().intValue()+1,
feature.getFeatureLoc().getStrand(),
feature.getFeatureLoc().getPhase());
return ps;
}
/**
* Convert byte array to char array
* @param b byte array
* @return char array
*/
private char[] getChars(final byte b[])
{
char[] c = new char[b.length];
for(int i = 0; i < b.length; i++)
c[i] = (char)b[i];
/**
* Get the <code>List</code> of available schemas.
* @return the <code>List</code> of available schemas
*/
public List getSchema()
{
return schema_list;
}
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
public Feature getFeatureByUniquename(final String uniqueName)
{
GmodDAO dao = getDAOOnly();
List features = dao.getFeaturesByUniqueName(uniqueName);
if(features == null || features.size() < 1)
return null;
return (Feature)(dao.getFeaturesByUniqueName(uniqueName).get(0));
}
/**
* Given a gene unique name return the poplypeptide chado features that belong
* to that gene
* @param geneName
* @return
*/
public Vector getPolypeptideFeatures(final String geneName)
{
Feature geneFeature = getFeatureByUniquename(geneName);
if(geneFeature == null)
return null;
Collection frs = geneFeature.getFeatureRelationshipsForObjectId();
Iterator it = frs.iterator();
List transcripts = new Vector(frs.size());
while(it.hasNext())
{
FeatureRelationship fr = (FeatureRelationship)it.next();
transcripts.add(fr.getFeatureBySubjectId());
}
Vector polypep = new Vector();
for(int i=0; i<transcripts.size(); i++)
{
org.gmod.schema.sequence.Feature transcript =
(org.gmod.schema.sequence.Feature) transcripts.get(i);
frs = transcript.getFeatureRelationshipsForObjectId();
it = frs.iterator();
while(it.hasNext())
{
FeatureRelationship fr = (FeatureRelationship)it.next();
if(fr.getCvTerm().getName().equalsIgnoreCase("derives_from"))
if(fr.getFeatureBySubjectId().getCvTerm().getName().equalsIgnoreCase("polypeptide"))
polypep.add(fr.getFeatureBySubjectId());
}
}
return polypep;
}
/**
* Given a gene unique name return the poplypeptides that belong
* to that gene
* @param geneName
* @return
*/
/*public Vector getPolypeptideNames(final String geneName)
{
Vector polypeptides = getPolypeptideFeatures(geneName);
Vector polypeptideNames = new Vector(polypeptides.size());
for(int i=0; i<polypeptides.size(); i++)
{
Feature feature = (Feature)polypeptides.get(i);
polypeptideNames.add(feature.getUniqueName());
}
return polypeptideNames;
public List getClustersByFeatureIds(final List featureIds)
{
GmodDAO dao = getDAOOnly();
return dao.getClustersByFeatureIds(featureIds);
}
public List getParentFeaturesByChildFeatureIds(final List subjectIds)
{
GmodDAO dao = getDAOOnly();
return dao.getParentFeaturesByChildFeatureIds(subjectIds);
}
public List getFeatureDbXRefsByFeatureId(final List featureIds)
{
GmodDAO dao = getDAOOnly();
return dao.getFeatureDbXRefsByFeatureId(featureIds);
}
* Used by SimilarityLazyQualifierValue.bulkRetrieve() to get the match features
* @param featureIds the <code>List</code> of feature_id's
* @return the corresponding features
*/
public List getFeaturesByListOfIds(final List featureIds)
{
GmodDAO dao = getDAOOnly();
return dao.getFeaturesByListOfIds(featureIds);
}
public List getFeaturePropByFeatureIds(final List featureIds)
{
GmodDAO dao = getDAOOnly();
return dao.getFeaturePropByFeatureIds(featureIds);
}
public List getSimilarityMatches(List featureIds)
GmodDAO dao = getDAOOnly();
if(featureIds == null)
return dao.getSimilarityMatches(new Integer(srcFeatureId));
else
return dao.getSimilarityMatchesByFeatureIds(featureIds);
public List getFeatureLocsByListOfIds(List featureIds)
{
GmodDAO dao = getDAOOnly();
return dao.getFeatureLocsByListOfIds(featureIds);
}
/**
* Create a hashtable of the available entries with residues.
* @return a <code>Hashtable</code> of the <code>String</code>
* representation (schema-type-feature_name) and the
* corresponding feature_id
* @throws ConnectException
* @throws java.sql.SQLException
*/
throws ConnectException, java.sql.SQLException
{
String schema = null;
schema = (String)it.next();
Iterator it_residue_features = list_residue_features.iterator();
while(it_residue_features.hasNext())
{
Feature feature = (Feature)it_residue_features.next();
String typeName = getCvtermName(feature.getCvTerm().getCvTermId(), getDAO(), gene_builder);
db.put(schema + " - " + typeName + " - " + feature.getUniqueName(),
Integer.toString(feature.getFeatureId()));
}
}
catch(RuntimeException e){}
catch(java.sql.SQLException sqlExp){}
}
}
catch(RuntimeException sqlExp)
{
JOptionPane.showMessageDialog(null, "SQL Problems...\n"+
sqlExp.getMessage(),
"SQL Error",
JOptionPane.ERROR_MESSAGE);
logger4j.debug(sqlExp.getMessage());
//sqlExp.printStackTrace();
}
catch(ConnectException exp)
{
JOptionPane.showMessageDialog(null, "Connection Problems...\n"+
exp.getMessage(),
"Connection Error",
JOptionPane.ERROR_MESSAGE);
throw exp;
}
catch(java.sql.SQLException sqlExp)
{
JOptionPane.showMessageDialog(null, "SQL Problems....\n"+
sqlExp.getMessage(),
"SQL Error",
JOptionPane.ERROR_MESSAGE);
logger4j.debug(sqlExp.getMessage());
throw sqlExp;
}
return db;
}
/**
* Create a hashtable of the available entries with residues.
* @return a <code>Hashtable</code> of the <code>String</code>
* representation (schema-type-feature_name) and the
* corresponding feature_id
* @throws ConnectException
* @throws java.sql.SQLException
*/
throws ConnectException, java.sql.SQLException
{
CvTermThread cvThread = new CvTermThread(dao);
cvThread.start();
/*Organism org = new Organism();
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
final List pg_schemas = dao.getSchema();
// build a lookup hash of residue features in the
// main schema
Hashtable residueFeaturesLookup = new Hashtable();
List list_residue_features = dao.getResidueFeatures();
for(int i=0; i<list_residue_features.size(); i++)
{
Feature feature = (Feature)list_residue_features.get(i);
Integer organismId = new Integer(feature.getOrganism().getOrganismId());
List features;
if(residueFeaturesLookup.containsKey(organismId))
features= (List)residueFeaturesLookup.get(organismId);
else
features = new Vector();
features.add(feature);
residueFeaturesLookup.put(organismId, features);
}
// loop over organisms to identify those with features
// containing residues
final Organism organism = (Organism)it.next();
String orgName = organism.getCommonName();
if(orgName == null || orgName.equals(""))
{
orgName = organism.getGenus() + "." + organism.getSpecies();
organism.setCommonName(orgName);
}
// search to see if this is in its own schema
Iterator schemasIt = pg_schemas.iterator();
while(schemasIt.hasNext())
{
if( schema.equalsIgnoreCase(organism.getCommonName()) )
{
reset((String)getLocation(), schema);
dao = getDAO();
orgName = schema;
{
Integer organismId = new Integer(organism.getOrganismId());
if(residueFeaturesLookup.containsKey(organismId))
list_residue_features = (List)residueFeaturesLookup.get(organismId);
else if(singleSchema && residueFeaturesLookup.size()>0)
continue;
else
list_residue_features = dao.getResidueFeatures(organismId);
}
list_residue_features =
dao.getResidueFeaturesByOrganismCommonName(organism.getCommonName());
Iterator it_residue_features = list_residue_features.iterator();
while(it_residue_features.hasNext())
{
final Feature feature = (Feature)it_residue_features.next();
final String typeName = feature.getCvTerm().getName();
if(organismNames == null)
organismNames = new Vector();
if(!organismNames.contains(orgName))
organismNames.add(orgName);
db.put(orgName + " - " + typeName + " - " + feature.getUniqueName(),
Integer.toString(feature.getFeatureId()));
}
}
catch(RuntimeException e)
{
e.printStackTrace();
}
}
// now wait for cvterm to be loaded
while(cvThread.isAlive())
Thread.sleep(10);
}
catch(RuntimeException sqlExp)
{
JOptionPane.showMessageDialog(null, "SQL Problems...\n"+
sqlExp.getMessage(),
"SQL Error",
JOptionPane.ERROR_MESSAGE);
logger4j.debug(sqlExp.getMessage());
//sqlExp.printStackTrace();
}
catch(ConnectException exp)
{
JOptionPane.showMessageDialog(null, "Connection Problems...\n"+
exp.getMessage(),
"Connection Error",
JOptionPane.ERROR_MESSAGE);
logger4j.debug(exp.getMessage());
throw exp;
}
catch(java.sql.SQLException sqlExp)
{
JOptionPane.showMessageDialog(null, "SQL Problems....\n"+
sqlExp.getMessage(),
"SQL Error",
JOptionPane.ERROR_MESSAGE);
catch(InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
*
*/
public void showCvTermLookUp()
{
try
{
new ChadoCvTermView( getDAO() );
}
catch(ConnectException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch(SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Get the data access object (DAO).
* @return data access object
*/
if(!iBatis)
{
if(jdbcDAO == null)
jdbcDAO = new JdbcDAO((String)getLocation(), pfield);
return jdbcDAO;
}
else
{
System.setProperty("chado", (String)getLocation());
/**
* Get the username for this connection
* @return
*/
public String getUserName()
{
// "localhost:10001/backup?chado"
String url = (String)getLocation();
int index = url.indexOf("?");
String userName = url.substring(index+1).trim();
if(userName.startsWith("user="))
userName = userName.substring(5);
return userName;
}
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
private GmodDAO getDAOOnly()
{
GmodDAO dao = null;
try
{
dao = getDAO();
}
catch(RuntimeException sqlExp)
{
JOptionPane.showMessageDialog(null, "SQL Problems...\n"+
sqlExp.getMessage(),
"SQL Error",
JOptionPane.ERROR_MESSAGE);
}
catch(ConnectException exp)
{
JOptionPane.showMessageDialog(null, "Connection Problems...\n"+
exp.getMessage(),
"Connection Error",
JOptionPane.ERROR_MESSAGE);
}
catch(java.sql.SQLException sqlExp)
{
JOptionPane.showMessageDialog(null, "SQL Problems....\n"+
sqlExp.getMessage(),
"SQL Error",
JOptionPane.ERROR_MESSAGE);
}
return dao;
}
* Create a new OutputStream object from this Document. The contents of the
* Document can be written from the stream.
*
* @exception IOException
* Thrown if the Document can't be written.
*/
public OutputStream getOutputStream() throws IOException
{
final File write_file = new File(System.getProperty("user.dir")+
System.getProperty("file.separator")+
getName());
final FileOutputStream file_output_stream =
new FileOutputStream(write_file);
if(write_file.getName().endsWith(".gz"))
{
// assume this file should be gzipped
return new java.util.zip.GZIPOutputStream (file_output_stream);
}
else
return file_output_stream;
/**
* Commit the <code>ChadoTransaction</code> SQL back to the
* database.
* @param sql the collection of <code>ChadoTransaction</code> objects
* @return
*/
public int commit(final Vector sql,
final boolean force)
final Hashtable featureIdStore = new Hashtable();
boolean useTransactions = false;
if(!force && dao instanceof IBatisDAO)
useTransactions = true;
((IBatisDAO) dao).startTransaction();
logger4j.debug("START TRANSACTION");
}
boolean unchanged;
//
// check feature timestamps have not changed
Vector names_checked = new Vector();
final Object uniquenames[] = getUniqueNames(tsn);
for(int j=0; j<uniquenames.length; j++)
final String uniquename = (String) uniquenames[j];
if(uniquename == null || names_checked.contains(uniquename))
continue;
names_checked.add(uniquename);
final String keyName = tsn.getFeatureKey();
unchanged = checkFeatureTimestamp(schema, uniquename,
if(!unchanged)
{
if(useTransactions)
((IBatisDAO) dao).endTransaction();
return 0;
}
final Timestamp ts = new Timestamp(new java.util.Date().getTime());
//
// commit to database
for(ncommit = 0; ncommit < sql.size(); ncommit++)
try
{
ChadoTransaction tsn = (ChadoTransaction) sql.get(ncommit);
}
catch (RuntimeException re)
{
if(!force)
throw re;
logger4j.warn(constructExceptionMessage(re, sql, ncommit));
logger4j.warn("NOW TRYING TO CONTINUE TO COMMIT");
}
//
// update timelastmodified timestamp
names_checked = new Vector();
final ChadoTransaction tsn = (ChadoTransaction) sql.get(i);
final Object uniquenames[] = getUniqueNames(tsn);
if(uniquenames == null)
if(tsn.getType() == ChadoTransaction.UPDATE &&
tsn.getFeatureObject() instanceof Feature)
{
for(int j=0; j<uniquenames.length; j++)
names_checked.add((String) uniquenames[j]);
continue;
}
for(int j=0; j<uniquenames.length; j++)
{
final String uniquename = (String) uniquenames[j];
if(uniquename == null || names_checked.contains(uniquename))
continue;
// retieve from featureId store
if(featureIdStore != null && featureIdStore.containsKey(uniquename))
{
Feature f = (Feature) featureIdStore.get(uniquename);
}
else
feature = dao.getFeatureByUniqueName(uniquename,
tsn.getFeatureKey());
if(feature != null)
{
feature.setTimeLastModified(ts);
feature.setName("0"); // do not change name
}
GFFStreamFeature gff_feature = (GFFStreamFeature) tsn.getGff_feature();
gff_feature.setLastModified(ts);
final String nocommit = System.getProperty("nocommit");
if( useTransactions &&
(nocommit == null || nocommit.equals("false")))
logger4j.debug("TRANSACTION COMPLETE");
else if(useTransactions &&
(nocommit != null && nocommit.equals("true")))
logger4j.debug("TRANSACTION NOT COMMITTED : nocommit property set to true");
JOptionPane.showMessageDialog(null, "Problems Writing...\n" +
JOptionPane.showMessageDialog(null, "Problems connecting..."+
conn_ex.getMessage(),
"Database Connection Error - Check Server",
JOptionPane.ERROR_MESSAGE);
catch (RuntimeException re)
{
final String msg = constructExceptionMessage(re, sql, ncommit);
JOptionPane.showMessageDialog(null, msg,
"Problems Writing to Database ",
JOptionPane.ERROR_MESSAGE);
logger4j.error(msg);
}
finally
{
if(useTransactions)
try
{
((IBatisDAO) dao).endTransaction();
catch(SQLException e){ e.printStackTrace(); }
if(featureIdStore != null)
featureIdStore.clear();
return ncommit;
}
/**
* Get the uniquenames involved in a transaction
* @param tsn
* @return
*/
private Object[] getUniqueNames(final ChadoTransaction tsn)
{
if(tsn.getGff_feature() == null)
return null;
if(tsn.getGff_feature().getSegmentRangeStore() == null ||
tsn.getGff_feature().getSegmentRangeStore().size() < 2 ||
tsn.getFeatureObject() instanceof FeatureProp)
return new Object[]{ tsn.getUniquename() };
else
return tsn.getGff_feature().getSegmentRangeStore().keySet().toArray();
}
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
/**
* Construct an exeption message from the ChadoTransaction
* @param re
* @param sql
* @param ncommit
* @return
*/
private String constructExceptionMessage(final RuntimeException re,
final Vector sql,
final int ncommit)
{
String msg = "";
if(ncommit > -1 && ncommit < sql.size())
{
final ChadoTransaction t_failed = (ChadoTransaction)sql.get(ncommit);
if(t_failed.getType() == ChadoTransaction.DELETE)
msg = "DELETE failed ";
else if(t_failed.getType() == ChadoTransaction.INSERT)
msg = "INSERT failed ";
else if(t_failed.getType() == ChadoTransaction.UPDATE)
msg = "UPDATE failed ";
if(t_failed.getUniquename() != null)
msg = msg + "for " + t_failed.getUniquename()+":";
else if(t_failed.getFeatureObject() != null &&
t_failed.getFeatureObject() instanceof Feature)
{
final Feature chadoFeature = (Feature)t_failed.getFeatureObject();
if(chadoFeature.getUniqueName() != null)
msg = msg + "for " + chadoFeature.getUniqueName() +":";
}
msg = msg+"\n";
}
return msg + re.getMessage();
}
/**
* Commit a single chado transaction
* @param tsn
* @param dao
*/
private void commitChadoTransaction(final ChadoTransaction tsn,
{
if(tsn.getType() == ChadoTransaction.UPDATE)
{
if(tsn.getFeatureObject() instanceof Feature)
{
Feature feature = (Feature)tsn.getFeatureObject();
if(feature.getUniqueName() != null)
{
final String uniquename;
if(tsn.getOldUniquename() != null)
uniquename = (String)tsn.getOldUniquename();
else
uniquename = feature.getUniqueName();
Feature old_feature
= dao.getFeatureByUniqueName(uniquename, tsn.getFeatureKey());
if(old_feature != null)
feature.setFeatureId( old_feature.getFeatureId() );
tsn.setOldUniquename(feature.getUniqueName());
}
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
}
dao.merge(tsn.getFeatureObject());
//dao.updateAttributes(tsn);
}
else if(tsn.getType() == ChadoTransaction.INSERT)
{
if(tsn.getFeatureObject() instanceof FeatureCvTerm)
ArtemisUtils.inserFeatureCvTerm(dao, (FeatureCvTerm)tsn.getFeatureObject());
else
{
// set srcfeature_id
if(tsn.getFeatureObject() instanceof Feature &&
((Feature) tsn.getFeatureObject()).getFeatureLoc() != null)
{
FeatureLoc featureloc = ((Feature) tsn.getFeatureObject()).getFeatureLoc();
Feature featureBySrcFeatureId = new Feature();
featureBySrcFeatureId.setFeatureId(Integer.parseInt(srcFeatureId));
featureloc.setFeatureBySrcFeatureId(featureBySrcFeatureId);
}
dao.persist(tsn.getFeatureObject());
}
}
else if(tsn.getType() == ChadoTransaction.DELETE)
{
if(tsn.getFeatureObject() instanceof FeatureCvTerm)
ArtemisUtils.deleteFeatureCvTerm(dao, (FeatureCvTerm)tsn.getFeatureObject());
else
dao.delete(tsn.getFeatureObject());
}
/**
* Check the <code>Timestamp</code> on a feature (for versioning).
* @param schema the schema
* @param uniquename the feature uniquename
* @param timestamp the last read feature timestamp
*/
public boolean checkFeatureTimestamp(final String schema,
final GmodDAO dao,
final Hashtable featureIdStore,
final ChadoTransaction tsn)
final Timestamp timestamp = tsn.getLastModified();
final Object featureObject = tsn.getFeatureObject();
final Feature feature = dao.getFeatureByUniqueName(uniquename, keyName);
if(featureObject instanceof FeatureProp)
((FeatureProp)featureObject).setFeature(feature);
else if(featureObject instanceof FeatureLoc)
{
if(((FeatureLoc)featureObject).getFeatureByFeatureId().getUniqueName().equals(uniquename))
{
logger4j.debug("Setting featureId for:" + uniquename );
((FeatureLoc)featureObject).setFeatureByFeatureId(feature);
}
}
{
now.setNanos(0);
timestamp.setNanos(0);
if(now.compareTo(timestamp) != 0)
{
new SimpleDateFormat("dd.MM.yyyy hh:mm:ss z");
//System.out.println(date_format.format(now)+" "+
// date_format.format(timestamp));
int select = JOptionPane.showConfirmDialog(null, uniquename +
date_format.format(now)+"\nOverwite?",
"Feature Changed",
JOptionPane.OK_CANCEL_OPTION);
if(select == JOptionPane.OK_OPTION)
return true;
else
return false;
public static void main(String args[])
{
try
{
DatabaseEntrySource src = new DatabaseEntrySource();
src.setLocation(true);
if(System.getProperty("ibatis") == null)
dao = new JdbcDAO(src.getLocation(), src.getPfield());
else
dao = new IBatisDAO(src.getPfield());
List schemas = new Vector();
schemas.add(args[1]);
featureList.add(dao.getFeatureByUniqueName(args[0], "polypeptide"));
int fmin = feature.getFeatureLoc().getFmin().intValue() + 1;
int fmax = feature.getFeatureLoc().getFmax().intValue();
((FeatureProp)(new Vector(feature.getFeatureProps()).get(0))).getCvTerm().getName();
System.out.print(" "+feature.getFeatureLoc().getStrand());
System.out.println(" "+Integer.toString(feature.getFeatureId()));
/* Hashtable synonyms = getAllFeatureSynonyms(dao, null);
Vector syns = (Vector)synonyms.get(new Integer(feature.getId()));
for(int j=0; j<syns.size(); j++)
{
FeatureSynonym alias = (FeatureSynonym)syns.get(j);