<commit_before>angular.module('npact')
  .factory('Track', function($log,  $q, $timeout, $http, Fetcher, ExtractParser, TrackBinSearchIndex, npactConstants) {
    'use strict';

    var ITrackIndex = TrackBinSearchIndex;
    function Track(filename, data, type, name) {
      // console.log('new Track ',name, data, type ,filename);
      if(!name && !type){
        if(filename.search('profiles')>=0){
          name="Hits";
          type='hits';
        }
        else if(filename.search('modified')>=0){
          name="Modified ORFs";
          type='extracts';
        }
        else if(filename.search('newcds')>=0){
          name="New ORFs";
          type='extracts';
        }
        else if(filename.search('genes')>=0){
          name="Input CDSs";
          type='extracts';
        }
        else{
          name = filename;
          type = 'extracts';
        }
      }
      this.active = true;
      this.filename = filename;
      this.name = name;
      this.type = type;
      if(!_.includes(['extracts', 'hits'], type)) {
        throw new Error("Unknown track type: ", type);
        //console.log("Unknown track type: ", type);
      }
      var trackStyle = npactConstants.trackStyle[type] || {};
      this.style = _.defaults(trackStyle, npactConstants.trackStyle['default']);
      this.data = null;
      this.index = null;
      this.loading = this.loadData(data);
      this.indexing = this.loading.then(_.bind(this.indexData, this));
    }

    Track.prototype.id = function () {
      var fn = _.last(this.filename.split('/'));
      return _.takeRight(fn.split('.'),2)[0];
    };

    Track.prototype.loadData = function(data) {
      var self = this;
      // console.log(data);
      if(typeof(data) === 'string'){ // trackfile to parse
        return (
          self.loading = ExtractParser.parseAsync(data)
            .then(function (data) {
              $log.log("Finished parsing", self.name, ", found ", data.length);
              self.data = [];
              self.metadata = {};
              _.each(data, function(v, k) {
                if(v.type === 'CDS') self.data.push(v);
                else if(v.type === 'META') self.metadata[k]=v;
              });
              self.loading = false;
              return data;
            })
            .catch(function(e) { $log.log('Track.loadData failed', name, e); throw e; }));
      }else{ // data must be parsed (probably json version of this object)
        if(Array.isArray(data))
          self.data = data;
        else if(data.cds || data.data)
          self.data = data.cds || data.data;
        return $q.when(self.data);
      }
    };

    Track.prototype.indexData = function(data) {
      return (
        this.indexing = $timeout(_.bind(function () {
          this.indexing = false;
          return (this.index = ITrackIndex(data));
        }, this))
          .catch(function(e) { $log.log('Track.indexData failed', name, e); throw e; }));
    };

    Track.prototype.reindex = function () {
      if(this.loading) {
        //shouldn't be able to get here but if this is true then we
        //haven't yet started indexing and nothing to do.
        return;
      }
      else if(this.indexing) {
        this.indexing.then(_.bind(function () {
          this.indexData(this.data);
        }, this));
      }
      else {
        this.indexData(this.data);
      }
   };
    Track.prototype.slice = function(opts) {
      if(this.indexing) {
        return this.indexing.then(function (index) {
          return index(opts);
        });
      }
      else {
        return this.index(opts);
      }
    };

    Track.prototype.findByName = function(substr) {
      var doFind = function () {
        if(!this.data) return [];
        var searcher = new RegExp(substr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i");
        return _.filter(this.data, function(orf) {
          return searcher.test(orf.name);
        });
      };<commit_msg>Fix promise logix in Track.js<commit_after>angular.module('npact')
  .factory('Track', function($log,  $q, $timeout, $http,
                      Fetcher, ExtractParser, TrackBinSearchIndex, npactConstants) {
    'use strict';

    var ITrackIndex = TrackBinSearchIndex;
    function Track(filename, data, type, name) {
      // console.log('new Track ',name, data, type ,filename);
      if(!name && !type){
        if(filename.search('profiles')>=0){
          name="Hits";
          type='hits';
        }
        else if(filename.search('modified')>=0){
          name="Modified ORFs";
          type='extracts';
        }
        else if(filename.search('newcds')>=0){
          name="New ORFs";
          type='extracts';
        }
        else if(filename.search('genes')>=0){
          name="Input CDSs";
          type='extracts';
        }
        else{
          name = filename;
          type = 'extracts';
        }
      }
      this.active = true;
      this.filename = filename;
      this.name = name;
      this.type = type;
      if(!_.includes(['extracts', 'hits'], type)) {
        throw new Error("Unknown track type: ", type);
        //console.log("Unknown track type: ", type);
      }
      var trackStyle = npactConstants.trackStyle[type] || {};
      this.style = _.defaults(trackStyle, npactConstants.trackStyle['default']);
      this.data = null;
      this.index = null;
      this.loading = this.loadData(data);
      this.reindex();
    }

    Track.prototype.id = function () {
      var fn = _.last(this.filename.split('/'));
      return _.takeRight(fn.split('.'),2)[0];
    };

    Track.prototype.loadData = function(data) {
      var self = this;
      // console.log(data);
      if(typeof(data) === 'string') { // trackfile to parse
        return (
          self.loading = ExtractParser.parseAsync(data)
            .then(function (data) {
              $log.log("Finished parsing", self.name, ", found ", data.length);
              self.data = [];
              self.metadata = {};
              _.each(data, function(v, k) {
                if(v.type === 'CDS') self.data.push(v);
                else if(v.type === 'META') self.metadata[k]=v;
              });
              self.loading = false;
              return data;
            })
            .catch(function(e) { $log.log('Track.loadData failed', name, e); throw e; }));
      }
      else { // data must be parsed (probably json version of this object)
        if(Array.isArray(data))
          self.data = data;
        else if(data.cds)
          self.data = data.cds;
        else if(data.data)
          self.data = data.data;
        return null;
      }
    };

    Track.prototype.indexData = function(data) {
      return (
        this.indexing = $timeout(_.bind(function () {
          this.indexing = false;
          return (this.index = ITrackIndex(data));
        }, this))
          .catch(function(e) { $log.log('Track.indexData failed', name, e); throw e; }));
    };

    Track.prototype.reindex = function () {
      if(this.loading) {
        this.loading.then(_.bind(this.indexData, this));
      }
      else if(this.indexing) {
        this.indexing.then(_.bind(this.indexData, this, this.data));
      }
      else {
        this.indexData(this.data);
      }
   };
    Track.prototype.slice = function(opts) {
      if(this.indexing) {
        return this.indexing.then(function (index) {
          return index(opts);
        });
      }
      else {
        return this.index(opts);
      }
    };

    Track.prototype.findByName = function(substr) {
      var doFind = function () {
        if(!this.data) return [];
        var searcher = new RegExp(substr.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "i");
        return _.filter(this.data, function(orf) {
          return searcher.test(orf.name);
        });
      };
{'fraction_non_alphanumeric': 0.15966709346991037, 'fraction_numerical': 0.0015364916773367477, 'mean_word_length': 1.901560178306092, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '4433926', 'n_tokens_mistral': 2358, 'n_tokens_neox': 2334, 'n_words': 460}