Exemplo n.º 1
0
  @Test
  public void testSequencerPartitionContainers() {
    try {
      // get row count of experiments in the dataset
      int expected = getDataSet().getTable("SequencerPartitionContainer").getRowCount();

      // get number of experiments from the DAO
      int actual = getSequencerPartitionContainerDAO().count();

      // test data contains 2 experiments, check size of returned list
      TestCase.assertEquals("Wrong number of containers", expected, actual);

      System.out.println("Expected number of containers: " + expected + ", actual: " + actual);

      for (SequencerPartitionContainer d : random(getSequencerPartitionContainerDAO(), actual, 5)) {
        TestCase.assertNotNull(d);
        TestCase.assertNotNull(d.getId());
      }
    } catch (Exception e) {
      e.printStackTrace();
      TestCase.fail();
    }
  }
  private Map<String, Run> processRunJSON(
      HealthType ht, JSONArray runs, RequestManager requestManager) {
    Map<String, Run> updatedRuns = new HashMap<String, Run>();
    List<Run> runsToSave = new ArrayList<Run>();
    // 2011-01-25 15:37:27.093
    DateFormat logDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    DateFormat simpleLogDateFormat = new SimpleDateFormat("yyyyMMdd");
    Pattern simpleDateRegex = Pattern.compile("[0-9]{8}");

    StringBuilder sb = new StringBuilder();

    for (JSONObject run : (Iterable<JSONObject>) runs) {
      String runName = run.getString("runName");
      sb.append("Processing " + runName + "\n");
      log.debug("Processing " + runName);

      if (run.has("status")) {
        String xml = run.getString("status");
        Status is = new SolidStatus(xml);
        is.setHealth(ht);
        is.setRunName(runName);

        Run r = null;
        Matcher m = p.matcher(runName);
        if (m.matches()) {
          try {
            r = requestManager.getRunByAlias(runName);
          } catch (IOException ioe) {
            log.warn(
                "Cannot find run by this alias. This usually means the run hasn't been previously imported. If attemptRunPopulation is false, processing will not take place for this run!");
          }
        }

        try {
          if (attemptRunPopulation) {
            if (r == null) {
              log.debug("Saving new run and status: " + is.getRunName());
              r = new SolidRun(xml);
              r.getStatus().setHealth(ht);
              if (run.has("fullPath")) {
                r.setFilePath(run.getString("fullPath"));
              }

              SequencerReference sr = null;
              if (run.has("sequencerName")) {
                sr = requestManager.getSequencerReferenceByName(run.getString("sequencerName"));
                r.getStatus().setInstrumentName(run.getString("sequencerName"));
                r.setSequencerReference(sr);
              }
              if (r.getSequencerReference() == null) {
                sr = requestManager.getSequencerReferenceByName(m.group(1));
                r.getStatus().setInstrumentName(m.group(1));
                r.setSequencerReference(sr);
              }
              if (r.getSequencerReference() == null) {
                sr = requestManager.getSequencerReferenceByName(r.getStatus().getInstrumentName());
                r.setSequencerReference(sr);
              }

              if (r.getSequencerReference() != null) {
                if (run.has("startDate")) {
                  try {
                    log.debug("Updating start date:" + run.getString("startDate"));

                    Matcher m2 = simpleDateRegex.matcher(run.getString("startDate"));
                    if (m2.matches()) {
                      r.getStatus()
                          .setStartDate(simpleLogDateFormat.parse(run.getString("startDate")));
                    } else {
                      r.getStatus().setStartDate(logDateFormat.parse(run.getString("startDate")));
                    }
                  } catch (ParseException e) {
                    log.error(e.getMessage());
                    e.printStackTrace();
                  }
                }

                if (run.has("completionDate")) {
                  try {
                    if (run.get("completionDate") != null
                        && !run.getString("completionDate").equals("null")) {
                      log.debug("Updating completion date:" + run.getString("completionDate"));
                      r.getStatus()
                          .setCompletionDate(logDateFormat.parse(run.getString("completionDate")));
                    } else {
                      r.getStatus().setCompletionDate(null);
                    }
                  } catch (ParseException e) {
                    log.error(e.getMessage());
                    e.printStackTrace();
                  }
                }
              }
            } else {
              log.debug("Updating existing run and status: " + is.getRunName());

              r.setAlias(runName);
              r.setPlatformType(PlatformType.SOLID);

              if (r.getSequencerReference() == null) {
                SequencerReference sr = null;
                if (run.has("sequencerName")) {
                  sr = requestManager.getSequencerReferenceByName(run.getString("sequencerName"));
                  r.getStatus().setInstrumentName(run.getString("sequencerName"));
                  r.setSequencerReference(sr);
                }
                if (r.getSequencerReference() == null) {
                  sr = requestManager.getSequencerReferenceByName(m.group(1));
                  r.getStatus().setInstrumentName(m.group(1));
                  r.setSequencerReference(sr);
                }
                if (r.getSequencerReference() == null) {
                  sr =
                      requestManager.getSequencerReferenceByName(r.getStatus().getInstrumentName());
                  r.setSequencerReference(sr);
                }
              }
              if (r.getSequencerReference() != null) {
                if (run.has("startDate")) {
                  try {
                    log.debug("Updating start date:" + run.getString("startDate"));

                    Matcher m2 = simpleDateRegex.matcher(run.getString("startDate"));
                    if (m2.matches()) {
                      r.getStatus()
                          .setStartDate(simpleLogDateFormat.parse(run.getString("startDate")));
                    } else {
                      r.getStatus().setStartDate(logDateFormat.parse(run.getString("startDate")));
                    }
                  } catch (ParseException e) {
                    log.error(e.getMessage());
                    e.printStackTrace();
                  }
                }

                if (run.has("completionDate")) {
                  try {
                    if (run.get("completionDate") != null
                        && !run.getString("completionDate").equals("null")) {
                      log.debug("Updating completion date:" + run.getString("completionDate"));
                      r.getStatus()
                          .setCompletionDate(logDateFormat.parse(run.getString("completionDate")));
                    } else {
                      r.getStatus().setCompletionDate(null);
                    }
                  } catch (ParseException e) {
                    log.error(e.getMessage());
                    e.printStackTrace();
                  }
                }

                // update path if changed
                if (run.has("fullPath")
                    && !"".equals(run.getString("fullPath"))
                    && r.getFilePath() != null
                    && !"".equals(r.getFilePath())) {
                  if (!run.getString("fullPath").equals(r.getFilePath())) {
                    log.debug(
                        "Updating run file path:"
                            + r.getFilePath()
                            + " -> "
                            + run.getString("fullPath"));
                    r.setFilePath(run.getString("fullPath"));
                  }
                }

                // update status if run isn't completed or failed
                if (!r.getStatus().getHealth().equals(HealthType.Completed)
                    && !r.getStatus().getHealth().equals(HealthType.Failed)) {
                  log.debug(
                      "Saving previously saved status: "
                          + is.getRunName()
                          + " ("
                          + r.getStatus().getHealth().getKey()
                          + " -> "
                          + is.getHealth().getKey()
                          + ")");
                  r.setStatus(is);
                }
              }
            }

            if (r.getSequencerReference() != null) {
              List<SequencerPartitionContainer<SequencerPoolPartition>> fs =
                  ((SolidRun) r).getSequencerPartitionContainers();
              if (fs.isEmpty()) {
                if (run.has("containerId") && !"".equals(run.getString("containerId"))) {
                  Collection<SequencerPartitionContainer<SequencerPoolPartition>> pfs =
                      requestManager.listSequencerPartitionContainersByBarcode(
                          run.getString("containerId"));
                  if (!pfs.isEmpty()) {
                    if (pfs.size() == 1) {
                      SequencerPartitionContainer lf =
                          new ArrayList<SequencerPartitionContainer<SequencerPoolPartition>>(pfs)
                              .get(0);
                      if (lf.getSecurityProfile() != null && r.getSecurityProfile() == null) {
                        r.setSecurityProfile(lf.getSecurityProfile());
                      }
                      if (lf.getPlatform() == null
                          && r.getSequencerReference().getPlatform() != null) {
                        lf.setPlatform(r.getSequencerReference().getPlatform());
                      }
                      //                      else {
                      //                        lf.setPlatformType(PlatformType.SOLID);
                      //                      }
                      ((RunImpl) r).addSequencerPartitionContainer(lf);
                    }
                  } else {
                    log.debug("No containers linked to run " + r.getId() + ": creating...");
                    SequencerPartitionContainer f = new SequencerPartitionContainerImpl();
                    f.setSecurityProfile(r.getSecurityProfile());
                    f.initEmptyPartitions();
                    f.setIdentificationBarcode(run.getString("containerNum"));
                    if (f.getPlatform() == null
                        && r.getSequencerReference().getPlatform() != null) {
                      f.setPlatform(r.getSequencerReference().getPlatform());
                    }
                    //                    else {
                    //                      f.setPlatformType(PlatformType.SOLID);
                    //                    }
                    // f.setPaired(r.getPairedEnd());
                    ((RunImpl) r).addSequencerPartitionContainer(f);
                  }
                }
              } else {
                SequencerPartitionContainer f = fs.iterator().next();
                log.debug("Got container " + f.getId());

                if (f.getSecurityProfile() == null) {
                  f.setSecurityProfile(r.getSecurityProfile());
                }

                if (f.getPlatform() == null && r.getSequencerReference().getPlatform() != null) {
                  f.setPlatform(r.getSequencerReference().getPlatform());
                }
                //                else {
                //                  f.setPlatformType(PlatformType.SOLID);
                //                }

                if (run.has("containerId") && !"".equals(run.getString("containerId"))) {
                  f.setIdentificationBarcode(run.getString("containerId"));
                }

                long flowId = requestManager.saveSequencerPartitionContainer(f);
                f.setId(flowId);
              }

              updatedRuns.put(r.getAlias(), r);
              runsToSave.add(r);
            }
          }
        } catch (IOException e) {
          log.error(e.getMessage());
          e.printStackTrace();
        }
      }
    }

    try {
      if (runsToSave.size() > 0) {
        int[] saved = requestManager.saveRuns(runsToSave);
        log.info("Batch saved " + saved.length + " / " + runs.size() + " runs");
      }
    } catch (IOException e) {
      log.error("Couldn't save run batch: " + e.getMessage());
      e.printStackTrace();
    }

    return updatedRuns;
  }