<commit_before>const pcServices = require('./pcServices');
const metadataMapperJson = require('./metadataMapperJson');
const metadataMapperXML = require('./metadataMapperXML');
const metadataMapperPC2 = require('./metadataMapperPC2');

//Get pathway name, description, and datasource
//Requires a valid pathway uri
function getPathwayLevelMetadata(uri) {
  let title, dataSource, comments, organism;

  return (Promise.resolve(pcServices.traversePC2(uri, 'Named/displayName'))
    .then(data => {
      title = data.traverseEntry[0].value;
      return pcServices.traversePC2(uri, 'Entity/dataSource/displayName');
    })
    .then(data => {
      dataSource = data.traverseEntry[0].value;
      return pcServices.traversePC2(uri, 'Entity/comment');
    })
    .then(data => {
      comments = data.traverseEntry[0].value;
      return pcServices.traversePC2(uri, 'Entity/organism/displayName');
    })
    .then(data => {
      organism = data.traverseEntry[0].value;
      return {
        comments,
        dataSource,
        title,
        organism
      }
    }));
}

//Get metadata enhanced cytoscape JSON
//Requires a valid pathway uri
function getMetadataJson(uri, parseType) {
  let sbgn, biopax;

  //Get SBGN XML
  return pcServices.getPC2(uri, 'sbgn').then(function (data) {
    sbgn = data;

    let downloadType = (parseType === 'pc2' ? 'jsonld' : parseType);

    //Get BioPax XML
    return pcServices.getPC2(uri, downloadType).then(function (data) {
      biopax = data;

      //Map metadata
      if (parseType === 'jsonld') { return metadataMapperJson(biopax, sbgn); }
      else if (parseType === 'biopax') { return metadataMapperXML(biopax, sbgn); }
      else if (parseType === 'pc2') { return metadataMapperPC2(biopax, sbgn); }
    })
  });
}

//Return enhanced cytoscape json
//Requires a valid pathway uri 
function getCytoscapeJson(uri, parseType = 'jsonld') {
  let pathwayMetadata;

  //Start Generation
  return getPathwayLevelMetadata(uri).then(function (data) {
    pathwayMetadata = data;
    return getMetadataJson(uri, parseType).then(function (data) {
      data.pathwayMetadata = pathwayMetadata;
      data.parseType = parseType;
      return data;
    })
  }).catch(function (e) {
    console.log(e);
    return { error: e };
  })
}


module.exports = {
  getCytoscapeJson,
  getPathwayLevelMetadata
};<commit_msg>Further Simplified Promise Chains<commit_after>const pcServices = require('./pcServices');
const metadataMapperJson = require('./metadataMapperJson');
const metadataMapperXML = require('./metadataMapperXML');
const metadataMapperPC2 = require('./metadataMapperPC2');

//Get pathway name, description, and datasource
//Requires a valid pathway uri
function getPathwayLevelMetadata(uri) {

  let title, dataSource, comments, organism;
  let getValue = data => data.traverseEntry[0].value;
  let get = path => pcServices.traversePC2(uri, path).then(getValue);

  return Promise.all([
    get('Named/displayName').then(value => title = value),
    get('Entity/dataSource/displayName').then(value => dataSource = value),
    get('Entity/comment').then(value => dataSource = value),
    get('Entity/organism/displayName').then(value => organism = value)
  ]).then(data => ({ comments, dataSource, title, organism }));
};

//Get metadata enhanced cytoscape JSON
//Requires a valid pathway uri
function getMetadataJson(uri, parseType) {
  let sbgn, biopax;
  let downloadType = (parseType === 'pc2' ? 'jsonld' : parseType);

  //Get SBGN and Biopax Files
  return Promise.all([
    pcServices.getPC2(uri, 'sbgn').then(file => sbgn = file),
    pcServices.getPC2(uri, downloadType).then(file => biopax = file)
  ]).then(files => {
    //Map metadata
    if (parseType === 'jsonld') { return metadataMapperJson(biopax, sbgn); }
    else if (parseType === 'biopax') { return metadataMapperXML(biopax, sbgn); }
    else if (parseType === 'pc2') { return metadataMapperPC2(biopax, sbgn); }
  })
}

//Return enhanced cytoscape json
//Requires a valid pathway uri 
function getCytoscapeJson(uri, parseType = 'jsonld') {
  let pathwayMetadata;

  //Start Generation
  return getPathwayLevelMetadata(uri).then(function (data) {
    pathwayMetadata = data;
    return getMetadataJson(uri, parseType).then(function (data) {
      data.pathwayMetadata = pathwayMetadata;
      data.parseType = parseType;
      return data;
    })
  }).catch(function (e) {
    console.log(e);
    return { error: e };
  })
}

module.exports = {
  getCytoscapeJson,
  getPathwayLevelMetadata
};
{'fraction_non_alphanumeric': 0.13500332520505431, 'fraction_numerical': 0.005320328086898692, 'mean_word_length': 3.867313915857605, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '815339', 'n_tokens_mistral': 1410, 'n_tokens_neox': 1374, 'n_words': 324}