<commit_before>// TODO: change this to accept options instead of wrangler_file_id
function ContrastMatrix (wrangler_file_id) {
  TabSeperatedFile.call(this, {
    wrangler_file_id: wrangler_file_id
  });

  this.setSubmissionType.call(this, 'contrast');
}

ContrastMatrix.prototype = Object.create(TabSeperatedFile.prototype);
ContrastMatrix.prototype.constructor = ContrastMatrix;

function ensureZeroOne (zeroOrOne) {
  return zeroOrOne === "1" || zeroOrOne === "0";
}

// returns "a" or "b", validates last two fields in brokenTabs
function getGroup (brokenTabs) {
  ensureZeroOne(brokenTabs[2]);
  ensureZeroOne(brokenTabs[3]);

  var samplesGroup;
  if (brokenTabs[2] === "1") {
    samplesGroup = "a";
  }

  if (brokenTabs[3] === "1") {
    if (samplesGroup) {
      throw "Sample " + brokenTabs[1] + " in study " + brokenTabs[0] +
          " can't be in both groups.";
    }
    samplesGroup = "b";
  }

  if (!samplesGroup) {
    throw "No group assignment for sample " + brokenTabs[1] + " in study " +
        brokenTabs[0] + ".";
  }

  return samplesGroup;
}

ContrastMatrix.prototype.parseLine =
    function (brokenTabs, lineNumber, line) {
  if (lineNumber % 1000 === 0) {
    console.log("lineNumber:", lineNumber);
  }

  this.ensureRectangular.call(this, brokenTabs, lineNumber);

  if (lineNumber === 1) { // header line
    if (brokenTabs.length !== 4) {
      throw "Expected 4 column tab file, got " + brokenTabs.length +
          " column tab file";
    }

    var contrast_label = this.wranglerFile.options.contrast_label;
    var existingContrast = Contrasts.findOne({
      // TODO: what other criteria is there?
      contrast_label: contrast_label,
    }, {
      sort: { version: -1 }
    });
    var version = 1;
    var description;
    if (existingContrast) {
      version = existingContrast.version + 1;
      description = existingContrast.description;
    } else {
      description = this.wranglerFile.options.description;
    }

    this.newContrast = {
      user_id: this.wranglerFile.user_id,
      collaborations: [this.wranglerFile.options.collaboration_label],
      contrast_label: contrast_label,
      version: version,
      description: description,
      a_name: brokenTabs[2],
      a_samples: [],
      b_name: brokenTabs[3],
      b_samples: [],
    };
  } else { // rest of file
    var study_label = brokenTabs[0];

    // check studies to see if the collaboration has access
    var collaboration_label = this.wranglerFile.options.collaboration_label;
    var study = Studies.findOne({
      id: study_label,
      collaborations: collaboration_label,
    });
    if (!study) {
      throw "Collaboration " + collaboration_label + " does not have access " +
          "to study " + study_label + ".";
    }

    var sample_label = brokenTabs[1];
    ensureClinicalExists.call(this, study_label, sample_label);
    var patient_label = Wrangler.wranglePatientLabel(sample_label);

    var group = getGroup(brokenTabs);
    this.newContrast[group + "_samples"].push({
      study_label: study_label,
      sample_label: sample_label,
      patient_label: patient_label,
    });

    if (this.wranglerPeek) {
      this.insertWranglerDocument({
        document_type: "contrast_sample",
        contents: {
          contrast_label: this.newContrast.contrast_label,
          contrast_version: this.newContrast.version,
          study_label: study_label,
          sample_label: sample_label,
          group_name: this.newContrast[group + "_name"],
        },
      });
    }
  }
};

