Esempio n. 1
0
  private List<Sample> getFileSamples(
      Study study,
      File file,
      ObjectMap indexFileModifyParams,
      boolean simulate,
      QueryOptions options,
      String sessionId)
      throws CatalogException {
    List<Sample> sampleList;
    QueryOptions queryOptions =
        new QueryOptions(
            "include",
            Arrays.asList("projects.studies.samples.id", "projects.studies.samples.name"));

    if (file.getSampleIds() == null || file.getSampleIds().isEmpty()) {
      // Read samples from file
      List<String> sampleNames = null;
      switch (file.getBioformat()) {
        case VARIANT:
          {
            if (file.getAttributes().containsKey("variantSource")) {
              Object variantSource = file.getAttributes().get("variantSource");
              if (variantSource instanceof VariantSource) {
                sampleNames = ((VariantSource) variantSource).getSamples();
              } else if (variantSource instanceof Map) {
                sampleNames = new ObjectMap((Map) variantSource).getAsStringList("samples");
              } else {
                logger.warn(
                    "Unexpected object type of variantSource ({}) in file attributes. Expected {} or {}",
                    variantSource.getClass(),
                    VariantSource.class,
                    Map.class);
              }
            }
            if (sampleNames == null) {
              VariantSource variantSource = readVariantSource(catalogManager, study, file);
              indexFileModifyParams
                  .get("attributes", ObjectMap.class)
                  .put("variantSource", variantSource);
              sampleNames = variantSource.getSamples();
            }
          }
          break;
        default:
          return new LinkedList<>();
          //                    throw new CatalogException("Unknown to get samples names from
          // bioformat " + file.getBioformat());
      }

      // Find matching samples in catalog with the sampleName from the VariantSource.
      queryOptions.add("name", sampleNames);
      sampleList = catalogManager.getAllSamples(study.getId(), queryOptions, sessionId).getResult();

      // check if all file samples exists on Catalog
      if (sampleList.size()
          != sampleNames.size()) { // Size does not match. Find the missing samples.
        Set<String> set = new HashSet<>(sampleNames);
        for (Sample sample : sampleList) {
          set.remove(sample.getName());
        }
        logger.warn("Missing samples: m{}", set);
        if (options.getBoolean(CREATE_MISSING_SAMPLES, true)) {
          for (String sampleName : set) {
            if (simulate) {
              sampleList.add(new Sample(-1, sampleName, file.getName(), null, null));
            } else {
              sampleList.add(
                  catalogManager
                      .createSample(
                          study.getId(), sampleName, file.getName(), null, null, null, sessionId)
                      .first());
            }
          }
        } else {
          throw new CatalogException(
              "Can not find samples " + set + " in catalog"); // FIXME: Create missing samples??
        }
      }
    } else {
      // Get samples from file.sampleIds
      queryOptions.add("id", file.getSampleIds());
      sampleList = catalogManager.getAllSamples(study.getId(), queryOptions, sessionId).getResult();
    }

    List<Integer> sampleIdsList = new ArrayList<>(sampleList.size());
    for (Sample sample : sampleList) {
      sampleIdsList.add(sample.getId());
      //
      // sampleIdsString.append(sample.getName()).append(":").append(sample.getId()).append(",");
    }
    indexFileModifyParams.put("sampleIds", sampleIdsList);

    return sampleList;
  }