// download interpro and uncompress before running this script
// 'ftp://ftp.ebi.ac.uk/pub/databases/interpro/interpro.xml.gz';
// 'ftp://ftp.ebi.ac.uk/pub/databases/interpro/ParentChildTreeFile.txt'


var parentChildFile = process.argv[2];
var xmlFile = process.argv[3];

var fs        = require('fs')
  , XmlStream = require('xml-stream');

// read the parentChildFile into memory
var ancestors = {}; // key is child, value is list of ancestors
var currentNodes = [];
var currentDepth = 0;
require('readline').createInterface({
  input: fs.createReadStream(parentChildFile),
  terminal: false
}).on('line', function(line) {
  var splittedLine = line.split('--');
  var depth = splittedLine.length - 1;
  var IPRwithNames = splittedLine[depth];
  var splittedIPR = IPRwithNames.split('::');
  var IPR = parseInt(splittedIPR[0].match(/\d+/));
  currentNodes[depth] = IPR;
  ancestors[IPR] = [];
  for (var i = depth; i>=0; i--) {
    ancestors[IPR].push(currentNodes[i])
  }
}).on('close', function() {
  // Create a file stream and pass it to XmlStream
  var stream = fs.createReadStream(xmlFile);
  var xml = new XmlStream(stream);

  xml.collect('db_xref');
  xml.on('endElement: interpro', function(item) {
    // console.log(item);
    var obj = {};
    obj._id = parseInt(item.$.id.match(/\d+/));
    obj.ancestors = (ancestors[obj._id]) ? ancestors[obj._id] : [obj._id];
    obj.id = item.$.id;
    obj.name = item.$.short_name;
    obj.type = item.$.type;
    obj.description = item.name;
    if (item.hasOwnProperty('abstract') && item.abstract.hasOwnProperty('p')) obj.abstract = item.abstract.p.$text;
    if (item.hasOwnProperty('member_list') && item.member_list.hasOwnProperty('db_xref')) {
      item.member_list.db_xref.forEach(function(xref) {
        obj[xref.$.db] = xref.$.dbkey;
      });
    }
    if (item.hasOwnProperty('external_doc_list') && item.external_doc_list.hasOwnProperty('db_xref')) {
      item.external_doc_list.db_xref.forEach(function(xref) {
        obj[xref.$.db] = xref.$.dbkey;
      });
    }
    if (item.hasOwnProperty('structure_db_links') && item.structure_db_links.hasOwnProperty('db_xref')) {
      item.structure_db_links.db_xref.forEach(function(xref) {
        if (! obj.hasOwnProperty(xref.$.db)) obj[xref.$.db] = [];
        obj[xref.$.db].push(xref.$.dbkey);
      })
    }
    console.log(JSON.stringify(obj));
  });
});

@@ -264,16 +264,69 @@
 argv%5B3%5D;
+%0Avar collections = require('gramene-mongodb-config');
 %0A%0Avar fs
@@ -389,16 +389,83 @@
 eam');%0A%0A
+var mongoDomainsPromise = collections.domains.mongoCollection();%0A%0A%0A
 // read 
@@ -2446,16 +2446,19 @@
    %7D%0A   
+ //
  console
@@ -2486,16 +2486,279 @@
 bj));%0A  
+  mongoDomainsPromise.then(function(domainsCollection) %7B%0A      domainsCollection.insertOne(obj, function(err, response) %7B%0A        if (err) %7B%0A          throw err;%0A        %7D%0A      %7D);%0A    %7D);%0A  %7D);%0A  xml.on('end', function() %7B%0A    collections.closeMongoDatabase()%0A  
 %7D)
-;
 %0A%7D);%0A

insert domains into mongo within the script