/** * Create a PathQuery to get results for a collection of items from an InterMineObject * * @param webConfig the WebConfig * @param os the production ObjectStore * @param object the InterMineObject * @param referencedClassName the collection type * @param field the name of the field for the collection in the InterMineObject * @return a PathQuery */ public static PathQuery makePathQueryForCollection( WebConfig webConfig, ObjectStore os, InterMineObject object, String referencedClassName, String field) { String className = TypeUtil.unqualifiedName(DynamicUtil.getSimpleClassName(object.getClass())); Path path; try { path = new Path(os.getModel(), className + "." + field); } catch (PathException e) { throw new IllegalArgumentException( "Could not build path for \"" + className + "." + field + "\"."); } List<Class<?>> types = new ArrayList<Class<?>>(); if (path.endIsCollection()) { CollectionDescriptor end = (CollectionDescriptor) path.getEndFieldDescriptor(); // Only look for types if the refClass exactly matches the path type. if (end.getReferencedClassName().equals(referencedClassName)) { types = queryForTypesInCollection(object, field, os); } if (types.isEmpty()) { // the collection was empty, but still generate a query with the collection type types.add(os.getModel().getClassDescriptorByName(referencedClassName).getType()); } } else if (path.endIsReference()) { types.add(path.getLastClassDescriptor().getType()); } return makePathQueryForCollectionForClass(webConfig, os.getModel(), object, field, types); }
/** {@inheritDoc} */ public ActionForward execute( @SuppressWarnings("unused") ActionMapping mapping, @SuppressWarnings("unused") ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { String type = request.getParameter("type"); if (type == null) { type = "webapp"; } if ("webapp".equals(type)) { response.getOutputStream().print("OK"); } else if ("query".equals(type)) { final InterMineAPI im = SessionMethods.getInterMineAPI(request.getSession()); ObjectStore os = im.getObjectStore(); Query q = new Query(); QueryClass c = new QueryClass(InterMineObject.class); q.addFrom(c); q.addToSelect(c); // Add a unique value to the select to avoid caching the query QueryValue token = new QueryValue(System.currentTimeMillis()); q.addToSelect(token); Results r = os.execute(q, 1, false, false, false); if (r.get(0) != null) { response.getOutputStream().print("OK"); } else { response.getOutputStream().print("NO RESULTS"); } } return null; }
/** {@inheritDoc} */ @Override public ActionForward execute( @SuppressWarnings("unused") ActionMapping mapping, @SuppressWarnings("unused") ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { HttpSession session = request.getSession(); final InterMineAPI im = SessionMethods.getInterMineAPI(session); ObjectStore os = im.getObjectStore(); WebConfig webConfig = SessionMethods.getWebConfig(request); Integer objectId = new Integer(request.getParameter("object")); String fieldName = request.getParameter("field"); String fileType = request.getParameter("type"); InterMineObject object = os.getObjectById(objectId); FieldExporter fieldExporter = null; Set classes = DynamicUtil.decomposeClass(object.getClass()); Iterator classIter = classes.iterator(); while (classIter.hasNext()) { Class c = (Class) classIter.next(); Type thisTypeConfig = webConfig.getTypes().get(c.getName()); FieldConfig fc = thisTypeConfig.getFieldConfigMap().get(fieldName); if (fc != null) { String fieldExporterClassName = fc.getFieldExporter(); if (fieldExporterClassName != null) { fieldExporter = (FieldExporter) Class.forName(fieldExporterClassName).newInstance(); break; } } } if (fieldExporter == null) { Object fieldValue = object.getFieldValue(fieldName); if (fileType == null || fileType.length() == 0) { response.setContentType("text/plain; charset=UTF-8"); response.setHeader("Content-Disposition ", "inline; filename=" + fieldName + ".txt"); } else { response.setContentType("text/" + fileType); response.setHeader( "Content-Disposition ", "inline; filename=" + fieldName + "." + fileType); } PrintStream out = new PrintStream(response.getOutputStream()); if (fieldValue instanceof ClobAccess) { ((ClobAccess) fieldValue).drainToPrintStream(out); } else { out.print(fieldValue); } out.flush(); } else { fieldExporter.exportField(object, fieldName, os, response); } return null; }
private InterMineObject getRequestedObject(InterMineAPI im, HttpServletRequest request) { String idString = request.getParameter("id"); Integer id = new Integer(Integer.parseInt(idString)); ObjectStore os = im.getObjectStore(); InterMineObject requestedObject = null; try { requestedObject = os.getObjectById(id); } catch (ObjectStoreException e) { Log.warn("Accessed report page with id: " + id + " but failed to find object.", e); } return requestedObject; }
public void testTranslation() throws Exception { ObjectStore os2 = new ObjectStoreTranslatingImpl( Model.getInstanceByName("testmodel"), ObjectStoreFactory.getObjectStore("os.unittest"), new CompanyTranslator()); Query q = new Query(); QueryClass qc = new QueryClass(InterMineObject.class); q.addToSelect(qc); q.addFrom(qc); Results res = os2.execute(q); assertEquals(2, res.size()); assertEquals("CompanyA", ((Bank) ((ResultsRow) res.get(0)).get(0)).getName()); assertEquals("CompanyB", ((Bank) ((ResultsRow) res.get(1)).get(0)).getName()); }
/** * Constructor. * * @param name The name of this bag. * @param type The type it is meant to have. * @param desc A free text description of the bag. * @param createdAt When this list was first created. * @param os The production object store. * @param profileId The internal DB id of the profile of the user this list belongs to. * @param savedBagId The internal DB id of the saved bag this list represents. * @param osb The bag that represents the production DB level stored set of object ids. * @param userprofileObjectStore The user-profile object store writer. * @throws ObjectStoreException If a ObjectStoreBag needs to be created and there is a problem * doing so. */ public InvalidBag( String name, String type, String desc, Date createdAt, ObjectStore os, Integer savedBagId, Integer profileId, ObjectStoreBag osb, ObjectStoreWriter userprofileObjectStore) throws ObjectStoreException { this.type = TypeUtil.unqualifiedName(type); this.name = name; this.description = desc; this.dateCreated = createdAt; checkArguments(os, userprofileObjectStore); this.os = os; if (osb == null) { this.osb = os.createObjectStoreBag(); } else { this.osb = osb; } this.uosw = userprofileObjectStore; this.profileId = profileId; this.savedBagId = savedBagId; }
/** * Retrieve the publications to be updated * * @param os The ObjectStore to read from * @return a List of publications */ protected List<Publication> getPublications(ObjectStore os) { Query q = new Query(); QueryClass qc = new QueryClass(Publication.class); q.addFrom(qc); q.addToSelect(qc); ConstraintSet cs = new ConstraintSet(ConstraintOp.OR); SimpleConstraint scTitle = new SimpleConstraint(new QueryField(qc, "title"), ConstraintOp.IS_NULL); cs.addConstraint(scTitle); SimpleConstraint scYear = new SimpleConstraint(new QueryField(qc, "year"), ConstraintOp.IS_NULL); cs.addConstraint(scYear); SimpleConstraint scFirstAuthor = new SimpleConstraint(new QueryField(qc, "firstAuthor"), ConstraintOp.IS_NULL); cs.addConstraint(scFirstAuthor); q.setConstraint(cs); @SuppressWarnings("unchecked") List<Publication> retval = (List<Publication>) ((List) os.executeSingleton(q)); return retval; }
/** * Add a contains constraint to Query (q) built with the query class and attribute given in input * * @param query The query to add a reference to. * @param qc The class the reference belongs to. * @param attribute the name of the field of the class. * @param attributePath Another similarly named field - I wish it had been documented! * @return The query class of the attributePath. */ protected QueryClass addReference( final Query query, final QueryClass qc, String attribute, String attributePath) { ConstraintSet cs = (ConstraintSet) query.getConstraint(); QueryReference qr = null; String type = ""; boolean useSubClass = false; if (WidgetConfigUtil.isPathContainingSubClass(os.getModel(), attribute)) { useSubClass = true; type = attribute.substring(attribute.indexOf("[") + 1, attribute.indexOf("]")); attribute = attribute.substring(0, attribute.indexOf("[")); } QueryClass qcTmp = null; try { qr = new QueryObjectReference(qc, attribute); if (useSubClass) { try { qcTmp = new QueryClass(Class.forName(os.getModel().getPackageName() + "." + type)); } catch (ClassNotFoundException cnfe) { LOG.error("The type " + type + " doesn't exist in the model."); } } else { qcTmp = new QueryClass(qr.getType()); } } catch (IllegalArgumentException e) { // Not a reference - try collection instead qr = new QueryCollectionReference(qc, attribute); if (useSubClass) { try { qcTmp = new QueryClass(Class.forName(os.getModel().getPackageName() + "." + type)); } catch (ClassNotFoundException cnfe) { LOG.error("The type " + type + " doesn't exist in the model."); } } else { qcTmp = new QueryClass(TypeUtil.getElementType(qc.getType(), attribute)); } } QueryClass ret; if (!queryClassInQuery.containsKey(attributePath)) { ret = qcTmp; query.addFrom(ret); cs.addConstraint(new ContainsConstraint(qr, ConstraintOp.CONTAINS, ret)); queryClassInQuery.put(attributePath, ret); } else { ret = queryClassInQuery.get(attributePath); } return ret; }
@Override public void execute() { System.out.println("Creating lucene index for keyword search..."); ObjectStore os; try { os = getObjectStore(); } catch (Exception e) { throw new BuildException(e); } if (!(os instanceof ObjectStoreInterMineImpl)) { // Yes, yes, this is horrific... throw new RuntimeException( "Got invalid ObjectStore - must be an " + "instance of ObjectStoreInterMineImpl!"); } /* String configFileName = "objectstoresummary.config.properties"; InputStream configStream = classLoader.getResourceAsStream(configFileName); if (configStream == null) { throw new RuntimeException("can't find resource: " + configFileName); } Properties properties = new Properties(); properties.load(configStream);*/ // read class keys to figure out what are keyFields during indexing InputStream is = this.getClass().getClassLoader().getResourceAsStream("class_keys.properties"); Properties classKeyProperties = new Properties(); try { classKeyProperties.load(is); } catch (NullPointerException e) { throw new BuildException("Could not find the class keys"); } catch (IOException e) { throw new BuildException("Could not read the class keys", e); } Map<String, List<FieldDescriptor>> classKeys = ClassKeyHelper.readKeys(os.getModel(), classKeyProperties); // index and save KeywordSearch.saveIndexToDatabase(os, classKeys); KeywordSearch.deleteIndexDirectory(); }
private static Map<String, List<FieldDescriptor>> getClassKeys(ObjectStore os) { Properties classKeyProps = new Properties(); try { InputStream inputStream = ProfileManagerBinding.class.getClassLoader().getResourceAsStream("class_keys.properties"); classKeyProps.load(inputStream); } catch (IOException ioe) { throw new BuildException("class_keys.properties not found", ioe); } return ClassKeyHelper.readKeys(os.getModel(), classKeyProps); }
/** * Fix this bag by changing its type. If fixed, the DB will be corrected to store the new state, * and this bag will be marked as deleted. * * @param newType The new type of this list. This must be a valid type in the current model. * @return A valid InterMineBag. * @throws UnknownBagTypeException If the new type is not in the current model. * @throws ObjectStoreException If there is a problem saving state to the DB. */ public InterMineBag amendTo(String newType) throws UnknownBagTypeException, ObjectStoreException { if (os.getModel().getClassDescriptorByName(newType) == null) { throw new UnknownBagTypeException(newType + " is not a valid class name"); } fireEvent(new DeletionEvent(this)); InvalidBag amended = new InvalidBag( name, newType, description, dateCreated, os, savedBagId, profileId, osb, uosw); SavedBag sb = amended.storeSavedBag(); deleted = true; return new InterMineBag(os, sb.getId(), uosw); }
/** * Initialises the state of this object. This is done lazily, because it requires the use of a * database connection to discover the length of the clob, and that cannot be done while inside * the ObjectStoreWriter while it has exclusive use of the connection. */ protected void init() { if (results == null) { Query q = new Query(); q.addToSelect(clob); results = os.executeSingleton(q, 20, false, false, true); int pageCount = results.size(); if (pageCount == 0) { length = 0; } else { String lastPage = (String) results.get(pageCount - 1); length = CLOB_PAGE_SIZE * (pageCount - 1) + lastPage.length(); } } }
/** * Search for the classes in a collection for a given InterMineObject, for example find all of the * sub-classes of Employee in the Department.employees collection of a given Department. If there * are no subclasses or the collection is empty a list with the type of the collection is * returned. * * @param object an InterMineObject to inspect * @param field the name if the collection to check * @param os the ObjectStore in which to execute the query * @return a list of classes in the collection */ public static List<Class<?>> queryForTypesInCollection( InterMineObject object, String field, ObjectStore os) { List<Class<?>> typesInCollection = new ArrayList<Class<?>>(); // if there are no subclasses there can only be one type in the collection Model model = os.getModel(); ClassDescriptor startCld = model.getClassDescriptorByName(DynamicUtil.getSimpleClassName(object)); CollectionDescriptor col = startCld.getCollectionDescriptorByName(field, true); ClassDescriptor colCld = col.getReferencedClassDescriptor(); if (model.getAllSubs(colCld).isEmpty()) { // there aren't any subclasses, so no need to do a query typesInCollection.add(colCld.getType()); } else { // there may be multiple subclasses in the collection, need to run a query Query query = new Query(); QueryClass qc = new QueryClass(colCld.getType()); query.addFrom(qc); query.addToSelect(new QueryField(qc, "class")); query.setDistinct(true); query.setConstraint( new ContainsConstraint( new QueryCollectionReference(object, field), ConstraintOp.CONTAINS, qc)); for (Object o : os.executeSingleton(query)) { typesInCollection.add((Class<?>) o); } // Collection was empty but add collection type to be consistent with collection types // without subclasses. if (typesInCollection.isEmpty()) { typesInCollection.add(colCld.getType()); } } return typesInCollection; }
/** * @param bag the intermine bag * @param os the object store * @param filter the filter * @param config The description of the widget. */ public WidgetLdr(InterMineBag bag, ObjectStore os, String filter, WidgetConfig config) { this.bag = bag; this.os = os; this.filter = filter; try { startClass = new QueryClass( Class.forName(os.getModel().getPackageName() + "." + config.getStartClass())); } catch (ClassNotFoundException e) { if (config instanceof EnrichmentWidgetConfig || config instanceof GraphWidgetConfig) { throw new IllegalArgumentException( "Not found the class set in startClass for the" + " widget " + config.getId(), e); } } }
/** * Constructor callable by the ProfileManager. * * @param savedBag The saved bag retrieved from the DB. * @param profileId The id of the user profile. * @param os The production object store. * @param userprofileObjectStore The userprofile object store. * @throws ObjectStoreException If there is a problem creating an ObjectStoreBag. */ protected InvalidBag( SavedBag savedBag, Integer profileId, ObjectStore os, ObjectStoreWriter userprofileObjectStore) throws ObjectStoreException { this.type = TypeUtil.unqualifiedName(savedBag.getType()); this.name = savedBag.getName(); this.description = savedBag.getDescription(); this.dateCreated = savedBag.getDateCreated(); checkArguments(os, userprofileObjectStore); this.os = os; this.osb = os.createObjectStoreBag(); this.uosw = userprofileObjectStore; this.profileId = profileId; this.savedBagId = savedBag.getId(); }
/** * @param os object store * @return map of name to so term * @throws ObjectStoreException if something goes wrong */ protected Map<String, SOTerm> populateSOTermMap(ObjectStore os) throws ObjectStoreException { Map<String, SOTerm> soTerms = new HashMap<String, SOTerm>(); Query q = new Query(); q.setDistinct(false); QueryClass qcSOTerm = new QueryClass(SOTerm.class); q.addToSelect(qcSOTerm); q.addFrom(qcSOTerm); q.addToOrderBy(qcSOTerm); Results res = os.execute(q); Iterator it = res.iterator(); while (it.hasNext()) { ResultsRow<InterMineObject> rr = (ResultsRow<InterMineObject>) it.next(); SOTerm soTerm = (SOTerm) rr.get(0); soTerms.put(soTerm.getName(), soTerm); } return soTerms; }
/** {@inheritDoc} */ @Override public ActionForward execute( ComponentContext context, ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { boolean canExportAsBED = false; HttpSession session = request.getSession(); final InterMineAPI im = SessionMethods.getInterMineAPI(session); Model model = im.getModel(); PathQuery query = new PathQuery(model); // org and dbkey for galaxy use Set<String> orgSet = new HashSet<String>(); Set<String> genomeBuildSet = new HashSet<String>(); // Build GenomicRegion pathquery, the request is from GenomicRegionSearch "export to Galaxy" if (request.getParameter("featureIds") != null) { String featureIds = request.getParameter("featureIds").trim(); String orgName = request.getParameter("orgName"); if (orgName != null && !"".equals(orgName)) { orgSet.add(orgName); } // Refer to GenomicRegionSearchService.getExportFeaturesQuery method String path = "SequenceFeature"; query.addView(path + ".primaryIdentifier"); query.addView(path + ".chromosomeLocation.locatedOn.primaryIdentifier"); query.addView(path + ".chromosomeLocation.start"); query.addView(path + ".chromosomeLocation.end"); query.addView(path + ".organism.name"); // use ids or pids String[] idsInStr = featureIds.split(","); Set<Integer> ids = new HashSet<Integer>(); boolean isIds = true; for (String id : idsInStr) { id = id.trim(); if (!Pattern.matches("^\\d+$", id)) { isIds = false; break; } ids.add(Integer.valueOf(id)); } if (isIds) { query.addConstraint(Constraints.inIds(path, ids)); } else { if (featureIds.contains("'")) { featureIds = featureIds.replaceAll("'", "\\\\'"); } query.addConstraint(Constraints.lookup(path, featureIds, null)); } canExportAsBED = true; } else { // request from normal result table String tableName = request.getParameter("table"); request.setAttribute("table", tableName); PagedTable pt = SessionMethods.getResultsTable(session, tableName); // Null check to page table, maybe session timeout? if (pt == null) { LOG.error("Page table is NULL..."); return null; } // Check if can export as BED TableHttpExporter tableExporter = new BEDHttpExporter(); try { canExportAsBED = tableExporter.canExport(pt); } catch (Exception e) { canExportAsBED = false; LOG.error("Caught an error running canExport() for: BEDHttpExporter. " + e); e.printStackTrace(); } LinkedHashMap<Path, Integer> exportClassPathsMap = getExportClassPaths(pt); List<Path> exportClassPaths = new ArrayList<Path>(exportClassPathsMap.keySet()); Map<String, String> pathMap = new LinkedHashMap<String, String>(); for (Path path : exportClassPaths) { String pathString = path.toStringNoConstraints(); String displayPath = pathString.replace(".", " > "); pathMap.put(pathString, displayPath); } Map<String, Integer> pathIndexMap = new LinkedHashMap<String, Integer>(); for (int index = 0; index < exportClassPaths.size(); index++) { String pathString = exportClassPaths.get(index).toStringNoConstraints(); Integer idx = exportClassPathsMap.get(exportClassPaths.get(index)); pathIndexMap.put(pathString, idx); } request.setAttribute("exportClassPaths", pathMap); request.setAttribute("pathIndexMap", pathIndexMap); // Support export public and private lists to Galaxy query = pt.getWebTable().getPathQuery(); ObjectStore os = im.getObjectStore(); Map<PathConstraint, String> constrains = query.getConstraints(); for (PathConstraint constraint : constrains.keySet()) { if (constraint instanceof PathConstraintBag) { String bagName = ((PathConstraintBag) constraint).getBag(); InterMineBag imBag = im.getBagManager().getBag(SessionMethods.getProfile(session), bagName); // find the classKeys Set<String> classKeySet = new LinkedHashSet<String>(); for (Integer id : imBag.getContentsAsIds()) { String classKey = pt.findClassKeyValue(im.getClassKeys(), os.getObjectById(id)); classKeySet.add(classKey); } String path = ((PathConstraintBag) constraint).getPath(); // replace constraint in the pathquery PathConstraintLookup newConstraint = new PathConstraintLookup( path, classKeySet.toString().substring(1, classKeySet.toString().length() - 1), null); query.replaceConstraint(constraint, newConstraint); } } orgSet = SequenceFeatureExportUtil.getOrganisms(query, session); } if (query instanceof TemplateQuery) { TemplateQuery templateQuery = (TemplateQuery) query; Map<PathConstraint, SwitchOffAbility> constraintSwitchOffAbilityMap = templateQuery.getConstraintSwitchOffAbility(); for (Map.Entry<PathConstraint, SwitchOffAbility> entry : constraintSwitchOffAbilityMap.entrySet()) { if (entry.getValue().compareTo(SwitchOffAbility.OFF) == 0) { templateQuery.removeConstraint(entry.getKey()); } } } String queryXML = PathQueryBinding.marshal(query, "", model.getName(), PathQuery.USERPROFILE_VERSION); // String encodedQueryXML = URLEncoder.encode(queryXML, "UTF-8"); String tableViewURL = new URLGenerator(request).getPermanentBaseURL() + "/service/query/results"; request.setAttribute("tableURL", tableViewURL); request.setAttribute("queryXML", queryXML); request.setAttribute("size", 1000000); // If can export as BED request.setAttribute("canExportAsBED", canExportAsBED); if (canExportAsBED) { String bedURL = new URLGenerator(request).getPermanentBaseURL() + "/service/query/results/bed"; request.setAttribute("bedURL", bedURL); genomeBuildSet = (Set<String>) OrganismGenomeBuildLookup.getGenomeBuildByOrgansimCollection(orgSet); String org = (orgSet.size() < 1) ? "Organism information not available" : StringUtil.join(orgSet, ","); // possible scenario: [null, ce3, null], should remove all null element and then join genomeBuildSet.removeAll(Collections.singleton(null)); String dbkey = (genomeBuildSet.size() < 1) ? "Genome Build information not available" : StringUtil.join(genomeBuildSet, ","); request.setAttribute("org", org); request.setAttribute("dbkey", dbkey); } // PathMap Map<String, String> pathsMap = new LinkedHashMap<String, String>(); List<String> views = query.getView(); for (String view : views) { String title = query.getGeneratedPathDescription(view); title = WebUtil.formatColumnName(title); pathsMap.put(view, title); } request.setAttribute("pathsMap", pathsMap); return null; }
public void testLoad() throws Exception { TSVFileReaderTask tsvTask = new TSVFileReaderTask(); tsvTask.setIgnoreDuplicates(true); tsvTask.setIntegrationWriterAlias("integration.unittestmulti"); tsvTask.setSourceName("testsource"); cleanObjects(tsvTask.getDirectDataLoader().getIntegrationWriter()); File tempFile = File.createTempFile("TSVFileReaderTaskTest", "tmp"); FileWriter fw = new FileWriter(tempFile); InputStream is = getClass().getClassLoader().getResourceAsStream("TSVFileReaderTaskTest.tsv"); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = br.readLine()) != null) { fw.write(line + "\n"); } fw.close(); FileSet fileSet = new FileSet(); fileSet.setFile(tempFile); tsvTask.addFileSet(fileSet); InputStream confInputStream = getClass().getClassLoader().getResourceAsStream("TSVFileReaderTaskTest.properties"); DelimitedFileConfiguration dfc = new DelimitedFileConfiguration(model, confInputStream); tsvTask.executeInternal(dfc, tempFile); // Check the results to see if we have some data... ObjectStore os = tsvTask.getDirectDataLoader().getIntegrationWriter().getObjectStore(); Query q = new Query(); QueryClass empQueryClass = new QueryClass(Employee.class); QueryField qf0 = new QueryField(empQueryClass, "age"); QueryField qf1 = new QueryField(empQueryClass, "name"); QueryField qf2 = new QueryField(empQueryClass, "fullTime"); q.addToSelect(qf0); q.addToSelect(qf1); q.addToSelect(qf2); q.addFrom(empQueryClass); q.addToOrderBy(qf1); Results r = os.execute(q); if (r.size() != 3) { for (List<Object> rr : (List<List<Object>>) ((List) r)) { System.err.print("row: "); for (Object obj : rr) { System.err.print("{" + obj + "} "); } System.err.println(); } } assertEquals(3, r.size()); List expectedRow0 = Arrays.asList(new Object[] {new Integer(10), "EmployeeA1", Boolean.FALSE}); assertEquals(expectedRow0, r.get(0)); List expectedRow1 = Arrays.asList(new Object[] {new Integer(20), "EmployeeA2", Boolean.TRUE}); assertEquals(expectedRow1, r.get(1)); List expectedRow2 = Arrays.asList(new Object[] {new Integer(0), "EmployeeA3", Boolean.FALSE}); assertEquals(expectedRow2, r.get(2)); }
/** {@inheritDoc} */ public PathQuery generatePathQuery( ObjectStore os, InterMineBag bag, String series, String category) { Model model = os.getModel(); PathQuery q = new PathQuery(model); Path geneSymbol = PathQuery.makePath(model, q, "Gene.symbol"); Path genePrimary = PathQuery.makePath(model, q, "Gene.primaryIdentifier"); Path haemAtlasSampleName = PathQuery.makePath(model, q, "Gene.probeSets.haemAtlasResults.sampleName"); Path haemAtlasGroup = PathQuery.makePath(model, q, "Gene.probeSets.haemAtlasResults.group"); Path haemAtlasAverage = PathQuery.makePath(model, q, "Gene.probeSets.haemAtlasResults.averageIntensity"); Path haemAtlasSample = PathQuery.makePath(model, q, "Gene.probeSets.haemAtlasResults.sample"); Path haemAtlasP = PathQuery.makePath(model, q, "Gene.probeSets.haemAtlasResults.detectionProbabilities"); Path haemAtlasIlluId = PathQuery.makePath(model, q, "Gene.probeSets.illuId"); List<Path> view = new ArrayList<Path>(); view.add(geneSymbol); view.add(genePrimary); view.add(haemAtlasSampleName); view.add(haemAtlasGroup); view.add(haemAtlasAverage); view.add(haemAtlasSample); view.add(haemAtlasP); view.add(haemAtlasIlluId); q.setViewPaths(view); String bagType = bag.getType(); ConstraintOp constraintOp = ConstraintOp.IN; String constraintValue = bag.getName(); String label = null, id = null, code = q.getUnusedConstraintCode(); Constraint c = new Constraint(constraintOp, constraintValue, false, label, code, id, null); q.addNode(bagType).getConstraints().add(c); constraintOp = ConstraintOp.EQUALS; code = q.getUnusedConstraintCode(); PathNode categoryNode = q.addNode("Gene.probeSets.haemAtlasResults.sampleName"); Constraint categoryConstraint = new Constraint(constraintOp, series, false, label, code, id, null); categoryNode.getConstraints().add(categoryConstraint); constraintOp = ConstraintOp.LESS_THAN; code = q.getUnusedConstraintCode(); PathNode seriesNode = q.addNode("Gene.probeSets.haemAtlasResults.detectionProbabilities"); Constraint seriesConstraint = new Constraint(constraintOp, 0.01, false, label, code, id, null); seriesNode.getConstraints().add(seriesConstraint); q.setConstraintLogic("A and B and C"); q.syncLogicExpression("and"); List<OrderBy> sortOrder = new ArrayList<OrderBy>(); sortOrder.add(new OrderBy(geneSymbol, "asc")); sortOrder.add(new OrderBy(genePrimary)); q.setSortOrder(sortOrder); return q; }
public void testCreateSpanningLocations() throws Exception { Exon exon1 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class)); exon1.setId(new Integer(107)); Exon exon2 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class)); exon2.setId(new Integer(108)); Exon exon3 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class)); exon3.setId(new Integer(109)); Location exon1OnChr = createLocation(getChromosome(), exon1, "1", 51, 100, Location.class); exon1OnChr.setId(new Integer(1010)); Location exon2OnChr = createLocation(getChromosome(), exon2, "1", 201, 250, Location.class); exon2OnChr.setId(new Integer(1011)); Location exon3OnChr = createLocation(getChromosome(), exon3, "1", 201, 400, Location.class); exon3OnChr.setId(new Integer(1012)); Transcript trans1 = (Transcript) DynamicUtil.createObject(Collections.singleton(Transcript.class)); trans1.setId(new Integer(201)); Transcript trans2 = (Transcript) DynamicUtil.createObject(Collections.singleton(Transcript.class)); trans2.setId(new Integer(202)); Location trans2OnChr = createLocation(getChromosome(), trans2, "1", 61, 300, Location.class); Gene gene = (Gene) DynamicUtil.createObject(Collections.singleton(Gene.class)); gene.setId(new Integer(301)); exon1.setTranscripts(new HashSet<Transcript>(Arrays.asList(new Transcript[] {trans1}))); exon2.setTranscripts(new HashSet<Transcript>(Arrays.asList(new Transcript[] {trans1}))); // the location of exon3 should be ignored by createSpanningLocations() because trans2 // already has a location exon3.setTranscripts(new HashSet<Transcript>(Arrays.asList(new Transcript[] {trans2}))); trans1.setGene(gene); trans2.setGene(gene); Set<InterMineObject> toStore = new HashSet<InterMineObject>( Arrays.asList( new InterMineObject[] { getChromosome(), gene, trans1, trans2, exon1, exon2, exon3, exon1OnChr, exon2OnChr, trans2OnChr })); for (InterMineObject imo : toStore) { osw.store(imo); } CalculateLocations cl = new CalculateLocations(osw); cl.createSpanningLocations("Transcript", "Exon", "exons"); cl.createSpanningLocations("Gene", "Transcript", "transcripts"); ObjectStore os = osw.getObjectStore(); Transcript resTrans1 = (Transcript) os.getObjectById(new Integer(201)); Assert.assertEquals(1, resTrans1.getLocations().size()); Location resTrans1Location = (Location) resTrans1.getLocations().iterator().next(); Assert.assertEquals(51, resTrans1Location.getStart().intValue()); Assert.assertEquals(250, resTrans1Location.getEnd().intValue()); Transcript resTrans2 = (Transcript) os.getObjectById(new Integer(202)); Assert.assertEquals(1, resTrans2.getLocations().size()); Location resTrans2Location = (Location) resTrans2.getLocations().iterator().next(); Assert.assertEquals(61, resTrans2Location.getStart().intValue()); Assert.assertEquals(300, resTrans2Location.getEnd().intValue()); Gene resGene = (Gene) os.getObjectById(new Integer(301)); Assert.assertEquals(1, resGene.getLocations().size()); Location resGeneLocation = (Location) resGene.getLocations().iterator().next(); Assert.assertEquals(51, resGeneLocation.getStart().intValue()); Assert.assertEquals(300, resGeneLocation.getEnd().intValue()); }
/** {@inheritDoc} */ @SuppressWarnings("unused") @Override public ActionForward execute( @SuppressWarnings("unused") ActionMapping mapping, @SuppressWarnings("unused") ActionForm form, HttpServletRequest request, @SuppressWarnings("unused") HttpServletResponse response) throws Exception { long startTime = System.currentTimeMillis(); HttpSession session = request.getSession(); InterMineAPI im = SessionMethods.getInterMineAPI(session); if (im.getBagManager().isAnyBagToUpgrade(SessionMethods.getProfile(session))) { recordError(new ActionMessage("login.upgradeListManually"), request); } // fetch & set requested object InterMineObject requestedObject = getRequestedObject(im, request); if (requestedObject != null) { ReportObjectFactory reportObjectFactory = SessionMethods.getReportObjects(session); ReportObject reportObject = reportObjectFactory.get(requestedObject); request.setAttribute("object", reportObject); request.setAttribute("reportObject", reportObject); request.setAttribute("requestedObject", requestedObject); // hell starts here TagManager tagManager = im.getTagManager(); ServletContext servletContext = session.getServletContext(); ObjectStore os = im.getObjectStore(); String superuser = im.getProfileManager().getSuperuser(); if (superuser.equals(SessionMethods.getProfile(session).getUsername())) { request.setAttribute("SHOW_TAGS", true); } // place InlineLists based on TagManager, reportObject is cached while Controller is not Map<String, List<InlineList>> placedInlineLists = new TreeMap<String, List<InlineList>>(); // traverse all unplaced (non-header) InlineLists for (InlineList list : reportObject.getNormalInlineLists()) { FieldDescriptor fd = list.getDescriptor(); String taggedType = getTaggedType(fd); // assign lists to any aspects they are tagged to or put in unplaced lists String fieldPath = fd.getClassDescriptor().getUnqualifiedName() + "." + fd.getName(); List<Tag> tags = tagManager.getTags(null, fieldPath, taggedType, superuser); for (Tag tag : tags) { String tagName = tag.getTagName(); if (AspectTagUtil.isAspectTag(tagName)) { List<InlineList> listsForAspect = placedInlineLists.get(tagName); if (listsForAspect == null) { listsForAspect = new ArrayList<InlineList>(); placedInlineLists.put(tagName, listsForAspect); } listsForAspect.add(list); } else if (tagName.equals(TagNames.IM_SUMMARY)) { List<InlineList> summaryLists = placedInlineLists.get(tagName); if (summaryLists == null) { summaryLists = new ArrayList<InlineList>(); placedInlineLists.put(tagName, summaryLists); } summaryLists.add(list); } } } // any lists that aren't tagged will be 'unplaced' List<InlineList> unplacedInlineLists = new ArrayList<InlineList>(reportObject.getNormalInlineLists()); unplacedInlineLists.removeAll(placedInlineLists.values()); long now = System.currentTimeMillis(); LOG.info("TIME placed inline lists: " + (now - startTime) + "ms"); long stepTime = now; request.setAttribute("mapOfInlineLists", placedInlineLists); request.setAttribute("listOfUnplacedInlineLists", unplacedInlineLists); Map<String, Map<String, DisplayField>> placementRefsAndCollections = new TreeMap<String, Map<String, DisplayField>>(); Set<String> aspects = new LinkedHashSet<String>(SessionMethods.getCategories(servletContext)); Set<ClassDescriptor> cds = os.getModel().getClassDescriptorsForClass(requestedObject.getClass()); for (String aspect : aspects) { placementRefsAndCollections.put( TagNames.IM_ASPECT_PREFIX + aspect, new TreeMap<String, DisplayField>(String.CASE_INSENSITIVE_ORDER)); } Map<String, DisplayField> miscRefs = new TreeMap<String, DisplayField>(reportObject.getRefsAndCollections()); placementRefsAndCollections.put(TagNames.IM_ASPECT_MISC, miscRefs); // summary refs and colls Map<String, DisplayField> summaryRefsCols = new TreeMap<String, DisplayField>(); placementRefsAndCollections.put(TagNames.IM_SUMMARY, summaryRefsCols); for (Iterator<Entry<String, DisplayField>> iter = reportObject.getRefsAndCollections().entrySet().iterator(); iter.hasNext(); ) { Map.Entry<String, DisplayField> entry = iter.next(); DisplayField df = entry.getValue(); if (df instanceof DisplayReference) { categoriseBasedOnTags( ((DisplayReference) df).getDescriptor(), "reference", df, miscRefs, tagManager, superuser, placementRefsAndCollections, SessionMethods.isSuperUser(session)); } else if (df instanceof DisplayCollection) { categoriseBasedOnTags( ((DisplayCollection) df).getDescriptor(), "collection", df, miscRefs, tagManager, superuser, placementRefsAndCollections, SessionMethods.isSuperUser(session)); } } // remove any fields overridden by displayers removeFieldsReplacedByReportDisplayers(reportObject, placementRefsAndCollections); request.setAttribute("placementRefsAndCollections", placementRefsAndCollections); String type = reportObject.getType(); request.setAttribute("objectType", type); String stableLink = PortalHelper.generatePortalLink(reportObject.getObject(), im, request); if (stableLink != null) { request.setAttribute("stableLink", stableLink); } stepTime = System.currentTimeMillis(); startTime = stepTime; // attach only non empty categories Set<String> allClasses = new HashSet<String>(); for (ClassDescriptor cld : cds) { allClasses.add(cld.getUnqualifiedName()); } TemplateManager templateManager = im.getTemplateManager(); Map<String, List<ReportDisplayer>> displayerMap = reportObject.getReportDisplayers(); stepTime = System.currentTimeMillis(); startTime = stepTime; List<String> categories = new LinkedList<String>(); for (String aspect : aspects) { // 1) Displayers // 2) Inline Lists if ((displayerMap != null && displayerMap.containsKey(aspect)) || placedInlineLists.containsKey(aspect)) { categories.add(aspect); } else { // 3) Templates if (!templateManager.getReportPageTemplatesForAspect(aspect, allClasses).isEmpty()) { categories.add(aspect); } else { // 4) References & Collections if (placementRefsAndCollections.containsKey("im:aspect:" + aspect) && placementRefsAndCollections.get("im:aspect:" + aspect) != null) { for (DisplayField df : placementRefsAndCollections.get("im:aspect:" + aspect).values()) { categories.add(aspect); break; } } } } } if (!categories.isEmpty()) { request.setAttribute("categories", categories); } now = System.currentTimeMillis(); LOG.info("TIME made list of categories: " + (now - stepTime) + "ms"); } return null; }
/** * Synchronize publications with pubmed using pmid * * @throws Exception if an error occurs */ public void execute() throws Exception { // Needed so that STAX can find it's implementation classes ClassLoader cl = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); Database db = null; Transaction txn = null; try { if (osAlias == null) { throw new BuildException("osAlias attribute is not set"); } if (outputFile == null) { throw new BuildException("outputFile attribute is not set"); } // environment is transactional EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setTransactional(true); envConfig.setAllowCreate(true); Environment env = new Environment(new File(cacheDirName), envConfig); DatabaseConfig dbConfig = new DatabaseConfig(); dbConfig.setTransactional(true); dbConfig.setAllowCreate(true); dbConfig.setSortedDuplicates(true); db = env.openDatabase(null, "publications_db", dbConfig); txn = env.beginTransaction(null, null); LOG.info("Starting EntrezPublicationsRetriever"); Writer writer = new FileWriter(outputFile); ObjectStore os = ObjectStoreFactory.getObjectStore(osAlias); Set<Integer> idsToFetch = new HashSet<Integer>(); itemFactory = new ItemFactory(os.getModel(), "-1_"); writer.write(FullRenderer.getHeader() + ENDL); for (Iterator<Publication> iter = getPublications(os).iterator(); iter.hasNext(); ) { String pubMedId = iter.next().getPubMedId(); Integer pubMedIdInteger; try { pubMedIdInteger = Integer.valueOf(pubMedId); } catch (NumberFormatException e) { // not a pubmed id continue; } if (seenPubMeds.contains(pubMedIdInteger)) { continue; } DatabaseEntry key = new DatabaseEntry(pubMedId.getBytes()); DatabaseEntry data = new DatabaseEntry(); if (db.get(txn, key, data, null).equals(OperationStatus.SUCCESS)) { try { ByteArrayInputStream mapInputStream = new ByteArrayInputStream(data.getData()); ObjectInputStream deserializer = new ObjectInputStream(mapInputStream); Map<String, Object> pubMap = (Map) deserializer.readObject(); writeItems(writer, mapToItems(itemFactory, pubMap)); seenPubMeds.add(pubMedIdInteger); } catch (EOFException e) { // ignore and fetch it again System.err.println( "found in cache, but igored due to cache problem: " + pubMedIdInteger); } } else { idsToFetch.add(pubMedIdInteger); } } Iterator<Integer> idIter = idsToFetch.iterator(); Set<Integer> thisBatch = new HashSet<Integer>(); while (idIter.hasNext()) { Integer pubMedIdInteger = idIter.next(); thisBatch.add(pubMedIdInteger); if (thisBatch.size() == BATCH_SIZE || !idIter.hasNext() && thisBatch.size() > 0) { try { // the server may return less publications than we ask for, so keep a Map Map<String, Map<String, Object>> fromServerMap = null; for (int i = 0; i < MAX_TRIES; i++) { BufferedReader br = new BufferedReader(getReader(thisBatch)); StringBuffer buf = new StringBuffer(); String line; while ((line = br.readLine()) != null) { buf.append(line + "\n"); } fromServerMap = new HashMap<String, Map<String, Object>>(); Throwable throwable = null; try { if (loadFullRecord) { SAXParser.parse( new InputSource(new StringReader(buf.toString())), new FullRecordHandler(fromServerMap), false); } else { SAXParser.parse( new InputSource(new StringReader(buf.toString())), new SummaryRecordHandler(fromServerMap), false); } } catch (Throwable e) { LOG.error("Couldn't parse PubMed XML", e); // try again or re-throw the Throwable throwable = e; } if (i == MAX_TRIES) { throw new RuntimeException( "failed to parse: " + buf.toString() + " - tried " + MAX_TRIES + " times", throwable); } else { if (throwable != null) { // try again continue; } } for (String id : fromServerMap.keySet()) { writeItems(writer, mapToItems(itemFactory, fromServerMap.get(id))); } addToDb(txn, db, fromServerMap); break; } thisBatch.clear(); } finally { txn.commit(); // start a new transaction incase there is an exception while parsing txn = env.beginTransaction(null, null); } } } writeItems(writer, authorMap.values()); writeItems(writer, meshTerms.values()); writer.write(FullRenderer.getFooter() + ENDL); writer.flush(); writer.close(); } catch (Throwable e) { throw new RuntimeException("failed to get all publications", e); } finally { txn.commit(); db.close(); Thread.currentThread().setContextClassLoader(cl); } }
/** * Read the UTRs collection of MRNA then set the fivePrimeUTR and threePrimeUTR fields with the * corresponding UTRs. * * @throws Exception if anything goes wrong */ public void createUtrRefs() throws Exception { long startTime = System.currentTimeMillis(); Query q = new Query(); q.setDistinct(false); QueryClass qcMRNA = new QueryClass(model.getClassDescriptorByName("MRNA").getType()); q.addFrom(qcMRNA); q.addToSelect(qcMRNA); q.addToOrderBy(qcMRNA); QueryClass qcUTR = new QueryClass(model.getClassDescriptorByName("UTR").getType()); q.addFrom(qcUTR); q.addToSelect(qcUTR); q.addToOrderBy(qcUTR); QueryCollectionReference mrnaUtrsRef = new QueryCollectionReference(qcMRNA, "UTRs"); ContainsConstraint mrnaUtrsConstraint = new ContainsConstraint(mrnaUtrsRef, ConstraintOp.CONTAINS, qcUTR); QueryObjectReference fivePrimeRef = new QueryObjectReference(qcMRNA, "fivePrimeUTR"); ContainsConstraint fivePrimeNullConstraint = new ContainsConstraint(fivePrimeRef, ConstraintOp.IS_NULL); QueryObjectReference threePrimeRef = new QueryObjectReference(qcMRNA, "threePrimeUTR"); ContainsConstraint threePrimeNullConstraint = new ContainsConstraint(threePrimeRef, ConstraintOp.IS_NULL); ConstraintSet cs = new ConstraintSet(ConstraintOp.AND); cs.addConstraint(mrnaUtrsConstraint); cs.addConstraint(fivePrimeNullConstraint); cs.addConstraint(threePrimeNullConstraint); q.setConstraint(cs); ObjectStore os = osw.getObjectStore(); ((ObjectStoreInterMineImpl) os).precompute(q, Constants.PRECOMPUTE_CATEGORY); Results res = os.execute(q, 500, true, true, true); int count = 0; InterMineObject lastMRNA = null; InterMineObject fivePrimeUTR = null; InterMineObject threePrimeUTR = null; osw.beginTransaction(); Class<? extends FastPathObject> fivePrimeUTRCls = model.getClassDescriptorByName("FivePrimeUTR").getType(); Iterator<?> resIter = res.iterator(); while (resIter.hasNext()) { ResultsRow<?> rr = (ResultsRow<?>) resIter.next(); InterMineObject mrna = (InterMineObject) rr.get(0); InterMineObject utr = (InterMineObject) rr.get(1); if (lastMRNA != null && !mrna.getId().equals(lastMRNA.getId())) { // clone so we don't change the ObjectStore cache InterMineObject tempMRNA = PostProcessUtil.cloneInterMineObject(lastMRNA); if (fivePrimeUTR != null) { tempMRNA.setFieldValue("fivePrimeUTR", fivePrimeUTR); fivePrimeUTR = null; } if (threePrimeUTR != null) { tempMRNA.setFieldValue("threePrimeUTR", threePrimeUTR); threePrimeUTR = null; } osw.store(tempMRNA); count++; } if (DynamicUtil.isInstance(utr, fivePrimeUTRCls)) { fivePrimeUTR = utr; } else { threePrimeUTR = utr; } lastMRNA = mrna; } if (lastMRNA != null) { // clone so we don't change the ObjectStore cache InterMineObject tempMRNA = PostProcessUtil.cloneInterMineObject(lastMRNA); tempMRNA.setFieldValue("fivePrimeUTR", fivePrimeUTR); tempMRNA.setFieldValue("threePrimeUTR", threePrimeUTR); osw.store(tempMRNA); count++; } LOG.info( "Stored MRNA " + count + " times (" + count * 2 + " fields set)" + " - took " + (System.currentTimeMillis() - startTime) + " ms."); osw.commitTransaction(); // now ANALYSE tables relating to class that has been altered - may be rows added // to indirection tables if (osw instanceof ObjectStoreWriterInterMineImpl) { ClassDescriptor cld = model.getClassDescriptorByName("MRNA"); DatabaseUtil.analyse(((ObjectStoreWriterInterMineImpl) osw).getDatabase(), cld, false); } }
@SuppressWarnings("unchecked") private Collection<E> executeCollection(Query q) { return (Collection) os.executeSingleton(q, batchSize, !noOptimise, !noExplain, true); }
public void testSetChromosomeLocationsAndLengths() throws Exception { Chromosome chr1 = (Chromosome) DynamicUtil.createObject(Collections.singleton(Chromosome.class)); chr1.setPrimaryIdentifier("1"); chr1.setId(new Integer(101)); Chromosome chr2 = (Chromosome) DynamicUtil.createObject(Collections.singleton(Chromosome.class)); chr1.setPrimaryIdentifier("2"); chr1.setId(new Integer(102)); Exon exon1 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class)); exon1.setId(new Integer(107)); exon1.setLength(new Integer(1000)); Exon exon2 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class)); exon2.setId(new Integer(108)); Exon exon3 = (Exon) DynamicUtil.createObject(Collections.singleton(Exon.class)); exon3.setId(new Integer(109)); // exon 2 has two chromosome locations, shouldn't get chromosome[Location] references Location exon1OnChr = createLocation(chr1, exon1, "1", 51, 100, Location.class); exon1OnChr.setId(new Integer(1010)); Location exon2OnChr = createLocation(chr2, exon2, "1", 201, 250, Location.class); exon2OnChr.setId(new Integer(1011)); Location exon2OnChrDup = createLocation(chr1, exon2, "1", 501, 550, Location.class); exon2OnChrDup.setId(new Integer(1012)); Location exon3OnChr = createLocation(chr2, exon3, "1", 601, 650, Location.class); exon3OnChr.setId(new Integer(1013)); Set<InterMineObject> toStore = new HashSet<InterMineObject>( Arrays.asList( new InterMineObject[] { chr1, chr2, exon1, exon2, exon3, exon1OnChr, exon2OnChr, exon2OnChrDup, exon3OnChr })); for (InterMineObject imo : toStore) { osw.store(imo); } CalculateLocations cl = new CalculateLocations(osw); cl.setChromosomeLocationsAndLengths(); ObjectStore os = osw.getObjectStore(); Exon resExon1 = (Exon) os.getObjectById(new Integer(107)); Exon resExon2 = (Exon) os.getObjectById(new Integer(108)); Exon resExon3 = (Exon) os.getObjectById(new Integer(109)); assertEquals(chr1.getId(), resExon1.getChromosome().getId()); assertEquals(exon1OnChr.getId(), resExon1.getChromosomeLocation().getId()); assertNull(resExon2.getChromosome()); assertNull(resExon2.getChromosomeLocation()); assertEquals(chr2.getId(), resExon3.getChromosome().getId()); assertEquals(exon3OnChr.getId(), resExon3.getChromosomeLocation().getId()); // exon1 has length set so should stay as 1000, exon3 should get length 50 set from location assertEquals(new Integer(1000), resExon1.getLength()); assertEquals(new Integer(50), resExon3.getLength()); // nothing done to exon2 assertNull(resExon2.getLength()); }