public HessInfo GetSubHessInfoByBlockHess(IList <int> idxs, ILinAlg ila) { // GetSubHessInfoByBlockHess(idxSele, ila, "pinv") Vector idxmass = mass.ToArray().HSelectByIndex(idxs); object[] idxatoms = atoms.HSelectByIndex(idxs); Vector[] idxcoords = coords.HSelectByIndex(idxs); HessMatrix idxhess = null; if (ila != null) { idxhess = Hess.GetHessCoarseBlkmat(hess, idxs, ila); } else { idxhess = Hess.GetHessCoarseBlkmat(hess, idxs); } return(new HessInfo { mass = idxmass, atoms = idxatoms, coords = idxcoords, hess = idxhess, numZeroEigval = numZeroEigval, }); }
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 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 MixHessInfo GetHessMixModel(Universe univ, IList <Vector> coords, ILinAlg la , IList <ResInfo> lstResAllAtom, FnGetHess GetHess, out string errmsg , bool bGetIntmInfo, string strBkbnReso ) { FnGetHess lGetHess = delegate(Universe luniv, IList <Vector> lcoords, int[] lidxAll, int[] lidxBuffer, int[] lidxCoarse, int[] idxBackbone) { // double check if all three indices are disjoint HDebug.Assert(lidxAll.HUnion().Length == lidxAll.Length); HDebug.Assert(lidxBuffer.HUnion().Length == lidxBuffer.Length); HDebug.Assert(lidxCoarse.HUnion().Length == lidxCoarse.Length); HDebug.Assert(lidxAll.HListCommonT(lidxBuffer).Count == 0); HDebug.Assert(lidxBuffer.HListCommonT(lidxCoarse).Count == 0); HDebug.Assert(lidxCoarse.HListCommonT(lidxAll).Count == 0); if (HDebug.IsDebuggerAttached) { foreach (int idx in lidxAll) { HDebug.Assert(lcoords[idx] != null); } foreach (int idx in lidxBuffer) { HDebug.Assert(lcoords[idx] != null); } foreach (int idx in lidxCoarse) { HDebug.Assert(lcoords[idx] != null); } } var hessinfo = GetHess(luniv, lcoords, lidxAll, lidxBuffer, lidxCoarse, idxBackbone); return(hessinfo); }; bool bVerifySteps = false; // HDebug.IsDebuggerAttached; Vector bfactorFull = null; if (bVerifySteps && HDebug.IsDebuggerAttached) { int[] idxAll = HEnum.HEnumCount(coords.Count).ToArray(); var hessinfo = lGetHess(univ, coords, idxAll, new int[0], new int[0], new int[0]); var modes = hessinfo.GetModesMassReduced(); var modesPosZero = modes.SeparateTolerants(); HDebug.Assert(modesPosZero.Item2.Length == 6); bfactorFull = modesPosZero.Item1.GetBFactor().ToArray(); } IList <Pdb.Atom> pdbatoms = univ.atoms.ListPdbAtoms(); ResInfo[] resinfos = pdbatoms.ListResInfo(true); int[] idxCa; // idxs of Ca atoms int[] idxBkbn; // idxs of backbone atoms int[] idxSele; // idxs of all atoms of lstResAllAtom (selected) int[] idxCntk; // idxs of all atoms contacting idxSele { string[] nameBkbn; switch (strBkbnReso) { case "NCaC": nameBkbn = new string[] { "N", "CA", "C" }; break; case "NCaC-O": nameBkbn = new string[] { "N", "CA", "C", "O" }; break; case "NCaC-OHn": nameBkbn = new string[] { "N", "CA", "C", "O", "HN" }; break; case "NCaC-O-Ha": nameBkbn = new string[] { "N", "CA", "C", "O", "HA" }; break; case "NCaC-OHn-HaCb": nameBkbn = new string[] { "N", "CA", "C", "O", "HN", "HA", "CB" }; break; default: throw new NotImplementedException(); } idxCa = univ.atoms.ListPdbAtoms().IdxByName(true, "CA"); //idxBkbn = univ.atoms.ListPdbAtoms().IdxByName(true, "N", "CA", "C", "O", "HA"); idxBkbn = univ.atoms.ListPdbAtoms().IdxByName(true, nameBkbn); idxSele = resinfos.HIdxEqual(ResInfo.Equals, lstResAllAtom.ToArray()); Vector[] coordSele = coords.HSelectByIndex(idxSele); int[] idxCtof = coords.HIdxWithinCutoff(4.5, coordSele); ResInfo[] resCtof = resinfos.HSelectByIndex(idxCtof); resCtof = resCtof.HUnion(); // remove redundant residues resCtof = resCtof.HRemoveAll(lstResAllAtom.ToArray()).ToArray(); // remove active site residues idxCntk = resinfos.HIdxEqual(ResInfo.Equals, resCtof); bool plot = false; if (plot) #region verify by plotting { Pdb.ToFile(@"C:\temp\mix.pdb", pdbatoms, coords); Pymol.Py.CgoOld.WriteBlank(@"C:\temp\mix.py", false); Pymol.Py.CgoOld.WriteSphere(@"C:\temp\mix.py", "Ca", coords.HSelectByIndex(idxCa).HRemoveAllNull(), 0.3); Pymol.Py.CgoOld.WriteSphere(@"C:\temp\mix.py", "backbone", coords.HSelectByIndex(idxBkbn).HRemoveAllNull(), 0.3); Pymol.Py.CgoOld.WriteSphere(@"C:\temp\mix.py", "active", coordSele.HRemoveAllNull() , 0.3 , red: 1, green: 0, blue: 0); Pymol.Py.CgoOld.WriteSphere(@"C:\temp\mix.py", "connect", coords.HSelectByIndex(idxCntk).HRemoveAllNull() , 0.3 , red: 0, green: 0, blue: 1); HFile.WriteAllLines(@"C:\temp\mix.pml", new string[] { "load mix.pdb", "run mix.py", "reset", }); } #endregion } //idxBkbn = HEnum.HSequence(coords.Count).ToArray(); int[] idxFull = idxSele.HUnionWith(idxCntk).HUnionWith(idxBkbn); Vector[] coordsFull = coords.HCopyIdx(idxFull, null); var hessinfoFull = lGetHess(univ, coordsFull , idxAll: idxSele , idxBuffer: idxCntk.HRemoveAll(idxSele) , idxCoarse: idxBkbn.HRemoveAll(idxSele).HRemoveAll(idxCntk) , idxBackbone: idxBkbn ); HDebug.Assert(hessinfoFull.hess.IsComputable() == false); //{ // var modes = hessinfoFull.GetModesMassReduced(false, la); // var modesPosZero = modes.SeparateTolerants(); // Vector bf = modesPosZero.Item1.GetBFactor().ToArray(); // Vector nma = ext as Vector; // double corr = BFactor.Corr(bf, nma); //} var hessinfoSub = hessinfoFull.GetSubHessInfo(idxFull); HDebug.Assert(hessinfoSub.hess.IsComputable() == true); if (HDebug.IsDebuggerAttached) { Mode[] lmodes = hessinfoSub.GetModesMassReduced(); if (lmodes.SeparateTolerants().Item2.Length != 6) { HDebug.Assert(false); //throw new Exception(); } } if (bVerifySteps && HDebug.IsDebuggerAttached) { var modes = hessinfoSub.GetModesMassReduced(); var modesPosZero = modes.SeparateTolerants(); Vector bf0 = modesPosZero.Item1.GetBFactor().ToArray(); Vector bf = new double[coords.Count]; bf.SetValue(double.NaN); for (int i = 0; i < idxFull.Length; i++) { bf[idxFull[i]] = bf0[i]; } int[] idxCax = idxCa.HRemoveAll(idxSele).HRemoveAll(idxCntk); double corr = HBioinfo.BFactor.Corr(bf, bfactorFull, idxFull); double corrCa = HBioinfo.BFactor.Corr(bf, bfactorFull, idxCa); double corrCax = HBioinfo.BFactor.Corr(bf, bfactorFull, idxCax); double corrBkbn = HBioinfo.BFactor.Corr(bf, bfactorFull, idxBkbn); double corrSele = HBioinfo.BFactor.Corr(bf, bfactorFull, idxSele); double corrCntk = HBioinfo.BFactor.Corr(bf, bfactorFull, idxCntk); } int[] idxMix; { Universe.Atom[] mixAtoms = hessinfoSub.atoms.HToType <object, Universe.Atom>(); Pdb.Atom[] mixPdbAtoms = mixAtoms.ListPdbAtoms(); ResInfo[] mixResinfos = mixPdbAtoms.ListResInfo(true); int[] mixIdxCa = mixPdbAtoms.IdxByName(true, "CA"); int[] mixIdxSele = mixResinfos.HIdxEqual(ResInfo.Equals, lstResAllAtom.ToArray()); idxMix = mixIdxCa.HUnionWith(mixIdxSele).HSort(); } HessMatrix hessMix = Hess.GetHessCoarseBlkmat(hessinfoSub.hess, idxMix, la); if (hessMix.IsComputable() == false) { // hessSub (=hessinfoSub.hess) is computable, but hessMix become in-computable. // This happens when 1. hessSub=[A,B;C,D] has more then 6 zero eigenvalues, // 2. the D matrix become singular, // 3. inv(D) is incomputable // 4. "A - B inv(D) C" becomes incomputable. errmsg = "hess(Sele,Cntk,Bkbn)->hess(Sele,Ca) becomes incomputable"; return(null); } MixHessInfo hessinfoMix = new MixHessInfo { hess = hessMix, mass = hessinfoSub.mass.ToArray().HSelectByIndex(idxMix), atoms = hessinfoSub.atoms.ToArray().HSelectByIndex(idxMix), coords = hessinfoSub.coords.ToArray().HSelectByIndex(idxMix), numZeroEigval = 6, }; if (bGetIntmInfo) { hessinfoMix.intmHessinfoAllMidBkbn = hessinfoSub; hessinfoMix.intmAtomCa = univ.atoms.ToArray().HSelectByIndex(idxCa); hessinfoMix.intmAtomAll = univ.atoms.ToArray().HSelectByIndex(idxSele); hessinfoMix.intmAtomMid = univ.atoms.ToArray().HSelectByIndex(idxCntk); hessinfoMix.intmAtomBkbn = univ.atoms.ToArray().HSelectByIndex(idxBkbn); } if (HDebug.IsDebuggerAttached) { Mode[] lmodes = hessinfoMix.GetModesMassReduced(); if (lmodes.SeparateTolerants().Item2.Length != 6) { throw new Exception(); } } if (bVerifySteps && HDebug.IsDebuggerAttached) { var modes = hessinfoMix.GetModesMassReduced(); var modesPosZero = modes.SeparateTolerants(); Vector bf0 = modesPosZero.Item1.GetBFactor().ToArray(); Vector bf = new double[coords.Count]; bf.SetValue(double.NaN); for (int i = 0; i < idxMix.Length; i++) { bf[idxFull[idxMix[i]]] = bf0[i]; } int[] idxCax = idxCa.HRemoveAll(idxSele).HRemoveAll(idxCntk); double corr = HBioinfo.BFactor.Corr(bf, bfactorFull, true); double corrCa = HBioinfo.BFactor.Corr(bf, bfactorFull, idxCa); double corrCax = HBioinfo.BFactor.Corr(bf, bfactorFull, idxCax); double corrBkbn = HBioinfo.BFactor.Corr(bf, bfactorFull, idxBkbn); double corrSele = HBioinfo.BFactor.Corr(bf, bfactorFull, idxSele); double corrCntk = HBioinfo.BFactor.Corr(bf, bfactorFull, idxCntk); } errmsg = null; return(hessinfoMix); }