/** {@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;
  }
Esempio n. 2
0
  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;
  }
  /** {@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(".", " &gt; ");
        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 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());
  }
  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());
  }