Пример #1
0
        public static Pdb GetPdb(string pdbid, bool forceToRedownload = false)
        {
            List <string> pdblines = GetPdbLines(pdbid, forceToRedownload);
            Pdb           pdb      = Pdb.FromLines(pdblines);

            return(pdb);
        }
Пример #2
0
            public static CNamd2 Namd2
                (IList <string> pdb_lines
                , IList <string> psf_lines
                , IList <string> prm_lines
                , string tempbase           // = null
                , string namd2version       // = "2.8"
                , string option             // = "+p3"
                , IList <string> infiles    = null
                , IList <string> outfiles   = null
                , IList <string> conf_lines = null
                )
            {
                string[] lines = null;
                if ((conf_lines != null) && (conf_lines.Count > 0))
                {
                    lines = conf_lines.ToArray();
                }
                else
                {
                    // http://www.msg.ucsf.edu/local/programs/Vega/pages/tu_namdmin.htm
                    lines = new string[]
                    { "numsteps                10000              " // minimization steps
                      //{ "numsteps                1000               " // minimization steps
                      , "minimization            on                 "
                      , "dielectric              1.0                "
                      , "coordinates             prot.pdb           " // coordinate file
                      , "outputname              output             " // output file: prot.coor
                      , "outputEnergies          1000               "
                      , "binaryoutput            no                 "
                      , "DCDFreq                 1000               "
                      , "restartFreq             1000               "
                      , "structure               prot.psf           " // psf file
                      , "paraTypeCharmm          on                 "
                      , "parameters              prot.prm           " // parameter file
                      //, "parameters              par_all22_vega.inp "
                      , "exclude                 scaled1-4          "
                      , "1-4scaling              1.0                "
                      , "switching               on                 "
                      , "switchdist              8.0                "
                      , "cutoff                  22.0               " //, "cutoff                  12.0               "
                      , "pairlistdist            23.5               " //, "pairlistdist            13.5               "
                      , "margin                  0.0                "
                      , "stepspercycle           20                 "
                      , "fixedAtoms              on                 " // fix atoms
                      , "fixedAtomsCol           O                  " // select fixing atoms from O
                    };
                }

                var    tmpdir   = HDirectory.CreateTempDirectory(tempbase);
                string currpath = HEnvironment.CurrentDirectory;

                HEnvironment.CurrentDirectory = tmpdir.FullName;
                string[] coor_lines = null;
                double   coor_rmsd  = double.NaN;

                {
                    {
                        //foreach(var respath_filename in GetResourcePaths("2.8", "psfgen"))
                        foreach (var respath_filename in GetResourcePaths(namd2version, "namd2"))
                        {
                            string respath  = respath_filename.Item1;
                            string filename = respath_filename.Item2;
                            HResource.CopyResourceTo <Tinker>(respath, filename);
                        }
                    }

                    Vector[] coords0 = Pdb.FromLines(pdb_lines).atoms.ListCoord().ToArray();
                    System.IO.File.WriteAllLines("prot.pdb", pdb_lines);
                    System.IO.File.WriteAllLines("prot.psf", psf_lines);
                    System.IO.File.WriteAllLines("prot.prm", prm_lines);
                    System.IO.File.WriteAllLines("prot.conf", lines);

                    if (option == null)
                    {
                        option = "";
                    }
                    string command = string.Format("namd2 {0} prot.conf", option);
                    HProcess.StartAsBatchInConsole(null, false, command);

                    if (HFile.Exists("output.coor"))
                    {
                        coor_lines = HFile.ReadAllLines("output.coor");
                        Vector[] coords1  = Pdb.FromLines(coor_lines).atoms.ListCoord().ToArray();
                        Vector[] coords1x = Align.MinRMSD.Align(coords0, coords1);
                        coor_rmsd = Align.MinRMSD.GetRMSD(coords0, coords1x);
                    }
                }
                HEnvironment.CurrentDirectory = currpath;
                try{ tmpdir.Delete(true); } catch {}

                if (coor_lines == null)
                {
                    return(null);
                }

                return(new CNamd2
                {
                    coor_lines = coor_lines,
                    coor_rmsd = coor_rmsd,
                });
            }
