private List<String> getPatientIds(DataMatrix dataMatrix) {
    List<LinkedList<String>> patientIds = dataMatrix.getColumnData(PATIENT_ID);
    if (patientIds.isEmpty()) {
      patientIds = dataMatrix.getColumnData(FOLLOW_UP_PATIENT_ID);
    }

    return (patientIds.isEmpty())
        ? Collections.<String>emptyList()
        : convertFollowupPatientIds(patientIds.get(0));
  }
  /**
   * Creates a staging file with contents from the given DataMatrix.
   *
   * @param portalMetadata PortalMetadata
   * @param cancerStudyMetadata CancerStudyMetadata
   * @param datatypeMetadata DatatypeMetadata
   * @param dataMatrix DataMatrix
   * @throws Exception
   */
  @Override
  public void writeStagingFile(
      PortalMetadata portalMetadata,
      CancerStudyMetadata cancerStudyMetadata,
      DatatypeMetadata datatypeMetadata,
      DataMatrix dataMatrix)
      throws Exception {

    // staging file
    String stagingFilename = datatypeMetadata.getStagingFilename();
    stagingFilename =
        stagingFilename.replaceAll(
            DatatypeMetadata.CANCER_STUDY_TAG, cancerStudyMetadata.toString());
    File stagingFile =
        org.apache.commons.io.FileUtils.getFile(
            portalMetadata.getStagingDirectory(),
            cancerStudyMetadata.getStudyPath(),
            stagingFilename);

    if (LOG.isInfoEnabled()) {
      LOG.info("writingStagingFile(), staging file: " + stagingFile);
    }

    FileOutputStream out = org.apache.commons.io.FileUtils.openOutputStream(stagingFile, false);
    dataMatrix.write(out);
    IOUtils.closeQuietly(out);

    // meta file
    if (datatypeMetadata.requiresMetafile()) {
      if (LOG.isInfoEnabled()) {
        LOG.info("writingStagingFile(), creating metadata file for staging file: " + stagingFile);
      }
      writeMetadataFile(portalMetadata, cancerStudyMetadata, datatypeMetadata, dataMatrix);
    }
  }
  /**
   * Method which writes a metadata file for the given DatatypeMetadata. DataMatrix may be null.
   *
   * @param portalMetadata PortalMetadata
   * @param cancerStudyMetadata CancerStudyMetadata
   * @param datatypeMetadata DatatypeMetadata
   * @param dataMatrix DataMatrix
   * @throws Exception
   */
  @Override
  public void writeMetadataFile(
      PortalMetadata portalMetadata,
      CancerStudyMetadata cancerStudyMetadata,
      DatatypeMetadata datatypeMetadata,
      DataMatrix dataMatrix)
      throws Exception {

    File metaFile =
        org.apache.commons.io.FileUtils.getFile(
            portalMetadata.getStagingDirectory(),
            cancerStudyMetadata.getStudyPath(),
            datatypeMetadata.getMetaFilename());
    if (LOG.isInfoEnabled()) {
      LOG.info("writeMetadataFile(), meta file: " + metaFile);
    }
    PrintWriter writer =
        new PrintWriter(org.apache.commons.io.FileUtils.openOutputStream(metaFile, false));
    writer.print("cancer_study_identifier: " + cancerStudyMetadata + "\n");
    writer.print(
        "genetic_alteration_type: " + datatypeMetadata.getMetaGeneticAlterationType() + "\n");
    String stableID = datatypeMetadata.getMetaStableID();
    stableID =
        stableID.replaceAll(DatatypeMetadata.CANCER_STUDY_TAG, cancerStudyMetadata.toString());
    writer.print("stable_id: " + stableID + "\n");
    writer.print(
        "show_profile_in_analysis_tab: "
            + datatypeMetadata.getMetaShowProfileInAnalysisTab()
            + "\n");
    String profileDescription = datatypeMetadata.getMetaProfileDescription();
    if (dataMatrix != null) {
      profileDescription =
          profileDescription.replaceAll(
              DatatypeMetadata.NUM_GENES_TAG, Integer.toString(dataMatrix.getGeneIDs().size()));
      profileDescription =
          profileDescription.replaceAll(
              DatatypeMetadata.NUM_CASES_TAG, Integer.toString(dataMatrix.getCaseIDs().size()));
    }
    profileDescription =
        profileDescription.replaceAll(
            DatatypeMetadata.TUMOR_TYPE_TAG, cancerStudyMetadata.getTumorType());
    writer.print("profile_description: " + profileDescription + "\n");
    writer.print("profile_name: " + datatypeMetadata.getMetaProfileName() + "\n");
    writer.flush();
    writer.close();
  }
  private String getValue(int index, String columnName, DataMatrix dataMatrix) {
    List<LinkedList<String>> columnData = dataMatrix.getColumnData(columnName);

    String value = MissingAttributeValues.getNotAvailable();
    if (!columnData.isEmpty()) {
      value = columnData.get(0).get(index);
    }
    return (value.isEmpty()) ? MissingAttributeValues.getNotAvailable() : value;
  }
  /**
   * Creates a staging file for mutation data (and meta file) with contents from the given
   * DataMatrix. This is called when the mutation file needs to be run through the Oncotator and
   * Mutation Assessor Tools.
   *
   * @param portalMetadata PortalMetadata
   * @param cancerStudy CancerStudyMetadata
   * @param datatypeMetadata DatatypeMetadata
   * @param dataMatrix DataMatrix
   * @throws Exception
   */
  @Override
  public void writeMutationStagingFile(
      PortalMetadata portalMetadata,
      CancerStudyMetadata cancerStudyMetadata,
      DatatypeMetadata datatypeMetadata,
      DataMatrix dataMatrix)
      throws Exception {

    // we only have data matrix at this point, we need to create a temp with its contents
    File oncotatorInputFile =
        org.apache.commons.io.FileUtils.getFile(
            org.apache.commons.io.FileUtils.getTempDirectory(), "oncotatorInputFile");
    FileOutputStream out = org.apache.commons.io.FileUtils.openOutputStream(oncotatorInputFile);
    dataMatrix.write(out);
    IOUtils.closeQuietly(out);

    // output should be the path/name of staging file
    String stagingFilename = datatypeMetadata.getStagingFilename();
    stagingFilename =
        stagingFilename.replaceAll(
            DatatypeMetadata.CANCER_STUDY_TAG, cancerStudyMetadata.toString());
    File stagingFile =
        org.apache.commons.io.FileUtils.getFile(
            portalMetadata.getStagingDirectory(),
            cancerStudyMetadata.getStudyPath(),
            stagingFilename);

    // call oncotateAF
    oncotateMAF(
        FileUtils.FILE_URL_PREFIX + oncotatorInputFile.getCanonicalPath(),
        FileUtils.FILE_URL_PREFIX + stagingFile.getCanonicalPath());

    // clean up
    if (oncotatorInputFile.exists()) {
      org.apache.commons.io.FileUtils.forceDelete(oncotatorInputFile);
    }

    // meta file
    if (datatypeMetadata.requiresMetafile()) {
      if (LOG.isInfoEnabled()) {
        LOG.info(
            "writingMutationStagingFile(), creating metadata file for staging file: "
                + stagingFile);
      }
      writeMetadataFile(portalMetadata, cancerStudyMetadata, datatypeMetadata, dataMatrix);
    }
  }