Example #1
0
def add_newlinks_gamma_to_gamma(path, args):
    """ function to add new links to above all of the betas """
    # Initialize default values
    path.reinit()

    # make a copy of the yvar and xvar for iteration, because I am going to be
    # messing with both lists
    _xvar = list(path.xvar)

    # Iterate over all exogenous variables (cols of gamma), turn them into an
    # endogenous variable 1 at a time and then add all betas
    for col, target in enumerate(_xvar):
        # Determine if gene has multiple isoforms
        tCnt = utils.isoCount(target)

        # Convert exogenous varaible to an endogenous variable
        path.convert_ExogToEndog(target)

        # Now iterate over the new endogenous variable list (cols of gamma) and
        # add links 1 at a time to gamma
        for index, gene in enumerate(path.xvar):
            model_type = "Adding Newlinks Gamma to Gamma :\n {0} -> {1}".format(gene, target)

            # Determine if gene has multiple isoforms
            isoCnt = utils.isoCount(gene)

            # Skip when one gene is already regulating the other
            path.gamma[-tCnt:, index:index+isoCnt] = 1
            createOutput(path, model_type, args)
            path.gamma[-tCnt:, index:index+isoCnt] = 0
            path.count_increment()

        # Initialize default values
        path.reinit()
Example #2
0
def add_newlinks_beta_to_beta(path, args):
    """ function to add new links to above all of the betas """
    # Initialize default values
    path.reinit()

    # Iterate overall of the endogenous variables (cols of beta)
    for index, gene in enumerate(path.yvar):
        # Determine if gene has multiple isoforms
        isoCnt = utils.isoCount(gene)

        # Iterate over the the endogenous varaibles (rows of beta) and add link
        # one at a time.
        for row, target in enumerate(path.yvar):
            # A gene cannot regulate itself in SEMs
            if row != index:
                # Determine if gene has multiple isoforms
                tCnt = utils.isoCount(target)

                # Skip when one gene is already regulating the other
                if path.beta[row, index] == 0 and path.beta[index, row] == 0:
                    model_type = "Adding Newlinks Beta to Beta:\n {0} -> {1}".format(gene, target)

                    path.beta[row:row+tCnt, index:index+isoCnt] = 1
                    createOutput(path, model_type, args)
                    path.beta[row:row+tCnt, index:index+isoCnt] = 0
                    path.count_increment()
Example #3
0
def add_genes_bt_beta(path, newgene, args):
    """ Iterate through beta matrix and add genes between two betas. """

    # Initialize default values again
    path.reinit()

    # Expand gamma and beta to account for new isoforms
    for iso in range(0, newgene.count):
        path.expand_beta()
        path.expand_gamma()

    # Make copy of oiriginal for iterating
    _yvar = list(path.yvar)

    # Append new gene to yvar
    path.yvar.append(newgene.name)

    # Iterate through BETA matrix and add genes between betas
    cIndex = 0
    for col in _yvar:
        # How many isoforms does the current column have
        colCnt = utils.isoCount(col)

        rIndex = 0
        for row in _yvar:
            # A gene cannot act on itself, so skip when row=col
            if col == row:
                continue

            # How many isoforms does the current row have
            rowCnt = utils.isoCount(row)

            # Zero slice, is the current location in Beta, if there are 1s
            # there I will turn them 0
            zSlice = np.s_[rIndex:rIndex + rowCnt, cIndex:cIndex + colCnt]

            # Target slice, is the bottom 'newgene.count' rows, that will get turned 1
            # showing that current 'col' is acting on the newgene
            tSlice = np.s_[-newgene.count:, cIndex:cIndex + colCnt]

            # Source slice, is the right most 'newgene.count' cols, that will get turned 1
            # showing that newgene is acting on the current row
            sSlice = np.s_[rIndex:rIndex + rowCnt, -newgene.count:]

            # If there was a previous interaction (i.e. 1) then make models
            if np.all(path.beta[zSlice]):
                model_type = "Adding Genes Between Betas:\n {0} -> {2} -> {1}".format(
                    col, row, newgene.name)

                path.beta[zSlice] = 0
                path.beta[tSlice] = 1
                path.beta[sSlice] = 1
                createOutput(path, model_type, args)
                path.beta[zSlice] = 1
                path.beta[tSlice] = 0
                path.beta[sSlice] = 0
                path.count_increment()

            rIndex += rowCnt
        cIndex += colCnt