Пример #3
0
            public static Vector[] GetRotTran(Vector[] coords, double[] masses)
            {
                #region source rtbProjection.m
                /// rtbProjection.m
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                /// function [P, xyz] = rtbProjection(xyz, mass)
                /// % the approach is to find the inertia. compute the principal axes. and then use them to determine directly translation or rotation.
                ///
                /// n = size(xyz, 1); % n: the number of atoms
                /// if nargin == 1
                ///     mass = ones(n,1);
                /// end
                ///
                /// M = sum(mass);
                /// % find the mass center.
                /// m3 = repmat(mass, 1, 3);
                /// center = sum (xyz.*m3)/M;
                /// xyz = xyz - center(ones(n, 1), :);
                ///
                /// mwX = sqrt (m3).*xyz;
                /// inertia = sum(sum(mwX.^2))*eye(3) - mwX'*mwX;
                /// [V,D] = eig(inertia);
                /// tV = V'; % tV: transpose of V. Columns of V are principal axes.
                /// for i=1:3
                ///     trans{i} = tV(ones(n,1)*i, :); % the 3 translations are along principal axes
                /// end
                /// P = zeros(n*3, 6);
                /// for i=1:3
                ///     rotate{i} = cross(trans{i}, xyz);
                ///     temp = mat2vec(trans{i});
                ///     P(:,i) = temp/norm(temp);
                ///     temp = mat2vec(rotate{i});
                ///     P(:,i+3) = temp/norm(temp);
                /// end
                /// m3 = mat2vec(sqrt(m3));
                /// P = repmat (m3(:),1,size(P,2)).*P;
                /// % now normalize columns of P
                /// P = P*diag(1./normMat(P,1));
                ///
                /// function vec = mat2vec(mat)
                /// % convert a matrix to a vector, extracting data *row-wise*.
                /// vec = reshape(mat',1,prod(size(mat)));
                //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                #endregion

                if (HDebug.Selftest())
                #region selftest
                {
                    // get test coords and masses
                    Vector[] tcoords = Pdb.FromLines(SelftestData.lines_1EVC_pdb).atoms.ListCoord().ToArray();
                    double[] tmasses = new double[tcoords.Length];
                    for (int i = 0; i < tmasses.Length; i++)
                    {
                        tmasses[i] = 1;
                    }
                    // get test rot/trans RTB vectors
                    Vector[] trottra = GetRotTran(tcoords, tmasses);
                    HDebug.Assert(trottra.Length == 6);
                    // get test ANM
                    var tanm = Hess.GetHessAnm(tcoords);
                    // size of vec_i == 1
                    for (int i = 0; i < trottra.Length; i++)
                    {
                        double dist = trottra[i].Dist;
                        HDebug.Assert(Math.Abs(dist - 1) < 0.00000001);
                    }
                    // vec_i and vec_j must be orthogonal
                    for (int i = 0; i < trottra.Length; i++)
                    {
                        for (int j = i + 1; j < trottra.Length; j++)
                        {
                            double dot = LinAlg.VtV(trottra[i], trottra[j]);
                            HDebug.Assert(Math.Abs(dot) < 0.00000001);
                        }
                    }
                    // vec_i' * ANM * vec_i == 0
                    for (int i = 0; i < trottra.Length; i++)
                    {
                        double eigi = LinAlg.VtMV(trottra[i], tanm, trottra[i]);
                        HDebug.Assert(Math.Abs(eigi) < 0.00000001);
                        Vector tvecx = trottra[i].Clone();
                        tvecx[1] += (1.0 / tvecx.Size) * Math.Sign(tvecx[1]);
                        tvecx     = tvecx.UnitVector();
                        double eigix = LinAlg.VtMV(tvecx, tanm, tvecx);
                        HDebug.Assert(Math.Abs(eigix) > 0.00000001);
                    }
                }
                #endregion

                Vector[] rottran;
                using (new Matlab.NamedLock(""))
                {
                    Matlab.PutMatrix("xyz", coords.ToMatrix(), true);
                    Matlab.Execute("xyz = xyz';");
                    Matlab.PutVector("mass", masses);
                    //Matlab.Execute("function [P, xyz] = rtbProjection(xyz, mass)                                                                                        ");
                    //Matlab.Execute("% the approach is to find the inertia. compute the principal axes. and then use them to determine directly translation or rotation. ");
                    Matlab.Execute("                                                                                 ");
                    Matlab.Execute("n = size(xyz, 1); % n: the number of atoms                                       ");
                    //Matlab.Execute("if nargin == 1;                                                                  ");
                    //Matlab.Execute("    mass = ones(n,1);                                                            ");
                    //Matlab.Execute("end                                                                              ");
                    Matlab.Execute("                                                                                 ");
                    Matlab.Execute("M = sum(mass);                                                                   ");
                    Matlab.Execute("% find the mass center.                                                          ");
                    Matlab.Execute("m3 = repmat(mass, 1, 3);                                                         ");
                    Matlab.Execute("center = sum (xyz.*m3)/M;                                                        ");
                    Matlab.Execute("xyz = xyz - center(ones(n, 1), :);                                               ");
                    Matlab.Execute("                                                                                 ");
                    Matlab.Execute("mwX = sqrt (m3).*xyz;                                                            ");
                    Matlab.Execute("inertia = sum(sum(mwX.^2))*eye(3) - mwX'*mwX;                                    ");
                    Matlab.Execute("[V,D] = eig(inertia);                                                            ");
                    Matlab.Execute("tV = V'; % tV: transpose of V. Columns of V are principal axes.                  ");
                    Matlab.Execute("for i=1:3                                                                        \n"
                                   + "    trans{i} = tV(ones(n,1)*i, :); % the 3 translations are along principal axes \n"
                                   + "end                                                                              \n");
                    Matlab.Execute("P = zeros(n*3, 6);                                                               ");
                    Matlab.Execute("mat2vec = @(mat) reshape(mat',1,prod(size(mat)));                                ");
                    Matlab.Execute("for i=1:3                                                                        \n"
                                   + "    rotate{i} = cross(trans{i}, xyz);                                            \n"
                                   + "    temp = mat2vec(trans{i});                                                    \n"
                                   + "    P(:,i) = temp/norm(temp);                                                    \n"
                                   + "    temp = mat2vec(rotate{i});                                                   \n"
                                   + "    P(:,i+3) = temp/norm(temp);                                                  \n"
                                   + "end                                                                              ");
                    Matlab.Execute("m3 = mat2vec(sqrt(m3));                                                          ");
                    Matlab.Execute("P = repmat (m3(:),1,size(P,2)).*P;                                               ");
                    //Matlab.Execute("% now normalize columns of P                                                     "); // already normalized
                    //Matlab.Execute("normMat = @(x) sqrt(sum(x.^2,2));                                                "); // already normalized
                    //Matlab.Execute("P = P*diag(1./normMat(P,1));                                                     "); // already normalized
                    //Matlab.Execute("                                                                                 "); // already normalized
                    //////////////////////////////////////////////////////////////////////////////////////////////////////
                    //Matlab.Execute("function vec = mat2vec(mat)                                                      ");
                    //Matlab.Execute("% convert a matrix to a vector, extracting data *row-wise*.                      ");
                    //Matlab.Execute("vec = reshape(mat',1,prod(size(mat)));                                           ");
                    //////////////////////////////////////////////////////////////////////////////////////////////////////
                    //Matlab.Execute("function amp = normMat(x)                                                        ");
                    //Matlab.Execute("amp = sqrt(sum(x.^2,2));                                                         ");

                    Matrix xyz = Matlab.GetMatrix("xyz", true);
                    Matrix P   = Matlab.GetMatrix("P", true);
                    rottran = P.GetColVectorList();
                }
                return(rottran);
            }
