/**
   * Populate the SequenceFeature.locatedFeatures() collection for: Gene, Transcript, Exon and CDS
   *
   * @throws Exception if anything goes wrong
   */
  public void populateCollection() throws Exception {
    Map<String, SOTerm> soTerms = populateSOTermMap(osw);
    Query q = getAllParents();
    Results res = osw.getObjectStore().execute(q);
    Iterator<Object> resIter = res.iterator();
    osw.beginTransaction();
    int parentCount = 0;
    int childCount = 0;

    while (resIter.hasNext()) {
      ResultsRow<InterMineObject> rr = (ResultsRow<InterMineObject>) resIter.next();
      InterMineObject parent = rr.get(0);
      SOTerm soTerm = (SOTerm) rr.get(1);
      InterMineObject o = PostProcessUtil.cloneInterMineObject(parent);
      Set<InterMineObject> newCollection = getChildFeatures(soTerms, soTerm, o);
      if (newCollection != null && !newCollection.isEmpty()) {
        o.setFieldValue(TARGET_COLLECTION, newCollection);
        osw.store(o);
        parentCount++;
        childCount += newCollection.size();
      }
    }
    osw.commitTransaction();
    LOG.info("Stored " + childCount + " child features for " + parentCount + " parent features");
  }
  public void testFailFast() throws Exception {
    Query q1 = new Query();
    QueryClass qc1 = new QueryClass(Employee.class);
    q1.addFrom(qc1);
    q1.addToSelect(qc1);

    Results r1 = writer.execute(q1);
    writer.store(data.get("EmployeeA1"));
    try {
      r1.iterator().hasNext();
      fail("Expected: ConcurrentModificationException");
    } catch (ConcurrentModificationException e) {
    }
  }
  /**
   * @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;
  }
  private static void runSpanValidationQuery(InterMineAPI im) {

    // a Map contains orgName and its chrInfo accordingly
    // e.g. <D.Melanogaster, (D.Melanogaster, X, 5000)...>
    chrInfoMap = new HashMap<String, List<ChromosomeInfo>>();

    try {
      Query q = new Query();

      QueryClass qcOrg = new QueryClass(Organism.class);
      QueryClass qcChr = new QueryClass(Chromosome.class);

      // Result columns
      QueryField qfOrgName = new QueryField(qcOrg, "shortName");
      QueryField qfChrPID = new QueryField(qcChr, "primaryIdentifier");
      QueryField qfChrLength = new QueryField(qcChr, "length");

      // As in SQL SELECT ?,?,?
      q.addToSelect(qfOrgName);
      q.addToSelect(qfChrPID);
      q.addToSelect(qfChrLength);

      // As in SQL FROM ?,?
      q.addFrom(qcChr);
      q.addFrom(qcOrg);

      // As in SQL WHERE ?
      QueryObjectReference organism = new QueryObjectReference(qcChr, "organism");
      ContainsConstraint ccOrg = new ContainsConstraint(organism, ConstraintOp.CONTAINS, qcOrg);
      q.setConstraint(ccOrg);

      Results results = im.getObjectStore().execute(q);

      // a List contains all the chrInfo (organism, chrPID, length)
      List<ChromosomeInfo> chrInfoList = new ArrayList<ChromosomeInfo>();
      // a Set contains all the orgName
      Set<String> orgSet = new HashSet<String>();

      // Handle results
      for (Iterator<?> iter = results.iterator(); iter.hasNext(); ) {
        ResultsRow<?> row = (ResultsRow<?>) iter.next();

        String org = (String) row.get(0);
        String chrPID = (String) row.get(1);
        Integer chrLength = (Integer) row.get(2);

        // Add orgName to HashSet to filter out duplication
        orgSet.add(org);

        if (chrLength != null) {
          ChromosomeInfo chrInfo = new ChromosomeInfo();
          chrInfo.setOrgName(org);
          chrInfo.setChrPID(chrPID);
          chrInfo.setChrLength(chrLength);

          // Add ChromosomeInfo to Arraylist
          chrInfoList.add(chrInfo);
        }
      }

      // Iterate orgSet and chrInfoList to put data in chrInfoMap which has the key as the
      // orgName and value as a ArrayList containing a list of chrInfo which has the same
      // orgName
      for (String o : orgSet) {

        // a List to store chrInfo for the same organism
        List<ChromosomeInfo> chrInfoSubList = new ArrayList<ChromosomeInfo>();

        for (ChromosomeInfo chrInfo : chrInfoList) {
          if (o.equals(chrInfo.getOrgName())) {
            chrInfoSubList.add(chrInfo);
            chrInfoMap.put(o, chrInfoSubList);
          }
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  /** The method to run all the queries. */
  @SuppressWarnings("rawtypes")
  private void queryExecutor() {

    // Use spanOverlapFullResultMap to store the data in the session
    @SuppressWarnings("unchecked")
    Map<String, Map<GenomicRegion, List<SpanQueryResultRow>>> spanOverlapFullResultMap =
        (Map<String, Map<GenomicRegion, List<SpanQueryResultRow>>>)
            request.getSession().getAttribute("spanOverlapFullResultMap");

    if (spanOverlapFullResultMap == null) {
      spanOverlapFullResultMap =
          new HashMap<String, Map<GenomicRegion, List<SpanQueryResultRow>>>();
    }

    Map<GenomicRegion, List<SpanQueryResultRow>> spanOverlapResultDisplayMap =
        Collections.synchronizedMap(new LinkedHashMap<GenomicRegion, List<SpanQueryResultRow>>());

    // GBrowse track
    @SuppressWarnings("unchecked")
    Map<String, Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>>
        gbrowseFullTrackMap =
            (HashMap<
                    String,
                    Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>>)
                request.getSession().getAttribute("gbrowseFullTrackMap");

    if (gbrowseFullTrackMap == null) {
      gbrowseFullTrackMap =
          new HashMap<
              String, Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>>();
    }

    Map<GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>> gbrowseTrackMap =
        Collections.synchronizedMap(
            new LinkedHashMap<
                GenomicRegion, LinkedHashMap<String, LinkedHashSet<GBrowseTrackInfo>>>());

    if (!spanOverlapFullResultMap.containsKey(spanUUIDString)) {
      spanOverlapFullResultMap.put(spanUUIDString, spanOverlapResultDisplayMap);
      request.getSession().setAttribute("spanOverlapFullResultMap", spanOverlapFullResultMap);

      gbrowseFullTrackMap.put(spanUUIDString, gbrowseTrackMap);
      request.getSession().setAttribute("gbrowseFullTrackMap", gbrowseFullTrackMap);

      try {
        Query q;
        for (GenomicRegion aSpan : spanList) {
          q = new Query();
          q.setDistinct(true);

          String chrPID = aSpan.getChr();
          Integer start = aSpan.getStart();
          Integer end = aSpan.getEnd();

          /*
          >>>>> TEST CODE <<<<<
          LOG.info("OrgName: " + orgName);
          LOG.info("chrPID: " + chrPID);
          LOG.info("start: " + start);
          LOG.info("end: " + end);
          LOG.info("FeatureTypes: " + ftKeys);
          LOG.info("Submissions: " + subKeys);
          >>>>> TEST CODE <<<<<
          */

          // DB tables
          QueryClass qcOrg = new QueryClass(Organism.class);
          QueryClass qcChr = new QueryClass(Chromosome.class);
          QueryClass qcFeature = new QueryClass(SequenceFeature.class);
          QueryClass qcLoc = new QueryClass(Location.class);
          QueryClass qcSubmission = new QueryClass(Submission.class);

          QueryField qfOrgName = new QueryField(qcOrg, "shortName");
          QueryField qfChrPID = new QueryField(qcChr, "primaryIdentifier");
          QueryField qfFeaturePID = new QueryField(qcFeature, "primaryIdentifier");
          QueryField qfFeatureId = new QueryField(qcFeature, "id");
          QueryField qfFeatureClass = new QueryField(qcFeature, "class");
          QueryField qfSubmissionTitle = new QueryField(qcSubmission, "title");
          QueryField qfSubmissionDCCid = new QueryField(qcSubmission, "DCCid");
          QueryField qfChr = new QueryField(qcChr, "primaryIdentifier");
          QueryField qfLocStart = new QueryField(qcLoc, "start");
          QueryField qfLocEnd = new QueryField(qcLoc, "end");

          q.addToSelect(qfFeatureId);
          q.addToSelect(qfFeaturePID);
          q.addToSelect(qfFeatureClass);
          q.addToSelect(qfChr);
          q.addToSelect(qfLocStart);
          q.addToSelect(qfLocEnd);
          q.addToSelect(qfSubmissionDCCid);
          q.addToSelect(qfSubmissionTitle);

          q.addFrom(qcChr);
          q.addFrom(qcOrg);
          q.addFrom(qcFeature);
          q.addFrom(qcLoc);
          q.addFrom(qcSubmission);

          q.addToOrderBy(qfLocStart, "ascending");

          ConstraintSet constraints = new ConstraintSet(ConstraintOp.AND);

          q.setConstraint(constraints);

          // SequenceFeature.organism = Organism
          QueryObjectReference organism = new QueryObjectReference(qcFeature, "organism");
          ContainsConstraint ccOrg = new ContainsConstraint(organism, ConstraintOp.CONTAINS, qcOrg);
          constraints.addConstraint(ccOrg);

          // Organism.name = orgName
          SimpleConstraint scOrg =
              new SimpleConstraint(qfOrgName, ConstraintOp.EQUALS, new QueryValue(orgName));
          constraints.addConstraint(scOrg);

          // Location.feature = SequenceFeature
          QueryObjectReference locSubject = new QueryObjectReference(qcLoc, "feature");
          ContainsConstraint ccLocSubject =
              new ContainsConstraint(locSubject, ConstraintOp.CONTAINS, qcFeature);
          constraints.addConstraint(ccLocSubject);

          // Location.locatedOn = Chromosome
          QueryObjectReference locObject = new QueryObjectReference(qcLoc, "locatedOn");
          ContainsConstraint ccLocObject =
              new ContainsConstraint(locObject, ConstraintOp.CONTAINS, qcChr);
          constraints.addConstraint(ccLocObject);

          // Chromosome.primaryIdentifier = chrPID
          SimpleConstraint scChr =
              new SimpleConstraint(qfChrPID, ConstraintOp.EQUALS, new QueryValue(chrPID));
          constraints.addConstraint(scChr);

          // SequenceFeature.submissions = Submission
          QueryCollectionReference submission =
              new QueryCollectionReference(qcFeature, "submissions");
          ContainsConstraint ccSubmission =
              new ContainsConstraint(submission, ConstraintOp.CONTAINS, qcSubmission);
          constraints.addConstraint(ccSubmission);

          // SequenceFeature.class in a list
          constraints.addConstraint(new BagConstraint(qfFeatureClass, ConstraintOp.IN, ftKeys));
          // Submission.CCDid in a list
          constraints.addConstraint(new BagConstraint(qfSubmissionDCCid, ConstraintOp.IN, subKeys));

          OverlapRange overlapInput =
              new OverlapRange(new QueryValue(start), new QueryValue(end), locObject);
          OverlapRange overlapFeature =
              new OverlapRange(
                  new QueryField(qcLoc, "start"), new QueryField(qcLoc, "end"), locObject);
          OverlapConstraint oc =
              new OverlapConstraint(overlapInput, ConstraintOp.OVERLAPS, overlapFeature);
          constraints.addConstraint(oc);

          Results results = im.getObjectStore().execute(q);

          /*
          >>>>> TEST CODE <<<<<
          LOG.info("Query: " + q.toString());
          LOG.info("Result Size: " + results.size());
          LOG.info("Result >>>>> " + results);
          >>>>> TEST CODE <<<<<
          */

          List<SpanQueryResultRow> spanResults = new ArrayList<SpanQueryResultRow>();
          if (results == null || results.isEmpty()) {
            spanOverlapResultDisplayMap.put(aSpan, null);
            gbrowseTrackMap.put(aSpan, null);
          } else {
            for (Iterator<?> iter = results.iterator(); iter.hasNext(); ) {
              ResultsRow<?> row = (ResultsRow<?>) iter.next();

              SpanQueryResultRow aRow = new SpanQueryResultRow();
              aRow.setFeatureId((Integer) row.get(0));
              aRow.setFeaturePID((String) row.get(1));
              aRow.setFeatureClass(((Class) row.get(2)).getSimpleName());
              aRow.setChr((String) row.get(3));
              aRow.setStart((Integer) row.get(4));
              aRow.setEnd((Integer) row.get(5));
              aRow.setSubDCCid((String) row.get(6));
              aRow.setSubTitle((String) row.get(7));

              spanResults.add(aRow);
            }
            spanOverlapResultDisplayMap.put(aSpan, spanResults);
            gbrowseTrackMap.put(aSpan, getSubGbrowseTrack(spanResults)); // Gbrowse
          }
        }

      } catch (Exception e) {
        e.printStackTrace();
      }
    }
  }