Example #4
0
def add_genes_bt_gamma_beta(path, newgene, args):
    """ Iterate through gamma matrix and place new genes between gamma and ds beta. """

    # Initialize default values again
    path.reinit()

    # Expand gamma and beta to account for new isoforms
    for iso in range(0, newgene.count):
        path.expand_beta()
        path.expand_gamma()

    # Make copy of oiriginal for iterating
    _yvar = list(path.yvar)

    # Add newGene to yvar because they are new endogenous genes
    path.yvar.append(newgene.name)

    # Iterate through each row of GAMMA matrix and place new gene in between
    # gamma and beta
    cIndex = 0
    for col in path.xvar:
        # How many isoforms does the current column have
        colCnt = utils.isoCount(col)

        rIndex = 0
        for row in _yvar:
            # How many isoforms does the current row have
            rowCnt = utils.isoCount(row)

            # Zero slice, is the current location in Gamma, if there are 1s
            # there I will turn them 0
            zSlice = np.s_[rIndex:rIndex + rowCnt, cIndex:cIndex + colCnt]

            # Target slice, is the bottom 'newgene.count' rows in GAMMA, that
            # will get turned 1 showing that current 'col' is acting on the
            # newgene
            tSlice = np.s_[-newgene.count:, cIndex:cIndex + colCnt]

            # Source slice, is the right most 'newgene.count' cols in BETA,
            # that will get turned 1 showing that newgene is acting on the
            # current row
            sSlice = np.s_[rIndex:rIndex + rowCnt, -newgene.count:]

            # If there was a previous interaction (i.e. 1) then make models
            if np.all(path.gamma[zSlice]):
                model_type = "Adding Genes Between Gamma and Beta:\n {0} -> {2} -> {1}".format(
                    col, row, newgene.name)

                path.gamma[zSlice] = 0
                path.gamma[tSlice] = 1
                path.beta[sSlice] = 1
                createOutput(path, model_type, args)
                path.gamma[zSlice] = 1
                path.gamma[tSlice] = 0
                path.beta[sSlice] = 0
                path.count_increment()
            rIndex += rowCnt
        cIndex += colCnt
Example #5
0
def add_genes_above_beta(path, newgene, args):
    """ Iterate through gamma matrix and place new genes upstream of betas. """

    # Initialize default values
    path.reinit()

    # Expand matrices GAMMA and PHI matrices
    for iso in range(0, newgene.count):
        path.expand_gamma(axis=1)  # Add column not row
        path.expand_phi()

    # Append new gene to yvar
    path.xvar.append(newgene.name)

    index = 0
    for gene in path.yvar:
        # How many isoforms does the current gene have
        isoCnt = utils.isoCount(gene)

        # Create slice index of right most 'newgene.count' columns of gamma and
        # alterating rows of gamma
        gSlice = np.s_[index:index + isoCnt, -newgene.count:]

        model_type = "Adding Genes Upstream of beta:\n {1} -> {0}".format(
            gene, newgene.name)

        path.gamma[gSlice] = 1
        createOutput(path, model_type, args)
        path.gamma[gSlice] = 0
        path.count_increment()
        index += isoCnt
Example #6
0
def add_genes_ds_gamma(path, newgene, args):
    """ Iterate through gamma matrix and place new genes downstream of each gamma. """

    # Initialize default values
    path.reinit()

    # Expand gamma and beta to account for new isoforms
    for iso in range(0, newgene.count):
        path.expand_beta()
        path.expand_gamma()

    # Append new gene to yvar
    path.yvar.append(newgene.name)

    # Iterate through GAMMA matrix and output all possible models
    index = 0
    for gene in path.xvar:
        # How many isoforms does the current gene have
        isoCnt = utils.isoCount(gene)

        # Create slice index of bottom 'newgene.count' rows of gamma and
        # alternating columns of gamma
        gSlice = np.s_[-newgene.count:, index:index + isoCnt]

        model_type = "Adding Genes Downstream of Gamma:\n {0} -> {1}".format(
            gene, newgene.name)

        path.gamma[gSlice] = 1
        createOutput(path, model_type, args)
        path.gamma[gSlice] = 0
        path.count_increment()
        index += isoCnt
Example #7
0
def buildGamma(yvar, xvar, paths):
    """ Gamma has 'yvar' rows and 'xvar' columns.  Gamma is a 0,1 matrix of the
    relationships between exogenous (xvar) to endogenous (yvar) genes.
    """

    # flatten yvar and xvar to remove isoform grouping
    fyvar = utils.flatten_list(yvar, 1)
    fxvar = utils.flatten_list(xvar, 1)

    # Initialize Gamma as all 0's
    nyvar = len(fyvar)
    nxvar = len(fxvar)
    Gamma = np.zeros((nyvar, nxvar))

    # Iterate through xvar and see if there are any relationships
    for colIndex, value in enumerate(xvar):

        # If the current gene has a downstream target continue
        if paths[value]:
            # get isoform count
            isoCnt = utils.isoCount(value)

            # get flattened list of downstream targets
            targets = utils.flatten_list(paths[value],1)

            # Add ones to correct spot in Gamma
            for target in targets:
                rowIndex = fyvar.index(target)
                Gamma[rowIndex, colIndex:colIndex+isoCnt] = 1
    return Gamma
