예제 #1
0
from rpy2.robjects import r
import rpy2.robjects as ro
import rpy2.robjects.numpy2ri
import rpy2.rpy_classic as rpy
import h5py

try:
    filename = sys.argv[1]
    projectname = sys.argv[2]
except Exception:
    print "USAGE:\npython rundecomp.py matrixfile projectname"


print "Filename: " + filename + "\nProject: " + projectname

mat = NamedMatrix.readfromfile(filename, nullvalue="-1", header=True, rownames=True, separator="\t", skip=0)

NamedMatrixHDF5.writeNamedMatrix(mat, projectname, "/freqs", overwrite=True)

sigfn = lambda x: str(",".join(map(lambda y: str(y), (x != float("inf")) * 1)))

setnames = np.apply_along_axis(sigfn, 1, mat.matrix)
unq_subs = np.unique(setnames)
colfn = lambda x: np.array(map(lambda y: y != str(0), x.split(",")))
cols = map(colfn, unq_subs)

h5 = h5py.File(projectname)
h5.create_group("subsets")
hndl = h5["/subsets"]
hndl.create_dataset("names", data=unq_subs)
hndl.create_dataset("columns", data=cols)
예제 #2
0
def calcTDI (mutcnaMatrixFN, degMatrixFN, alphaNull = [1, 1], alphaIJKList = [2, 1, 1, 2], 
              v0=0.3,   outputPath = ".", opFlag = None, rowBegin=0, rowEnd = None, GeGlobalDriverDict = None):
    """ 
    calcTCI (mutcnaMatrix, degMatrix, alphaIJList, alphaIJKList, dictGeneLength)
    
    Calculate the causal scores between each pair of SGA and DEG observed in each tumor
    
    Inputs:
        mutcnaMatrixFN      A file containing a N x G binary matrix containing the mutation and CNA 
                            data of all tumorss.  N is the number of tumors and 
                            G is number of total number of unique genes.  For a
                            tumor, genes that have SGAs are indicated by "1"s and "0" 
                            otherwise. 
        degMatrixFN         A file contains a N x G' binary matrix representing DEG
                            status.  A "1" indicate a gene is differentially expressed
                            in a tumor.
        
        alphaIJList         A list of Dirichlet hyperparameters defining the prior
                            that a mutation event occurs
                            
        alphaIJKList        A list of Dirichlet hyperparameters for caulate the prior
                            of condition probability parameters. alphaIJK[0]: mut == 0 && deg == 0;
                            alphaIJK[1]: mut == 0 && deg == 1; alphaIJK[2]: mut == 1 && deg == 0;
                            alphaIJK[3]: mut == 1 && deg == 1
                            
        v0                  A float scalar indicate the prior probability that a DEG
                            is caused by a non-SGA factor 
        
    
        rowBegin, rowEnd    These two arguments control allow user to choose which block out of all tumors (defined by the two 
                            row numbers) will be processes in by this function.  This can be used to process
                            mulitple block in a parallel fashion.

        GeGlobalDriverDict  A dictionary, in which GEs are keys and values are top two GTs that have strongest global association with GEs


    """
    
    # read in data in the form of NamedMatrix 
    try:
        mutcnaMatrix  = NamedMatrix(mutcnaMatrixFN)
    except:
        print "Failed to import data matrix %s\n" % mutcnaMatrixFN
        sys.exit() 
        
    try:
        degMatrix = NamedMatrix(degMatrixFN)
    except:
        print "Failed to import data matrix %s\n" % degMatrixFN
        sys.exit()
        
    exprsTumorNames = [x.replace("\"", "") for x in degMatrix.getRownames()]
    mutTumorNames = [x.replace("\"", "") for x in mutcnaMatrix.getRownames()]
    if exprsTumorNames != mutTumorNames:
        print "The tumors for mutcnaMatrix and degMatrix do not fully overlap!"
        print degMatrix.getRownames()
        print mutcnaMatrix.getRownames()
        sys.exit()
                         
    tumorNames = degMatrix.getRownames()
    nTumors, nMutGenes = mutcnaMatrix.shape()
    
    mutGeneNames = mutcnaMatrix.getColnames()
    degGeneNames = degMatrix.getColnames()
    
    # now we iterate through each tumor to infer the causal relationship between each 
    # pair of mut - deg
    # loop through individual tumors and calculate the causal scores between each pair of SGA and DEG    
    if not rowEnd:
        rowEnd = nTumors - 1
    else:
        if rowEnd >= nTumors:
            rowEnd = nTumors - 1
        elif rowEnd < rowBegin:
            print "Invalid rowEnd < rowBegin arguments given."
            sys.exit()

    if rowBegin > rowEnd:
        print "Invlid rowBegin > rowEnd argument given."
        sys.exit()

    print "Done with loading data, start processing tumor " + str(rowBegin)
    for t in range(rowBegin, rowEnd):
        #print pacifier
        if t % 50 == 0:
            print "Processed %s tumors" % str(t)
        
        
        # collect data related to DEGs.  Identify the genes that are differentially expressed in a tumor,
        # then collect
        degGeneIndx = [i for i, j in enumerate(degMatrix.data[t,:]) if j == 1]
        tumorDEGGenes = [degGeneNames[i] for i in degGeneIndx]
        nTumorDEGs = len(degGeneIndx)  # corresponding to n, the number of DEGs in a given tumor
        tumorDEGMatrix = degMatrix.data[:, degGeneIndx]
        
        # find the global drivers for the DEGs
        allTumorGlobDrivers = np.zeros((nTumors, nTumorDEGs))
        gDriverGeneNames = []
        gDriverIndices = []
        g2ndDriverGeneNames = []
        missingGDrivers = []
        for  j, ge in enumerate(tumorDEGGenes):
            if ge not in GeGlobalDriverDict:
                ge = ge.replace("-", ".")
                if ge not in GeGlobalDriverDict: # if still cannot find it, rel
                    missingGDrivers.append(j)
                    gDriverIndices.append(0)
                    continue
            top2Driver4CurGE = GeGlobalDriverDict[ge] # query the dictionary to retrive the top two global dirvers
            geGlobalDriver = top2Driver4CurGE[0]  
            gDriverGeneNames.append(geGlobalDriver)
            g2ndDriverGeneNames.append(top2Driver4CurGE[1])
            gDriverIndices.append(mutGeneNames.index(geGlobalDriver))
        allTumorGlobDrivers = mutcnaMatrix.data [:, gDriverIndices]
        for m in missingGDrivers:
            allTumorGlobDrivers[:, m] = 1

        # collect data related to mutations
        tumormutGeneIndx = [i for i, j in enumerate(mutcnaMatrix.data[t,:]) if j == 1]
        tumorMutGenes = [mutGeneNames[i] for i in tumormutGeneIndx] 
        print "Processing tumor " + tumorNames[t] + " with " + str(nTumorDEGs) + " DEGs, " + str(len(tumormutGeneIndx)) + " GTs"  
        # the matrix that cotnains the likelihood of pair wise causal structure, rows are GTs; columns are GEs
        tumorLnFScore = np.zeros((len(tumorMutGenes), nTumorDEGs))

        # We need to loop through individual GTs, divide tumors into blocks: one with a GT is set to ones, the other in which GT is 0,
        # then calculate the using the GT can explain the DEGs in a tumor well, while enhance the capbility of and other 
        # global driver to explain the GE in the tumors that do not have the mutation in the GT of interest  
        for i, gtName in enumerate(tumorMutGenes):
            gtIndx = tumormutGeneIndx[i]
            #print "Index of current GT: ", str(gtIndx) + " and name: " + gtName
            indxTumorWithGT = np.where(mutcnaMatrix.data[:, gtIndx] == 1)[0]
            indxTumorNotGT = np.where(mutcnaMatrix.data[:,gtIndx] == 0)[0] 

            # first calculate the FScore for tumors with T.  We can use the CalNullF which deal with all GT == 1 cases
            DEGMatrixOfTumorsWithGT = tumorDEGMatrix[indxTumorWithGT,:]
            tumorLnFScore[i,:] = calcNullF(DEGMatrixOfTumorsWithGT, [1, 2])

            # now dealing with tumors without GT perturbed
            DEGMatrixOfTumorsNoGT = tumorDEGMatrix[indxTumorNotGT,:]
            # extract the global drivers for the DEGs
            globalDriver4DEGs = allTumorGlobDrivers[indxTumorNotGT,:]
            if gtName in gDriverGeneNames:  # swap global driver if the cur GT is one
                indxOfColumnToSwap = gDriverGeneNames.index(gtName)
                indxOfReplaceGDriver = mutGeneNames.index(g2ndDriverGeneNames[indxOfColumnToSwap])
                globalDriver4DEGs[:,indxOfColumnToSwap] = mutcnaMatrix.data [indxTumorNotGT, indxOfReplaceGDriver]
                
            # call calcF, which should return a vector gloabal drivers to GEs,
            # Note that we do not need to calcualte the prior for global drivers because they are the same for a
            # given GE, therefore will be cancelled out when normalize across GT
            tumorLnFScore[i, :] = tumorLnFScore[i, :] + calcF(globalDriver4DEGs, DEGMatrixOfTumorsNoGT, alphaIJKList)
        # Done with data likelihood p(D|S) section

       # Calculate the likelihood of expression data conditioning on A0, and then stack to 
        # the LnFScore, equivalent to adding a column of '1' to represent the A0 in tumorMutMatrix
        nullFscore = calcNullF(tumorDEGMatrix, alphaNull)
        tumorLnFScore = np.vstack((tumorLnFScore, nullFscore))  
               
        # calcualte the prior probability that any of mutated genes can be a cause for a DEG,
        # tile it up to make an nTumorMutGenes x nTumorDEG matrix
        lntumorMutPriors = calcLnPriorBasedOnFreqWithV0(tumormutGeneIndx, mutcnaMatrix, v0)
        lnFScore = np.add(tumorLnFScore.T, lntumorMutPriors).T
        
        # now we need to caclculate the normalized lnFScore so that each         
        columnAccumLogSum = calcColNormalizer (lnFScore) 
        posterior = np.exp(np.add(lnFScore, - columnAccumLogSum))
        
        #write out the results                       
        tumorMutGenes.append('A0') 
        tumorPosterior = NamedMatrix(npMatrix = posterior, rownames = tumorMutGenes, colnames = tumorDEGGenes)
        if "\"" in tumorNames[t]:
            tumorNames[t] = tumorNames[t].replace("\"", "")    
        tumorPosterior.writeToText(filePath = outputPath, filename = tumorNames[t] + ".csv")
예제 #3
0
def readfromfile(hdf5file, url, *args, **kwargs):
    mat = nm.readfromfile(*args, **kwargs)
    nmat = writeNamedMatrix( mat, hdf5file, url, overwrite=True)
    return nmat