// Constructor and methods for Biological Process objects

function BioProcess(index, id, name, desc, pv,g) {
	this.category = "Temp Category";
	this.bioFunction = "Temp Function";
	this.index = index;
	this.name = name.toTitleCase();
	this.pValue = pv;
	this.hasRank = true;
	this.rank = 0;
	this.id = id;
	this.desc = this.name + ": " + desc;
	this.genes = [];
	this.fullGeneList = [];
	this.genesNotInDataset = [];
	this.graphics={wheel:null, matrix:null};
	this.inContext = true;
	this.citations = [];
	this.fullGeneList = g;
	this.topic = "bioProcesses";
	this.topicLabel = "biological process";
  // Same as Gene - where is bioProcess coming from?!?!
  // this.topicArray = bioProcesses;
	
	for (var i=0; i<this.fullGeneList.length; i++) {
		var gt = genes.indexById(this.fullGeneList[i]);
		if(gt != -1) {
			this.genes.push(gt);
			genes.list[gt].bioProcesses.push(this.index);
		} else {
			this.genesNotInDataset.push(this.fullGeneList[i]);
		}
	}
	
	this.getRank = function() {
		this.rank = 1.0 - (this.pValue - bioProcesses.rankRange.min)*bioProcesses.rankRange.inv;
		return this.rank;
	};
	
	this.select = function() {
		selectionMgr.set(this);
	}

	this.getGeneList = function() {
		return this.genes;
	}
	this.getGenesInContext = function() {
		var lst = [];
		for(var i=0; i<this.genes.length; i++) {
			if(genes.list[this.genes[i]].inContext) lst.push(this.genes[i])
		}
		return lst;
	};
	
	this.getHierarchyObject = function() {
		var hier;
		var curNode = null;
		for (var i=0; i<hierarchies.length; i++) {  // shouldn't have to do this now.. should be done at initialization
			if(hierarchies[i].topic.indexOf("process") >=0) hier = hierarchies[i];
		}
		var hierObj = null;
		for (var i=0; i < hier.nodes.length; i++) {
			if(hier.nodes[i].id == this.id) {
				hierObj = hier.nodes[i];
			};
		}
		return hierObj;
	};
	
	this.getRimObject = function() {
		var hierObj = this.getHierarchyObject();
		var curNode = null;
		if(hierObj) {
			curNode = hierObj;
			while(curNode.rimChip == null) {
				curNode = curNode.parentObj;
			}
			// curNode is the closest ancestor that has a place on the wheel rim
		}
		return curNode;
	};
	
	this.updateDescription = function(){
		updateProcessDescription(this.id);
	};
	this.isInContext = function() {
		var gl = this.getGenesInContext();
		this.inContext = (gl.length > 0)? true : false;
		return this.inContext;
	}
	
	this.getRankQuartile = function() {
		return quartile(this.pValue, bioProcesses.rankRange);	// min -> 3,  max -> 0  ... based on pValue
	}
	
	this.getTopic 		= function() { return this.topic};
	this.getTopicLabel 	= function() { return this.topicLabel};
	this.getTopicArray  = function() {return this.topicArray;};
	this.getName 		= function() { return this.name};
	this.getIndex 		= function() {return this.index};
	this.getDesc 		= function() {return this.desc};
	this.getId   		= function() {return this.id};
}

function initBioProcesses(updateBioProcessesList, processList, callback) {
  updateBioProcessesList(processData[reportId], processList, callback);
}

function updateProcessDescription(did) {
  var options = {
    async: true,
    callback: function() {
      var pJson = json_parse(this.responseText);
      if (pJson.desc != null) {
        x$("#dbpDesc").html(pJson.desc);
      }
    }
  };
  
  x$(window).xhr('/bda/data/processes/desc/' + did, options );
}

var updateProcessFunc = function updateBioProcessesList(pJson, processList, dataLoadStatus) {
	var pValueMin = 999999999;
	var pValueMax = -pValueMin;
	var mngpp = 0;

  var len = (gDataSize == 0) ? pJson.processes.length : gDataSize;
	for (var i=0; i < len ; i++) {	  
	  
		var pro = pJson.processes[i];
		processList.list.push(new BioProcess(i,pro.did , pro.name, "", pro.pvalue, pro.molIds));
		pValueMin = Math.min(pValueMin, pro.pvalue);
		pValueMax = Math.max(pValueMax, pro.pvalue);
		var indx = processList.list.length-1;
		mngpp = Math.max(processList.list[indx].genes.length, mngpp);
	}

	var maxNumMembers = mngpp;
	var rrInv = 0;
	if(pValueMin != pValueMax) rrInv = 1.0/(pValueMax-pValueMin);
	
	var rankRange = {min:pValueMin, max:pValueMax, inv:rrInv};

  processList.updateValues(maxNumMembers, rankRange);
  dataLoadStatus();
}

function BioProcessesList() {
	this.name = 'processes';
	this.list = new Array();

	this.getName = function(){
		return this.name;
	};
	
	this.updateValues = function(maxNumMembers, rankRange) {
		this.maxNumMembers = maxNumMembers;
		this.rankRange = rankRange;
	};

	this.numberInContext = function() {
		var num=0;
		for (var i=0; i<this.list.length; i++) {
			if(this.list[i].inContext) num++;
		}
		return num;
	};

	this.listInContext = function() {
		var l = new Array();
		for (var i=0;i<this.list.length; i++) {
			if(this.list[i].inContext) l.push(i);
		}
		return l;
	};
	
	this.byIndex = function(i) {
		return this.list[i];
	};
	
	this.sortBy = function(opt, inContext) {
		var l = [];
		if(inContext) {
			l = this.listInContext();
		} else {
			for (var i=0; i<this.list.length; i++) {
				l[i] = i;
			}
		}
		l.sort(function(a,b) {
			return bioProcesses.list[a].pValue - bioProcesses.list[b].pValue;
		});

		return l;
	};
	
	this.getByID = function(id) {
		for (var i=0; i<this.list.length; i++) {
			if(this.list[i].id == id) return this.list[i];
		}
		return null;
	}
	
}

