@Test public void checkReferenceMoleculeAndClassUsage() throws Exception { MySQLAdaptor dba = new MySQLAdaptor("reactomedev.oicr.on.ca", "gk_central", "authortool", "**REMOVED**"); // Collection<GKInstance> c = // dba.fetchInstancesByClass(ReactomeJavaConstants.ReferenceGroupCount); // System.out.println("Total ReferenceMolecule: " + c.size()); Collection<GKInstance> c1 = dba.fetchInstancesByClass(ReactomeJavaConstants.ReferenceMoleculeClass); System.out.println("Total ReferenceMoleculeClass: " + c1.size()); Set<GKInstance> all = new HashSet<GKInstance>(); // all.addAll(c); all.addAll(c1); System.out.println("ReferenceMolecule\tReferrers"); for (GKInstance inst : all) { Collection<?> refAtts = inst.getSchemClass().getReferers(); Set<GKInstance> allReferrers = new HashSet<GKInstance>(); for (Iterator<?> it1 = refAtts.iterator(); it1.hasNext(); ) { GKSchemaAttribute att = (GKSchemaAttribute) it1.next(); Collection<GKInstance> referrers = inst.getReferers(att); if (referrers != null) allReferrers.addAll(referrers); } if (allReferrers.size() > 0) { System.out.println(inst + "\t" + join(allReferrers)); } } }
/** * This method is used to dump all human pathway diagrams into a specified directory. * * @param dir * @throws Exception */ public void dumpHumanPathwayDiagrams(File dir) throws Exception { Collection<?> diagrams = adaptor.fetchInstancesByClass(ReactomeJavaConstants.PathwayDiagram); SchemaClass cls = adaptor.fetchSchema().getClassByName(ReactomeJavaConstants.PathwayDiagram); SchemaAttribute att = cls.getAttribute(ReactomeJavaConstants.representedPathway); adaptor.loadInstanceAttributeValues(diagrams, att); // Group all human pathways for (Iterator<?> it = diagrams.iterator(); it.hasNext(); ) { GKInstance diagram = (GKInstance) it.next(); GKInstance pathway = (GKInstance) diagram.getAttributeValue(ReactomeJavaConstants.representedPathway); GKInstance species = (GKInstance) pathway.getAttributeValue(ReactomeJavaConstants.species); if (species == null) continue; if (species.getDBID().equals(48887L)) { String fileName = getFileName(pathway); File file = new File(dir, fileName); convertReactomeToGPML(pathway, file.getAbsolutePath()); } } }
/** * Sets up lists of instances that are different between database and local instances. If * uncheckableList is null, then the "difference" between database and local will be used to * generate these lists. If uncheckableList contains something, then the contents of the list * (assumed to be of type GKInstance) will be taken instead. * * @param fileAdaptor * @param dbAdaptor * @param uncheckableList */ public void setAdaptors( XMLFileAdaptor fileAdaptor, MySQLAdaptor dbAdaptor, List uncheckableList) { try { this.fileAdaptor = fileAdaptor; this.dbAdaptor = dbAdaptor; typeMap = new HashMap(); if (uncheckableList != null) { // Create a "fake" syncMap if a list of instances // already exists. syncMap = new HashMap(); syncMap.put(CHANGED_KEY, uncheckableList); // Create the corresponding "fake" type map. This // is needed so that the correct actions are assigned // to these instances, e.g. "update from db". GKInstance uncheckableInstance; for (Iterator it = uncheckableList.iterator(); it.hasNext(); ) { uncheckableInstance = (GKInstance) it.next(); typeMap.put(uncheckableInstance, NEW_CHANGE_IN_DB_KEY); } } else // The usual case // Create a syncMap based on the discrepancies // between local and database instances. syncMap = synchronize(fileAdaptor, dbAdaptor); if (syncMap.size() == 0) isSame = true; else { isSame = false; JPanel mapPane = createDisplayMapPane(); getContentPane().add(mapPane, BorderLayout.CENTER); setTitle( "Synchronization Results vs. " + dbAdaptor.getDBName() + "@" + dbAdaptor.getDBHost()); } } catch (Exception e) { System.err.println("SynchronizationPane.setAdaptors(): " + e); e.printStackTrace(); } }
private void showComparison() { // Find the instance that can be used for comparision. This instance // should be chosen from changedList or localHasUnexpInstance GKInstance instance = null; if (changedList != null) { // Check changedList first List selection = changedList.getSelection(); if (selection.size() > 0) instance = (GKInstance) selection.get(0); } if (instance == null && localHasMoreIEList != null) { List selection = localHasMoreIEList.getSelection(); if (selection.size() > 0) instance = (GKInstance) selection.get(0); } if (instance == null) return; final InstanceComparisonPane comparisonPane = new InstanceComparisonPane(); // Fine tune comparison pane for making comparisons with // database comparisonPane.setSaveDialogHideUnusedButtons(true); comparisonPane.setSaveDialogSaveAsNewBtnFirst(false); comparisonPane.setSaveDialogSaveAsNewBtnTitle( "Create new local instance and put merge into that"); comparisonPane.setSaveDialogReplaceFirstBtnTitle( "Overwrite existing instance with merge (recommended)"); comparisonPane.setCloseAfterSaving(true); try { String clsName = instance.getSchemClass().getName(); Long instanceId = instance.getDBID(); final GKInstance localCopy = fileAdaptor.fetchInstance(clsName, instanceId); dbAdaptor.setUseCache(false); // final GKInstance dbCopy = dbAdaptor.fetchInstance(clsName, instanceId); // The class type might be changed locally or remotely. So not clsName should be used // to fetch database instance. Otherwise, null exception will be thrown. The same thing // is done also in creating synchronization result. final GKInstance dbCopy = dbAdaptor.fetchInstance(instanceId); dbAdaptor.setUseCache(true); comparisonPane.setInstances(localCopy, dbCopy); String title = "Comparing Instances \"" + instance.getDisplayName() + "\" in the local and DB repositories"; JDialog parentDialog = (JDialog) SwingUtilities.getAncestorOfClass(JDialog.class, centerPane); final JDialog dialog = new JDialog(parentDialog, title); // Try to update the list automatically dialog.addWindowListener( new WindowAdapter() { public void windowClosed(WindowEvent e) { handleMergeResult(localCopy, dbCopy, comparisonPane); // To prevent double calling. It is called again when parentDialog is closed. dialog.removeWindowListener(this); } }); dialog.getContentPane().add(comparisonPane, BorderLayout.CENTER); dialog.setModal(true); dialog.setSize(800, 600); GKApplicationUtilities.center(dialog); dialog.setVisible(true); // Update synchronization dialog panels depending on user // choice. if (comparisonPane.getSaveMergeOption() == InstanceComparisonPane.SAVE_AS_NEW) { GKInstance merged = comparisonPane.getMerged(); if (newList == null) { List list = new ArrayList(); list.add(merged); newList = initInstanceListPane(list, "Instances created locally: " + list.size()); } else newList.addInstance(merged); } else if (comparisonPane.getSaveMergeOption() == InstanceComparisonPane.OVERWRITE_FIRST) { dbAdaptor.setUseCache(false); GKInstance remoteCopy = dbAdaptor.fetchInstance(instance.getDBID()); dbAdaptor.setUseCache(true); if (remoteCopy != null) { InstanceComparer comparer = new InstanceComparer(); int reply = comparer.compare(instance, remoteCopy); if (reply == InstanceComparer.LOCAL_HAS_MORE_IE) { if (localHasMoreIEList == null) { List list = new ArrayList(); list.add(instance); localHasMoreIEList = initInstanceListPane( list, "Local instances having unexpected InstanceEdits: " + list.size()); } else localHasMoreIEList.addInstance(instance); } else if (reply == InstanceComparer.IS_IDENTICAL) { // Merge has made local and database copies // identical - don't need to check in anymore changedList.deleteInstance(instance); } else if (reply != InstanceComparer.NEW_CHANGE_IN_LOCAL) { // Merge has produced a conflict typeMap.put(mapCompareResultToString(reply), instance); } else // The user should now be allowed to commit this // instance, because it will now have changed. commitToDBAction.setEnabled(true); JOptionPane.showMessageDialog( parentDialog, "Merge successful, you will need to check the merged instance into the database.\n\n", "Merge OK", JOptionPane.INFORMATION_MESSAGE); } } } catch (Exception e1) { System.err.println("Synchronization.createDisplayMapPane(): " + e1); e1.printStackTrace(); JOptionPane.showMessageDialog( this, "Error in comparing: " + instance.toString(), "Error in Comparing", JOptionPane.ERROR_MESSAGE); } }
private void updateFromDB() { InstanceListPane touchedList = null; // Find out which instances have been selected in all relevant lists // Update the attribute values for changed list. try { if (changedList != null) { java.util.List list = new ArrayList(changedList.getSelection()); // To support it.remove() // Use a new ArrayList. if (list != null && list.size() > 0) { for (Iterator it = list.iterator(); it.hasNext(); ) { GKInstance instance = (GKInstance) it.next(); // Fetch GKInstance based on its DB_ID since SchemaClass might be changed. GKInstance localCopy = fileAdaptor.fetchInstance(instance.getDBID()); dbAdaptor.setUseCache(false); GKInstance dbCopy = dbAdaptor.fetchInstance(instance.getDBID()); dbAdaptor.setUseCache(true); if (SynchronizationManager.getManager().updateFromDB(localCopy, dbCopy, this)) { AttributeEditManager.getManager().attributeEdit(localCopy); fileAdaptor.removeDirtyFlag(localCopy); } else it.remove(); // Remove from the list } // Remove all selected instances changedList.deleteInstances(list); touchedList = changedList; } } if (localHasMoreIEList != null && localHasMoreIEList.getSelection().size() > 0) { List list = new ArrayList(localHasMoreIEList.getSelection()); for (Iterator it = list.iterator(); it.hasNext(); ) { GKInstance localCopy = (GKInstance) it.next(); dbAdaptor.setUseCache(false); GKInstance dbCopy = dbAdaptor.fetchInstance(localCopy.getDBID()); dbAdaptor.setUseCache(true); if (SynchronizationManager.getManager().updateFromDB(localCopy, dbCopy, this)) { AttributeEditManager.getManager().attributeEdit(localCopy); fileAdaptor.removeDirtyFlag(localCopy); } else it.remove(); // Remove from the list, if it it cancelled by the user. } localHasMoreIEList.deleteInstances(list); touchedList = localHasMoreIEList; } // Delete the local copy for deleteInDBList. if (deleteInDBList != null) { java.util.List list = deleteInDBList.getSelection(); if (list != null && list.size() > 0) { for (Iterator it = list.iterator(); it.hasNext(); ) { GKInstance instance = (GKInstance) it.next(); fileAdaptor.deleteInstance(instance); } } deleteInDBList.deleteInstances(list); touchedList = deleteInDBList; } // Get another copy for the local repository if (deleteList != null) { java.util.List<?> list = deleteList.getSelection(); if (list != null && list.size() > 0) { Map<SchemaClass, Set<GKInstance>> checkOutMap = new HashMap<SchemaClass, Set<GKInstance>>(); // All checked out Instances for (Iterator<?> it = list.iterator(); it.hasNext(); ) { GKInstance instance = (GKInstance) it.next(); Map<SchemaClass, List<GKInstance>> schemaMap = InstanceUtilities.listDownloadableInstances(instance); // Have to merge schemaMap to checkOutMap for (SchemaClass key : schemaMap.keySet()) { List<GKInstance> list1 = schemaMap.get(key); if (list1 != null && list1.size() > 0) { Set<GKInstance> list2 = checkOutMap.get(key); if (list2 == null) { list2 = new HashSet<GKInstance>(); checkOutMap.put(key, list2); } // Remove first to avoid any duplication list2.addAll(list1); } } } fileAdaptor.store(checkOutMap); InstanceUtilities.clearShellFlags(checkOutMap); // There are maybe more than selected instances after checking out List<GKInstance> checkedOut = new ArrayList<GKInstance>(); for (Set<GKInstance> set : checkOutMap.values()) { checkedOut.addAll(set); } deleteList.deleteInstances(checkedOut); // deleteList.deleteInstances(list); // Need to clear out these deletion records List<Long> dbIds = new ArrayList<Long>(); for (GKInstance inst : checkedOut) dbIds.add(inst.getDBID()); fileAdaptor.clearDeleteRecord(dbIds); touchedList = deleteList; } } } catch (Exception e) { System.err.println("SynchronizationDialog.updateFromDB(): " + e); e.printStackTrace(); } updateInstanceList(touchedList); }
public void check() throws Exception { if (dba == null) throw new IllegalStateException("InstanceReferenceChecker.check(): No database specified."); GKSchema schema = (GKSchema) dba.getSchema(); Set instanceAtts = new HashSet(); GKSchemaAttribute att = null; for (Iterator it = schema.getOriginalAttributes().iterator(); it.hasNext(); ) { att = (GKSchemaAttribute) it.next(); if (att.isInstanceTypeAttribute()) { instanceAtts.add(att); } } System.out.println("Instance Atts: " + instanceAtts.size()); Connection conn = dba.getConnection(); Statement stat = conn.createStatement(); ResultSet resultSet = null; GKSchemaClass cls = null; Map attMap = new HashMap(); Map tableNameMap = new HashMap(); String query = null; for (Iterator it = instanceAtts.iterator(); it.hasNext(); ) { att = (GKSchemaAttribute) it.next(); cls = (GKSchemaClass) att.getOrigin(); System.out.println("Checking " + att.getName() + " in " + cls.getName() + "..."); String tableName = null; if (!att.isMultiple()) { tableName = cls.getName(); } else { tableName = cls.getName() + "_2_" + att.getName(); } query = "SELECT distinct " + att.getName() + ", " + att.getName() + "_class FROM " + tableName; resultSet = stat.executeQuery(query); while (resultSet.next()) { long dbID = resultSet.getLong(1); String clsName = resultSet.getString(2); if (dbID == 0 && clsName == null) { // errorMessage.append("0, null occur for " + att.getName() + " in " + tableName); // errorMessage.append("\n"); continue; } Long dbIDLong = new Long(dbID); String tmpCls = (String) attMap.get(dbIDLong); if (tmpCls != null && !tmpCls.equals(clsName)) { errorMessage.append(dbID + ", " + tmpCls + " in "); errorMessage.append(tableNameMap.get(dbIDLong) + " is different from "); errorMessage.append(tableName); errorMessage.append("\n"); } else { attMap.put(dbIDLong, clsName); tableNameMap.put(dbIDLong, tableName); } } resultSet.close(); } stat.close(); System.out.println("The size of the map: " + attMap.size()); // Check DB_ID in the database query = "SELECT _class FROM DatabaseObject WHERE DB_ID = ?"; PreparedStatement prepStat = conn.prepareStatement(query); int c = 0; // Set notInSet = new HashSet(); for (Iterator it = attMap.keySet().iterator(); it.hasNext(); ) { Long dbID = (Long) it.next(); prepStat.setLong(1, dbID.longValue()); resultSet = prepStat.executeQuery(); c = 0; while (resultSet.next()) { c++; String clsName = resultSet.getString(1); String tmpClsName = (String) attMap.get(dbID); if (!tmpClsName.equals(clsName)) { errorMessage.append(dbID + ", " + tmpClsName); errorMessage.append(" in " + tableNameMap.get(dbID)); errorMessage.append(" is different in DatabaseObject ("); errorMessage.append(clsName); errorMessage.append(")\n"); } } if (c == 0) { errorMessage.append(dbID + ", " + attMap.get(dbID)); errorMessage.append(" in " + tableNameMap.get(dbID)); errorMessage.append(" is not in the DatabaseObject"); errorMessage.append("\n"); } else if (c > 1) { errorMessage.append(dbID + " occurs in more than once in DatabaseObject"); errorMessage.append("\n"); } resultSet.close(); } prepStat.close(); // Print out an empty line System.out.println(); if (errorMessage.length() == 0) { System.out.println("Nothing Wrong!"); } else { FileWriter fileWriter = new FileWriter("error.txt"); BufferedWriter writer = new BufferedWriter(fileWriter); writer.write(errorMessage.toString()); writer.close(); fileWriter.close(); System.out.println("Done. See errors in error.txt."); } }
private void convertReactomeToGPML(Long dbID, String outputFileName) throws Exception { GKInstance pathway = adaptor.fetchInstance(dbID); convertReactomeToGPML(pathway, outputFileName); }