Exemplo n.º 1
0
 private void saveAnnotationsReport() throws IOException {
   IOUtils.write(outdir + "/annotations_per_db.txt", annotationReport.toString());
   result
       .getOutputItems()
       .add(
           1,
           new Item(
               "annotations_per_db",
               "annotations_per_db.txt",
               "Id annotations per DB",
               Item.TYPE.FILE,
               Arrays.asList("TABLE", "SUMMARY_TABLE"),
               new HashMap<String, String>(),
               "Summary"));
 }
Exemplo n.º 2
0
  public static void saveAsCGHInput(Dataset dataset, String outFileName)
      throws IOException, InvalidIndexException {
    dataset.load();

    List<String> sampleNames = dataset.getSampleNames();

    String header = "#ID\tChr\tStart\tEnd\t" + ListUtils.toString(sampleNames, "\t") + "\n";

    StringBuilder line = new StringBuilder();
    ;
    List<String> lines = new ArrayList<String>();

    DataFrame featureDataFrame = dataset.getFeatureData().getDataFrame();

    DataFrame dataFrame = new DataFrame();
    dataFrame.setRowNames(featureDataFrame.getColumn("ProbeName"));
    dataFrame.addColumn(featureDataFrame.getColumn("loc.chromosome"));
    dataFrame.addColumn(featureDataFrame.getColumn("loc.start"));
    dataFrame.addColumn(featureDataFrame.getColumn("loc.end"));
    for (int i = 0; i < sampleNames.size(); i++) {
      dataFrame.addColumn(ArrayUtils.toStringList(dataset.getDoubleMatrix().getColumn(i)));
    }

    System.out.println("---> writting " + outFileName);
    IOUtils.write(new File(outFileName), header + dataFrame.toString(true, false));

    //		System.out.println("feature column names = " +
    // ListUtils.toString(dataset.getFeatureData().getDataFrame().getColumn("ProbeUID")));

    //		line.append("#ID\tChr\tStart\tEnd\t").append(ListUtils.toString(dataset.getSampleNames(),
    // "\t"));
    //		lines.add(line.toString());
    //
    //		for(int i=0 ; i<dataset.getRowDimension() ; i++) {
    //
    //		}
    //
    //
    //		IOUtils.write(new File(outFileName), lines);
  }
