Esempio n. 1
0
    def residst(self):
        self.xyzedges("surface")
        ztop = self.zedgep  #Ugly!

        resd = {}
        try:
            atoms = self.cmd.get_model("protein")
        except:
            logger.error("Could not find protein model.")
            return 0

        for atoma in atoms.atom:
            try:
                reid = int(atoma.resi)
                z = float(atoma.coord[2])
            except ValueError:
                reid = -1
                z = 0.0
            except IndexError:
                z = 0.0
                logger.error("No Z coord value")
            if reid != -1 and reid not in resd or resd[reid] > z - ztop:
                resd[reid] = z - ztop

        resl = []
        for i in range(len(resd)):
            j = i + 1
            if j in resd:
                resl.append(resd[j])

        return np.array(resl)
Esempio n. 2
0
    def loaddb(self, name=""):
        try:
            with open(name, "r") as openfile:
                self.jsdb = json.load(openfile)
        except (IOError, TypeError):
            logger.error("Can't open the file: {}".format(name))
            return 1

        return 0
Esempio n. 3
0
    def loadenergy(self, gbestf):
        """
        gbestf if the gbest.txt file path.
        need to convert to gbest_energy.txt
        return the gbest final energy.
        """
        gbeste = re.sub("gbest.txt", "gbest_energy.txt", gbestf)
        try:
            v = np.loadtxt(gbeste, delimiter=",")
            laste = v[-1][-1]
        except:
            logger.error("Can NOT read the energy values of {}".format(gbeste))
            laste = 0

        return int(laste)
Esempio n. 4
0
    def load_file(self, filenm):
        self.filenm = ""
        if not os.path.isfile(filenm):
            logger.error("No such ({}) pdb file!".format(filenm))
            return 0

        self.cmd.delete("*")  #Clean everything first
        try:
            self.cmd.load(filenm, "system")
            self.filenm = filenm
            self.cmd.select("surface", "resn PTS or resn SUB")
            self.cmd.select("protein",
                            "not resn SOL and not resn PTS and not resn SUB")
        except:
            logger.error("Loading file ERROR!!!")
            return 0

        return 1
Esempio n. 5
0
    def xyzedges(self, s="surface"):
        """measure the X/Y/Z edges and length."""
        #PyMol already implemented get_extent function to get the X Y Z min/max values. see: http://www.pymolwiki.org/index.php/Get_extent
        try:
            atoms = self.cmd.get_model(s)
        except:
            logger.error("Can not find model({})".format(s))
            return 0

        self.xedgep = -999999999
        self.xedgen = +999999999
        self.yedgep = -999999999
        self.yedgen = +999999999
        self.zedgep = -999999999
        self.zedgen = +999999999

        for atom in atoms.atom:
            try:
                x = float(atom.coord[0])
                y = float(atom.coord[1])
                z = float(atom.coord[2])
            except ValueError:
                x = y = z = 0
            if x > self.xedgep:
                self.xedgep = x
            if x < self.xedgen:
                self.xedgen = x
            if y > self.yedgep:
                self.yedgep = y
            if y < self.yedgen:
                self.yedgen = y
            if z > self.zedgep:
                self.zedgep = z
            if z < self.zedgen:
                self.zedgen = z

        self.centerp = [
            (j - i) / 2 + i
            for i, j in [(self.xedgen, self.xedgep), (
                self.yedgen, self.yedgep), (self.zedgen, self.zedgep)]
        ]
Esempio n. 6
0
    def loadadsite(self, gbestf, cutoff=5.0):
        """
        gbestf is gbest.txt file path
        read it and filter out all residues within cut-off
        """
        gbests = re.sub("gbest.txt", "gbest_sorted.txt", gbestf)
        try:
            L = np.loadtxt(gbests, delimiter=",")
            logger.debug("Loaded gbest: {}".format(gbests))
        except:
            logger.error("Can NOT read the numbers in {}".format(gbestf))
            L = []

        R = []
        if len(L) > 0 and len(L[0]) > 1:
            #The residues height was stored in second column
            min_h = L[:,1].min() * 1.0
            n_cutoff = min_h + cutoff
            #filter out with-in cut-off
            R = L[L[:,1] < n_cutoff][:,0]
            logger.debug("filter cutoff={}: {}".format(n_cutoff, str(R)))

        return R
