public static HessCoarseResiIter.HessInfoCoarseResiIter GetHessCoarseResiIter_SymrcmBlockWise (Hess.HessInfo hessinfo , Vector[] coords , double?symrcm_filter_blckwise_interact // null (use 1 as default) , ILinAlg ila , int clus_width ///= 18 | (18,500) is good for large sparse proteins , int num_atom_merge ///= 500 | (14,400) is good for small densely packed globular proteins , double thres_zeroblk ///= 0.001 | 0.001 could be fairly good for ssNMA (possibly for sbNMA and NMA, too) , string[] nameToKeep ) { if (clus_width <= 0) { throw new Exception("clus_width should be > 0"); } HessCoarseResiIter.FuncGetIdxKeepListRemv GetIdxKeepListRemv = delegate(object[] latoms, Vector[] lcoords) { Universe.Atom[] lunivatoms = latoms.HToType(null as Universe.Atom[]); return(HessCoarseResiIter.GetIdxKeepListRemv_ResiCluster_SymrcmBlockWise(lunivatoms, lcoords, hessinfo.hess, clus_width, num_atom_merge, symrcm_filter_blckwise_interact, nameToKeep)); }; return(HessCoarseResiIter.GetHessCoarseResiIter (hessinfo, coords, GetIdxKeepListRemv, ila, thres_zeroblk , HessCoarseResiIter.IterOption.Matlab_experimental )); }
public static HessCoarseResiIter.HessInfoCoarseResiIter GetHessCoarseResiIter_BlockWise (Hess.HessInfo hessinfo , Vector[] coords , ILinAlg ila , int clus_width ///= 18 | (18,500) is good for large sparse proteins , int num_atom_merge ///= 500 | (14,400) is good for small densely packed globular proteins , double thres_zeroblk ///= 0.001 | 0.001 could be fairly good for ssNMA (possibly for sbNMA and NMA, too) , IList <Universe.Atom> keeps , HessCoarseResiIter.IterOption iteropt , params string[] options ) { if (clus_width <= 0) { throw new Exception("clus_width should be > 0"); } HessCoarseResiIter.FuncGetIdxKeepListRemv GetIdxKeepListRemv = delegate(object[] latoms, Vector[] lcoords) { Universe.Atom[] lunivatoms = latoms.HToType(null as Universe.Atom[]); return(HessCoarseResiIter.GetIdxKeepListRemv_ResiCluster2(lunivatoms, lcoords, clus_width, num_atom_merge, keeps)); }; return(HessCoarseResiIter.GetHessCoarseResiIter (hessinfo, coords, GetIdxKeepListRemv, ila, thres_zeroblk , iteropt , options: options )); }
public static HessForcInfo From(Hess.HessInfo hessinfo) { return(new HessForcInfo { atoms = hessinfo.atoms, mass = hessinfo.mass, coords = hessinfo.coords, hess = hessinfo.hess, }); }
public static HessForcInfo From(Hess.HessInfo hessinfo, Vector[] forc) { return(new HessForcInfo { atoms = hessinfo.atoms, mass = hessinfo.mass, coords = hessinfo.coords, hess = hessinfo.hess, forc = forc, }); }
public static HessCoarseResiIter.HessInfoCoarseResiIter GetHessCoarseResiIter_SymrcmAtomWise (Hess.HessInfo hessinfo , Vector[] coords , ILinAlg ila , int num_atom_merge ///= 500 | (14,400) is good for small densely packed globular proteins , double thres_zeroblk ///= 0.001 | 0.001 could be fairly good for ssNMA (possibly for sbNMA and NMA, too) , string[] nameToKeep ) { HessCoarseResiIter.FuncGetIdxKeepListRemv GetIdxKeepListRemv = delegate(object[] latoms, Vector[] lcoords) { Universe.Atom[] lunivatoms = latoms.HToType(null as Universe.Atom[]); return(HessCoarseResiIter.GetIdxKeepListRemv_ResiCluster_SymrcmAtomWise(lunivatoms, lcoords, hessinfo.hess, num_atom_merge, thres_zeroblk, nameToKeep)); }; return(HessCoarseResiIter.GetHessCoarseResiIter (hessinfo, coords, GetIdxKeepListRemv, ila, thres_zeroblk , HessCoarseResiIter.IterOption.Matlab_experimental )); }
public static HessCoarseResiIter.HessInfoCoarseResiIter GetHessCoarseResiIter_BlockWise (Hess.HessInfo hessinfo , Vector[] coords , ILinAlg ila , int clus_width ///= 18 | (18,500) is good for large sparse proteins , int num_atom_merge ///= 500 | (14,400) is good for small densely packed globular proteins , double thres_zeroblk ///= 0.001 | 0.001 could be fairly good for ssNMA (possibly for sbNMA and NMA, too) ) { string[] nameToKeep = new string[] { "CA" }; return(GetHessCoarseResiIter_BlockWise (hessinfo , coords , ila , clus_width , num_atom_merge , thres_zeroblk , nameToKeep )); }
public static HessCoarseResiIter.HessInfoCoarseResiIter GetHessCoarseResiIter (Hess.HessInfo hessinfo , Vector[] coords , HessCoarseResiIter.FuncGetIdxKeepListRemv GetIdxKeepListRemv , ILinAlg ila , double thres_zeroblk = 0.001 , HessCoarseResiIter.IterOption iteropt = HessCoarseResiIter.IterOption.Matlab_experimental , string[] options = null ) { return(HessCoarseResiIter.GetHessCoarseResiIter (hessinfo: hessinfo , coords: coords , GetIdxKeepListRemv: GetIdxKeepListRemv , ila: ila , thres_zeroblk: thres_zeroblk , iteropt: iteropt , options: options )); }
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 Tuple <double, double, double[]> GetQuality (string pathcache , Universe univ , Func <Hess.HessInfo> GetHessInfo , double GetHessCoarseResiIter_thres_zeroblk ) { if (HFile.Exists(pathcache) == false) { double corr = double.NaN; double wovlp = double.NaN; double[] ovlps = new double[] { double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, }; try { Hess.HessInfo hessinfo = GetHessInfo(); Mode[] camodes_orig; { int[] idxca = (hessinfo.atoms as Universe.Atom[]).ListPdbAtomName(true).HIdxEqual("CA"); Matrix cahess = Hess.GetHessCoarseBlkmat(hessinfo.hess, idxca, "inv"); Mode[] lcamodes = Hess.GetModesFromHess(cahess, la); var camodes_nzero_zero = lcamodes.SeparateTolerants(); if (bool.Parse("false")) { camodes_nzero_zero = lcamodes.SeparateTolerantsByCountSigned(6); } /// manually fix 3LKY, 4EDL if (camodes_nzero_zero.Item2.Length != 6) { throw new HException("# zero-eigval != 6"); } camodes_orig = camodes_nzero_zero.Item1; } GC.Collect(); Vector cabfactor_orig = camodes_orig.GetBFactor().ToArray(); Mode[] camodes_iter; { var cahess = Hess.GetHessCoarseResiIter_BlockWise(hessinfo, univ.GetCoords(), la, 18, 500, GetHessCoarseResiIter_thres_zeroblk); Mode[] lcamodes = Hess.GetModesFromHess(cahess.hess, la); var camodes_nzero_zero = lcamodes.SeparateTolerantsByCountSigned(6); if (camodes_nzero_zero.Item2.Length != 6) { throw new HException("# zero-eigval != 6"); } camodes_iter = camodes_nzero_zero.Item1; } GC.Collect(); Vector cabfactor_iter = camodes_iter.GetBFactor().ToArray(); corr = HBioinfo.BFactor.Corr(cabfactor_orig, cabfactor_iter); var lwovlp = HBioinfo.OverlapWeightedByEigval(camodes_orig, camodes_iter, la, false, "corresponding index"); wovlp = lwovlp.woverlap; ovlps = new double[] { lwovlp.overlaps[0], lwovlp.overlaps[1], lwovlp.overlaps[2], lwovlp.overlaps[3], lwovlp.overlaps[4], lwovlp.overlaps[5], lwovlp.overlaps[6], lwovlp.overlaps[7], lwovlp.overlaps[8], lwovlp.overlaps[9], }; } catch (Exception e) { if (e.Message != "# zero-eigval != 6") { throw; } } HSerialize.SerializeText(pathcache, new double[] { corr, wovlp, ovlps[0], ovlps[1], ovlps[2], ovlps[3], ovlps[4], ovlps[5], ovlps[6], ovlps[7], ovlps[8], ovlps[9], }); } { double[] buff; HSerialize.DeserializeText(pathcache, out buff); double corr = buff[0]; double wovlp = buff[1]; double[] ovlps = new double[] { buff[2], buff[3], buff[4], buff[5], buff[6] , buff[7], buff[8], buff[9], buff[10], buff[11] }; if (double.IsNaN(corr)) { HDebug.Assert(false); } return(new Tuple <double, double, double[]>(corr, wovlp, ovlps)); } }
public static Quality GetQuality (string pathcache , Universe univ , Func <Hess.HessInfo> GetHessInfo , double GetHessCoarseResiIter_thres_zeroblk ) { double corr = double.NaN; double wovlp = double.NaN; double sparsityall = double.NaN; double sparsityca = double.NaN; double[] ovlps = new double[] { double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, }; double[] eigvals = new double[] { double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, double.NaN, }; try { Hess.HessInfo hessinfo = GetHessInfo(); double lsparsityall = 1 - hessinfo.hess.RatioUsedBlocks; Mode[] camodes_orig; { int[] idxca = (hessinfo.atoms as Universe.Atom[]).ListPdbAtomName(true).HIdxEqual("CA"); HessMatrix cahess = Hess.GetHessCoarseBlkmat(hessinfo.hess, idxca, "inv"); Mode[] lcamodes = Hess.GetModesFromHess(cahess, la); var camodes_nzero_zero = lcamodes.SeparateTolerants(); if (bool.Parse("false")) { camodes_nzero_zero = lcamodes.SeparateTolerantsByCountSigned(6); } /// manually fix 3LKY, 4EDL if (camodes_nzero_zero.Item2.Length != 6) { throw new HException("# zero-eigval != 6"); } camodes_orig = camodes_nzero_zero.Item1; } GC.Collect(); Vector cabfactor_orig = camodes_orig.GetBFactor().ToArray(); double lsparsityca; Mode[] camodes_iter; Mode[] camodes; { var coords = univ.GetCoords(); var cahess = Hess.GetHessCoarseResiIter_BlockWise(hessinfo, coords, la, 18, 500, GetHessCoarseResiIter_thres_zeroblk); lsparsityca = 1 - cahess.hess.RatioUsedBlocks; camodes = Hess.GetModesFromHess(cahess.hess, la); var camodes_nzero_zero = camodes.SeparateTolerantsByCountSigned(6); if (camodes_nzero_zero.Item2.Length != 6) { throw new HException("# zero-eigval != 6"); } camodes_iter = camodes_nzero_zero.Item1; } GC.Collect(); Vector cabfactor_iter = camodes_iter.GetBFactor().ToArray(); corr = HBioinfo.BFactor.Corr(cabfactor_orig, cabfactor_iter); var lwovlp = HBioinfo.OverlapWeightedByEigval(camodes_orig, camodes_iter, la, false, "corresponding index"); wovlp = lwovlp.woverlap; sparsityall = lsparsityall; sparsityca = lsparsityca; ovlps = new double[] { lwovlp.overlaps[0], lwovlp.overlaps[1], lwovlp.overlaps[2], lwovlp.overlaps[3], lwovlp.overlaps[4], lwovlp.overlaps[5], lwovlp.overlaps[6], lwovlp.overlaps[7], lwovlp.overlaps[8], lwovlp.overlaps[9], }; eigvals = new double[] { camodes[0].eigval, camodes[1].eigval, camodes[2].eigval, camodes[3].eigval, camodes[4].eigval, camodes[5].eigval, camodes[6].eigval, camodes[7].eigval, camodes[8].eigval, camodes[9].eigval, }; } catch (Exception e) { if (e.Message != "# zero-eigval != 6") { throw; } } return(new Quality { corr = corr, wovlp = wovlp, sparsity_all = sparsityall, sparsity_ca = sparsityca, ovlps = ovlps, eigvals = eigvals, }); }
public static HessInfo GetHessMixAnm(IList <Vector> coords , IList <int> lstIdxCa , IList <int> lstIdxAll , double CaCutoff, double CaSprcst , double AaCutoff, double AaSprcst ) { int size = coords.Count; Vector[] mixCoords = new Vector[size]; double[] cutoffs = new double[size]; cutoffs = cutoffs.HUpdateValueAll(double.NaN); double[] sprcsts = new double[size]; sprcsts = sprcsts.HUpdateValueAll(double.NaN); foreach (int idx in lstIdxCa) { mixCoords[idx] = coords[idx].Clone(); cutoffs[idx] = CaCutoff; sprcsts[idx] = CaSprcst; } foreach (int idx in lstIdxAll) { mixCoords[idx] = coords[idx].Clone(); cutoffs[idx] = AaCutoff; sprcsts[idx] = AaSprcst; } double[,] hesskij = new double[size, size]; hesskij = hesskij.HUpdateValueAll(double.NaN); for (int i = 0; i < size; i++) { Vector coordi = mixCoords[i]; if (coordi == null) { continue; } double cutoffi = cutoffs[i]; double sprcsti = sprcsts[i]; double cutoffi3 = cutoffi * cutoffi * cutoffi; for (int j = i; j < size; j++) { if (i == j) { hesskij[i, j] = 0; continue; } Vector coordj = mixCoords[j]; if (coordj == null) { continue; } double cutoffj = cutoffs[j]; double sprcstj = sprcsts[j]; double cutoffj3 = cutoffj * cutoffj * cutoffj; double distij = (coordi - coordj).Dist; double cutoffij; if (cutoffi == cutoffj) { cutoffij = cutoffi; } else { cutoffij = Math.Pow((cutoffi3 + cutoffj3) / 2, 1 / 3.0); } double sprcstij = 0; if (distij < cutoffij) { sprcstij = (sprcsti + sprcstj) / 2; } hesskij[i, j] = sprcstij; hesskij[j, i] = sprcstij; } } HessMatrix hess = GetHessAnm(mixCoords, hesskij); Vector mass = new double[size]; mass.SetValue(1); var hessinfo = new Hess.HessInfo { hess = hess, mass = mass, coords = mixCoords, atoms = null, numZeroEigval = 6, }; return(hessinfo); }