Exemplo n.º 3
0
  private void saveGeneSetAnalysisResults(GeneSetAnalysis gsea, FunctionalDbDescriptor filterInfo)
      throws IOException {

    String fileName = filterInfo.getName() + ".txt";
    String annotFileName = filterInfo.getName() + ".annot";

    // save statistic results
    IOUtils.write(outdir + "/" + fileName, ListUtils.toString(gsea.resultsToStringList(), "\n"));
    result.addOutputItem(
        new Item(
            filterInfo.getName(),
            fileName,
            filterInfo.getTitle(),
            Item.TYPE.FILE,
            Arrays.asList(filterInfo.getPrefix().toUpperCase() + "_TERM"),
            new HashMap<String, String>(),
            "All results"));

    // save significant results
    int numberOfSignificantTerms;
    List<GeneSetAnalysisTestResult> significant;
    String formattedPValue;
    for (int i = 0; i < DEFAULT_PVALUES.length; i++) {
      formattedPValue = pvalueFormatter.format(DEFAULT_PVALUES[i]);
      significant = gsea.getSignificant(DEFAULT_PVALUES[i]);
      if (significant != null && significant.size() > 0) {
        IOUtils.write(
            outdir + "/significant_" + filterInfo.getName() + "_" + formattedPValue + ".txt",
            gsea.significantResultsToStringList(significant, DEFAULT_PVALUES[i]));
        numberOfSignificantTerms = significant.size();
      } else {
        numberOfSignificantTerms = 0;
      }
      significantCount
          .get(i)
          .append(filterInfo.getTitle())
          .append("\t")
          .append(numberOfSignificantTerms)
          .append("\n");
      if (numberOfSignificantTerms > 0) {
        // go graph
        if (filterInfo.getPrefix().equalsIgnoreCase("go")) {
          try {
            String limitMessage = "";
            boolean outOfbounds = createGseaGoGraph(significant, DEFAULT_PVALUES[i], filterInfo);
            if (outOfbounds) limitMessage = " just most 100 significant terms";
            Item item =
                new Item(
                    "go_graph_significant_" + filterInfo.getName() + "_" + formattedPValue,
                    "go_graph_" + filterInfo.getName() + "_" + formattedPValue + "_graphimage.png",
                    filterInfo.getTitle()
                        + " DAG (significant terms, pvalue<"
                        + formattedPValue
                        + ") "
                        + limitMessage,
                    Item.TYPE.IMAGE,
                    Arrays.asList("SIGNIFICANT,THUMBNAIL,GO_GRAPH_VIZ_JNLP"),
                    new HashMap<String, String>(),
                    "Significant Results." + filterInfo.getTitle());
            item.setContext("pvalue==" + formattedPValue);
            result.getOutputItems().add(4, item);
          } catch (GoGraphException gge) {
            Item item =
                new Item(
                    "go_graph_significant_" + filterInfo.getName() + "_" + formattedPValue,
                    "Graph not found",
                    filterInfo.getTitle()
                        + " DAG (significant terms, pvalue<"
                        + formattedPValue
                        + ")",
                    Item.TYPE.MESSAGE,
                    Arrays.asList("ERROR"),
                    new HashMap<String, String>(),
                    "Significant Results." + filterInfo.getTitle());
            item.setContext("pvalue==" + formattedPValue);
            result.getOutputItems().add(4, item);
          }
        }
        // table
        Item item =
            new Item(
                "significant_" + filterInfo.getName(),
                "significant_" + filterInfo.getName() + "_" + formattedPValue + ".txt",
                filterInfo.getTitle() + " significant terms (pvalue<" + formattedPValue + ")",
                Item.TYPE.FILE,
                Arrays.asList(
                    "SIGNIFICANT",
                    "TABLE",
                    gsea.getMethod().toUpperCase() + "_TABLE",
                    filterInfo.getPrefix().toUpperCase() + "_TERM"),
                new HashMap<String, String>(),
                "Significant Results." + filterInfo.getTitle());
        item.setContext("pvalue==" + formattedPValue);
        result.getOutputItems().add(4, item);

      } else {
        Item item =
            new Item(
                "significant_" + filterInfo.getName(),
                "No significant terms for current pvalue " + formattedPValue,
                filterInfo.getTitle() + " significant terms (pvalue<" + formattedPValue + ")",
                Item.TYPE.MESSAGE,
                Arrays.asList("WARNING"),
                new HashMap<String, String>(),
                "Significant Results." + filterInfo.getTitle());
        item.setContext("pvalue==" + formattedPValue);
        result.getOutputItems().add(4, item);
      }
    }

    // annotations report
    addAnnotationReport(gsea, filterInfo.getTitle());

    // save annotations
    if (gsea.getAnnotations() != null && gsea.getAnnotations().size() > 0) {
      IOUtils.write(outdir + "/" + annotFileName, gsea.getAnnotations().toString());
      result.addOutputItem(
          new Item(
              "annot_" + filterInfo.getName(),
              annotFileName,
              "Annotations for " + filterInfo.getTitle(),
              Item.TYPE.FILE,
              Arrays.asList("ANNOTATION"),
              new HashMap<String, String>(),
              "Annotation files"));
    } else {
      result.addOutputItem(
          new Item(
              "annot_" + filterInfo.getName(),
              "no annotations found for input ids",
              "Annotations for " + filterInfo.getTitle(),
              Item.TYPE.MESSAGE,
              Arrays.asList("WARNING"),
              new HashMap<String, String>(),
              "Annotation files"));
    }
  }