Esempio n. 7
0
    def avgenergy(self, paras):
        """
        paras: list of gbest.txt file path

        load the list of gbest final energy values (in np array)
        the average value can be computed through numpy methods
        """
        psoeng = [ re.sub("gbest.txt", "gbest_energy.txt", x) for x in paras ]
        engval = []
        for f in psoeng:
            try:
                v = np.loadtxt(f, delimiter=',')
            except:
                logger.error("Can NOT open the numbers in textfile: {}".format(f))
                v = []

            if len(v) > 0:
                engval.append(v[-1])
            else:
                logger.error("Can NOT open the numbers in textfile: {}".format(f))

        #engval = np.array(engval)
        return np.array(engval)
Esempio n. 8
0
    def energyP(self, target=-1):
        """
        Just plot the gbest's energy values and location as function of iterations

        Its design can be imporved, it mixed up tow tasks in this function.
        Locate the target bird's values and find the gbest bird.
        I think these should be two separated tasks.
        The current version is try to save time, find the gbest bird with its values on-the-fly
        """
        try:
            target = int(target)
        except ValueError:
            logger.error("Please gives the target ID, should be integer!")
            return 1

        if self.loaddb(self.args.jdb) != 0:
            logger.error("Please check the db file existed or not!")
            return 1

        try:
            birds = self.jsdb["birds"]
        except KeyError:
            logger.error("jsdb does not have birds!")
            return 1

        if not isinstance(birds, list):
            logger.error("there are no birds in it!")
            return 1

        lasti = 0
        I = []
        E = []
        X = []
        Y = []
        Z = []
        A = []
        B = []
        C = []
        P = []
        EE = []
        maxe = -999999999
        mine = +999999999
        Pnp = np.array([])

        laste = 0
        lastxr = lastyr = lastzr = 0
        lastxt = lastyt = lastzt = 0

        for bird in birds:
            try:
                n = int(bird["bird"])
                i = int(bird["iteration"])
                e = float(bird["energy"]) * -1
                g = bool(bird["gbest"])
                v = bird["velocity"]
                p = bird["position"]
                xr = float(p[0])
                yr = float(p[1])
                zr = float(p[2])
                xt = float(p[3])
                yt = float(p[4])
                zt = float(p[5])
            except (TypeError, IndexError, ValueError, KeyError):
                logger.error("There are alien in the birds!")
                return 1

            if i == 0:
                P.append(e)
            if i == 1:
                Pnp = np.array(P)
            if P[n] > e:
                P[n] = e
            if mine > e:
                mine = e
            if maxe < e:
                maxe = e
            if (target == -1 and g) or (target != -1 and target == n):
                laste = e  #will case ERROR if this statement never runs
                lastxr = xr
                lastyr = yr
                lastzr = zr
                lastxt = xt
                lastyt = yt
                lastzt = zt
            if (target != -1 and target == n) or (target == -1 and i > lasti):
                if target == -1:  #fixed incorrect iteration number for non-gbest target
                    ino = lasti  #for gbest, the data is added after the iteration
                else:
                    ino = i  #for non-gbest, the data is added on-the-fly
                I.append(ino)
                E.append(laste)
                A.append(lastxr)
                B.append(lastyr)
                C.append(lastzr)
                X.append(lastxt)
                Y.append(lastyt)
                Z.append(lastzt)
                lasti = i
                maxe = -999999999
                mine = +999999999
                if i == 0:  #TODO: can get EE for non-gbest target
                    EE.append(0)  #because the data is added on-the-fly
                elif len(Pnp) > 0:
                    #it's OK if added after the iteration
                    EE.append(Pnp.std())

        #gbest must be added after the iteration
        #because there are more than one gbest records in one iteration
        if target == -1:
            I.append(lasti)
            E.append(laste)
            A.append(lastxr)
            B.append(lastyr)
            C.append(lastzr)
            X.append(lastxt)
            Y.append(lastyt)
            Z.append(lastzt)
            lasti = i
            maxe = -999999999
            mine = +999999999
            if len(Pnp) > 0:
                EE.append(Pnp.std())

        I = np.array(I)
        E = np.array(E)
        X = np.array(X)
        Y = np.array(Y)
        Z = np.array(Z)
        A = np.array(A)
        B = np.array(B)
        C = np.array(C)
        EE = np.array(EE)

        #save all the numbers into txt
        EN = np.arange(1, len(E) + 1)
        ES = np.column_stack((EN, E))
        np.savetxt("gbest_energy.txt",
                   ES,
                   fmt="%03d, %2.9e",
                   header="iteration, ProtPOS score (kJ/mol)")
        logger.info(
            "Gbest energy evolution:                       gbest_energy.txt")
        EV = np.column_stack((EN, A, B, C, X, Y, Z))
        np.savetxt("gbest_vector.txt", EV, fmt="%03d, %2.9e, %2.9e, %2.9e, %2.9e, %2.9e, %2.9e", \
            header="iteration, (orientation:) rotation-X, rotation-Y, rotation-Z, translation-X, translation-Y, translation-Z")
        logger.info(
            "Gbest orientation evolution:                  gbest_vector.txt")
