def _initHiddenStates(self): hiddenNodes = [ n for n in self.network if not self.network.node[n]['nodeObj'].bMeasured ] phosNodes = [ n for n in self.network if self.network.node[n]['nodeObj'].type == 'PHOSPHORYLATIONSTATE' ] #print str(phosNodes) nCases, nAntibody = self.obsData.shape() caseNames = self.obsData.getRownames() self.nodeStates = list() for c in range(self.nChains): tmp = np.zeros((nCases, len(hiddenNodes))) tmp[np.random.rand(nCases, len(hiddenNodes)) < 0.3] = 1 tmp = np.column_stack((tmp, self.perturbData.data)) colnames = hiddenNodes + self.perturbData.colnames self.nodeStates.append( NamedMatrix(npMatrix=tmp, colnames=colnames, rownames=caseNames)) #initialize phos state based on the observed fluo for node in phosNodes: fluoNode = node + 'F' #print "phosNode:" + node + "; fluoNode: " + fluoNode fluoNodeObj = self.network.node[fluoNode]['nodeObj'] fluoData = self.obsData.getValuesByCol(fluoNode) tmp = np.zeros(nCases) phosProbOne = - np.log(fluoNodeObj.sigmas[c, 1])\ - 0.5 * np.square(fluoData - fluoNodeObj.mus[c, 1]) / np.square(fluoNodeObj.sigmas[c, 1]) phosProbZero = - np.log(fluoNodeObj.sigmas[c, 0])\ - 0.5 * np.square(fluoData - fluoNodeObj.mus[c, 0]) / np.square(fluoNodeObj.sigmas[c, 0]) tmp[phosProbOne > phosProbZero] = 1 nodeIndx = self.nodeStates[c].findColIndices(node) self.nodeStates[c].data[:, nodeIndx] = tmp # take care of missing values by random sampling if self.missingDataMatrix: if node in self.missingDataMatrix.getColnames(): #print "processing node with missing values: " + nodeId missingCases = self.missingDataMatrix.getValuesByCol( node) == 1 tmp = np.zeros(sum(missingCases)) tmp[np.random.rand(len(tmp)) <= 0.3] = 1 self.nodeStates[c].data[missingCases, nodeIndx] = tmp
def __init__(self, nodeFile, dataMatrixFile, perturbMatrix=None, missingDataMatrix=None): self.network = None self.obsData = None self.missingDataMatrix = None perturbInstances = None self.nChains = 1 self.dictPerturbEffect = {'AKT1' : [('GSK690693', 0), \ ('GSK690693_GSK1120212', 0)], 'MAP2K1' : [('GSK690693_GSK1120212', 0)],\ 'EGFR': [('EGF' , 1), ('FGF1', 1)]} # self.stimuli = ['EGF', 'FGF1', 'HGF', 'IGF1', 'Insulin', 'NRG1', 'PBS', 'Serum'] # parse data mastrix by calling NamedMatrix class if not dataMatrixFile: raise Exception( "Cannot create PyCAMP obj without 'dataMatrixFile'") return self.obsData = NamedMatrix(dataMatrixFile) nCases, nAntibodies = np.shape(self.obsData.data) self.obsData.colnames = map(lambda s: s + 'F', self.obsData.colnames) self.obsDataFileName = dataMatrixFile if perturbMatrix: self.perturbData = NamedMatrix(perturbMatrix) perturbInstances = self.perturbData.getColnames() self.perturbInstances = perturbInstances if missingDataMatrix: self.missingDataMatrix = NamedMatrix(missingDataMatrix) allMissing = np.sum(self.missingDataMatrix, 0) == nCases if np.any(allMissing): raise Exception("Data matrix contain data-less columns") self.missingDataMatrix.colnames = map( lambda s: s + 'F', self.missingDataMatrix.colnames) if not nodeFile: raise Exception("Calling 'intiNetwork' with empty nodeFile name") return try: nf = open(nodeFile, "r") nodeLines = nf.readlines() if len(nodeLines ) == 1: # Mac files end a line with \r instead of \n nodeLines = nodeLines[0].split("\r") nf.close() except IOError: raise Exception("Failed to open the file containing nodes") return print "Creating network" self.network = nx.DiGraph() self.dictProteinToAntibody = dict() self.dictAntibodyToProtein = dict() # parse nodes for line in nodeLines: #print line protein, antibody = line.rstrip().split(',') if protein not in self.dictProteinToAntibody: self.dictProteinToAntibody[protein] = [] self.dictProteinToAntibody[protein].append(antibody) self.dictAntibodyToProtein[antibody] = protein fluo = antibody + 'F' if protein not in self.network: self.network.add_node(protein, nodeObj=SigNetNode( protein, 'ACTIVATIONSTATE', False)) self.network.add_node(antibody, nodeObj=SigNetNode(antibody, 'PHOSPHORYLATIONSTATE', False)) self.network.add_node(fluo, nodeObj=SigNetNode(fluo, 'FLUORESCENCE', True)) self.network.add_edge(antibody, protein) self.network.add_edge(antibody, fluo) for perturb in perturbInstances: self.network.add_node(perturb, nodeObj=SigNetNode(perturb, 'PERTURBATION', True)) # Add edges between PERTURBATION, protein activity,and phosphorylation layers for pro in self.dictProteinToAntibody: for phos in self.dictAntibodyToProtein: if self.dictAntibodyToProtein[phos] == pro: continue self.network.add_edge(pro, phos) for perturb in perturbInstances: self.network.add_edge(perturb, pro)