Пример #4
0
        static PdbInfo GetPdbInfo(string pdbid, List <string> lines)
        {
            Pdb pdb = Pdb.FromLines(lines);

            return(GetPdbInfo(pdbid, pdb));
        }
Пример #5
0
        public static IEnumerable <Tuple <int, int, double> > EnumHessAnmSpr(IList <Vector> coords, double cutoff, double sprcst)
        {
            if (HDebug.Selftest())
            {
                Vector[] _coords = Pdb.FromLines(SelftestData.lines_1L2Y_pdb).atoms.SelectByName("CA").ListCoord().ToArray().HSelectCount(10);
                HashSet <Tuple <int, int, double> > sprs0 = //EnumHessAnmSpr_obsolete(_coords, 7, 1).HToHashSet();
                                                            new HashSet <Tuple <int, int, double> >
                {
                    new Tuple <int, int, double>(0, 1, 1), new Tuple <int, int, double>(1, 0, 1), new Tuple <int, int, double>(0, 2, 1), new Tuple <int, int, double>(2, 0, 1), new Tuple <int, int, double>(0, 3, 1),
                    new Tuple <int, int, double>(3, 0, 1), new Tuple <int, int, double>(0, 4, 1), new Tuple <int, int, double>(4, 0, 1), new Tuple <int, int, double>(1, 2, 1), new Tuple <int, int, double>(2, 1, 1),
                    new Tuple <int, int, double>(1, 3, 1), new Tuple <int, int, double>(3, 1, 1), new Tuple <int, int, double>(1, 4, 1), new Tuple <int, int, double>(4, 1, 1), new Tuple <int, int, double>(1, 5, 1),
                    new Tuple <int, int, double>(5, 1, 1), new Tuple <int, int, double>(2, 3, 1), new Tuple <int, int, double>(3, 2, 1), new Tuple <int, int, double>(2, 4, 1), new Tuple <int, int, double>(4, 2, 1),
                    new Tuple <int, int, double>(2, 5, 1), new Tuple <int, int, double>(5, 2, 1), new Tuple <int, int, double>(2, 6, 1), new Tuple <int, int, double>(6, 2, 1), new Tuple <int, int, double>(3, 4, 1),
                    new Tuple <int, int, double>(4, 3, 1), new Tuple <int, int, double>(3, 5, 1), new Tuple <int, int, double>(5, 3, 1), new Tuple <int, int, double>(3, 6, 1), new Tuple <int, int, double>(6, 3, 1),
                    new Tuple <int, int, double>(3, 7, 1), new Tuple <int, int, double>(7, 3, 1), new Tuple <int, int, double>(4, 5, 1), new Tuple <int, int, double>(5, 4, 1), new Tuple <int, int, double>(4, 6, 1),
                    new Tuple <int, int, double>(6, 4, 1), new Tuple <int, int, double>(4, 7, 1), new Tuple <int, int, double>(7, 4, 1), new Tuple <int, int, double>(4, 8, 1), new Tuple <int, int, double>(8, 4, 1),
                    new Tuple <int, int, double>(5, 6, 1), new Tuple <int, int, double>(6, 5, 1), new Tuple <int, int, double>(5, 7, 1), new Tuple <int, int, double>(7, 5, 1), new Tuple <int, int, double>(5, 8, 1),
                    new Tuple <int, int, double>(8, 5, 1), new Tuple <int, int, double>(6, 7, 1), new Tuple <int, int, double>(7, 6, 1), new Tuple <int, int, double>(6, 8, 1), new Tuple <int, int, double>(8, 6, 1),
                    new Tuple <int, int, double>(6, 9, 1), new Tuple <int, int, double>(9, 6, 1), new Tuple <int, int, double>(7, 8, 1), new Tuple <int, int, double>(8, 7, 1), new Tuple <int, int, double>(7, 9, 1),
                    new Tuple <int, int, double>(9, 7, 1), new Tuple <int, int, double>(8, 9, 1), new Tuple <int, int, double>(9, 8, 1),
                };
                HashSet <Tuple <int, int, double> > sprs1 = EnumHessAnmSpr(_coords, 7, 1).HToHashSet();
                HDebug.Exception(sprs0.Count == sprs1.Count);
                foreach (var spr in sprs0)
                {
                    HDebug.Exception(sprs1.Contains(spr));
                }
            }

            KDTreeDLL.KDTree <object> kdtree = new KDTreeDLL.KDTree <object>(3);
            for (int i = 0; i < coords.Count; i++)
            {
                kdtree.insert(coords[i], i);
            }

            int    size        = coords.Count;
            double cutoff2     = cutoff * cutoff;
            int    num_springs = 0;

            for (int c = 0; c < coords.Count; c++)
            {
                Vector lowk = coords[c] - (new double[] { cutoff, cutoff, cutoff });
                Vector uppk = coords[c] + (new double[] { cutoff, cutoff, cutoff });
                foreach (int r in kdtree.range(lowk, uppk))
                {
                    if (c >= r)
                    {
                        continue;
                    }
                    double dist2 = (coords[c] - coords[r]).Dist2;
                    if (dist2 < cutoff2)
                    {
                        yield return(new Tuple <int, int, double>(c, r, sprcst));

                        yield return(new Tuple <int, int, double>(r, c, sprcst));

                        num_springs += 2;
                    }
                }
            }
            double ratio_springs = ((double)num_springs) / (size * size);
        }