Exemplo n.º 4
0
  @Override
  protected void execute() {
    try {

      // update status
      jobStatus.addStatusMessage("10", "Preparing data");

      // infrared connector
      //			DBConnector dbConnector = new DBConnector(species, new File(babelomicsHomePath +
      // "/conf/infrared.properties"));

      // prepare params
      prepare();

      // run fatigo's
      if (filterList.size() == 0 && !isYourAnnotations) {
        throw new ParseException("No biological database selected (eg. --go-bp)");
      } else {

        // save id lists
        IOUtils.write(outdir + "/ranked_list.txt", rankedList.toString());
        result.addOutputItem(
            new Item(
                "ranked_list",
                "ranked_list.txt",
                "Ranked list",
                Item.TYPE.FILE,
                Arrays.asList("RANKED_LIST", "CLEAN"),
                new HashMap<String, String>(),
                "Input data"));

        // order ranked list and save genes and rank separately
        //				FatiScan fatiscan = new
        // FatiScan(rankedList,null,dbConnector,FatiScan.DEFAULT_NUMBER_OF_PARTITIONS,testMode,outputFormat,order,duplicatesMode);
        FatiScan fatiscan =
            new FatiScan(
                rankedList,
                null,
                null,
                FatiScan.DEFAULT_NUMBER_OF_PARTITIONS,
                testMode,
                outputFormat,
                order,
                duplicatesMode);
        fatiscan.prepareLists();
        IOUtils.write(outdir + "/id_list.txt", fatiscan.getIdList());
        result.addOutputItem(
            new Item(
                "id_list",
                "id_list.txt",
                "Id list (sorted)",
                Item.TYPE.FILE,
                Arrays.asList("IDLIST", "SORTED"),
                new HashMap<String, String>(),
                "Input data"));
        IOUtils.write(outdir + "/statistic.txt", ListUtils.toStringArray(fatiscan.getStatistic()));
        result.addOutputItem(
            new Item(
                "statistic",
                "statistic.txt",
                "Statistic (sorted)",
                Item.TYPE.FILE,
                Arrays.asList("STATISTIC", "SORTED"),
                new HashMap<String, String>(),
                "Input data"));

        // significant count
        significantCount = new ArrayList<StringBuilder>();
        for (int i = 0; i < DEFAULT_PVALUES.length; i++) {
          significantCount.add(new StringBuilder("#DB\tNº of significant terms\n"));
        }

        // annotation report
        annotationReport = new StringBuilder();
        annotationReport.append("#DB").append("\t").append("Number of annotations").append("\n");

        // do fatiscans
        double progress = 20;
        double inc = 60.0 / filterList.size();
        for (FunctionalFilter filter : filterList) {
          doTest(rankedList, filter, method);
          jobStatus.addStatusMessage("" + progress, "Executing test");
          progress += inc;
        }

        // update status
        jobStatus.addStatusMessage("90", "Executing test");

        if (isYourAnnotations) {
          doYourAnnotationsTest(rankedList, yourAnnotations, method);
        }

        // update status
        jobStatus.addStatusMessage("95", "Saving results");

        saveAnnotationsReport();

        // save significant count table
        for (int i = 0; i < DEFAULT_PVALUES.length; i++) {
          IOUtils.write(
              outdir + "/significant_count_" + pvalueFormatter.format(DEFAULT_PVALUES[i]) + ".txt",
              significantCount.get(i).toString());
        }
        result
            .getOutputItems()
            .add(
                3,
                new Item(
                    "significant",
                    "significant_count_${pvalue}.txt",
                    "Number of significant terms per DB",
                    Item.TYPE.FILE,
                    Arrays.asList("TABLE,SUMMARY_TABLE,SIGNIFICANT_COUNT_TABLE"),
                    new HashMap<String, String>(),
                    "Significant Results"));
        result.addMetaItem(new Item("flags", "SHOW_PVALUES", "", TYPE.MESSAGE));
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 5
0
  private void setFoldChangeResults(
      Dataset subDataset, String test, String testLabel, double[] res, String className)
      throws InvalidIndexException, IOException {

    //		int[] columnOrder =
    // ListUtils.order(subDataset.getVariables().getByName(className).getValues());
    int[] rowOrder = ListUtils.order(ArrayUtils.toList(res), true);

    DataFrame dataFrame = new DataFrame(subDataset.getFeatureNames().size(), 0);
    dataFrame.addColumn(test, ListUtils.ordered(ArrayUtils.toStringList(res), rowOrder));
    dataFrame.setRowNames(ListUtils.ordered(subDataset.getFeatureNames(), rowOrder));

    FeatureData featureData = new FeatureData(dataFrame);
    File file = new File(outdir + "/" + test + "_foldchange.txt");
    IOUtils.write(file, dataFrame.toString(true, true));

    /** Get significative terms, babelomics 5 * */
    List<String> featureNames = new ArrayList<String>();
    List<String> featureValues = new ArrayList<String>();

    List<String> featureNamesUp = new ArrayList<String>();
    List<String> featureNamesDown = new ArrayList<String>();

    for (String rowName : dataFrame.getRowNames()) {
      List<String> row = dataFrame.getRow(rowName);
      double stats = Double.parseDouble(row.get(0));
      if (Math.abs(stats) >= foldChangeValue) {
        featureNames.add(rowName);
        featureValues.add(row.get(0));
        if (stats >= 0) featureNamesUp.add(rowName);
        else featureNamesDown.add(rowName);
      }
    }

    dataFrame = new DataFrame(featureNames.size(), 0);
    dataFrame.addColumn(test, featureValues);
    dataFrame.setRowNames(featureNames);

    List<Integer> sigRowIndexes = new ArrayList<Integer>();
    for (String feat : featureNames) {
      int idx = 0;
      for (String featSub : subDataset.getFeatureNames()) {
        if (feat.equalsIgnoreCase(featSub)) sigRowIndexes.add(idx);
        idx++;
      }
    }
    DoubleMatrix doubleMatrix =
        new DoubleMatrix(dataFrame.getRowDimension(), subDataset.getColumnDimension());
    for (int i = 0; i < sigRowIndexes.size(); i++) {
      doubleMatrix.setRow(i, subDataset.getDoubleMatrix().getRow(sigRowIndexes.get(i)));
    }
    File fileXX = new File(outdir + "/" + test + "_foldchange_significative_dataset.txt");
    Dataset sigDataset = new Dataset(subDataset.getSampleNames(), featureNames, doubleMatrix);
    sigDataset.save(fileXX);

    File fileAux = new File(outdir + "/" + test + "_foldchange_significative_table.txt");
    IOUtils.write(fileAux, dataFrame.toString(true, true));

    fileAux = new File(outdir + "/" + test + "_foldchange_significative_table_up.txt");
    IOUtils.write(fileAux, featureNamesUp);

    fileAux = new File(outdir + "/" + test + "_foldchange_significative_table_down.txt");
    IOUtils.write(fileAux, featureNamesDown);

    //        fileAux = new File(outdir + "/" + test + "_foldchange_significative_dataset.txt");
    //        IOUtils.write(fileAux, featureNamesDown);

    //
    //		List<String> featuresUp = new ArrayList<String>();
    //		List<String> featuresDown = new ArrayList<String>();
    //		for (String rowName : dataFrame.getRowNames()) {
    //                List<String> row = dataFrame.getRow(rowName);
    //                double stats = Double.parseDouble(row.get(0));
    //                if(stats>=foldChangeValue){
    //
    //
    ////                    featuresUp.add(rowName);
    ////                else
    ////                    featuresDown.add(rowName);
    //				}
    //            }
    //			file = new File(tool.getOutdir() + "/" + test + "_significative_table_up.txt");
    //			IOUtils.write(file, featuresUp);
    //			file = new File(tool.getOutdir() + "/" + test + "_significative_table_down.txt");
    //			IOUtils.write(file, featuresDown);

    // featureData.save(file);
    if (file.exists()) {
      result.addOutputItem(
          new Item(
              test + "_foldchange",
              file.getName(),
              testLabel + " fold-change output file",
              TYPE.FILE,
              new ArrayList<String>(),
              new HashMap<String, String>(),
              testLabel + " fold-change.Output files"));

      String json =
          "{\\\"paramfilename\\\": \\\"input_params.txt\\\", \\\"testfilename\\\": \\\""
              + file.getName()
              + "\\\"}";
      result.addOutputItem(
          new Item(
              "diff_expr_" + StringUtils.randomString(8),
              json,
              "Significative results",
              TYPE.FILE,
              StringUtils.toList("DIFF_EXPRESSION_VIEWER"),
              new HashMap<String, String>(),
              testLabel + " fold-change. Significative results"));

      DiffExpressionUtils.createFatiScanRedirection(
          dataFrame, test, test, result, outdir, testLabel + " fold-change.");
    }

    /*
    		List<Double> orderedRes = ListUtils.ordered(ArrayUtils.toList(res), rowOrder);
    		int posValues = 0;
    		int negValues = 0;
    		for(int i=0 ; i<orderedRes.size() ; i++) {
    			if (Math.abs(orderedRes.get(i))>foldChangeValue) {
    				if (orderedRes.get(i)>0) {
    					posValues++;
    				} else {
    					negValues++;
    				}
    			}
    		}

    		if (posValues + negValues == 0) {
    			result.addOutputItem(new Item("no_sig_results", "No significative results (fold-change value = " + foldChangeValue + ")", "Significative results", TYPE.MESSAGE, new ArrayList<String>(), new HashMap<String, String>(2), testLabel + " fold-change.Significative results"));
    			return;
    		}

    		int halfDisplay = maxDisplay/2;
    		int posValuesToDisplay = posValues;
    		int negValuesToDisplay = negValues;
    		if (posValues + negValues > maxDisplay) {
    			if (Math.min(posValues, negValues)>halfDisplay) {
    				posValuesToDisplay = halfDisplay;
    				negValuesToDisplay = halfDisplay;
    			} else {
    				posValuesToDisplay = posValues>negValues ? (maxDisplay-negValues) : posValues;
    				negValuesToDisplay = negValues>posValues ? (maxDisplay-posValues) : negValues;
    			}
    		}

    		int nbToDisplay = posValuesToDisplay + negValuesToDisplay;
    		DoubleMatrix doubleMatrix = new DoubleMatrix(nbToDisplay, subDataset.getColumnDimension());
    		int rowIndex = 0;
    		int negLimit = rowOrder.length-negValuesToDisplay;
    		List<Integer> sigRowIndexes = new ArrayList<Integer>();
    		for(int i=0 ; i<rowOrder.length ; i++) {
    			if (i<posValuesToDisplay || i>=negLimit) {
    				doubleMatrix.setRow(rowIndex++, subDataset.getDoubleMatrix().getRow(rowOrder[i]));
    				//System.out.println(subDataset.getFeatureNames().get(sigOrder[i]));
    				sigRowIndexes.add(rowOrder[i]);
    			}
    		}

    		file = new File(outdir + "/" + test +"_fold_change_significative_dataset.txt");
    		Dataset sigDataset = new Dataset(subDataset.getSampleNames(), ListUtils.subList(subDataset.getFeatureNames(), ListUtils.toIntArray(sigRowIndexes)), doubleMatrix);
    		sigDataset.setVariables(subDataset.getVariables());
    		sigDataset.validate();
    		sigDataset.save(file);
    		if (file.exists()) {
    			String tags = "datamatrix,expression";
    			result.addOutputItem(new Item(test + "_sig_dataset", file.getName(), "Significative values dataset (fold-change value = " + foldChangeValue + ")", TYPE.DATA, StringUtils.toList(tags, ","), new HashMap<String, String>(2),  testLabel + " fold-change.Significative results"));

    			File redirectionFile = new File(outdir + "/clustering.redirection");
    			DiffExpressionUtils.createClusteringRedirectionFile(redirectionFile, file);
    			if ( redirectionFile.exists() ) {
    				tags = "REDIRECTION(" + redirectionFile.getName() + ":Send to Clustering tool...)";
    				result.addOutputItem(new Item(test + "_sig_dataset", file.getName(), "Significative values dataset (fold-change value = " + foldChangeValue + ")", TYPE.FILE, StringUtils.toList(tags, ","), new HashMap<String, String>(2),  testLabel + " fold-change.Significative results"));
    			}
    		}

    		rowOrder = ListUtils.order(ListUtils.subList(ArrayUtils.toList(res), ListUtils.toIntArray(sigRowIndexes)), true);

    		DataFrame sigDataFrame = new DataFrame(sigDataset.getFeatureNames().size(), 0);

    		sigDataFrame.addColumn(test, ListUtils.toStringList(ListUtils.ordered(ListUtils.subList(ArrayUtils.toList(res), ListUtils.toIntArray(sigRowIndexes)), rowOrder)));
    		sigDataFrame.setRowNames(ListUtils.ordered(ListUtils.subList(subDataset.getFeatureNames(), ListUtils.toIntArray(sigRowIndexes)), rowOrder));

    		// adding table to results
    		//
    		file = new File(outdir + "/" + test + "fold_change_significative_table.txt");
    		IOUtils.write(file, sigDataFrame.toString(true, true));
    		if ( file.exists() ) {
    			result.addOutputItem(new Item(test + "fold_change_table", file.getName(), "Significative values table (fold-change value = " + foldChangeValue + ")", TYPE.FILE, StringUtils.toList("TABLE," + test.toUpperCase() + "_FOLD_CHANGE_TABLE", ","), new HashMap<String, String>(2), testLabel + " fold-change.Significative results"));
    		}

    		// adding heatmap to results
    		//
    		Canvas sigHeatmap = DiffExpressionUtils.generateHeatmap(sigDataset, className, columnOrder, rowOrder, testLabel, ListUtils.toDoubleArray(ListUtils.subList(ArrayUtils.toList(res), ListUtils.toIntArray(sigRowIndexes))), null, null);
    		if (sigHeatmap == null) {
    			printError("ioexception_execute_fold_change_classcomparison", "ERROR", "Error generating " + test + " fold-change heatmap image");
    		} else {
    			try {
    				File sigHeatmapFile = new File(outdir + "/" + test + "fold_change_heatmap_significative.png");
    				sigHeatmap.save(sigHeatmapFile.getAbsolutePath());
    				if (sigHeatmapFile.exists()) {
    					result.addOutputItem(new Item(test + "_fold_change_heatmap_significative", sigHeatmapFile.getName(), testLabel + " fold-change heatmap with significative values (fold-change value = " + foldChangeValue + ")", TYPE.IMAGE, new ArrayList<String>(2), new HashMap<String, String>(2), testLabel + " fold-change.Significative results"));
    				}
    			} catch (IOException e) {
    				printError("ioexception_execute_fold_change_classcomparison", "ERROR", "Error saving " + test + " fold-change heatmap image");
    			}
    		}
    		DiffExpressionUtils.createFatiGoRedirection(dataFrame.getRowNames(), dataFrame.getColumnAsDoubleArray(test), test, result, outdir, testLabel + " fold-change.");
    		DiffExpressionUtils.createFatiScanRedirection(sigDataFrame, test, test, result, outdir, testLabel + " fold-change.");
    */
  }
Exemplo n.º 6
0
  public LayoutResp layout(
      String layoutAlgorithm,
      String outputFormat,
      String dotData,
      String filename,
      String base64,
      String jsonpCallback) {
    LayoutResp resp = new LayoutResp();

    logger.debug("LayoutWSServer: layout() method");
    if (graphvizLayoutAlgorithms.contains(layoutAlgorithm)) {
      if (graphvizOutputFormats.containsKey(outputFormat)) {
        if (dotData != null && !dotData.equals("")) {
          logger.debug(
              "Algorithm layout: "
                  + layoutAlgorithm
                  + ", output format: "
                  + outputFormat
                  + ", dot: "
                  + dotData);
          try {
            logger.info("defaultConfig:" + config.toString());
            String randomFolder =
                config.getProperty("TMP.FOLDER") + "/" + StringUtils.randomString(20) + "_layout";
            logger.debug("Creating output folder: " + randomFolder);
            FileUtils.createDirectory(randomFolder);

            String inputFile = randomFolder + "/input.dot";
            String outputFile = randomFolder + "/" + filename + "." + outputFormat;
            logger.debug("Writting dot data file: " + inputFile);
            IOUtils.write(inputFile, dotData);

            int exitValue =
                executeGraphviz(
                    new File(inputFile), layoutAlgorithm, outputFormat, new File(outputFile));
            if (exitValue == 0) {
              FileUtils.checkFile(outputFile);
              if (base64 != null && base64.trim().equalsIgnoreCase("true")) {
                logger.debug("Encoding in Base64 the dot output file...");
                byte[] binaryBytes = toByteArray(new FileInputStream(outputFile));
                byte[] base64Bytes = Base64.encodeBase64(binaryBytes);
                String encodedString = new String(base64Bytes);

                if (jsonpCallback != null && !jsonpCallback.equals("")) {
                  //									return Response.ok("var " + jsonpCallback + " = (" + encodedString +
                  // ")", MediaType.APPLICATION_JSON_TYPE).build();
                  resp.setData("var " + jsonpCallback + " = (" + encodedString + ")");
                  resp.setType("json");
                  return resp;
                } else {
                  //									return Response.ok(encodedString,
                  // MediaType.TEXT_PLAIN).header("content-disposition","attachment; filename =
                  // "+filename+"."+outputFormat).build();
                  //									return Response.ok(encodedString, MediaType.TEXT_PLAIN).build();
                  resp.setData(encodedString);
                  resp.setType("text");
                  return resp;
                }
              } else {
                // returning the Graphviz output file
                byte[] bytes = toByteArray(new FileInputStream(new File(outputFile)));
                //								return Response.ok(bytes,
                // MediaType.APPLICATION_OCTET_STREAM).header("content-disposition","attachment;
                // filename = "+filename+"."+outputFormat).build();
                //								return createOkResponse(bytes, MediaType.APPLICATION_OCTET_STREAM_TYPE,
                // filename+"."+outputFormat);
                resp.setData(bytes);
                resp.setType("bytes");
                resp.setFileName(filename + "." + outputFormat);
                return resp;
              }
            } else {
              //							return Response.ok("Graphviz exit status not 0: '"+exitValue+"'",
              // MediaType.TEXT_PLAIN).header("content-disposition","attachment; filename =
              // "+filename+".err.log").build();
              //							return createOkResponse("Graphviz exit status not 0: '"+exitValue+"'",
              // MediaType.TEXT_PLAIN_TYPE, filename+".err.log");
              resp.setData("Graphviz exit status not 0: '" + exitValue + "'");
              resp.setType("text");
              resp.setFileName(filename + ".err.log");
              return resp;
            }
          } catch (Exception e) {
            logger.error(
                "Error in LayoutWSServer, layout() method: " + StringUtils.getStackTrace(e));
            if (base64 != null && base64.trim().equalsIgnoreCase("true")) {
              //							return Response.ok("Error in LayoutWSServer, layout()
              // method:\n"+StringUtils.getStackTrace(e), MediaType.TEXT_PLAIN).build();
              //							return createOkResponse("Error in LayoutWSServer, layout()
              // method:\n"+StringUtils.getStackTrace(e), MediaType.TEXT_PLAIN_TYPE);
              resp.setData(
                  "Error in LayoutWSServer, layout() method:\n" + StringUtils.getStackTrace(e));
              resp.setType("text");
              return resp;
            } else {
              //							return Response.ok("Error in LayoutWSServer, layout()
              // method:\n"+StringUtils.getStackTrace(e),
              // MediaType.TEXT_PLAIN).header("content-disposition","attachment; filename =
              // "+filename+".err.log").build();
              //							return createOkResponse("Error in LayoutWSServer, layout()
              // method:\n"+StringUtils.getStackTrace(e), MediaType.TEXT_PLAIN_TYPE,
              // filename+".err.log");
              resp.setData(
                  "Error in LayoutWSServer, layout() method:\n" + StringUtils.getStackTrace(e));
              resp.setType("text");
              resp.setFileName(filename + ".err.log");
              return resp;
            }
          }
        } else {
          if (base64 != null && base64.trim().equalsIgnoreCase("true")) {
            //						return Response.ok("dot data '"+dotData+"' is not valid",
            // MediaType.TEXT_PLAIN).build();
            //						return createOkResponse("dot data '"+dotData+"' is not valid",
            // MediaType.TEXT_PLAIN_TYPE);
            resp.setData("dot data '" + dotData + "' is not valid");
            resp.setType("text");
            return resp;
          } else {
            //						return Response.ok("dot data '"+dotData+"' is not valid",
            // MediaType.TEXT_PLAIN).header("content-disposition","attachment; filename =
            // "+filename+".err.log").build();
            //						return createOkResponse("dot data '"+dotData+"' is not valid",
            // MediaType.TEXT_PLAIN_TYPE, filename+".err.log");
            resp.setData("dot data '" + dotData + "' is not valid");
            resp.setType("text");
            resp.setFileName(filename + ".err.log");
            return resp;
          }
        }
      } else {
        if (base64 != null && base64.trim().equalsIgnoreCase("true")) {
          //					return Response.ok("Format '"+outputFormat+"' is not valid",
          // MediaType.TEXT_PLAIN).build();
          //					return createOkResponse("Format '"+outputFormat+"' is not valid",
          // MediaType.TEXT_PLAIN_TYPE);
          resp.setData("Format '" + outputFormat + "' is not valid");
          resp.setType("text");
          return resp;
        } else {
          //					return Response.ok("Format '"+outputFormat+"' is not valid",
          // MediaType.TEXT_PLAIN).header("content-disposition","attachment; filename =
          // "+filename+".err.log").build();
          //					return createOkResponse("Format '"+outputFormat+"' is not valid",
          // MediaType.TEXT_PLAIN_TYPE, filename+".err.log");
          resp.setData("Format '" + outputFormat + "' is not valid");
          resp.setType("text");
          resp.setFileName(filename + ".err.log");
          return resp;
        }
      }
    } else {
      if (base64 != null && base64.trim().equalsIgnoreCase("true")) {
        //				return Response.ok("Algorithm '"+layoutAlgorithm+"' is not valid",
        // MediaType.TEXT_PLAIN).build();
        //				return createOkResponse("Algorithm '"+layoutAlgorithm+"' is not valid",
        // MediaType.TEXT_PLAIN_TYPE);
        resp.setData("Algorithm '" + layoutAlgorithm + "' is not valid");
        resp.setType("text");
        return resp;
      } else {
        //				return Response.ok("Algorithm '"+layoutAlgorithm+"' is not valid",
        // MediaType.TEXT_PLAIN).header("content-disposition","attachment; filename =
        // "+filename+".err.log").build();
        //				return createOkResponse("Algorithm '"+layoutAlgorithm+"' is not valid",
        // MediaType.TEXT_PLAIN_TYPE, filename+".err.log");
        resp.setData("Algorithm '" + layoutAlgorithm + "' is not valid");
        resp.setType("text");
        resp.setFileName(filename + ".err.log");
        return resp;
      }
    }
  }
Exemplo n.º 7
0
  public LayoutResp coordinates(String layoutAlgorithm, String dotData, String jsonpCallback) {
    LayoutResp resp = new LayoutResp();

    logger.debug("LayoutWSServer:  coordinates() method");
    if (graphvizLayoutAlgorithms.contains(layoutAlgorithm)) {
      StringBuilder sb = new StringBuilder("{");
      try {
        String randomFolder =
            config.getProperty("TMP.FOLDER") + "/" + StringUtils.randomString(20) + "_layout";
        logger.debug("Creating output folder: " + randomFolder);
        FileUtils.createDirectory(randomFolder);

        String inputFile = randomFolder + "/input.dot";
        String outputFile = randomFolder + "/output.plain";
        logger.debug("Writting dot data file: " + inputFile);
        IOUtils.write(inputFile, dotData);

        int exitValue =
            executeGraphviz(new File(inputFile), layoutAlgorithm, "plain", new File(outputFile));
        if (exitValue == 0) {
          FileUtils.checkFile(outputFile);
          // getting the coords form the file
          List<String> lines = IOUtils.grep(new File(outputFile), "^node.+");
          String[] fields;
          double min = Double.POSITIVE_INFINITY;
          double max = Double.NEGATIVE_INFINITY;
          String[] ids = new String[lines.size()];
          double[][] coords = new double[lines.size()][2];
          for (int i = 0; i < lines.size(); i++) {
            fields = lines.get(i).split(" ");
            ids[i] = fields[1];
            coords[i][0] = Double.parseDouble(fields[2]);
            coords[i][1] = Double.parseDouble(fields[3]);
            min = Math.min(min, Math.min(coords[i][0], coords[i][1]));
            max = Math.max(max, Math.max(coords[i][0], coords[i][1]));
          }
          // max needs to be calculated after subtract min
          max -= min;
          for (int i = 0; i < ids.length; i++) {
            sb.append("\"" + ids[i] + "\"")
                .append(": {")
                .append("\"id\":\"")
                .append(ids[i])
                .append("\", \"x\": ")
                .append((coords[i][0] - min) / max)
                .append(", \"y\": ")
                .append((coords[i][1] - min) / max)
                .append("}");
            if (i < ids.length - 1) {
              sb.append(", ");
            }
          }
          sb.append("}");

          if (jsonpCallback != null && !jsonpCallback.equals("")) {
            //						return Response.ok("var " + jsonpCallback + " = (" + sb.toString() + ")",
            // MediaType.APPLICATION_JSON_TYPE).build();
            //						return createOkResponse("var " + jsonpCallback + " = (" + sb.toString() + ")",
            // MediaType.APPLICATION_JSON_TYPE);
            resp.setData("var " + jsonpCallback + " = (" + sb.toString() + ")");
            resp.setType("json");
            return resp;
          } else {
            //						return Response.ok(sb.toString(), MediaType.TEXT_PLAIN).build();
            //						return createOkResponse(sb.toString(), MediaType.TEXT_PLAIN_TYPE);
            resp.setData(sb.toString());
            resp.setType("text");
            return resp;
          }
        } else {
          //					return Response.ok("Graphviz exit status not 0: '"+exitValue+"'",
          // MediaType.TEXT_PLAIN).build();
          //					return createOkResponse("Graphviz exit status not 0: '"+exitValue+"'",
          // MediaType.TEXT_PLAIN_TYPE);
          resp.setData("Graphviz exit status not 0: '" + exitValue + "'");
          resp.setType("text");
          return resp;
        }
      } catch (Exception e) {
        logger.error("Error in LayoutWSServer, layout() method: " + StringUtils.getStackTrace(e));
        //				return Response.ok("Error in LayoutWSServer, coordinates()
        // method:\n"+StringUtils.getStackTrace(e), MediaType.TEXT_PLAIN).build();
        //				return createOkResponse("Error in LayoutWSServer, coordinates()
        // method:\n"+StringUtils.getStackTrace(e), MediaType.TEXT_PLAIN_TYPE);
        resp.setData(
            "Error in LayoutWSServer, coordinates() method:\n" + StringUtils.getStackTrace(e));
        resp.setType("text");
        return resp;
      }
    } else {
      //			return Response.ok("Algorithm '"+layoutAlgorithm+"' is not valid",
      // MediaType.TEXT_PLAIN).build();
      //			return createOkResponse("Algorithm '"+layoutAlgorithm+"' is not valid",
      // MediaType.TEXT_PLAIN_TYPE);
      resp.setData("Algorithm '" + layoutAlgorithm + "' is not valid");
      resp.setType("text");
      return resp;
    }
  }