Example #8
0
def buildBeta(yvar, paths):
    """ Beta is a square matrix where the rows and columns are equal to the
    endogenous genes. In other words, Beta is a 0,1 matrix of the relationships
    between endogenous (yvar) genes.
    """
    # flatten the yvar list to remove isoform grouping
    fyvar = utils.flatten_list(yvar, 1)

    # Initialize Beta as all 0's
    nyvar = len(fyvar)
    Beta = np.zeros((nyvar, nyvar))

    # Iterate through yvar and see if there are any relationships
    for colIndex, value in enumerate(yvar):
        # If the current gene has a downstream target continue
        if paths[value]:
            # get isoform count
            isoCnt = utils.isoCount(value)

            # get flattened list of downstream targets
            targets = utils.flatten_list(paths[value],1)

            # Add ones to correct spot in Beta
            for target in targets:
                rowIndex = fyvar.index(target)
                Beta[rowIndex, colIndex:colIndex+isoCnt] = 1
    return Beta
Example #9
0
    def __init__(self, newGene):

        try:
            # Determine if the gene we are adding to the model has multiple isoforms
            # and create a counter
            self.name = newGene
            self.count = utils.isoCount(self.name)

        except ValueError:
            logger.error(
                "If you are adding genes you need to create a new gene object."
            )
Example #10
0
def add_newlinks_gamma_to_beta(path, args):
    """ function to add new links to above all of the betas """
    # Initialize default values
    path.reinit()

    # Iterate overall of the exogenous variables
    for index, gene in enumerate(path.xvar):
        # Determine if gene has multiple isoforms
        isoCnt = utils.isoCount(gene)

        # Iterate over the rows of gamma and add link one at a time. In other words,
        # add new links to each of the endogenous variables.
        for row, target in enumerate(path.yvar):
            # Determine if gene has multiple isoforms
            tCnt = utils.isoCount(target)

            if path.gamma[row, index] == 0:
                model_type = "Adding Newlinks Gamma to Beta:\n {0} -> {1}".format(gene, target)

                path.gamma[row:row+tCnt, index:index+isoCnt] = 1
                createOutput(path, model_type, args)
                path.gamma[row:row+tCnt, index:index+isoCnt] = 0
                path.count_increment()
Example #11
0
    def convert_ExogToEndog(self, gene):
        """ Convert a gene/isoform from being exogenous to endogenous """

        # Get gene location from xvar
        xInd = self.xvar.index(gene)

        # Remove the gene/isoform from the xvar list and add it to the yvar list
        curr = self.xvar.pop(xInd)
        self.yvar.append(curr)

        # How many isoforms does this gene have
        isoCnt = utils.isoCount(gene)

        # Create a range of indices so that we can remove this gene from gamma
        # and phi
        matRange = range(xInd, xInd + isoCnt)

        # Pull a list of causative effects from gamma, we need to transfer
        # these to the beta matrix.
        # NOTE: I am assuming that isoforms have same effects
        effects = self.gamma[:, xInd]

        # Delete the corresponding col from gamma and row and col from phi
        self.del_col_gamma(matRange)
        self.del_row_col_phi(matRange)

        # Expand gamma and beta to account for new isoforms
        for cnt in matRange:
            self.expand_gamma()
            self.expand_beta()

        # Identify where the effects from gamma are equal to 1 and set those in
        # beta
        causeInd = np.where(np.array(effects) == 1)[0]
        for ind2 in causeInd:
            self.beta[ind2, -isoCnt:] = 1

        # Set row/col counts
        self.rc_count()
Example #12
0
def add_genes_above_gamma(path, newgene, args):
    """ Iterate through gamma matrix and place new genes upstream of gammas. """

    # Initialize default values
    path.reinit()

    # Make copy of oiriginal for iterating
    _xvar = list(path.xvar)

    for gene in _xvar:
        # How many isoforms does the current gene have
        isoCnt = utils.isoCount(gene)

        # Convert the current exogenous gene to an enogenous gene
        path.convert_ExogToEndog(gene)

        # Append new gene to xvar
        path.xvar.append(newgene.name)

        # Expand gamma and phi matrices for my new gene
        for iso in range(0, newgene.count):
            path.expand_gamma(axis=1)  # Add column not row
            path.expand_phi()

        # Create slice index of right most 'newgene.count' columns of gamma and
        # the bottom 'isoCnt' rows of gamma.
        gSlice = np.s_[-isoCnt:, -newgene.count:]

        model_type = "Adding Genes Upstream of Gamma:\n {1} -> {0}".format(
            gene, newgene.name)

        path.gamma[gSlice] = 1
        createOutput(path, model_type, args)
        path.count_increment()

        # Reset I need to move different exogenous genes
        path.reinit()