diff --git a/uk/ac/sanger/artemis/chado/ChadoTransaction.java b/uk/ac/sanger/artemis/chado/ChadoTransaction.java index 041287e8629a98cf145408c5ea048d97526737ca..7b365bc7ecbaa5a61813acbfd539eb7ee7ff90ab 100644 --- a/uk/ac/sanger/artemis/chado/ChadoTransaction.java +++ b/uk/ac/sanger/artemis/chado/ChadoTransaction.java @@ -55,6 +55,8 @@ public class ChadoTransaction private Timestamp lastmodified; /** the feature object */ private Object feature_obj; + /** the feature key */ + private String featureKey; private GFFStreamFeature gff_feature; @@ -71,12 +73,14 @@ public class ChadoTransaction public ChadoTransaction(final int type, final Object feature_obj, final Timestamp lastmodified, - final GFFStreamFeature gff_feature) + final GFFStreamFeature gff_feature, + final String featureKey) { this.type = type; this.lastmodified = lastmodified; this.feature_obj = feature_obj; this.gff_feature = gff_feature; + this.featureKey = featureKey; } @@ -162,4 +166,10 @@ public class ChadoTransaction this.gff_feature = gff_feature; } + + public String getFeatureKey() + { + return featureKey; + } + } diff --git a/uk/ac/sanger/artemis/chado/ChadoTransactionManager.java b/uk/ac/sanger/artemis/chado/ChadoTransactionManager.java index dbaebbe81987cc2c8ee36c9532deb90df60758ed..458c9782a2fbdb8da67a8b72a6784721a28b60c2 100644 --- a/uk/ac/sanger/artemis/chado/ChadoTransactionManager.java +++ b/uk/ac/sanger/artemis/chado/ChadoTransactionManager.java @@ -136,49 +136,6 @@ public class ChadoTransactionManager { int start = event.getPosition(); int length = event.getSubSequence().length(); - - FeatureVector features = entryGroup.getAllFeatures(); - - for(int i=0; i<features.size(); i++) - { - Feature feature = features.elementAt(i); - - if(feature.getLocation().getFirstBase() >= start) - { - GFFStreamFeature gffFeature = (GFFStreamFeature)feature.getEmblFeature(); - FeatureSegmentVector segments = feature.getSegments(); - - for(int j=0; j<segments.size(); j++) - { - FeatureSegment segment = segments.elementAt(j); - String seg_id = gffFeature.getSegmentID( segment.getRawRange() ); - Range range_new; - try - { - if(event.getType() == SequenceChangeEvent.DELETION) - range_new = new Range(feature.getLocation().getFirstBase()-length, - feature.getLocation().getLastBase()-length); - else - range_new = new Range(feature.getLocation().getFirstBase()+length, - feature.getLocation().getLastBase()+length); - - // featureloc update - FeatureLoc featureloc = getFeatureLoc(gffFeature, seg_id, range_new); - ChadoTransaction tsn = new ChadoTransaction(ChadoTransaction.UPDATE, - featureloc, - gffFeature.getLastModified(), gffFeature); - - sql.add(tsn); - - } - catch(OutOfRangeException e) - { - e.printStackTrace(); - } - - } - } - } // // update residues in srcfeature @@ -187,6 +144,7 @@ public class ChadoTransactionManager FeatureForUpdatingResidues chadoFeature = new FeatureForUpdatingResidues(); chadoFeature.setStartBase(start-1); + chadoFeature.setLength(length); int newSequenceLength = entryGroup.getSequenceEntry().getEMBLEntry().getSequence().length(); @@ -207,7 +165,7 @@ public class ChadoTransactionManager entryGroup.getSequenceEntry().getEMBLEntry().getSequence().length())); ChadoTransaction tsn = - new ChadoTransaction(ChadoTransaction.UPDATE, chadoFeature, null, null); + new ChadoTransaction(ChadoTransaction.UPDATE, chadoFeature, null, null, null); sql.add(tsn); } else @@ -259,7 +217,7 @@ public class ChadoTransactionManager ideleted = ((Integer)deleted.elementAt(i)).intValue(); Range range_old = (Range)rv_old.elementAt(ideleted); String seg_id = feature.getSegmentID(range_old); - deleteFeature(seg_id); + deleteFeature(seg_id, feature.getKey().getKeyString()); feature.getSegmentRangeStore().remove(seg_id); } @@ -392,7 +350,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.UPDATE, featureloc, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); sql.add(tsn); } @@ -467,20 +426,20 @@ public class ChadoTransactionManager (String)(qualifier_uniquename.getValues()).elementAt(0); Splash.logger4j.debug("FEATURE_DELETED "+feature_uniquename); + GFFStreamFeature gff_feature = + (GFFStreamFeature)event.getFeature().getEmblFeature(); if(event.getFeature().getSegments().size() > 1) { - GFFStreamFeature gff_feature = - (GFFStreamFeature)event.getFeature().getEmblFeature(); RangeVector ranges = gff_feature.getLocation().getRanges(); for(int i=0; i<ranges.size(); i++) { Range range = (Range)ranges.get(i); feature_uniquename = gff_feature.getSegmentID(range); - deleteFeature(feature_uniquename); + deleteFeature(feature_uniquename, gff_feature.getKey().getKeyString()); } } else - deleteFeature(feature_uniquename); + deleteFeature(feature_uniquename, gff_feature.getKey().getKeyString()); } catch(InvalidRelationException e) { @@ -538,7 +497,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.UPDATE, feature_relationship, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); sql.add(tsn); } } @@ -665,7 +625,7 @@ public class ChadoTransactionManager ChadoTransaction tsn = new ChadoTransaction(ChadoTransaction.INSERT, chado_feature, - null, (GFFStreamFeature)null); + null, (GFFStreamFeature)null, null); sql.add(tsn); } @@ -775,7 +735,7 @@ public class ChadoTransactionManager ChadoTransaction tsn = new ChadoTransaction(ChadoTransaction.INSERT, chado_feature, - null, (GFFStreamFeature)null); + null, (GFFStreamFeature)null, null); sql.add(tsn); } @@ -783,7 +743,7 @@ public class ChadoTransactionManager /** * Set the transaction for deleting a feature. */ - private void deleteFeature(final String uniquename) + private void deleteFeature(final String uniquename, final String featureType) { org.gmod.schema.sequence.Feature chado_feature = new org.gmod.schema.sequence.Feature(); @@ -791,7 +751,7 @@ public class ChadoTransactionManager ChadoTransaction tsn = new ChadoTransaction(ChadoTransaction.DELETE, chado_feature, - null, (GFFStreamFeature)null); + null, (GFFStreamFeature)null, featureType); sql.add(tsn); } @@ -950,7 +910,7 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.UPDATE, chado_feature, - feature.getLastModified(), feature); + feature.getLastModified(), feature, null); sql.add(tsn); } @@ -1122,7 +1082,7 @@ public class ChadoTransactionManager lcvterm_id, rank); tsn = new ChadoTransaction(type, featureprop, - feature.getLastModified(), feature); + feature.getLastModified(), feature, feature.getKey().getKeyString()); tsn.setUniquename(uniquename); sql.add(tsn); @@ -1135,7 +1095,7 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(type, featureprop, - feature.getLastModified(), feature); + feature.getLastModified(), feature, feature.getKey().getKeyString()); sql.add(tsn); } } @@ -1187,7 +1147,7 @@ public class ChadoTransactionManager (String)old_qualifier.getValues().get(0)); ChadoTransaction tsn = new ChadoTransaction(ChadoTransaction.UPDATE, chado_feature, - feature.getLastModified(), feature); + feature.getLastModified(), feature, feature.getKey().getKeyString()); tsn.setOldUniquename( (String)old_qualifier.getValues().get(0) ); sql.add(tsn); @@ -1216,7 +1176,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.DELETE, old_dbxref, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); sql.add(tsn); } else if(qualifier_name.equals("codon_start")) @@ -1228,7 +1189,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.UPDATE, featureloc, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); sql.add(tsn); } else if(isCvTag(qualifier_name)) @@ -1266,7 +1228,8 @@ public class ChadoTransactionManager uniquename); tsn = new ChadoTransaction(ChadoTransaction.DELETE, feature_cvterm, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); sql.add(tsn); } else if(isSynonymTag(qualifier_name)) @@ -1279,7 +1242,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.DELETE, feature_synonym, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); sql.add(tsn); } @@ -1307,7 +1271,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.INSERT, new_dbxref, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); } else if(qualifier_name.equals("codon_start")) { @@ -1317,7 +1282,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.UPDATE, featureloc, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); //sql.add(tsn); } else if(isCvTag(qualifier_name)) @@ -1328,7 +1294,8 @@ public class ChadoTransactionManager qualifier_string, uniquename); tsn = new ChadoTransaction(ChadoTransaction.INSERT, feature_cvterm, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); } else if(isSynonymTag(qualifier_name)) { @@ -1340,7 +1307,8 @@ public class ChadoTransactionManager tsn = new ChadoTransaction(ChadoTransaction.INSERT, feature_synonym, - feature.getLastModified(), feature); + feature.getLastModified(), feature, + feature.getKey().getKeyString()); } sql.add(tsn); } diff --git a/uk/ac/sanger/artemis/chado/IBatisDAO.java b/uk/ac/sanger/artemis/chado/IBatisDAO.java index 4eafc56416b7189fdcc1629a73ef0a13c3de882e..6e6f7c2b1d4cb100f2bd367a8f1fdb7191ceb844 100644 --- a/uk/ac/sanger/artemis/chado/IBatisDAO.java +++ b/uk/ac/sanger/artemis/chado/IBatisDAO.java @@ -93,15 +93,28 @@ public class IBatisDAO extends GmodDAO org.gmod.schema.sequence.Feature feature = new org.gmod.schema.sequence.Feature(); feature.setFeatureId(id); - return getLazyFeature(feature); + return (Feature)sqlMap.queryForObject("getLazyFeature", feature); + } + + public List getFeaturesByUniqueName(String uniquename) + { + org.gmod.schema.sequence.Feature feature = + new org.gmod.schema.sequence.Feature(); + feature.setUniqueName(uniquename); + return sqlMap.queryForList("getLazyFeature", feature); } - public Feature getFeatureByUniqueName(String uniquename) + public Feature getFeatureByUniqueName(String uniquename, String featureType) { org.gmod.schema.sequence.Feature feature = new org.gmod.schema.sequence.Feature(); feature.setUniqueName(uniquename); - return getLazyFeature(feature); + + CvTerm cvTerm = new CvTerm(); + cvTerm.setName(featureType); + feature.setCvTerm(cvTerm); + + return (Feature)sqlMap.queryForObject("getLazyFeature", feature); } @@ -362,21 +375,7 @@ public class IBatisDAO extends GmodDAO { return null; } - - ////// - ////// - - - /** - * Get the properties of a feature. - * @param uniquename the unique name of the feature - * @return the <code>List</code> of <code>Feature</code> - */ - private Feature getLazyFeature( - final org.gmod.schema.sequence.Feature feature) - { - return (Feature)sqlMap.queryForObject("getLazyFeature", feature); - } + ////// ////// SchemaDaoI @@ -494,7 +493,10 @@ public class IBatisDAO extends GmodDAO else if(o instanceof Feature) { if(o instanceof FeatureForUpdatingResidues) + { + sqlMap.update("updateFeatureLocByChangingSequence", o); sqlMap.update("updateFeatureResidues", o); + } else sqlMap.update("updateFeature", o); } @@ -605,9 +607,11 @@ public class IBatisDAO extends GmodDAO feature_dbxref.setDbXRef(dbXRef); // get the feature id's - Feature feature = (Feature)getFeatureByUniqueName( + List features = getFeaturesByUniqueName( feature_dbxref.getFeature().getUniqueName()); - feature_dbxref.getFeature().setFeatureId( feature.getFeatureId() ); + + feature_dbxref.getFeature().setFeatureId( + ((Feature)features.get(0)).getFeatureId() ); sqlMap.insert("insertFeatureDbXRef", feature_dbxref); } @@ -693,7 +697,16 @@ public class IBatisDAO extends GmodDAO protected Integer getDbId(Db db) { - return (Integer)sqlMap.queryForObject("getDbId", db); + Integer dbId = (Integer)sqlMap.queryForObject("getDbId", db); + + if(dbId == null) + { + List dbIds = sqlMap.queryForList("getDbIdIgnoreCase", db); + if(dbIds.size() > 0) + dbId = (Integer)dbIds.get(0); + } + + return dbId; } protected Integer getDbXRefId(DbXRef dbXRef) diff --git a/uk/ac/sanger/artemis/chado/JdbcDAO.java b/uk/ac/sanger/artemis/chado/JdbcDAO.java index 5d37e69e172ab5b5e58141eb22d348aad12bb03b..b17e8cc29cd3c07ce241d8ea041db642dffd94d3 100644 --- a/uk/ac/sanger/artemis/chado/JdbcDAO.java +++ b/uk/ac/sanger/artemis/chado/JdbcDAO.java @@ -109,14 +109,30 @@ public class JdbcDAO extends GmodDAO * @param name the systematic id * @return the Feature, or null */ - public Feature getFeatureByUniqueName(String uniquename) + public Feature getFeatureByUniqueName(final String uniquename, final String featureType) { Feature feature = new Feature(); feature.setUniqueName(uniquename); feature.setFeatureId(-1); + + CvTerm cvTerm = new CvTerm(); + cvTerm.setName(featureType); + feature.setCvTerm(cvTerm); + return getLazyFeature(feature); } + /** + * Return a features with this systematic id + * + * @param name the systematic id + * @return the Feature, or null + */ + public List getFeaturesByUniqueName(String uniquename) + { + return getFeatureQuery(uniquename, -1, -1, null); + } + /** * Return a list of features with any current (ie non-obsolete) name or synonym * @@ -131,7 +147,7 @@ public class JdbcDAO extends GmodDAO // getFeatureSynonymsByName() needs implementing //List feature_synonym_list = getFeatureSynonymsByName(); - return getFeatureQuery(name, -1, -1); + return getFeatureQuery(name, -1, -1, null); } /** @@ -172,7 +188,7 @@ public class JdbcDAO extends GmodDAO public List getFeaturesByLocatedOnFeature(final Feature feature) { return getFeatureQuery(null, - feature.getFeatureLoc().getFeatureBySrcFeatureId().getFeatureId(), -1); + feature.getFeatureLoc().getFeatureBySrcFeatureId().getFeatureId(), -1, null); } /** @@ -727,7 +743,8 @@ public class JdbcDAO extends GmodDAO private Feature getLazyFeature(final Feature feature) { List list = getFeatureQuery(feature.getUniqueName(), - -1, feature.getFeatureId()); + -1, feature.getFeatureId(), + feature.getCvTerm()); if(list == null || list.size() < 1) return null; @@ -742,7 +759,8 @@ public class JdbcDAO extends GmodDAO */ private List getFeatureQuery(final String uniquename, final int parentFeatureID, - final int feature_id) + final int feature_id, + final CvTerm cvTerm) { final List list = new Vector(); try @@ -759,8 +777,12 @@ public class JdbcDAO extends GmodDAO + "fr.subject_id=" + "f.feature_id" + " LEFT JOIN featureprop fp ON " + "fp.feature_id=" + "f.feature_id" + " LEFT JOIN featureloc fl ON " + "f.feature_id=" + "fl.feature_id" - + " LEFT JOIN organism ON organism.organism_id=f.organism_id" - + " WHERE "; + + " LEFT JOIN organism ON organism.organism_id=f.organism_id "; + + if(cvTerm != null && cvTerm.getName() != null) + sql = sql + "LEFT JOIN cvterm ON f.type_id=cvterm.cvterm_id "; + + sql = sql + " WHERE "; if(uniquename != null) sql = sql + "uniquename LIKE '" + uniquename + "'"; @@ -771,7 +793,10 @@ public class JdbcDAO extends GmodDAO if(feature_id > -1) sql = sql + "f.feature_id = " + feature_id; - sql = sql // + " AND (fl.rank=fr.rank OR fr.rank IS NULL)" + if(cvTerm != null && cvTerm.getName() != null) + sql = sql + " AND cvterm.name="+cvTerm.getName(); + + sql = sql + " ORDER BY f.type_id, uniquename"; appendToLogFile(sql, sqlLog); @@ -1322,23 +1347,38 @@ public class JdbcDAO extends GmodDAO */ private void updateFeatureResidues(FeatureForUpdatingResidues feature) { - String sql = " UPDATE feature SET "+ - "residues=substring(residues from 1 for "+ feature.getStartBase() + " || "; + String sql1 = + "UPDATE featureloc SET "; + + if(feature.getNewSubSequence() != null) + sql1 = sql1 + "fmin=fmin+" + feature.getLength() + + " , fmax=fmax+" + feature.getLength(); + else + sql1 = sql1 + "fmin=fmin-" + feature.getLength() + + " , fmax=fmax-" + feature.getLength(); + + sql1 = sql1 + " WHERE fmin >= " + feature.getStartBase() + + " AND srcfeature_id="+feature.getFeatureId(); + appendToLogFile(sql1, sqlLog); + + String sql2 = " UPDATE feature SET "+ + "residues=substring(residues from 1 for "+ feature.getStartBase() + ") || "; if(feature.getNewSubSequence() != null) - sql = sql + feature.getNewSubSequence() + " || "; + sql2 = sql2 + "'" + feature.getNewSubSequence() + "' || "; - sql = sql + "substring(residues from "+ feature.getEndBase() + + sql2 = sql2 + "substring(residues from "+ feature.getEndBase() + " for "+ feature.getSeqLen() + "), "+ "seqlen=" + feature.getSeqLen() + " WHERE feature_id="+feature.getFeatureId(); - appendToLogFile(sql, sqlLog); + appendToLogFile(sql2, sqlLog); try { Statement st = conn.createStatement(); - st.executeUpdate(sql); + st.executeUpdate(sql1); + st.executeUpdate(sql2); } catch(SQLException sqle) { diff --git a/uk/ac/sanger/artemis/util/DatabaseDocument.java b/uk/ac/sanger/artemis/util/DatabaseDocument.java index af8f764168f4e0551a690a528ee29529e83c7b87..ba7e4311415a37eb064d093f5540551d07877d37 100644 --- a/uk/ac/sanger/artemis/util/DatabaseDocument.java +++ b/uk/ac/sanger/artemis/util/DatabaseDocument.java @@ -680,7 +680,7 @@ public class DatabaseDocument extends Document reset((String)getLocation(), (String)schema_search.get(0)); dao = getDAO(); Feature feature = - (Feature)dao.getFeatureByUniqueName(search_gene); + (Feature)(dao.getFeaturesByUniqueName(search_gene).get(0)); ChadoCanonicalGene chado_gene = new ChadoCanonicalGene(); id_store.put(Integer.toString(feature.getFeatureId()), feature.getUniqueName()); @@ -1596,6 +1596,8 @@ public class DatabaseDocument extends Document { GmodDAO dao = getDAO(); + if(dao instanceof IBatisDAO) + ((IBatisDAO) dao).startTransaction(); boolean unchanged; // @@ -1613,23 +1615,23 @@ public class DatabaseDocument extends Document if(uniquename == null) continue; + if(names_checked.contains(uniquename)) continue; - + names_checked.add(uniquename); + String keyName = tsn.getFeatureKey(); unchanged = checkFeatureTimestamp(schema, uniquename, - tsn.getLastModified(), dao); + tsn.getLastModified(), dao, + keyName, tsn.getFeatureObject()); if(!unchanged) return 0; } try { - if(dao instanceof IBatisDAO) - ((IBatisDAO) dao).startTransaction(); - // // commit to database for(i = 0; i < sql.size(); i++) @@ -1651,7 +1653,7 @@ public class DatabaseDocument extends Document uniquename = feature.getUniqueName(); Feature old_feature - = dao.getFeatureByUniqueName(uniquename); + = dao.getFeatureByUniqueName(uniquename, tsn.getFeatureKey()); if(old_feature != null) feature.setFeatureId( old_feature.getFeatureId() ); @@ -1711,7 +1713,7 @@ public class DatabaseDocument extends Document names_checked.add(uniquename); - Feature feature = dao.getFeatureByUniqueName(uniquename); + Feature feature = dao.getFeatureByUniqueName(uniquename, tsn.getFeatureKey()); if(feature != null) { feature.setTimeLastModified(ts); @@ -1763,11 +1765,19 @@ public class DatabaseDocument extends Document public boolean checkFeatureTimestamp(final String schema, final String uniquename, final Timestamp timestamp, - final GmodDAO dao) + final GmodDAO dao, + final String keyName, + final Object featureObject) { - Feature feature = dao.getFeatureByUniqueName(uniquename); + Feature feature = dao.getFeatureByUniqueName(uniquename, keyName); if(feature == null) return true; + + if(featureObject instanceof FeatureProp) + ((FeatureProp)featureObject).setFeature(feature); + else if(featureObject instanceof FeatureLoc) + ((FeatureLoc)featureObject).setFeatureByFeatureId(feature); + Timestamp now = feature.getTimeLastModified(); if(now != null && timestamp != null) @@ -1796,6 +1806,7 @@ public class DatabaseDocument extends Document return true; } + public static void main(String args[]) { try @@ -1814,7 +1825,7 @@ public class DatabaseDocument extends Document List schemas = new Vector(); schemas.add(args[1]); List featureList = new Vector(); - featureList.add(dao.getFeatureByUniqueName(args[0])); + featureList.add(dao.getFeatureByUniqueName(args[0], "polypeptide")); System.out.println("FINISHED getFeature()"); for(int i = 0; i < featureList.size(); i++) {