ContrastMatrix.prototype.endOfFile = function () {
  if (this.newContrast.a_samples.length === 0 ||
      this.newContrast.b_samples.length === 0) {
    throw "Each group must have at least one sample.";
  }

  if (this.wranglerPeek) {
    this.insertWranglerDocument.call(this, {
      document_type: "contrast_summary",
      contents: {
        contrast_label: this.newContrast.contrast_label,
        version: this.newContrast.version,
        description: this.newContrast.description,
        a_name: this.newContrast.a_name,
        b_name: this.newContrast.b_name,
        a_samples_count: this.newContrast.a_samples.length,
        b_samples_count: this.newContrast.b_samples.length,
      },
    });
  } else {
    Contrasts.insert(this.newContrast);<commit_msg>changed contrast file format<commit_after>// TODO: change this to accept options instead of wrangler_file_id
function ContrastMatrix (wrangler_file_id) {
  TabSeperatedFile.call(this, {
    wrangler_file_id: wrangler_file_id
  });

  this.setSubmissionType.call(this, 'contrast');
}

ContrastMatrix.prototype = Object.create(TabSeperatedFile.prototype);
ContrastMatrix.prototype.constructor = ContrastMatrix;

// returns "a" or "b", validates last two fields in brokenTabs
// also sets this.newContrast.a/b_name
function getGroup (brokenTabs) {
  var groupName = brokenTabs[2];
  console.log("groupName:", groupName);

  if (!this.newContrast.a_name) {
    // must be group a because it's the first one
    this.newContrast.a_name = groupName;
    console.log("this.newContrast.a_name:", this.newContrast.a_name);
    return "a";
  }
  if (groupName === this.newContrast.a_name) {
    return "a";
  }

  if (!this.newContrast.b_name) {
    // it's the second new name we've found, thus it must be group b
    this.newContrast.b_name = groupName;
    return "b";
  }
  if (groupName === this.newContrast.b_name) {
    return "b";
  }

  throw "Third group found: " + groupName + " for sample " + brokenTabs[1] +
      " in study " + brokenTabs[0] + ".";
}

ContrastMatrix.prototype.parseLine =
    function (brokenTabs, lineNumber, line) {
  if (lineNumber % 1000 === 0) {
    console.log("lineNumber:", lineNumber);
  }

  this.ensureRectangular.call(this, brokenTabs, lineNumber);

  if (lineNumber === 1) { // header line
    if (brokenTabs.length !== 3) {
      throw "Expected 3 column tab file, got " + brokenTabs.length +
          " column tab file";
    }

    var contrast_label = this.wranglerFile.options.contrast_label;
    var existingContrast = Contrasts.findOne({
      // TODO: what other criteria is there?
      contrast_label: contrast_label,
    }, {
      sort: { version: -1 }
    });
    var version = 1;
    var description;
    if (existingContrast) {
      version = existingContrast.version + 1;
      description = existingContrast.description;
    } else {
      description = this.wranglerFile.options.description;
    }

    this.newContrast = {
      user_id: this.wranglerFile.user_id,
      collaborations: [this.wranglerFile.options.collaboration_label],
      contrast_label: contrast_label,
      version: version,
      description: description,
      // a/b_name set in getGroup
      a_samples: [],
      b_samples: [],
    };
  } else { // rest of file
    var study_label = brokenTabs[0];

    // check studies to see if the collaboration has access
    var collaboration_label = this.wranglerFile.options.collaboration_label;
    var study = Studies.findOne({
      id: study_label,
      collaborations: collaboration_label,
    });
    if (!study) {
      throw "Collaboration " + collaboration_label + " does not have access " +
          "to study " + study_label + ".";
    }

    var sample_label = brokenTabs[1];
    ensureClinicalExists.call(this, study_label, sample_label);
    var patient_label = Wrangler.wranglePatientLabel(sample_label);

    var group = getGroup.call(this, brokenTabs);
    console.log("group:", group);
    console.log("this.newContrast:", this.newContrast);
    this.newContrast[group + "_samples"].push({
      study_label: study_label,
      sample_label: sample_label,
      patient_label: patient_label,
    });

    if (this.wranglerPeek) {
      this.insertWranglerDocument({
        document_type: "contrast_sample",
        contents: {
          contrast_label: this.newContrast.contrast_label,
          contrast_version: this.newContrast.version,
          study_label: study_label,
          sample_label: sample_label,
          group_name: this.newContrast[group + "_name"],
        },
      });
    }
  }
};

ContrastMatrix.prototype.endOfFile = function () {
  console.log("this.newContrast:", this.newContrast);

  if (!this.newContrast.a_name ||
      !this.newContrast.b_name) {
    throw "At least two groups must be defined in the file";
  }

  if (this.wranglerPeek) {
    this.insertWranglerDocument.call(this, {
      document_type: "contrast_summary",
      contents: {
        contrast_label: this.newContrast.contrast_label,
        version: this.newContrast.version,
        description: this.newContrast.description,
        a_name: this.newContrast.a_name,
        b_name: this.newContrast.b_name,
        a_samples_count: this.newContrast.a_samples.length,
        b_samples_count: this.newContrast.b_samples.length,
      },
    });
  } else {
    Contrasts.insert(this.newContrast);
{'fraction_non_alphanumeric': 0.11535437492979894, 'fraction_numerical': 0.0050544760193193304, 'mean_word_length': 3.069469835466179, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3805015', 'n_tokens_mistral': 2942, 'n_tokens_neox': 2796, 'n_words': 583}