Пример #6
0
        public static CPsfgenExt PsfgenExt
            (IList <Tuple <string, string, Pdb.IAtom[]> > lstSegFileAtoms
            , string[] toplines
            , string[] parlines
            , Pdb alignto
            , string[] psfgen_lines
            , IList <string> minimize_conf_lines = null
            , HOptions options = null
            )
        {
            if (options == null)
            {
                options = new HOptions((string)null);
            }
            string tempbase       = @"C:\temp\";
            string psfgen_workdir = null;
            string topname        = "prot.top";
            string parname        = "prot.par";

            List <string> psf_lines = null;
            List <string> pdb_lines = null;

            using (var temp = new HTempDirectory(tempbase, null))
            {
                temp.EnterTemp();

                HFile.WriteAllLines(topname, toplines);
                HFile.WriteAllLines(parname, parlines);

                if ((HFile.Exists("prot.pdb") == false) || (HFile.Exists("prot.psf") == false))
                {
                    var psfgen = Namd.RunPsfgen
                                     (lstSegFileAtoms, tempbase, null, "2.10"
                                     , new string[] { topname }
                                     , new string[] {}
                                     , topname
                                     , psfgen_lines: psfgen_lines
                                     , psfgen_workdir: psfgen_workdir
                                     , options: options
                                     );

                    psf_lines = psfgen.psf_lines;
                    pdb_lines = psfgen.pdb_lines;
                    if (alignto != null)
                    {
                        HDebug.Exception("check!!!");
                        ////////////////////////////
                        Pdb prot = Pdb.FromLines(pdb_lines);
                        prot      = PsfgenExt_AlignTo(prot, alignto);
                        pdb_lines = prot.ToLines().ToList();
                    }
                    HFile.WriteAllLines("prot.pdb", pdb_lines);
                    HFile.WriteAllLines("prot.psf", psf_lines);
                }

                if (options.Contains("nomin") == false)
                {
                    if ((HFile.Exists("protmin.coor") == false) || (HFile.Exists("protmin.pdb") == false))
                    {
                        List <string> psfgen_pdb_lines = System.IO.File.ReadLines("prot.pdb").ToList();
                        List <string> psfgen_psf_lines = System.IO.File.ReadLines("prot.psf").ToList();

                        List <string> prm_lines = System.IO.File.ReadLines(parname).ToList();
                        string        Namd2_opt = null;
                        if (options.HSelectStartsWith("minimize option:").Length >= 1)
                        {
                            Namd2_opt = options.HSelectStartsWith("minimize option:").First().Replace("minimize option:", "");
                        }
                        var minpdb = Namd.Run.Namd2
                                         (psfgen_pdb_lines
                                         , psfgen_psf_lines
                                         , prm_lines
                                         , tempbase
                                         , "2.10"
                                         , ((Namd2_opt == null) ? "+p3" : Namd2_opt)
                                         , conf_lines: minimize_conf_lines
                                         );
                        HFile.WriteAllLines("protmin.coor", minpdb.coor_lines);

                        Pdb prot0 = Pdb.FromLines(psfgen_pdb_lines);
                        Pdb prot1 = Pdb.FromLines(minpdb.coor_lines);
                        HDebug.Exception(prot0.atoms.Length == prot1.atoms.Length);
                        HDebug.Exception(prot0.elements.Length == prot1.elements.Length);
                        // update conformation to minimized conformation
                        for (int i = 0; i < prot0.elements.Length; i++)
                        {
                            if (prot0.elements[i].GetType() != prot1.elements[i].GetType())
                            {
                                throw new HException("prot0.elements[i].GetType() != prot1.elements[i].GetType()");
                            }
                            if ((prot0.elements[i] is Pdb.IAtom) == false)
                            {
                                continue;
                            }
                            Pdb.IAtom iatom0 = prot0.elements[i] as Pdb.IAtom;
                            Pdb.IAtom iatom1 = prot1.elements[i] as Pdb.IAtom;
                            Vector    coord0 = iatom0.coord;
                            Vector    coord1 = iatom1.coord;
                            double    dist   = (coord0 - coord1).Dist;
                            if (iatom0.occupancy != 0)
                            {
                                if (dist != 0)
                                {
                                    throw new HException("iatom0.coord - iatom1.coord != 0");
                                }
                            }
                            if (dist != 0)
                            {
                                if (iatom0 is Pdb.Atom)
                                {
                                    string   nline0 = (iatom0 as Pdb.Atom).GetUpdatedLine(coord1);
                                    Pdb.Atom natom0 = Pdb.Atom.FromString(nline0);
                                    prot0.elements[i] = natom0;
                                    continue;
                                }
                                if (iatom0 is Pdb.Hetatm)
                                {
                                    string     nline0 = (iatom0 as Pdb.Hetatm).GetUpdatedLine(coord1);
                                    Pdb.Hetatm natom0 = Pdb.Hetatm.FromString(nline0);
                                    prot0.elements[i] = natom0;
                                    continue;
                                }
                            }
                        }
                        if ((prot0.elements[0] is Pdb.Remark) && (prot1.elements[0] is Pdb.Remark))
                        {
                            prot0.elements[0] = Pdb.Remark.FromString(prot1.elements[0].line);
                        }
                        prot0.ToFile("protmin.pdb");
                        pdb_lines = System.IO.File.ReadLines("protmin.pdb").ToList();
                    }
                }

                //{
                //    Pdb confpdb = GetConfPdb(options);
                //    var psf    = Namd.Psf.FromFile("prot.psf");
                //    var prm    = Namd.Prm.FromFile(parname);
                //    List<string> log = new List<string>();
                //    Universe  univ = Universe.BuilderNamd.Build(psf, prm, confpdb, true, new TextLogger(log));
                //    return univ;
                //}
                temp.QuitTemp();
            }

            return(new CPsfgenExt
            {
                psflines = psf_lines,
                pdblines = pdb_lines,
            });
        }