<commit_before>define([
	"dojo/_base/declare", "dojo/request",
	"dojo/store/Memory", "dojo/store/util/QueryResults",
	"dojo/when", "dojo/_base/lang", "dojo/Stateful", "dojo/_base/Deferred",
	"dojo/topic", "./HeatmapDataTypes"
], function(declare, request,
			Memory, QueryResults,
			when, lang, Stateful, Deferred,
			Topic){

	var pfState = {
		familyType: 'figfam', // default
		heatmapAxis: '',
		genomeIds: [],
		genomeFilterStatus: {},
		clusterRowOrder: [],
		clusterColumnOrder: [],
		perfectFamMatch: 'A',
		min_member_count: null,
		max_member_count: null,
		min_genome_count: null,
		max_genome_count: null
	};

	return declare([Memory, Stateful], {
		baseQuery: {},
		apiServer: window.App.dataServiceURL,
		idProperty: "family_id",
		state: null,
		pfState: pfState,

		constructor: function(options){
			this._loaded = false;
			if(options.apiServer){
				this.apiServer = options.apiServer;
			}

			var self = this;

			Topic.subscribe("ProteinFamilies", function(){
				// console.log("received:", arguments);
				var key = arguments[0], value = arguments[1];

				switch(key){
					case "setFamilyType":
						self.pfState.familyType = value;
						self.reload();
						self.currentData = self.getHeatmapData(self.pfState);
						Topic.publish("ProteinFamilies", "updateHeatmapData", self.currentData);
						break;
					case "applyConditionFilter":
						self.conditionFilter(value);
						self.currentData = self.getHeatmapData(self.pfState);
						Topic.publish("ProteinFamilies", "updateHeatmapData", self.currentData);
						break;
					case "requestHeatmapData":
						self.currentData = self.getHeatmapData(value);
						Topic.publish("ProteinFamilies", "updateHeatmapData", self.currentData);
						break;
					default:
						break;
				}
			});
		},
		conditionFilter: function(gfs){
			var self = this;
			if(self._filtered == undefined){ // first time
				self._filtered = true;
				self._original = this.query("", {});
			}
			var data = self._original;
			var newData = [];

			// var tsStart = window.performance.now();
			data.forEach(function(family){

				var skip = false;

				Object.keys(gfs).forEach(function(genomeId){
					var index = gfs[genomeId].getIndex();
					var status = gfs[genomeId].getStatus();
					//console.log(family.family_id, genomeId, index, status, family.genomes, parseInt(family.genomes.substr(index * 2, 2), 16));
					if(status == 1 && parseInt(family.genomes.substr(index * 2, 2), 16) > 0){
						skip = true;
					}
					else if(status == 0 && parseInt(family.genomes.substr(index * 2, 2), 16) == 0){
						skip = true;
					}
				});
				if(!skip){
					newData.push(family);
				}
			});
			// console.log("genomeFilter took " + (window.performance.now() - tsStart), " ms");

			self.setData(newData);
			self.set("refresh");
		},
		reload: function(){
			var self = this;
			delete self._loadingDeferred;
			self._loaded = false;
			self.loadData();
			self.set("refresh");
		},

		query: function(query, opts){
			query = query || {};
			//console.warn("query: ", query, opts);
			if(opts.sort == undefined){
				opts.sort = [{attribute: "family_id", descending: false}];
			}
			if(this._loaded){
				return this.inherited(arguments);
			}
			else{
				var _self = this;
				var results;
				var qr = QueryResults(when(this.loadData(), function(){
					results = _self.query(query, opts);
					qr.total = when(results, function(results){
						return results.total || results.length
					});
					return results;
				}));

				return qr;
			}
		},

		get: function(id, opts){
			if(this._loaded){
				return this.inherited(arguments);
			}else{
				var _self = this;
				return when(this.loadData(), function(){
					return _self.get(id, options)
				})
			}
		},

		loadData: function(){
			if(this._loadingDeferred){
				return this._loadingDeferred;
			}

			var _self = this;

			// console.warn(this.state.genome_ids, !this.state.genome_ids);
			if(!this.state.genome_ids){
				// console.log("No Genome IDS, use empty data set for initial store");

				//this is done as a deferred instead of returning an empty array
				//in order to make it happen on the next tick.  Otherwise it
				//in the query() function above, the callback happens before qr exists
				var def = new Deferred();
				setTimeout(lang.hitch(_self, function(){
					_self.setData([]);
					_self._loaded = true;
					def.resolve(true);
				}), 0);
				return def.promise;
			}<commit_msg>fix scope<commit_after>define([
	"dojo/_base/declare", "dojo/request",
	"dojo/store/Memory", "dojo/store/util/QueryResults",
	"dojo/when", "dojo/_base/lang", "dojo/Stateful", "dojo/_base/Deferred",
	"dojo/topic", "./HeatmapDataTypes"
], function(declare, request,
			Memory, QueryResults,
			when, lang, Stateful, Deferred,
			Topic){

	var pfState = {
		familyType: 'figfam', // default
		heatmapAxis: '',
		genomeIds: [],
		genomeFilterStatus: {},
		clusterRowOrder: [],
		clusterColumnOrder: [],
		perfectFamMatch: 'A',
		min_member_count: null,
		max_member_count: null,
		min_genome_count: null,
		max_genome_count: null
	};

	return declare([Memory, Stateful], {
		baseQuery: {},
		apiServer: window.App.dataServiceURL,
		idProperty: "family_id",
		state: null,
		pfState: pfState,

		constructor: function(options){
			this._loaded = false;
			if(options.apiServer){
				this.apiServer = options.apiServer;
			}

			var self = this;

			Topic.subscribe("ProteinFamilies", function(){
				// console.log("received:", arguments);
				var key = arguments[0], value = arguments[1];

				switch(key){
					case "setFamilyType":
						self.pfState.familyType = value;
						self.reload();
						self.currentData = self.getHeatmapData(self.pfState);
						Topic.publish("ProteinFamilies", "updateHeatmapData", self.currentData);
						break;
					case "applyConditionFilter":
						self.conditionFilter(value);
						self.currentData = self.getHeatmapData(self.pfState);
						Topic.publish("ProteinFamilies", "updateHeatmapData", self.currentData);
						break;
					case "requestHeatmapData":
						self.currentData = self.getHeatmapData(value);
						Topic.publish("ProteinFamilies", "updateHeatmapData", self.currentData);
						break;
					default:
						break;
				}
			});
		},
		conditionFilter: function(gfs){
			var self = this;
			if(self._filtered == undefined){ // first time
				self._filtered = true;
				self._original = this.query("", {});
			}
			var data = self._original;
			var newData = [];

			// var tsStart = window.performance.now();
			data.forEach(function(family){

				var skip = false;

				Object.keys(gfs).forEach(function(genomeId){
					var index = gfs[genomeId].getIndex();
					var status = gfs[genomeId].getStatus();
					//console.log(family.family_id, genomeId, index, status, family.genomes, parseInt(family.genomes.substr(index * 2, 2), 16));
					if(status == 1 && parseInt(family.genomes.substr(index * 2, 2), 16) > 0){
						skip = true;
					}
					else if(status == 0 && parseInt(family.genomes.substr(index * 2, 2), 16) == 0){
						skip = true;
					}
				});
				if(!skip){
					newData.push(family);
				}
			});
			// console.log("genomeFilter took " + (window.performance.now() - tsStart), " ms");

			self.setData(newData);
			self.set("refresh");
		},
		reload: function(){
			var self = this;
			delete self._loadingDeferred;
			self._loaded = false;
			self.loadData();
			self.set("refresh");
		},

		query: function(query, opts){
			query = query || {};
			//console.warn("query: ", query, opts);
			if(opts.sort == undefined){
				opts.sort = [{attribute: "family_id", descending: false}];
			}
			if(this._loaded){
				return this.inherited(arguments);
			}
			else{
				var _self = this;
				var results;
				var qr = QueryResults(when(this.loadData(), function(){
					results = _self.query(query, opts);
					qr.total = when(results, function(results){
						return results.total || results.length
					});
					return results;
				}));

				return qr;
			}
		},

		get: function(id, opts){
			if(this._loaded){
				return this.inherited(arguments);
			}else{
				var _self = this;
				return when(this.loadData(), function(){
					return _self.get(id, options)
				})
			}
		},

		loadData: function(){
			if(this._loadingDeferred){
				return this._loadingDeferred;
			}

			var _self = this;

			// console.warn(this.state.genome_ids, !this.state.genome_ids);
			if(!this.state.genome_ids){
				// console.log("No Genome IDS, use empty data set for initial store");

				//this is done as a deferred instead of returning an empty array
				//in order to make it happen on the next tick.  Otherwise it
				//in the query() function above, the callback happens before qr exists
				var def = new Deferred();
				setTimeout(lang.hitch(_self, function(){
					this.setData([]);
					this._loaded = true;
					def.resolve(true);
				}), 0);
				return def.promise;
			}
{'fraction_non_alphanumeric': 0.15178168696436625, 'fraction_numerical': 0.004285069914298602, 'mean_word_length': 3.1795475966069744, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 5, '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': '3219051', 'n_tokens_mistral': 3714, 'n_tokens_neox': 2834, 'n_words': 617}