Esempio n. 9
0
    def plot(self):
        '''output the db.json to csv'''
        if self.args.no_plot:
            logger.info("skipped the plot progress...")
            return 0

        try:
            a = self.jsdb["confs"]
            steps = self.jsdb["steps"]
            mindst = self.jsdb["mindst"]
        except KeyError:
            logger.error(
                "Can not find the confs/steps from the json db file. It might be corrupted."
            )
            return 1

        count = 0

        x = []
        y = []
        z = []
        xt = []
        yt = []
        zt = []
        ang = 360.0 / steps
        lx = []
        ly = []
        ld = []
        minres = "UNKNOW"
        mineng = 7777777
        mincnt = 0
        maxeng = -7777777
        maxres = "UNKNOW"
        minxra = 0
        minyra = 0

        logger.debug("preparing X Y Z")
        for i in a:
            try:
                enegy = float(i["coul"]) + float(i["ljsr"])
                resnm = i["id"]
                xi = int(i["xi"])
                yi = int(i["yi"])
            except (ValueError, KeyError):
                enegy = 7777777
                resnm = "UNKNOW"
                xi = 0
                yi = 0
            if enegy < mineng:
                mineng = enegy
                minres = resnm
                mincnt = count
                minxra = xi
                minyra = yi
                logger.debug("Found a lower energy residue: {}@{}".format(
                    minres, mineng))
            if enegy > maxeng:
                maxeng = enegy
                logger.debug("Found a higher energy residue: {}@{}".format(
                    maxres, maxeng))

            xti = float(xi * ang - self.args.init_xr)
            yti = float(yi * ang - self.args.init_yr)
            xt.append(xti)
            yt.append(yti)
            zt.append(enegy)
            lx.append(count)
            ly.append(enegy)
            ld.append(resnm)
            count += 1
            if (count % steps) == 0:
                x.append(xt)
                xt = []
                y.append(yt)
                yt = []
                z.append(zt)
                zt = []
        x = np.array(x)
        y = np.array(y)
        z = np.array(z)
        logger.debug("get X, Y and Z, then plot the graph.")

        figa = plt.figure()
        cmap = plt.cm.get_cmap("RdBu_r")
        if self.args.minlv != None and self.args.maxlv != None:
            a = self.args.minlv
            b = self.args.maxlv
            i = (
                b - a
            ) / self.ctlvs  #ASSUMED: the maxlv is always bigger than minlv
            levels = range(a, b, i)
            logger.debug("The contour map levels: {}".format(levels))
        else:
            levels = None
            logger.debug("Use the default contour map levels.")
        plt.plot(112, -65, 'ko')  #the PSO ans
        #plot the minimum location
        plt.plot(float(minxra * ang - self.args.init_xr),
                 float(minyra * ang - self.args.init_yr), 'kx')
        Da = plt.contourf(x,
                          y,
                          z,
                          cmap=cmap,
                          levels=levels,
                          norm=mpl.colors.SymLogNorm(011))
        plt.title(u"contour diagram\ndistance={}Å".format(mindst))
        plt.xlabel("X rotation angle")
        plt.ylabel("Y rotation angle")
        cbar = plt.colorbar(Da)
        cbar.ax.set_ylabel("energy level")
        plt.savefig("diagram_0a.pdf")
        logger.debug("plot contour diagram and save as pdf file.")

        figb = plt.figure()
        plt.title(u"energy line\ndistance={}Å".format(mindst))
        plt.xlabel("Iteration Number")
        plt.ylabel("Energy Value")
        Db = plt.plot(lx, ly, 'k')
        plt.plot(mincnt, mineng, 'bo')
        plt.plot([mincnt, mincnt * 1.1], [mineng, mineng], 'k')
        plt.text(mincnt * 1.13,
                 mineng,
                 "id: {}\nen: {}".format(minres, mineng),
                 verticalalignment="center",
                 horizontalalignment="left")
        logger.debug("plot energy line only.")
        if self.args.minlv != None:
            plt.ylim(ymin=self.args.minlv)
            logger.debug("set the y-axis minimum range.")
        if self.args.maxlv != None:
            plt.ylim(ymax=self.args.maxlv)
            logger.debug("set the y-axis maximum range.")
        plt.savefig("diagram_0k.pdf")
        ly = np.array(ly)
        ld = np.array(ld)
        L = np.column_stack((ly, ld))
        np.savetxt("energy.txt.gz", L, delimiter=" ", fmt="%11s %11s")
        logger.debug("plot energy line diagram and save as pdf file.")

        fige = plt.figure()
        plt.title(u"normalized energy line\ndistance={}Å".format(mindst))
        plt.ylabel("Energy Value")
        plt.xlabel("residues")
        plt.axis("off")
        plt.grid("on")
        plt.xticks([])
        plt.yticks([])
        sly = np.sort(ly)
        sry = sly[::-1]
        nly = (sry - mineng) / (maxeng - mineng)
        De = plt.plot(range(len(nly)), nly)
        plt.text(0, 0, "id: {}@{}".format(minres, mineng))
        logger.debug("plot energy histogram.")
        plt.savefig("diagram_0e.pdf")
        logger.debug("plot energy histogram diagram and saved as pdf file.")

        figh = plt.figure()
        plt.title(u"normalized energy line\ndistance={}Å".format(mindst))
        Dh = plt.hist(ly, 100)
        plt.xlabel("The lowest configuration is id: {}@eng: {}".format(
            minres, mineng))
        plt.savefig("diagram_0h.pdf")
        logger.debug("plot another histogram diagram and saved as pdf file.")

        if self.args.no_plot_re:
            logger.info("skipped the residues plot progress...")
            return 0
        logger.debug(
            "plot residues configuration graph, go through all configurations..."
        )
        self.cmd.load("system00003.pse")
        for con in a:
            cid = con["id"]
            try:
                nid = re.findall('\d+', cid)[0]
                nid = int(nid)
            except:
                logger.warning("function plot - can not find digit from cid.")
                nid = 0
            logger.debug("find the digit: {} from cid.".format(nid))
            cen = float(con["coul"]) + float(con["ljsr"])
            logger.debug("processing conf: {}, the energy value is: {}".format(
                cid, cen))
            logger.debug("Now create the system state for processing...")
            self.cmd.create("system", "mov", nid + 1, 1)
            atoms = self.cmd.get_model("system")
            resds = atoms.get_residues()
            xr = []  # x axis: residues id
            yr = []  # y axis: mini distance
            mindt = [999999.99] * len(resds)
            surfc = self.surfaceh()
            szv = surfc["stop"]
            for atom in atoms.atom:
                rid = int(atom.resi) - 1
                azv = atom.coord[2]
                dst = azv - szv
                if dst < mindt[rid]:
                    mindt[rid] = dst

            #save the graph under jsdb["confd"] directory
            figs = plt.figure()
            Ds = plt.plot(range(1, len(mindt)+1), mindt, 'r+', \
                    range(1, len(mindt)+1), mindt, 'k')
            plt.title(
                u"residues configuraions diagram\ndistance={}Å; energy={}kj".
                format(self.jsdb["mindst"], cen))
            plt.savefig(os.path.join(self.jsdb["confd"], cid + ".pdf"))
            logger.debug("plot a residues diagram and saved as pdf.")