Beispiel #1
0
            public static HessInfoCoarseResiIter GetHessCoarseResiIter
                (Hess.HessInfo hessinfo
                , Vector[] coords
                , FuncGetIdxKeepListRemv GetIdxKeepListRemv
                , ILinAlg ila
                , double thres_zeroblk = 0.001
                , IterOption iteropt   = IterOption.Matlab_experimental
                , string[] options     = null
                )
            {
                bool rediag = true;

                HessMatrix H = null;

                List <int>[] lstNewIdxRemv = null;
                int          numca         = 0;

                double[] reMass   = null;
                object[] reAtoms  = null;
                Vector[] reCoords = null;
                Tuple <int[], int[][]> idxKeepRemv = null;

                //System.Console.WriteLine("begin re-indexing hess");
                {
                    object[] atoms = hessinfo.atoms;
                    idxKeepRemv = GetIdxKeepListRemv(atoms, coords);
                    int[]   idxKeep  = idxKeepRemv.Item1;
                    int[][] idxsRemv = idxKeepRemv.Item2;
                    {
                        List <int> check = new List <int>();
                        check.AddRange(idxKeep);
                        foreach (int[] idxRemv in idxsRemv)
                        {
                            check.AddRange(idxRemv);
                        }
                        check = check.HToHashSet().ToList();
                        if (check.Count != coords.Length)
                        {
                            throw new Exception("the re-index contains the duplicated atoms or the missing atoms");
                        }
                    }
                    List <int> idxs = new List <int>();
                    idxs.AddRange(idxKeep);
                    foreach (int[] idxRemv in idxsRemv)
                    {
                        idxs.AddRange(idxRemv);
                    }
                    HDebug.Assert(idxs.Count == idxs.HToHashSet().Count);

                    H        = hessinfo.hess.ReshapeByAtom(idxs);
                    numca    = idxKeep.Length;
                    reMass   = hessinfo.mass.ToArray().HSelectByIndex(idxs);
                    reAtoms  = hessinfo.atoms.ToArray().HSelectByIndex(idxs);
                    reCoords = coords.HSelectByIndex(idxs);

                    int nidx = idxKeep.Length;
                    lstNewIdxRemv = new List <int> [idxsRemv.Length];
                    for (int i = 0; i < idxsRemv.Length; i++)
                    {
                        lstNewIdxRemv[i] = new List <int>();
                        foreach (var idx in idxsRemv[i])
                        {
                            lstNewIdxRemv[i].Add(nidx);
                            nidx++;
                        }
                    }
                    HDebug.Assert(nidx == lstNewIdxRemv.Last().Last() + 1);
                    HDebug.Assert(nidx == idxs.Count);
                }
                GC.Collect(0);
                HDebug.Assert(numca == H.ColBlockSize - lstNewIdxRemv.HListCount().Sum());

                //if(bool.Parse("false"))
                {
                    if (bool.Parse("false"))
                    #region
                    {
                        int[]      idxKeep  = idxKeepRemv.Item1;
                        int[][]    idxsRemv = idxKeepRemv.Item2;
                        Pdb.Atom[] pdbatoms = hessinfo.atomsAsUniverseAtom.ListPdbAtoms();
                        Pdb.ToFile(@"C:\temp\coarse-keeps.pdb", pdbatoms.HSelectByIndex(idxKeep), false);
                        if (HFile.Exists(@"C:\temp\coarse-graining.pdb"))
                        {
                            HFile.Delete(@"C:\temp\coarse-graining.pdb");
                        }
                        foreach (int[] idxremv in idxsRemv.Reverse())
                        {
                            List <Pdb.Element> delatoms = new List <Pdb.Element>();
                            foreach (int idx in idxremv)
                            {
                                if (pdbatoms[idx] == null)
                                {
                                    continue;
                                }
                                string   line    = pdbatoms[idx].GetUpdatedLine(coords[idx]);
                                Pdb.Atom delatom = Pdb.Atom.FromString(line);
                                delatoms.Add(delatom);
                            }
                            Pdb.ToFile(@"C:\temp\coarse-graining.pdb", delatoms.ToArray(), true);
                        }
                    }
                    #endregion

                    if (bool.Parse("false"))
                    #region
                    {
                        // export matrix to matlab, so the matrix can be checked in there.
                        int[] idxca  = HEnum.HEnumCount(numca).ToArray();
                        int[] idxoth = HEnum.HEnumFromTo(numca, coords.Length - 1).ToArray();
                        Matlab.Register(@"C:\temp\");
                        Matlab.PutSparseMatrix("H", H.GetMatrixSparse(), 3, 3);
                        Matlab.Execute("figure; spy(H)");
                        Matlab.Clear();
                    }
                    #endregion

                    if (bool.Parse("false"))
                    #region
                    {
                        HDirectory.CreateDirectory(@"K:\temp\$coarse-graining\");
                        {   // export original hessian matrix
                            List <int> cs = new List <int>();
                            List <int> rs = new List <int>();
                            foreach (ValueTuple <int, int, MatrixByArr> bc_br_bval in hessinfo.hess.EnumBlocks())
                            {
                                cs.Add(bc_br_bval.Item1);
                                rs.Add(bc_br_bval.Item2);
                            }
                            Matlab.Clear();
                            Matlab.PutVector("cs", cs.ToArray());
                            Matlab.PutVector("rs", rs.ToArray());
                            Matlab.Execute("hess = sparse(cs+1, rs+1, ones(size(cs)));");
                            Matlab.Execute("hess = float(hess);");
                            Matlab.Execute("figure; spy(hess)");
                            Matlab.Execute("cs = int32(cs+1);");
                            Matlab.Execute("rs = int32(rs+1);");
                            Matlab.Execute(@"save('K:\temp\$coarse-graining\hess-original.mat', 'cs', 'rs', '-v6');");
                            Matlab.Clear();
                        }
                        {   // export reshuffled hessian matrix
                            List <int> cs = new List <int>();
                            List <int> rs = new List <int>();
                            foreach (ValueTuple <int, int, MatrixByArr> bc_br_bval in H.EnumBlocks())
                            {
                                cs.Add(bc_br_bval.Item1);
                                rs.Add(bc_br_bval.Item2);
                            }
                            Matlab.Clear();
                            Matlab.PutVector("cs", cs.ToArray());
                            Matlab.PutVector("rs", rs.ToArray());
                            Matlab.Execute("H = sparse(cs+1, rs+1, ones(size(cs)));");
                            Matlab.Execute("H = float(H);");
                            Matlab.Execute("figure; spy(H)");
                            Matlab.Execute("cs = int32(cs+1);");
                            Matlab.Execute("rs = int32(rs+1);");
                            Matlab.Execute(@"save('K:\temp\$coarse-graining\hess-reshuffled.mat', 'cs', 'rs', '-v6');");
                            Matlab.Clear();
                        }
                    }
                    #endregion

                    if (bool.Parse("false"))
                    #region
                    {
                        int[] idxca  = HEnum.HEnumCount(numca).ToArray();
                        int[] idxoth = HEnum.HEnumFromTo(numca, coords.Length - 1).ToArray();

                        HessMatrix A = H.SubMatrixByAtoms(false, idxca, idxca);
                        HessMatrix B = H.SubMatrixByAtoms(false, idxca, idxoth);
                        HessMatrix C = H.SubMatrixByAtoms(false, idxoth, idxca);
                        HessMatrix D = H.SubMatrixByAtoms(false, idxoth, idxoth);
                        Matlab.Clear();
                        Matlab.PutSparseMatrix("A", A.GetMatrixSparse(), 3, 3);
                        Matlab.PutSparseMatrix("B", B.GetMatrixSparse(), 3, 3);
                        Matlab.PutSparseMatrix("C", C.GetMatrixSparse(), 3, 3);
                        Matlab.PutSparseMatrix("D", D.GetMatrixSparse(), 3, 3);
                        Matlab.Clear();
                    }
                    #endregion
                }

                List <HessCoarseResiIterInfo> iterinfos = null;
                {
                    object[] atoms = reAtoms; // reAtoms.HToType(null as Universe.Atom[]);
                    CGetHessCoarseResiIterImpl info = null;
                    switch (iteropt)
                    {
                    case IterOption.ILinAlg_20150329: info = GetHessCoarseResiIterImpl_ILinAlg_20150329(H, lstNewIdxRemv, thres_zeroblk, ila, false);                    break;

                    case IterOption.ILinAlg: info = GetHessCoarseResiIterImpl_ILinAlg(H, lstNewIdxRemv, thres_zeroblk, ila, false);                             break;

                    case IterOption.Matlab: info = GetHessCoarseResiIterImpl_Matlab(atoms, H, lstNewIdxRemv, thres_zeroblk, ila, false, options);              break;

                    case IterOption.Matlab_experimental: info = GetHessCoarseResiIterImpl_Matlab_experimental(atoms, H, lstNewIdxRemv, thres_zeroblk, ila, false, options); break;

                    case IterOption.Matlab_IterLowerTri: info = GetHessCoarseResiIterImpl_Matlab_IterLowerTri(atoms, H, lstNewIdxRemv, thres_zeroblk, ila, false, options); break;

                    case IterOption.LinAlg_IterLowerTri: info = GetHessCoarseResiIterImpl_LinAlg_IterLowerTri.Do(atoms, H, lstNewIdxRemv, thres_zeroblk, ila, false, options); break;
                    }
                    ;
                    H         = info.H;
                    iterinfos = info.iterinfos;
                }
                //{
                //    var info = GetHessCoarseResiIterImpl_Matlab(H, lstNewIdxRemv, thres_zeroblk);
                //    H = info.H;
                //}
                GC.Collect(0);

                if (HDebug.IsDebuggerAttached)
                {
                    int   nidx  = 0;
                    int[] ikeep = idxKeepRemv.Item1;
                    foreach (int idx in ikeep)
                    {
                        bool equal = object.ReferenceEquals(hessinfo.atoms[idx], reAtoms[nidx]);
                        if (equal == false)
                        {
                            HDebug.Assert(false);
                        }
                        HDebug.Assert(equal);
                        nidx++;
                    }
                }

                if (rediag)
                {
                    H = H.CorrectHessDiag();
                }
                //System.Console.WriteLine("finish fixing diag");

                return(new HessInfoCoarseResiIter
                {
                    hess = H,
                    mass = reMass.HSelectCount(numca),
                    atoms = reAtoms.HSelectCount(numca),
                    coords = reCoords.HSelectCount(numca),
                    numZeroEigval = 6,
                    iterinfos = iterinfos,
                });
            }
                public static void Main
                    (string[] args
                    , string[] hesstypes
                    , double[] threszeroblks
                    )
                {
                    string cachebase = @"K:\cache\CoarseGraining-20150111\proteins-177\";
                    string lockbase  = cachebase + @"lock\"; if (HDirectory.Exists(lockbase) == false)
                    {
                        HDirectory.CreateDirectory(lockbase);
                    }

                    Dictionary <string, List <Tuple <double, double, double[]> > > data = new Dictionary <string, List <Tuple <double, double, double[]> > >();

                    foreach (string pdbid in pdbids)
                    {
                        if (locks.ContainsKey(pdbid) == false)
                        {
                            var filelock = HFile.LockFile(lockbase + pdbid);
                            if (filelock == null)
                            {
                                System.Console.WriteLine("{0} is locked", pdbid);
                                continue;
                            }
                            locks.Add(pdbid, filelock);
                        }

                        string pathbase = cachebase + pdbid + @"\";
                        // load univ
                        var pdb0 = Pdb.FromFile(pathbase + "xry-struc.pdb");
                        var xyz0 = Tinker.Xyz.FromFile(pathbase + "xry-struc.xyz", false);
                        var xyz1 = Tinker.Xyz.FromFile(pathbase + "min-struc-charmm22.xyz", false);
                        var prm  = Tinker.Prm.FromFile(cachebase + "charmm22.prm");
                        if (HFile.Exists(pathbase + "min-struc-charmm22-screened.xyz") == false)
                        {
                            var newton = Tinker.Run.Newton
                                             (xyz1, prm, tempbase
                                             , null       // copytemp
                                             , "A 0.0001" // param
                                             , null       // atomsToFix
                                             , false
                                             , "CUTOFF 9", "TAPER"
                                             );
                            newton.minxyz.ToFile(pathbase + "min-struc-charmm22-screened.xyz", false);
                        }
                        var      xyz2      = Tinker.Xyz.FromFile(pathbase + "min-struc-charmm22-screened.xyz", false);
                        Universe univ      = Universe.BuilderTinker.Build(xyz1, prm, pdb0, xyz0, 0.001);
                        Universe univ_scrn = Universe.BuilderTinker.Build(xyz2, prm, pdb0, xyz0, 0.001);

                        if (HDebug.IsDebuggerAttached)
                        {
                            var grad  = Tinker.Run.Testgrad(xyz1, prm, tempbase);
                            var forc  = grad.anlyts.GetForces(xyz1.atoms);
                            var mforc = forc.Dist().Max();
                            HDebug.Assert(mforc < 0.1);

                            grad = Tinker.Run.Testgrad(xyz2, prm, tempbase, new string[] { "CUTOFF 9", "TAPER" }
                                                       , optOutSource: null
                                                       );
                            forc  = grad.anlyts.GetForces(xyz1.atoms);
                            mforc = forc.Dist().Max();
                            HDebug.Assert(mforc < 0.1);
                        }

                        System.Console.Write(pdbid + " : ");
                        foreach (string hesstype in hesstypes)
                        {
                            foreach (double threszeroblk in threszeroblks)
                            {
                                double GetHessCoarseResiIter_thres_zeroblk        = threszeroblk;
                                Tuple <double, double, double[]> corr_wovlp_ovlps = GetQuality(pathbase, univ, univ_scrn, hesstype, GetHessCoarseResiIter_thres_zeroblk);
                                if (corr_wovlp_ovlps == null)
                                {
                                    System.Console.Write(hesstype + "(Unknown exception                                                            ),    ");
                                    continue;
                                }
                                System.Console.Write(hesstype + "(");
                                System.Console.Write("thod " + threszeroblk + " : ");
                                System.Console.Write("corr {0,6:0.0000}, ", corr_wovlp_ovlps.Item1);
                                System.Console.Write("wovlp {0,6:0.0000} : ", corr_wovlp_ovlps.Item2);
                                System.Console.Write("{0}),    ", corr_wovlp_ovlps.Item3.HToStringSeparated("{0,4:0.00}", ","));

                                string datakey = hesstype + "-" + threszeroblk;
                                if (data.ContainsKey(datakey) == false)
                                {
                                    data.Add(datakey, new List <Tuple <double, double, double[]> >());
                                }
                                data[datakey].Add(corr_wovlp_ovlps);
                            }
                        }
                        System.Console.WriteLine();
                    }

                    System.Console.WriteLine("=================================================================================================");
                    System.Console.Write("avg  : ");
                    foreach (string hesstype in hesstypes)
                    {
                        foreach (double threszeroblk in threszeroblks)
                        {
                            string datakey = hesstype + "-" + threszeroblk;
                            List <Tuple <double, double, double[]> > datai = data[datakey];

                            double[]   lst_corr  = datai.HListItem1().ToArray();
                            double[]   lst_wovlp = datai.HListItem2().ToArray();
                            double[][] lst_ovlps = datai.HListItem3().ToArray();

                            double   corr  = lst_corr.HRemoveAll(double.NaN).Average();
                            double   wovlp = lst_wovlp.HRemoveAll(double.NaN).Average();
                            double[] ovlps = new double[10];
                            for (int i = 0; i < 10; i++)
                            {
                                double[] lst_ovlpsi = new double[lst_ovlps.Length];
                                for (int j = 0; j < lst_ovlps.Length; j++)
                                {
                                    lst_ovlpsi[j] = lst_ovlps[j][i];
                                }
                                ovlps[i] = lst_ovlpsi.HRemoveAll(double.NaN).Average();
                            }

                            System.Console.Write(hesstype + "(");
                            System.Console.Write("thod " + threszeroblk + " : ");
                            System.Console.Write("corr {0,6:0.0000}, ", corr);
                            System.Console.Write("wovlp {0,6:0.0000} : ", wovlp);
                            System.Console.Write("{0}),    ", ovlps.HToStringSeparated("{0,4:0.00}", ","));
                        }
                    }
                    System.Console.WriteLine();
                    System.Console.Write("min  : ");
                    foreach (string hesstype in hesstypes)
                    {
                        foreach (double threszeroblk in threszeroblks)
                        {
                            string datakey = hesstype + "-" + threszeroblk;
                            List <Tuple <double, double, double[]> > datai = data[datakey];

                            double[]   lst_corr  = datai.HListItem1().ToArray();
                            double[]   lst_wovlp = datai.HListItem2().ToArray();
                            double[][] lst_ovlps = datai.HListItem3().ToArray();

                            double   corr  = lst_corr.HRemoveAll(double.NaN).Min();
                            double   wovlp = lst_wovlp.HRemoveAll(double.NaN).Min();
                            double[] ovlps = new double[10];
                            for (int i = 0; i < 10; i++)
                            {
                                double[] lst_ovlpsi = new double[lst_ovlps.Length];
                                for (int j = 0; j < lst_ovlps.Length; j++)
                                {
                                    lst_ovlpsi[j] = lst_ovlps[j][i];
                                }
                                ovlps[i] = lst_ovlpsi.HRemoveAll(double.NaN).Min();
                            }

                            System.Console.Write(hesstype + "(");
                            System.Console.Write("thod " + threszeroblk + " : ");
                            System.Console.Write("corr {0,6:0.0000}, ", corr);
                            System.Console.Write("wovlp {0,6:0.0000} : ", wovlp);
                            System.Console.Write("{0}),    ", ovlps.HToStringSeparated("{0,4:0.00}", ","));
                        }
                    }
                    System.Console.WriteLine();
                    System.Console.Write("#miss: ");
                    foreach (string hesstype in hesstypes)
                    {
                        foreach (double threszeroblk in threszeroblks)
                        {
                            string datakey = hesstype + "-" + threszeroblk;
                            List <Tuple <double, double, double[]> > datai = data[datakey];

                            double[]   lst_corr  = datai.HListItem1().ToArray();
                            double[]   lst_wovlp = datai.HListItem2().ToArray();
                            double[][] lst_ovlps = datai.HListItem3().ToArray();

                            int   cnt_corr  = lst_corr.Length - lst_corr.HRemoveAll(double.NaN).Length;
                            int   cnt_wovlp = lst_wovlp.Length - lst_wovlp.HRemoveAll(double.NaN).Length;
                            int[] cnt_ovlps = new int[10];
                            for (int i = 0; i < 10; i++)
                            {
                                double[] lst_ovlpsi = new double[lst_ovlps.Length];
                                for (int j = 0; j < lst_ovlps.Length; j++)
                                {
                                    lst_ovlpsi[j] = lst_ovlps[j][i];
                                }
                                cnt_ovlps[i] = lst_ovlpsi.Length - lst_ovlpsi.HRemoveAll(double.NaN).Length;
                            }

                            System.Console.Write(hesstype + "(");
                            System.Console.Write("thod " + threszeroblk + " : ");
                            System.Console.Write("corr {0,6}, ", cnt_corr);
                            System.Console.Write("wovlp {0,6} : ", cnt_wovlp);
                            System.Console.Write("{0}),    ", cnt_ovlps.HToStringSeparated("{0,4}", ","));
                        }
                    }
                    System.Console.WriteLine();
                }