Example #1
0
 def test5_motif_to_img(self):
     """ Motif to img """
     seqlogo = which("seqlogo")
     if seqlogo:
         m = Motif(self.pfm)
         m.to_img("test/test.png", fmt="png", seqlogo=seqlogo)
         self.assertTrue(os.path.exists("test/test.png"))
         os.unlink("test/test.png")
     else:
         print("seqlogo not found, skipping.")
Example #2
0
class OneMotif:

    bases = ["A", "C", "G", "T"]

    def __init__(self, motifid=None, name=None, counts=None):

        self.id = motifid if motifid != None else ""  #should be unique
        self.name = name if name != None else ""  #does not have to be unique

        self.prefix = ""  #output prefix set in set_prefix
        self.counts = counts if counts != None else [
            [] for _ in range(4)
        ]  #counts, list of 4 lists (A,C,G,T) (each as long as motif)
        self.strand = "+"  #default strand is +
        self.length = 0  #length of motif

        #Set later
        self.pfm = None
        self.bg = np.array([0.25, 0.25, 0.25,
                            0.25])  #background set to equal by default
        self.pssm = None  #pssm calculated from get_pssm
        self.threshold = None  #threshold calculated from get_threshold
        self.gimme_obj = None  #gimmemotif obj

    def __str__(self):
        """ Used for printing """
        return ("{0}".format(self.__dict__))

    def set_prefix(self, naming="name_id"):
        """ Set name to be used in 4th column and as output prefix """

        if naming == "name":
            prefix = self.name
        elif naming == "id":
            prefix = self.id
        elif naming == "name_id":
            prefix = self.name + "_" + self.id
        elif naming == "id_name":
            prefix = self.id + "_" + self.name
        else:
            prefix = "None"

        self.prefix = filafy(prefix)
        return (self)

    def get_pfm(self):
        self.pfm = self.counts / np.sum(self.counts, axis=0)

    def get_gimmemotif(self):
        """ Get gimmemotif object for motif 
			Reads counts from self.counts """

        self.length = len(self.counts[0])

        motif_rows = []
        for pos_id in range(self.length):
            row = [self.counts[letter][pos_id] for letter in range(4)
                   ]  # each row represents one position in motif ( A C G T )
            motif_rows.append(row)

        self.gimme_obj = Motif(
            motif_rows)  # generate gimmemotif motif instance
        self.gimme_obj.id = self.id + " " + self.name

        return (self)

    def get_biomotif(self):
        """ Get biomotif object for motif """

        self.biomotif_obj = ""

    def get_reverse(self):
        """ Reverse complement motif """
        if self.pfm is None:
            self.get_pfm()

        #Create reverse motif obj
        reverse_motif = OneMotif()  #empty
        for att in ["id", "name", "prefix", "strand", "length"]:
            setattr(reverse_motif, att, getattr(self, att))

        reverse_motif.strand = "-" if self.strand == "+" else "+"
        reverse_motif.pfm = MOODS.tools.reverse_complement(self.pfm, 4)
        return (reverse_motif)  #OneMotif object

    def get_pssm(self, ps=0.01):
        """ Calculate pssm from pfm """

        if self.pfm is None:
            self.get_pfm()

        bg_col = self.bg.reshape((-1, 1))
        pseudo_vector = ps * bg_col

        pssm = np.log(
            np.true_divide(self.pfm + pseudo_vector,
                           np.sum(self.pfm + pseudo_vector,
                                  axis=0))) - np.log(bg_col)
        pssm = tuple([tuple(row) for row in pssm])
        self.pssm = pssm

    def get_threshold(self, pvalue):
        """ Get threshold for moods scanning """
        if self.pssm is None:
            self.get_pssmm()

        self.threshold = MOODS.tools.threshold_from_p(self.pssm, self.bg,
                                                      pvalue, 4)
        return (self)

    def calc_bit_score(self):
        """ Bits for logo plots (?) """
        if self.pfm is None:
            self.get_pfm()

        pfm_arr = np.copy(self.pfm)
        pfm_arr[pfm_arr == 0] = np.nan

        #Info content per pos
        entro = pfm_arr * np.log2(pfm_arr)
        entro[np.isnan(entro)] = 0
        info_content = 2 - (-np.sum(entro, axis=0)
                            )  #information content per position in motif
        self.bits = self.pfm * info_content

    def logo_to_file(self, filename):
        """ Plots the motif to pdf/png/jpg file """

        ext = os.path.splitext(filename)[-1]

        #Currently only working with pdf
        filename = filename.replace(ext, ".pdf")  #hack

        if ext == "jpg":
            filename[-3:] = "png"
            warnings.warn(
                "The 'jpg' format is not supported for motif image. Type is set tp 'png'"
            )

        self.gimme_obj.to_img(filename)

    def logo_to_ax(self):
        ax = None
        #René TODO
        pass
        return (ax)