{"size": 1677, "ext": "js", "max_stars_repo_path": "app/actions/analysis/tool/define-user-independent-settings.js", "max_stars_repo_name": "knightjdr/prohits-viz-ap", "max_stars_repo_stars_event_min_datetime": "2019-02-18T01:52:02.000Z", "max_stars_repo_stars_event_max_datetime": "2019-02-18T01:52:02.000Z", "max_issues_repo_path": "app/actions/analysis/tool/define-user-independent-settings.js", "max_issues_repo_name": "knightjdr/prohits-viz-ap", "max_issues_repo_issues_event_min_datetime": "2020-12-14T19:49:25.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-21T13:57:26.000Z", "max_forks_repo_path": "app/actions/analysis/tool/define-user-independent-settings.js", "max_forks_repo_name": "knightjdr/prohits-viz-api", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 27.4918032787, "max_line_length": 92, "alphanum_fraction": 0.6547406082}
import fs from 'fs/promises';

import config from '../../../config/config.js';

const addFile = async (filename, workDir) => {
  const source = `${process.cwd()}/${config.dataDir}${filename}`;
  const target = `${workDir}/helper-files/${filename}`;
  await fs.symlink(source, target);
  return `helper-files/${filename}`;
};

const addUserFileName = (value, name) => {
  if (Array.isArray(value) && value.length > 0) {
    return name;
  }
  return '';
};

const addKnownnessFile = async (knownOption, customFile, workDir) => {
  if (knownOption === 'interaction') {
    return addFile('interactions.json', workDir);
  } if (knownOption === 'custom') {
    return addUserFileName(customFile, 'helper-files/knownness.txt');
  }
  return '';
};

const defineUserIndependentSettings = async (settings, workDir) => {
  const {
    conditionMapFile,
    known,
    knownFile,
    proteinTissues,
    readoutMapFile,
    rnaTissues,
    type: tool,
  } = settings;

  if (tool === 'scv') {
    const files = await Promise.all([
      addFile('gene-db.json', workDir),
      proteinTissues.length > 0 ? addFile('protein-expression.json', workDir) : '',
      rnaTissues.length > 0 ? addFile('rna-expression.json', workDir) : '',
      addKnownnessFile(known, knownFile, workDir),
    ]);

    return {
      conditionMapFile: addUserFileName(conditionMapFile, 'helper-files/condition-map.txt'),
      geneFile: files[0],
      knownFile: files[3],
      proteinExpressionFile: files[1],
      readoutMapFile: addUserFileName(readoutMapFile, 'helper-files/readout-map.txt'),
      rnaExpressionFile: files[2],
    };
  }

  return {};
};

export default defineUserIndependentSettings;
