<commit_before>var GpmlUtilities = require('./gpml-utilities.js')
  , Async = require('async')
  , Biopax = require('biopax2json')
  // , Anchor = require('./anchor.js')
  , BiopaxRef = require('./biopax-ref.js')
  // , Comment = require('./comment.js')
  , DataNode = require('./data-node.js')
  // , Element = require('./element.js')
  , fs = require('fs')
  , GraphicalLine = require('./graphical-line.js')
  // , Graphics = require('./graphics.js')
  , Group = require('./group.js')
  , Interaction = require('./interaction.js')
  , Label = require('./label.js')
  // , Point = require('./point.js')
  , Shape = require('./shape.js')
  , State = require('./state.js')
  , _ = require('lodash')
  // , Text = require('./text.js')
  ;

module.exports = {
  toPvjson: function(gpmlPathwaySelection, pathwayMetadata, callbackOutside){

    var xmlns = gpmlPathwaySelection.attr('xmlns');
    gpmlPathwaySelection = this.fixBiopax(this.addIsPartOfAttribute(this.makeExplicit(gpmlPathwaySelection)));
    var pvjson = {};

    var pathwayIri = 'http://identifiers.org/wikipathways/' + pathwayMetadata.dbId;

    var globalContext = [];
    // TODO update this to remove test2.
    //globalContext.push('http://test2.wikipathways.org/v2/contexts/pathway.jsonld');
    globalContext.push('http://test2.wikipathways.org/v2/contexts/biopax.jsonld');
    globalContext.push('http://test2.wikipathways.org/v2/contexts/organism.jsonld');
    globalContext.push('http://test2.wikipathways.org/v2/contexts/display.jsonld');
    //globalContext.push('http://test2.wikipathways.org/v2/contexts/interaction-type.jsonld');
    pvjson['@context'] = globalContext;
    var localContext = {};
    localContext = {};
    localContext['@base'] = pathwayIri + '/';
    pvjson['@context'].push(localContext);
    pvjson.type = 'Pathway';
    // using full IRI, because otherwise I would have to indicate the id as something like "/", which is ugly.
    pvjson.id = pathwayIri;
    pvjson.idVersion = pathwayMetadata.idVersion;
    pvjson.xrefs = [];

    pvjson.elements = [];

    /* Dev only
    var pd = require('pretty-data').pd;
    var rawGpmlAsString = gpmlPathwaySelection.find('*').html();
    var rawGpmlAsPrettyString = pd.xml(rawGpmlAsString);
    //console.log('rawGpmlAsPrettyString');
    //console.log(rawGpmlAsPrettyString);
    var updatedGpmlAsString = gpmlPathwaySelection.find('*').html();
    var processedGpmlAsPrettyString = pd.xml(updatedGpmlAsString);
    console.log('processedGpmlAsPrettyString');
    console.log(processedGpmlAsPrettyString);
    //*/

    // test for whether file is GPML
    if (GpmlUtilities.supportedNamespaces.indexOf(xmlns) === -1) {
      callbackOutside('Pathvisiojs does not support the data format provided. Please convert to GPML and retry.', {});
    } else {
      // test for whether the GPML file version matches the latest version (only the latest version will be supported by pathvisiojs).
      if (GpmlUtilities.supportedNamespaces.indexOf(xmlns) !== 0) {
        // TODO call the Java RPC updater or in some other way call for the file to be updated.
        callbackOutside('Pathvisiojs may not fully support the version of GPML provided (xmlns: ' + xmlns + '). Please convert to the supported version of GPML (xmlns: ' + GpmlUtilities.supportedNamespaces[0] + ').', {});
      } else {
        Async.waterfall([
          function(callbackWaterfall) {
            var jsonBiopax;
            var xmlBiopaxSelection = gpmlPathwaySelection.find('Biopax');
            if (!!xmlBiopaxSelection && xmlBiopaxSelection.length > 0) {
              // TODO check whether this will always be completed by the time it is needed
              // look at http://www.biopax.org/owldoc/Level3/ for correct terms
              // TODO look at whether ontology terms or other items need to be updated
              var biopaxString = xmlBiopaxSelection.toString().replace(/bp:ID/g, 'bp:id').replace(/bp:DB/g, 'bp:db').replace(/bp:TITLE/g, 'bp:title').replace(/bp:SOURCE/g, 'bp:source').replace(/bp:YEAR/g, 'bp:year').replace(/bp:AUTHORS/g, 'bp:author');
              Biopax.toJson(biopaxString, pathwayMetadata, function(err, thisJsonBiopax) {
                jsonBiopax = thisJsonBiopax;
                if (!!jsonBiopax && !!jsonBiopax.entities && jsonBiopax.entities.length > 0) {
                  pvjson.elements = pvjson.elements.concat(jsonBiopax.entities);
                  callbackWaterfall(null, jsonBiopax);
                } else {
                  callbackWaterfall(null, null);
                }
              });
            } else {
              callbackWaterfall(null, null);
            }
          },
          function(jsonBiopax, callbackWaterfall) {
            Async.parallel({
              BiopaxRef: function(callback){
                var biopaxRefsSelection = gpmlPathwaySelection.find('Pathway > BiopaxRef');
                // TODO don't repeat this code with the same code in element.js
                if (biopaxRefsSelection.length > 0 && !!jsonBiopax && !!jsonBiopax.entities) {
                  pvjson.xrefs = pvjson.xrefs || [];
                  biopaxRefsSelection.each(function() {
                    var biopaxRefSelection = $( this );<commit_msg>use jQuery .eq(0) to get first element for Biopax<commit_after>var GpmlUtilities = require('./gpml-utilities.js')
  , Async = require('async')
  , Biopax = require('biopax2json')
  // , Anchor = require('./anchor.js')
  , BiopaxRef = require('./biopax-ref.js')
  // , Comment = require('./comment.js')
  , DataNode = require('./data-node.js')
  // , Element = require('./element.js')
  , fs = require('fs')
  , GraphicalLine = require('./graphical-line.js')
  // , Graphics = require('./graphics.js')
  , Group = require('./group.js')
  , Interaction = require('./interaction.js')
  , Label = require('./label.js')
  // , Point = require('./point.js')
  , Shape = require('./shape.js')
  , State = require('./state.js')
  , _ = require('lodash')
  // , Text = require('./text.js')
  ;

module.exports = {
  toPvjson: function(gpmlPathwaySelection, pathwayMetadata, callbackOutside){

    var xmlns = gpmlPathwaySelection.attr('xmlns');
    gpmlPathwaySelection = this.fixBiopax(this.addIsPartOfAttribute(this.makeExplicit(gpmlPathwaySelection)));
    var pvjson = {};

    var pathwayIri = 'http://identifiers.org/wikipathways/' + pathwayMetadata.dbId;

    var globalContext = [];
    // TODO update this to remove test2.
    //globalContext.push('http://test2.wikipathways.org/v2/contexts/pathway.jsonld');
    globalContext.push('http://test2.wikipathways.org/v2/contexts/biopax.jsonld');
    globalContext.push('http://test2.wikipathways.org/v2/contexts/organism.jsonld');
    globalContext.push('http://test2.wikipathways.org/v2/contexts/display.jsonld');
    //globalContext.push('http://test2.wikipathways.org/v2/contexts/interaction-type.jsonld');
    pvjson['@context'] = globalContext;
    var localContext = {};
    localContext = {};
    localContext['@base'] = pathwayIri + '/';
    pvjson['@context'].push(localContext);
    pvjson.type = 'Pathway';
    // using full IRI, because otherwise I would have to indicate the id as something like "/", which is ugly.
    pvjson.id = pathwayIri;
    pvjson.idVersion = pathwayMetadata.idVersion;
    pvjson.xrefs = [];

    pvjson.elements = [];

    /* Dev only
    var pd = require('pretty-data').pd;
    var rawGpmlAsString = gpmlPathwaySelection.find('*').html();
    var rawGpmlAsPrettyString = pd.xml(rawGpmlAsString);
    //console.log('rawGpmlAsPrettyString');
    //console.log(rawGpmlAsPrettyString);
    var updatedGpmlAsString = gpmlPathwaySelection.find('*').html();
    var processedGpmlAsPrettyString = pd.xml(updatedGpmlAsString);
    console.log('processedGpmlAsPrettyString');
    console.log(processedGpmlAsPrettyString);
    //*/

    // test for whether file is GPML
    if (GpmlUtilities.supportedNamespaces.indexOf(xmlns) === -1) {
      callbackOutside('Pathvisiojs does not support the data format provided. Please convert to GPML and retry.', {});
    } else {
      // test for whether the GPML file version matches the latest version (only the latest version will be supported by pathvisiojs).
      if (GpmlUtilities.supportedNamespaces.indexOf(xmlns) !== 0) {
        // TODO call the Java RPC updater or in some other way call for the file to be updated.
        callbackOutside('Pathvisiojs may not fully support the version of GPML provided (xmlns: ' + xmlns + '). Please convert to the supported version of GPML (xmlns: ' + GpmlUtilities.supportedNamespaces[0] + ').', {});
      } else {
        Async.waterfall([
          function(callbackWaterfall) {
            var jsonBiopax;
            var xmlBiopaxSelection = gpmlPathwaySelection.find('Biopax').eq(0);
            if (!!xmlBiopaxSelection && xmlBiopaxSelection.length > 0) {
              // TODO check whether this will always be completed by the time it is needed
              // look at http://www.biopax.org/owldoc/Level3/ for correct terms
              // TODO look at whether ontology terms or other items need to be updated
              var biopaxString = xmlBiopaxSelection.html().replace(/bp:ID/g, 'bp:id').replace(/bp:DB/g, 'bp:db').replace(/bp:TITLE/g, 'bp:title').replace(/bp:SOURCE/g, 'bp:source').replace(/bp:YEAR/g, 'bp:year').replace(/bp:AUTHORS/g, 'bp:author');
              Biopax.toJson(biopaxString, pathwayMetadata, function(err, thisJsonBiopax) {
                jsonBiopax = thisJsonBiopax;
                if (!!jsonBiopax && !!jsonBiopax.entities && jsonBiopax.entities.length > 0) {
                  pvjson.elements = pvjson.elements.concat(jsonBiopax.entities);
                  callbackWaterfall(null, jsonBiopax);
                } else {
                  callbackWaterfall(null, null);
                }
              });
            } else {
              callbackWaterfall(null, null);
            }
          },
          function(jsonBiopax, callbackWaterfall) {
            Async.parallel({
              BiopaxRef: function(callback){
                var biopaxRefsSelection = gpmlPathwaySelection.find('Pathway > BiopaxRef');
                // TODO don't repeat this code with the same code in element.js
                if (biopaxRefsSelection.length > 0 && !!jsonBiopax && !!jsonBiopax.entities) {
                  pvjson.xrefs = pvjson.xrefs || [];
                  biopaxRefsSelection.each(function() {
                    var biopaxRefSelection = $( this );
{'fraction_non_alphanumeric': 0.1394505913773369, 'fraction_numerical': 0.0038153376573826785, 'mean_word_length': 2.847706422018349, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 26}, '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': '5162072', 'n_tokens_mistral': 3277, 'n_tokens_neox': 3154, 'n_words': 638}