示例#6
0
  @Override
  protected void execute() throws Exception {
    String pathString = request.getParameter("path");

    Map<String, Object> attributes = getHeaderAttributes();
    output.setHeaderAttributes(attributes);

    if (isEmpty(pathString)) {
      throw new BadRequestException("No path provided");
    }
    attributes.put("path", pathString);
    kvPairs.put("path", pathString);

    String typeConstraintStr = request.getParameter("typeConstraints");
    Map<String, String> typeMap = new HashMap<String, String>();
    if (!isEmpty(typeConstraintStr)) {
      logger.debug(typeConstraintStr);
      JSONObject typeJO = new JSONObject(typeConstraintStr);
      Iterator<String> it = (Iterator<String>) typeJO.keys();
      while (it.hasNext()) {
        String name = it.next();
        String subType = typeJO.getString(name);
        typeMap.put(name, subType);
      }
    }

    Model model = im.getModel();

    Path path;
    try {
      if (typeMap.isEmpty()) {
        path = new Path(model, pathString);
      } else {
        path = new Path(model, pathString, typeMap);
      }
    } catch (PathException e) {
      throw new BadRequestException("Bad path given: " + pathString, e);
    }

    Query q = new Query();

    attributes.put("class", path.getLastClassDescriptor().getUnqualifiedName());
    attributes.put("field", path.getLastElement());
    String type = ((AttributeDescriptor) path.getEndFieldDescriptor()).getType();
    String[] parts = split(type, '.');
    reverse(parts);
    attributes.put("type", parts[0]);

    QueryClass qc = new QueryClass(path.getPrefix().getEndType());
    q.addFrom(qc);

    QueryField qf1 = new QueryField(qc, path.getLastElement());
    q.addToSelect(qf1);

    QueryFunction qf = new QueryFunction();
    q.addToSelect(qf);
    q.addToGroupBy(qf1);

    int count = im.getObjectStore().count(q, ObjectStore.SEQUENCE_IGNORE);

    if (formatIsCount()) {
      output.addResultItem(Arrays.asList(String.valueOf(count)));
    } else {
      attributes.put("count", count);

      Results results = im.getObjectStore().execute(q, DEFAULT_BATCH_SIZE, true, true, false);
      Iterator<Object> iter = results.iterator();

      while (iter.hasNext()) {
        @SuppressWarnings("rawtypes")
        List row = (List) iter.next();
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("value", row.get(0));
        jsonMap.put("count", row.get(1));
        JSONObject jo = new JSONObject(jsonMap);
        List<String> forOutput = new ArrayList<String>();
        forOutput.add(jo.toString());
        if (iter.hasNext()) {
          // Standard hack to ensure commas
          forOutput.add("");
        }
        output.addResultItem(forOutput);
      }
    }
  }
示例#7
0
  /**
   * 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);
    }
  }