@Test
 public void testMFCombiningWithUserNetwork() throws Exception {
   RelatedGenesEngineResponseDto relatedResponse = regressionHelper(CombiningMethod.MF);
   assertNotNull(relatedResponse);
   assertNotNull(relatedResponse.getNetworks());
   assertFalse(relatedResponse.getNetworks().size() == 0);
 }
 private RelatedGenesEngineResponseDto runQuery(RelatedGenesEngineRequestDto request)
     throws DataStoreException {
   try {
     request.setProgressReporter(NullProgressReporter.instance());
     RelatedGenesEngineResponseDto result;
     result = mania.findRelated(request);
     request.setCombiningMethod(result.getCombiningMethodApplied());
     networkUtils.normalizeNetworkWeights(result);
     return result;
   } catch (ApplicationException e) {
     Logger logger = Logger.getLogger(getClass());
     logger.error("Unexpected error", e); // $NON-NLS-1$
     return null;
   }
 }
 private double[] computeEdgeWeightExtrema(RelatedGenesEngineResponseDto response) {
   double[] extrema = new double[] {1, 0};
   for (NetworkDto network : response.getNetworks()) {
     for (InteractionDto interaction : network.getInteractions()) {
       double weight = interaction.getWeight() * network.getWeight();
       if (extrema[0] > weight) {
         extrema[0] = weight;
       }
       if (extrema[1] < weight) {
         extrema[1] = weight;
       }
     }
   }
   return extrema;
 }
  public void dumpNetworks(RelatedGenesEngineResponseDto relatedResponse) {

    System.out.println("num networks in result: " + relatedResponse.getNetworks().size());
    for (NetworkDto n : relatedResponse.getNetworks()) {
      System.out.println(
          "  network id: "
              + n.getId()
              + " weight: "
              + n.getWeight()
              + " num interactions: "
              + n.getInteractions().size());
      for (InteractionDto interaction : n.getInteractions()) {
        /*
        System.out.println(String.format("   inter: node %s %s, node %s %s, %s",
        interaction.getNodeVO1().getId(),
        interaction.getNodeVO1().getScore(),
        interaction.getNodeVO2().getId(),
        interaction.getNodeVO2().getScore(),
        interaction.getWeight()
        ));
         */
      }
    }
  }
  private EnrichmentEngineRequestDto createEnrichmentRequest(
      RelatedGenesEngineResponseDto response) {
    if (human.getOntology() == null) {
      return null;
    }
    EnrichmentEngineRequestDto request = new EnrichmentEngineRequestDto();
    request.setProgressReporter(NullProgressReporter.instance());
    request.setMinCategories(MIN_CATEGORIES);
    request.setqValueThreshold(Q_VALUE_THRESHOLD);

    request.setOrganismId(human.getId());
    request.setOntologyId(human.getOntology().getId());

    Set<Long> nodes = new HashSet<Long>();
    for (NetworkDto network : response.getNetworks()) {
      for (InteractionDto interaction : network.getInteractions()) {
        nodes.add(interaction.getNodeVO1().getId());
        nodes.add(interaction.getNodeVO2().getId());
      }
    }
    request.setNodes(nodes);
    return request;
  }
  /**
   * add a user-network to data set, then compute a result using BP combining from the core networks
   * *not* including the user network that was just uploaded
   */
  @Test
  public void testBPCombiningWithoutUserNetwork() throws Exception {
    System.out.println("averageCombiningWithUserNetworkChaChaCha");
    int orgId = 1;
    int userNetworkId = -1;
    String userName = "******";

    // add a user network

    //        INetworkSymMatrixProvider provider = new CacheNetworkSymMatrixProvider(userName,
    // orgId, randomCacheBuilder.getCache());
    UserNetworkProcessor instance =
        new UserNetworkProcessor(randomCacheBuilder.getCache(), randomCacheBuilder.getCacheDir());

    UploadNetworkEngineRequestDto uploadRequest = new UploadNetworkEngineRequestDto();
    uploadRequest.setLayout(DataLayout.PROFILE);
    uploadRequest.setMethod(NetworkProcessingMethod.PEARSON);
    uploadRequest.setNamespace(userName);
    uploadRequest.setProgressReporter(NullProgressReporter.instance());
    uploadRequest.setOrganismId(orgId);
    uploadRequest.setNetworkId(userNetworkId);
    uploadRequest.setSparsification(50);

    String data = "id\tf1\tf2\tf3\n10000\t1.5\t3.0\t4.0\n10001\t2.0\t2.2\t2.1\n";
    uploadRequest.setData(new StringReader(data));

    UploadNetworkEngineResponseDto uploadResponse = instance.process(uploadRequest);
    assertNotNull(uploadResponse);

    // now send in a request for related genes, involving all the old networks plus
    // the new one

    IMania mania = new Mania2(randomCacheBuilder.getCache());

    // create request for organism 1
    RelatedGenesEngineRequestDto relatedRequest = new RelatedGenesEngineRequestDto();
    relatedRequest.setNamespace(userName);
    relatedRequest.setOrganismId(orgId);
    relatedRequest.setCombiningMethod(CombiningMethod.BP);
    relatedRequest.setScoringMethod(ScoringMethod.DISCRIMINANT);

    long[] nodeIds = randomCacheBuilder.getCache().getNodeIds(orgId).getNodeIds();

    // first 10 nodes as +ve
    ArrayList<Long> posNodes = new ArrayList<Long>();
    for (int i = 0; i < 10; i++) {
      posNodes.add((long) nodeIds[i]);
    }
    relatedRequest.setPositiveNodes(posNodes);

    // all networks less one
    Collection<Collection<Long>> requestNetworks = new ArrayList<Collection<Long>>();

    ArrayList<Long> group = new ArrayList<Long>();
    for (int i = 0; i < networkIds.length; i++) {
      group.add(networkIds[i]);
    }

    // group.add((long) userNetworkId);

    requestNetworks.add(group);
    relatedRequest.setInteractionNetworks(requestNetworks);
    relatedRequest.setLimitResults(10);
    relatedRequest.setProgressReporter(NullProgressReporter.instance());

    // compute result
    RelatedGenesEngineResponseDto relatedResponse = mania.findRelated(relatedRequest);
    assertNotNull(relatedResponse);
    assertNotNull(relatedResponse.getNetworks());
    assertFalse(relatedResponse.getNetworks().size() == 0);

    System.out.println("num networks in result: " + relatedResponse.getNetworks().size());
    for (NetworkDto n : relatedResponse.getNetworks()) {
      System.out.println(
          "  network id: "
              + n.getId()
              + " weight: "
              + n.getWeight()
              + " num interactions: "
              + n.getInteractions().size());
      for (InteractionDto interaction : n.getInteractions()) {
        /*
        System.out.println(String.format("   inter: node %s %s, node %s %s, %s",
        interaction.getNodeVO1().getId(),
        interaction.getNodeVO1().getScore(),
        interaction.getNodeVO2().getId(),
        interaction.getNodeVO2().getScore(),
        interaction.getWeight()
        ));
         */
      }
    }

    // TODO: send the same request through again, but this time using the old mania api

  }