aboutsummaryrefslogtreecommitdiff
path: root/web/webqtl/correlationMatrix/tissueCorrelationMatrix.py
blob: 23dc14eb6e742b19e3417949c00c0c1180444ce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright (C) University of Tennessee Health Science Center, Memphis, TN.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# This program is available from Source Forge: at GeneNetwork Project
# (sourceforge.net/projects/genenetwork/).
#
# Contact Drs. Robert W. Williams and Xiaodong Zhou (2010)
# at rwilliams@uthsc.edu and xzhou15@uthsc.edu
#
# This module is used by GeneNetwork project (www.genenetwork.org)
#
# Created by GeneNetwork Core Team 2010/11/10
#
# Last updated by Ning Liu, 2011/01/26


#tissueCorrelationMatrix: funciton part for TissueCorrelationPage.py
from htmlgen import HTMLgen2 as HT
from correlation import correlationFunction
from dbFunction import webqtlDatabaseFunction
import sys

#########################################
#      Tissue Correlation Page
#########################################

class  tissueCorrelationMatrix:
	def __init__(self,tissueProbeSetFreezeId=None):
		
		#initialize parameters
		self.tProbeSetFreezeId = tissueProbeSetFreezeId
		self.cursor = webqtlDatabaseFunction.getCursor()



	#retreive dataSet info from database table TissueProbeSetFreeze to get all TissueProbeSetFreezeId(List), Name(List) and FullName(List)
	def getTissueDataSet(self):	
		tissProbeSetFreezeIds,Names,fullNames = webqtlDatabaseFunction.getTissueDataSet(cursor=self.cursor)
		return tissProbeSetFreezeIds,Names,fullNames


	#retrieve DatasetName, DatasetFullName based on TissueProbeSetFreezeId, return DatasetName(string), DatasetFullName(string)
	def getFullnameofCurrentDataset(self):
	
		DatasetName, DatasetFullName =webqtlDatabaseFunction.getDatasetNamesByTissueProbeSetFreezeId(cursor=self.cursor, TissueProbeSetFreezeId=self.tProbeSetFreezeId)		
		return DatasetName, DatasetFullName

				
	#retrieve how many tissue used in the specific dataset based on TissueProbeSetFreezeId, return TissueCount(int)
	def getTissueCountofCurrentDataset(self):
	
		TissueCount =webqtlDatabaseFunction.getTissueCountByTissueProbeSetFreezeId(cursor=self.cursor,TissueProbeSetFreezeId=self.tProbeSetFreezeId)
		return TissueCount


		
	#retrieve corrArray(array), pvArray(array) for display by calling  calculation function:calZeroOrderCorrForTiss
	def getTissueCorrPvArray(self,geneNameLst=None,dataIdDict=None):	
		#retrieve SymbolValuePairDict(Dict), dictionary of Symbol and Value Pair.key is symbol, value is one list of expression values of one probeSet
		symbolValuepairDict =correlationFunction.getGeneSymbolTissueValueDict(cursor=self.cursor,symbolList=geneNameLst,dataIdDict=dataIdDict)
		corrArray,pvArray = correlationFunction.getCorrPvArray(cursor=self.cursor,priGeneSymbolList=geneNameLst,symbolValuepairDict=symbolValuepairDict)
		return corrArray,pvArray


		
	#retrieve symbolList,geneIdList,dataIdList,ChrList,MbList,descList,pTargetDescList (all are list type) to 
	#get multi lists for short and long label functions, and for getSymbolValuePairDict and 
	#getGeneSymbolTissueValueDict to build dict to get CorrPvArray
	def getTissueProbeSetXRefInfo(self,GeneNameLst=[]):
		symbolList,geneIdDict,dataIdDict,ChrDict,MbDict,descDict,pTargetDescDict =correlationFunction.getTissueProbeSetXRefInfo(cursor=self.cursor,GeneNameLst=GeneNameLst,TissueProbeSetFreezeId=self.tProbeSetFreezeId)
		return symbolList,geneIdDict,dataIdDict,ChrDict,MbDict,descDict,pTargetDescDict		



	#retrieve corrArray(array), pvArray(array) for gene symbol pair
	def getCorrPvArrayForGeneSymbolPair(self,geneNameLst=None):
		corrArray = None
		pvArray = None

		if len(geneNameLst) == 2:
			#retrieve SymbolValuePairDict(Dict), dictionary of Symbol and Value Pair.key is symbol, value is one list of expression values of one probeSet
			symbolList,geneIdDict,dataIdDict,ChrDict,MbDict,descDict,pTargetDescDict =correlationFunction.getTissueProbeSetXRefInfo(cursor=self.cursor,GeneNameLst=geneNameLst,TissueProbeSetFreezeId=self.tProbeSetFreezeId)	
			symbolValuepairDict =correlationFunction.getGeneSymbolTissueValueDict(cursor=self.cursor,symbolList=geneNameLst,dataIdDict=dataIdDict)
			corrArray,pvArray = correlationFunction.getCorrPvArray(cursor=self.cursor,priGeneSymbolList=geneNameLst,symbolValuepairDict=symbolValuepairDict)

		return corrArray,pvArray


	#retrieve symbolCorrDict(dict), symbolPvalueDict(dict) to get all tissues' correlation value and P value; key is symbol	
	def calculateCorrOfAllTissueTrait(self, primaryTraitSymbol=None, method='0'):
		symbolCorrDict, symbolPvalueDict = correlationFunction.calculateCorrOfAllTissueTrait(cursor=self.cursor, primaryTraitSymbol=primaryTraitSymbol, TissueProbeSetFreezeId=self.tProbeSetFreezeId,method=method)
		
		return symbolCorrDict, symbolPvalueDict

	#Translate GeneId to gene symbol and keep the original order.
	def getGeneSymbolLst(self, geneSymbols=None):
		geneSymbolLst=[]
		geneIdLst=[]
		#split the input string at every occurrence of the delimiter '\r', and return the substrings in an array. 
		tokens=geneSymbols.strip().split('\r')

		#Ning: To keep the original order of input symbols and GeneIds
		for i in tokens:
			i=i.strip()
			if (len(i) >0) and (i not in geneSymbolLst):
				geneSymbolLst.append(i)
				# if input includes geneId(s), then put it/them into geneIdLst
				if i.isdigit():
					geneIdLst.append(i)

		#Ning: Replace GeneId with symbol if applicable
		if len(geneIdLst)>0:
			# if input includes geneId(s), replace geneId by geneSymbol; 
			geneIdSymbolPair =webqtlDatabaseFunction.getGeneIdSymbolPairByGeneId(cursor=self.cursor, geneIdLst =geneIdLst)
			for geneId in geneIdLst:
				if geneIdSymbolPair[geneId]:
					index = geneSymbolLst.index(geneId)
					geneSymbolLst[index] =geneIdSymbolPair[geneId]

		return geneSymbolLst