Example #1
0
        public static Residue[] ListResidue(this IList <Atom> atoms)
        {
            List <Residue> residues = new List <Residue>();
            Dictionary <char, List <Atom> > chain2atoms = atoms.GroupChainID();

            foreach (char ChainID in chain2atoms.Keys)
            {
                int?prv_resseq = null;
                Dictionary <int, List <Atom> > resi2atoms = chain2atoms[ChainID].GroupResSeq();
                foreach (int ResSeq in resi2atoms.Keys)
                {
                    string  ResName = resi2atoms[ResSeq][0].resName;
                    Residue residue = new Residue {
                        ChainID = ChainID,
                        ResName = ResName,
                        ResSeq  = ResSeq,
                        atoms   = resi2atoms[ResSeq]
                    };
                    residues.Add(residue);

                    if (prv_resseq != null)
                    {
                        HDebug.Assert(prv_resseq < residue.ResSeq);
                    }
                    prv_resseq = residue.ResSeq;
                }
            }
            return(residues.ToArray());
        }
Example #2
0
            public static double[] GetRotAngles(Universe univ
                                                , Vector[] coords
                                                , Vector[] dcoords
                                                , MatrixByArr J
                                                , ILinAlg ila
                                                )
            {
                Vector dangles;

                using (ila.NewDisposables())
                {
                    Vector R      = Vector.FromBlockvector(dcoords);
                    Vector M      = univ.GetMasses(3);
                    var    RR     = ila.ToILMat(R).AddDisposable();
                    var    MM     = ila.ToILMat(M).Diag().AddDisposable();
                    var    JJ     = ila.ToILMat(J).AddDisposable();
                    var    invJMJ = ila.Inv(JJ.Tr * MM * JJ).AddDisposable();
                    var    AA     = invJMJ * JJ.Tr * MM * RR;
                    dangles = AA.ToArray().HToArray1D();
                }

                if (HDebug.False && HDebug.IsDebuggerAttached)
                {
                    Vector tdangles = GetRotAngles(univ, coords, dcoords, J);

                    HDebug.Assert(0.9999 < (tdangles.Dist / dangles.Dist), (tdangles.Dist / dangles.Dist) < 1.0001);
                    HDebug.Assert(LinAlg.DotProd(tdangles, dangles) / (tdangles.Dist * dangles.Dist) > 0.9999);
                    HDebug.Assert((tdangles - dangles).Dist / tdangles.Dist < 0.0001);
                }

                return(dangles);
            }
Example #3
0
        public static Pdb.Compnd[] SortByContinuation(this IList <Pdb.Compnd> compnds)
        {
            Dictionary <int, Pdb.Compnd> dict = new Dictionary <int, Pdb.Compnd>();

            foreach (var compnd in compnds)
            {
                string sconti = compnd.continuation;
                int?   iconti = sconti.HParseInt();
                if (iconti == null)
                {
                    iconti = 0;
                }
                dict.Add(iconti.Value, compnd);
            }
            HDebug.Assert(compnds.Count == dict.Count);

            int[] keys = dict.Keys.ToArray().HSort();

            Pdb.Compnd[] sortcompnds = new Pdb.Compnd[compnds.Count];
            for (int i = 0; i < sortcompnds.Length; i++)
            {
                var key    = keys[i];
                var compnd = dict[key];
                sortcompnds[i] = compnd;
            }

            return(sortcompnds);
        }
Example #4
0
                public static Nonbonded FromString(string line, ITextLogger logger)
                {
                    Element elem = Element.FromString(line, 1, 6);

                    //Debug.Assert(0  == elem.parms[0]); // "FE     0.010000   0.000000     0.650000" happens
                    if (!(0 == elem.parms[0]))
                    {
                        logger.Log(string.Format("Warning: Nonbonded params ({0}): 1st parameter should be in general == 0", line));
                    }
                    double epsilon = elem.parms[1];
                    double Rmin2   = elem.parms[2]; //Debug.Assert(Rmin2   > 0);

                    if (!(Rmin2 > 0))
                    {
                        logger.Log(string.Format("Error  : Nonbonded params ({0}): 3rd parameter (Rmin2) must be '> 0'", line));
                    }
                    double eps_14   = double.NaN;
                    double Rmin2_14 = double.NaN;

                    if (elem.parms.Length > 3)
                    {
                        HDebug.Assert(0 == elem.parms[3]);
                        eps_14   = elem.parms[4];
                        Rmin2_14 = elem.parms[5];    //Debug.Assert(Rmin2_14 > 0);
                        if (!(Rmin2_14 > 0))
                        {
                            logger.Log(string.Format("Error  : Nonbonded params ({0}): 6th parameter (Rmin2_14) must be '> 0'", line));
                        }
                    }
                    Nonbonded nonbonded = new Nonbonded(line, elem.types, epsilon, Rmin2, eps_14, Rmin2_14);

                    return(nonbonded);
                }
Example #5
0
        public static double FreqToPsec(double freq)
        {
            /// You asked how to convert 5 / cm to 7psec?
            ///
            /// This is actually a hack, invented by spectropscopists who excite particular modes using photons: they
            /// essentially equate the wavelength with a photon frequency of the same wavelength.
            ///
            /// so mulitply 5 / cm by speed of light, and invert:
            ///
            ///                5 / cm x 3 E10 cm/ s = 15 x E10/ sec.This inverst to .6666E-12 or ~7psec.
            if (FreqToPsec_selftest)
            {
                FreqToPsec_selftest = false;
                double t_freq = 5;
                double t_psec = FreqToPsec(t_freq);
                HDebug.Assert(Math.Abs(t_psec - 6.66666666666666666666) < 0.00000001);
            }

            /// psec
            /// = 1 / ( freq (1/cm) x 3*E10 (cm/s)) * 10^12
            /// = 1 / ( freq * 3 * 10^10) * 10^12
            /// = 1 / ( freq * 3 ) * 10^-10 * 10^12
            /// = 1 / (freq * 3) * 100
            /// = 100 / (freq * 3)
            double psec = 100.0 / (freq * 3.0);

            return(psec);
        }
Example #6
0
        public static Vector GetBFactor(Matrix hess, Vector mass, double?thresEigval, int?numZeroEigval, InfoPack extra = null)
        {
            HDebug.Assert(GetBFactorSelfTest());
            HDebug.Assert(hess.ColSize == hess.RowSize);
            HDebug.Assert(hess.ColSize % 3 == 0);
            HDebug.Assert(hess.ColSize / 3 == mass.Size);

            Matrix mhess = GetMassWeightedHess(hess, mass);

            // ei  : mass weighted eigenvector
            // wi2 : mass weighted eigenvalue
            // Ui' : ei*ei/wi2 = diag(eigenvec * 1/eigenval * eigenvec')
            Vector bfactor = GetBFactor(mhess, thresEigval, numZeroEigval, extra);

            // vi : mass free eigenvector
            // Ui := (Kb T) * (vi * vi / wi2)
            //     = (Kb T / mi) * ((vi*mi^-2) * (vi*mi^-2) / wi2)
            //     = (Kb T / mi) * (ei * ei / wi2)
            //     = (Kb T / mi) * Ui'
            int n = mass.Size;

            for (int i = 0; i < n; i++)
            {
                // assume Kb, T are constant (=1)
                bfactor[i] = bfactor[i] / mass[i];
            }

            return(bfactor);
        }
Example #7
0
            public static void Compute(Vector[] coords, ref double energy, ref Vector[] forces, ref MatrixByArr[,] hessian,
                                       double Kchi, int n, double delta, double[,] pwfrc = null, double[,] pwspr = null)
            {
                double Kpsi = Kchi;

                HDebug.Assert(n == 0);
                double psi0 = delta;

                double lenergy, force03, spring03;

                Compute(coords, out lenergy, out force03, out spring03, Kpsi, psi0);
                ///////////////////////////////////////////////////////////////////////////////
                // energy
                energy += lenergy;
                ///////////////////////////////////////////////////////////////////////////////
                // force
                if (forces != null)
                {
                    Vector frc0, frc3;
                    GetForceVector(coords[0], coords[3], force03, out frc0, out frc3);
                    forces[0] += frc0;
                    forces[3] += frc3;
                }
                ///////////////////////////////////////////////////////////////////////////////
                // hessian
                if (hessian != null)
                {
                    hessian[0, 3] += GetHessianBlock(coords[0], coords[3], spring03, force03);
                    hessian[3, 0] += GetHessianBlock(coords[3], coords[0], spring03, force03);
                }
            }
Example #8
0
        public void _SetBlock(int bc, int br, int br1, int br2, double[,] blk)
        {
            if (blk == null)
            {
                _SetBlockNull(bc, br, br1, br2);
                return;
            }

            if (bc == br)
            {
                diag[bc] = blk;
            }
            else
            {
                HDebug.Assert(br2 == br % layersize);
                HDebug.Assert(br1 == (br - br2) / layersize);
                double[][][,] offdiag_bc     = offdiag[bc];
                double[][,]   offdiag_bc_br1 = offdiag_bc[br1];

                if (offdiag_bc_br1 == null)
                {
                    HDebug.Assert(offdiag_count[bc][br1] == 0);
                    int br2_size = layersize; // rowblksize % layersize+1;
                    offdiag_bc_br1         = new double[br2_size][, ];
                    offdiag_bc[br1]        = offdiag_bc_br1;
                    offdiag_count[bc][br1] = 0;
                }

                if (offdiag_bc_br1[br2] == null)
                {
                    offdiag_count[bc][br1]++;
                }
                offdiag_bc_br1[br2] = blk;
            }
        }
Example #9
0
 public static void SelfTest()
 {
     if (HDebug.Selftest())
     {
         Random rand       = new Random(0);
         int    colblksize = 3;
         int    rowblksize = 10;
         int    layersize  = 3;
         var    mat        = Matrix.Zeros(colblksize * 3, rowblksize * 3);
         var    hess       = new HessMatrixLayeredArray(colblksize * 3, rowblksize * 3, layersize);
         HDebug.Assert(mat.ColSize == hess.ColSize);
         HDebug.Assert(mat.RowSize == hess.RowSize);
         int count = mat.ColSize * mat.RowSize * 10;
         for (int i = 0; i < count; i++)
         {
             int    c = rand.NextInt(0, colblksize * 3 - 1);
             int    r = rand.NextInt(0, rowblksize * 3 - 1);
             double v = rand.NextDouble();
             mat[c, r]  = v;
             hess[c, r] = v;
             HDebug.AssertTolerance(double.Epsilon, (mat - hess).ToArray());
         }
         for (int i = 0; i < 700; i++)
         {
             int    c = rand.NextInt(0, colblksize * 3 - 1);
             int    r = rand.NextInt(0, rowblksize * 3 - 1);
             double v = 0;
             mat[c, r]  = v;
             hess[c, r] = v;
             HDebug.AssertTolerance(double.Epsilon, (mat - hess).ToArray());
         }
     }
 }
Example #10
0
        public double[,] _GetBlock(int bc, int br, int br1, int br2)
        {
            if (bc == br)
            {
                double[,] diag_bc = diag[bc];
                return(diag_bc);
            }
            else
            {
                HDebug.Assert(br2 == br % layersize);
                HDebug.Assert(br1 == (br - br2) / layersize);
                double[][][,] offdiag_bc     = offdiag[bc];
                double[][,]   offdiag_bc_br1 = offdiag_bc[br1];

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

                double[,]   offdiag_bc_br1br2 = offdiag_bc_br1[br2];
                if (offdiag_bc_br1br2 == null)
                {
                    return(null);
                }

                return(offdiag_bc_br1br2);
            }
        }
Example #11
0
        public void _SetBlockNull(int bc, int br, int br1, int br2)
        {
            if (bc == br)
            {
                diag[bc] = null;
            }
            else
            {
                HDebug.Assert(br2 == br % layersize);
                HDebug.Assert(br1 == (br - br2) / layersize);
                double[][,]   offdiag_bc_br1 = offdiag[bc][br1];

                if (offdiag_bc_br1 == null)
                {
                    return;
                }

                double[,] offdiag_bc_br1br2 = offdiag_bc_br1[br2];
                if (offdiag_bc_br1br2 == null)
                {
                    return;
                }

                offdiag_bc_br1[br2] = null;
                offdiag_count[bc][br1]--;
            }
        }
Example #12
0
        public Mode GetNormalized()
        {
            if (HDebug.Selftest())
            {
                Mode lmode0 = new Mode {
                    eigval = 0.123, eigvec = new double[] { 1, 2, 3, 2, 3, 4, 3, 4, 5 }
                };
                Mode lmode1 = lmode0.GetNormalized();
                HDebug.Assert(lmode0.eigvec.Size == lmode1.eigvec.Size);
                MatrixByArr lhess0 = new double[lmode0.eigvec.Size, lmode0.eigvec.Size];
                MatrixByArr lhess1 = new double[lmode1.eigvec.Size, lmode1.eigvec.Size];
                lmode0.GetHessian(lhess0);
                lmode1.GetHessian(lhess1);
                double tolbase = Math.Max(lhess0.ToArray().HMax(), lhess1.ToArray().HMax());
                HDebug.AssertTolerance(tolbase * 0.00000001, lhess0 - lhess1);
            }

            /// H = sum vi' * di * vi
            ///   = sum (vi/|vi|)'  * (di |vi|^2) * (vi/|vi|)
            double leigvec = eigvec.Dist;
            Vector neigvec = eigvec.UnitVector();

            return(new Mode
            {
                th = th,
                eigval = eigval * leigvec * leigvec,
                eigvec = neigvec
            });
        }
Example #13
0
 public void GetHessian(MatrixByArr hess)
 {
     if (HDebug.Selftest())
     {
         Mode tmode = new Mode();
         tmode.eigval = 2;
         tmode.eigvec = new double[] { 1, 2, 3 };
         MatrixByArr thess0 = new double[, ] {
             { 2, 4, 6 }
             , { 4, 8, 12 }
             , { 6, 12, 18 }
         };
         MatrixByArr thess1 = new double[3, 3];
         tmode.GetHessian(thess1);
         HDebug.AssertTolerance(0.00000001, thess0 - thess1);
     }
     HDebug.Assert(hess.RowSize == eigvec.Size, hess.ColSize == eigvec.Size);
     //unsafe
     {
         double[] pvec = eigvec._data;
         {
             for (int c = 0; c < eigvec.Size; c++)
             {
                 for (int r = 0; r < eigvec.Size; r++)
                 {
                     hess[c, r] += eigval * pvec[c] * pvec[r];
                 }
             }
         }
     }
 }
Example #14
0
        public static IList <ATOM> SelectByRefNameResSeq <ATOM>(this IList <ATOM> atoms, IList <ATOM> refatoms)
            where ATOM : IAtom
        {
            //HDebug.Assert(atoms.Count == refatoms.Count);
            ATOM[] select = new ATOM[refatoms.Count];

            var resseq2atoms = atoms.ToDictionaryByResseq();

            for (int i = 0; i < select.Length; i++)
            {
                string      name   = refatoms[i].name;
                int         resseq = refatoms[i].resSeq;
                List <ATOM> latoms = resseq2atoms[resseq].SelectByName(name);
                if (latoms.Count >= 1)
                {
                    HDebug.Assert(latoms.Count == 1);
                    string refResName = refatoms[i].resName;
                    string lResName   = latoms[0].resName;
                    //HDebug.Assert(refResName.Trim() == lResName.Trim());
                    select[i] = latoms[0];
                }
                else
                {
                    HDebug.Assert(false);
                    select[i] = null;
                }
            }
            return(select);
        }
        public HessMatrix ReshapeByAtomImpl0(IList <int> idxatms, bool ignNegIdx)
        {
            HDebug.Assert(idxatms.Count == idxatms.HUnion().Length); // check no-duplication of indexes
            HessMatrix reshape = Zeros(idxatms.Count * 3, idxatms.Count * 3);

            for (int nbc = 0; nbc < idxatms.Count; nbc++)
            {
                for (int nbr = 0; nbr < idxatms.Count; nbr++)
                {
                    int bc = idxatms[nbc]; if (ignNegIdx && bc < 0)
                    {
                        continue;
                    }
                    int br = idxatms[nbr]; if (ignNegIdx && br < 0)
                    {
                        continue;
                    }
                    if (HasBlock(bc, br) == false)
                    {
                        if (HDebug.IsDebuggerAttached)
                        {
                            if (GetBlock(bc, br) != null)
                            {
                                HDebug.AssertTolerance(0, GetBlock(bc, br).ToArray());
                            }
                        }
                        continue;
                    }
                    MatrixByArr blkrc = GetBlock(bc, br);
                    reshape.SetBlock(nbc, nbr, blkrc.CloneT());
                }
            }
            return(reshape);
        }
Example #16
0
        //////////////////////////////////////////////////////////////////////////////////////////////////
        // HessMatrix
        HessMatrixLayeredArray(int colsize, int rowsize, int layersize)
        {
            if (HDebug.Selftest())
            {
                SelfTest();
            }

            HDebug.Assert(colsize % 3 == 0);
            HDebug.Assert(rowsize % 3 == 0);
            this.colblksize = colsize / 3;
            this.rowblksize = rowsize / 3;
            this.layersize  = layersize;

            int br2_size = rowblksize % layersize;
            int br1_size = (rowblksize - br2_size) / layersize + 1;

            diag          = new List <double    [, ]>(colblksize);
            offdiag       = new List <double[][][, ]>(colblksize);
            offdiag_count = new List <int   []>(colblksize);
            for (int bc = 0; bc < colblksize; bc++)
            {
                diag.Add(null);
                offdiag.Add(new double[br1_size][][, ]);
                offdiag_count.Add(new int   [br1_size]);
                for (int br1 = 0; br1 < br1_size; br1++)
                {
                    offdiag      [bc][br1] = null;
                    offdiag_count[bc][br1] = 0;
                }
            }
        }
Example #17
0
        public static Vector GetBFactor(Matrix hess, Vector mass
                                        , int numIgnoreSmallEigval = 6      // the number to ignore the eigenvalues
                                        , bool zeroForNegEigval    = true   // set zero for the negative eigen values
                                        )
        {
            HDebug.Assert(GetBFactorSelfTest());
            HDebug.Assert(hess.ColSize == hess.RowSize);
            HDebug.Assert(hess.ColSize % 3 == 0);
            HDebug.Assert(hess.ColSize / 3 == mass.Size);

            Matrix mhess = GetMassWeightedHess(hess, mass);

            // ei  : mass weighted eigenvector
            // wi2 : mass weighted eigenvalue
            // Ui' : ei*ei/wi2 = diag(eigenvec * 1/eigenval * eigenvec')
            Vector bfactor = GetBFactor(mhess
                                        , numIgnoreSmallEigval: numIgnoreSmallEigval
                                        , zeroForNegEigval: zeroForNegEigval
                                        );

            // vi : mass free eigenvector
            // Ui := (Kb T) * (vi * vi / wi2)
            //     = (Kb T / mi) * ((vi*mi^-2) * (vi*mi^-2) / wi2)
            //     = (Kb T / mi) * (ei * ei / wi2)
            //     = (Kb T / mi) * Ui'
            int n = mass.Size;

            for (int i = 0; i < n; i++)
            {
                // assume Kb, T are constant (=1)
                bfactor[i] = bfactor[i] / mass[i];
            }

            return(bfactor);
        }
Example #18
0
            public static void GetEnergies(AlignData data1, IList <Vector> coords2
                                           , out double pot_rmsd, out double pot_enrg, out double pot_enrg_full, out double pot_enrg_anisou
                                           , Pdb pdb2           = null
                                           , string pdb2outpath = null
                                           )
            {
                Anisou[] anisous1;// = data1.GetAnisous();
                double[] bfactor1;
                {
                    List <Mode> modes = new List <Mode>(data1.GetModes());
                    for (int i = 0; i < 6; i++)
                    {
                        modes.RemoveAt(0);
                    }
                    bfactor1 = HBioinfo.GetBFactor(modes.ToArray(), data1.masses);
                    HDebug.Assert(data1.size == bfactor1.Length);
                    anisous1 = Anisou.FromBFactor(bfactor1, scale: 10000 * 1000);
                }
                //Trans3 trans = MinAnisou.GetTrans(data1.coords, anisous1, coords2);
                Trans3        trans        = GetTrans(data1.coords, bfactor1, coords2);
                List <Vector> coords2trans = new List <Vector>(trans.GetTransformed(coords2));

                if (pdb2 != null && pdb2outpath != null)
                {
                    data1.pdb.ToFile(pdb2outpath, coords2trans, anisous: anisous1.GetUs());
                }

                pot_rmsd        = data1.GetRmsdFrom(coords2trans);
                pot_enrg        = data1.GetEnergyFromDiag(coords2trans);
                pot_enrg_full   = data1.GetEnergyFromFull(coords2trans);
                pot_enrg_anisou = data1.GetEnergyFromAnisou(coords2trans);
            }
Example #19
0
        public static Vector GetBFactor(Matrix hess, double?thresEigval, int?numZeroEigval, InfoPack extra = null)
        {
            HDebug.Assert(GetBFactorSelfTest());

            int n = hess.ColSize / 3;

            HDebug.Assert(n * 3 == hess.ColSize);

            HDebug.Assert(hess.RowSize == hess.ColSize);
            InfoPack lextra  = new InfoPack();
            Matrix   invhess = NumericSolver.InvEig(hess, thresEigval, numZeroEigval, lextra);
            //Matrix invhess = NumericSolver.InvEig(hess, 0.00000001, null, lextra);

            Vector bfactor = new double[n];

            for (int i = 0; i < n; i++)
            {
                bfactor[i] = invhess[i * 3 + 0, i *3 + 0] + invhess[i * 3 + 1, i *3 + 1] + invhess[i * 3 + 2, i *3 + 2];
            }

            if (extra != null)
            {
                extra["num_zero_eigvals"] = lextra["num_zero_eigvals"];
                extra["eigenvalues"]      = lextra["eigenvalues"];
            }

            return(bfactor);
        }
Example #20
0
        public override void UpdateMul(double other)
        {
            double[,] debug_prv = null;
            if (HDebug.IsDebuggerAttached)
            {
                debug_prv = this.ToArray();
            }

            foreach (var bc_br_bval in EnumBlocks())
            {
                int         bc   = bc_br_bval.Item1;
                int         br   = bc_br_bval.Item2;
                MatrixByArr bmat = bc_br_bval.Item3;
                SetBlock(bc, br, bmat * other);
            }

            if (HDebug.IsDebuggerAttached)
            {
                double[,] debug_now = this.ToArray();
                for (int c = 0; c < ColSize; c++)
                {
                    for (int r = 0; r < RowSize; r++)
                    {
                        double val0 = debug_now[c, r];
                        double val1 = other * debug_prv[c, r];
                        HDebug.Assert(val0 == val1);
                    }
                }
            }
        }
Example #21
0
 public Nonbonded FindNonbonded(string type0, ITextLogger logger)
 {
     if (_FindNonbonded.ContainsKey(type0) == false)
     {
         Nonbonded found = null;
         foreach (Nonbonded nonbonded in nonbondeds)
         {
             if (nonbonded.types[0] == type0)
             {
                 if (found != null)
                 {
                     bool writelog = ((found.epsilon != nonbonded.epsilon) ||
                                      (found.Rmin2 != nonbonded.Rmin2) ||
                                      (found.eps_14 != nonbonded.eps_14) ||
                                      (found.Rmin2_14 != nonbonded.Rmin2_14));
                     if (writelog)
                     {
                         logger.Log(string.Format("Nonbonded params of {0}-(eps {1}, rmin2 {2}, esp_14 {3}, rmin2_14 {4}) is replaced to ({5}, {6}, {7}, {8})",
                                                  type0, found.epsilon, found.Rmin2, found.eps_14, found.Rmin2_14,
                                                  nonbonded.epsilon, nonbonded.Rmin2, nonbonded.eps_14, nonbonded.Rmin2_14));
                     }
                 }
                 found = nonbonded;
             }
         }
         HDebug.Assert(found != null);
         HDebug.AssertIf(double.IsNaN(found.Rmin2) == false, found.Rmin2 > 0);
         HDebug.AssertIf(double.IsNaN(found.Rmin2_14) == false, found.Rmin2_14 > 0);
         _FindNonbonded.Add(type0, found);
     }
     return(_FindNonbonded[type0]);
 }
            ///////////////////////////
            public void Add2(int id0, int id1, Vector[] lcoords, Vector[] lforces)
            {
                if (forceij == null)
                {
                    return;
                }
                HDebug.Assert(lcoords.Length == 2);
                HDebug.Assert(lforces.Length == 2);
                if ((lforces[0].Dist2 == 0) && (lforces[1].Dist2 == 0))
                {
                    return;
                }
                if (HDebug.IsDebuggerAttached)
                {
                    // check net force
                    HDebug.AssertTolerance(0.0001, lforces[0] + lforces[1]);

                    Vector diff = lcoords[1] - lcoords[0];
                    HDebug.AssertTolerance(0.0001, LinAlg.Angle(diff, lforces[0]));
                    HDebug.AssertTolerance(0.0001, LinAlg.Angle(diff, lforces[1]));
                    HDebug.AssertTolerance(0.0001, LinAlg.Angle(lforces[0], lforces[1]));
                }

                this[id0, id1] += lforces[0];
                this[id1, id0] += lforces[1];
            }
Example #23
0
        public static double GetHessTraceSpringBwAtoms(this Matrix hess, int atomi, int atomj)
        {
            HDebug.Assert(hess.ColSize == hess.RowSize);
            HDebug.Assert(hess.ColSize % 3 == 0);
            HDebug.Assert(hess.RowSize % 3 == 0);
            HDebug.Assert(atomi < hess.ColSize / 3);
            HDebug.Assert(atomj < hess.ColSize / 3);

            double bibj_trace = 0;
            {
                bibj_trace += hess[atomi * 3 + 0, atomj *3 + 0];
                bibj_trace += hess[atomi * 3 + 1, atomj *3 + 1];
                bibj_trace += hess[atomi * 3 + 2, atomj *3 + 2];
            }
            double bjbi_trace = 0;
            {
                bjbi_trace += hess[atomj * 3 + 0, atomi *3 + 0];
                bjbi_trace += hess[atomj * 3 + 1, atomi *3 + 1];
                bjbi_trace += hess[atomj * 3 + 2, atomi *3 + 2];
            }

            double threshold = 0.00001;

            HDebug.Assert(Math.Abs(bibj_trace - bjbi_trace) < threshold);

            double spr_ij = -1 * (bibj_trace + bjbi_trace) / 2;

            return(spr_ij);
        }
            public static Tuple <int[], int[][]> GetIdxKeepListRemv_ResiWise(Universe.Atom[] atoms, Vector[] coords)
            {
                List <Universe.Atom[]> resis = atoms.GroupByResidue();
                var resi_ca_others           = resis.HSplitByNames("CA").HToTuple();

                Universe.Atom[]   cas     = new Universe.Atom[resi_ca_others.Length];
                Universe.Atom[][] otherss = new Universe.Atom[resi_ca_others.Length][];
                for (int i = 0; i < resi_ca_others.Length; i++)
                {
                    var ca_others = resi_ca_others[i];
                    HDebug.Assert(ca_others.Item1.Length == 1);
                    cas    [i] = ca_others.Item1[0];
                    otherss[i] = ca_others.Item2;
                    if (i >= 1)
                    {
                        HDebug.Assert(cas[i - 1].ResiduePdbId < cas[i].ResiduePdbId);
                    }
                }

                return(new Tuple <int[], int[][]>
                       (
                           cas.ListIDs(),
                           otherss.ListIDs()
                       ));
            }
Example #25
0
            public static List <T> FindTypes <T>(IList <T> list, params string[] query)
                where T : IHKeyStrings
            {
                List <T> founds         = null;
                int      founds_count_X = int.MaxValue;

                foreach (T item in list)
                {
                    int count_X = FindType(item.GetKeyStrings(), query);
                    if (count_X == -1)
                    {
                        continue;
                    }
                    if (count_X > founds_count_X)
                    {
                        continue;
                    }
                    if (count_X < founds_count_X)
                    {
                        founds_count_X = count_X;
                        founds         = new List <T>();
                    }
                    founds.Add(item);
                }
                HDebug.Assert(founds != null);
                return(founds);
            }
Example #26
0
        public static Matrix ModesCorr(this IList <Mode> modes)
        {
            /// HBioinfo.Corr.Dmitry07.pdf
            ///     http://www.cell.com/structure/abstract/S0969-2126(07)00029-9
            ///     http://www.ncbi.nlm.nih.gov/pubmed/17292835
            ///     Structure. 2007 Feb;15(2):169-77.
            ///     Protein structural variation in computational models and crystallographic data.
            ///     Kondrashov DA, Van Wynsberghe AW, Bannen RM, Cui Q, Phillips GN Jr.
            ///     DOI: http://dx.doi.org/10.1016/j.str.2006.12.006
            double kB  = 1;
            double T   = 300;
            Matrix cov = ModesCov(modes, kB, T);

            HDebug.Assert(cov.ColSize == cov.RowSize);
            int n = cov.ColSize;

            Vector sqrt_cov_ii = new double[n];

            for (int i = 0; i < n; i++)
            {
                sqrt_cov_ii[i] = Math.Sqrt(cov[i, i]);
            }

            Matrix corr = Matrix.Zeros(n, n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    corr[i, j] = cov[i, j] / (sqrt_cov_ii[i] * sqrt_cov_ii[j]);
                }
            }
            return(corr);
        }
Example #27
0
        public static Tuple <Mode[], Mode[]> SeparateTolerantsByCountSigned(this Mode[] modes, int zerocount)
        {
            double[] eigvals     = modes.ListEigval().ToArray();
            double[] srt_eigvals = eigvals.HSort().ToArray();
            int   [] idxsrt      = eigvals.HIdxSorted();

            int[] idxZero    = idxsrt.HSelectFromCount(0, zerocount).ToArray();
            int[] idxNonzero = idxsrt.HSelectFromCount(zerocount, modes.Length - zerocount).ToArray();
            if (HDebug.IsDebuggerAttached)
            {
                HDebug.Assert(modes.Length == idxZero.Length + idxNonzero.Length);
                HashSet <int> setIdx = new HashSet <int>();
                foreach (int idx in idxZero)
                {
                    setIdx.Add(idx);
                }
                foreach (int idx in idxNonzero)
                {
                    setIdx.Add(idx);
                }
                HDebug.Assert(modes.Length == setIdx.Count);
            }

            Mode[] modesNonzero = modes.HSelectByIndex(idxNonzero).ToArray();
            Mode[] modesZero    = modes.HSelectByIndex(idxZero).ToArray();

            return(new Tuple <Mode[], Mode[]>(modesNonzero, modesZero));
        }
        ///////////////////////////////////////////////////////////////////////
        // MakeNearZeroBlockAsZero
        public int MakeNearZeroBlockAsZero(double thres_absmax)
        {
            double min_absmax_bval = double.MaxValue;
            List <Tuple <int, int> > lstIdxToMakeZero = new List <Tuple <int, int> >();

            foreach (ValueTuple <int, int, MatrixByArr> bc_br_bval in EnumBlocks())
            {
                int    bc          = bc_br_bval.Item1;
                int    br          = bc_br_bval.Item2;
                var    bval        = bc_br_bval.Item3;
                double absmax_bval = bval.HAbsMax();
                min_absmax_bval = Math.Min(min_absmax_bval, absmax_bval);
                if (absmax_bval < thres_absmax)
                {
                    lstIdxToMakeZero.Add(new Tuple <int, int>(bc, br));
                }
            }
            foreach (var bc_br in lstIdxToMakeZero)
            {
                int bc = bc_br.Item1;
                int br = bc_br.Item2;
                HDebug.Assert(GetBlock(bc, br).HAbsMax() < thres_absmax);
                SetBlock(bc, br, null);
            }
            return(lstIdxToMakeZero.Count);
        }
Example #29
0
 public new void Add(Dihedral dihedral)
 {
     HDebug.Assert(dihedral.atoms.Length == 4);
     HDebug.Assert(dihedral.atoms[0].ID < dihedral.atoms[3].ID);
     //HDebug.Assert(this.Contains(dihedral) == false); //
     base.Add(dihedral);
 }
Example #30
0
        public static Matrix GetMassWeightedHessGnm(Matrix hess, Vector mass)
        {
            if (HDebug.Selftest())
            //#region selftest
            {
            }
            //#endregion

            HDebug.Assert(hess.ColSize == hess.RowSize, hess.RowSize == mass.Size);
            Vector mass05 = mass.ToArray().HSqrt();

            // mass weighted hessian
            // MH = M^(-1/2) * H * M^(-1/2)
            // MH_ij = H_IJ * sqrt(M[i] * M[j])
            Matrix mwhess = hess.Clone();

            {
                // mass weighted block hessian
                for (int c = 0; c < hess.ColSize; c++)
                {
                    for (int r = 0; r < hess.RowSize; r++)
                    {
                        mwhess[c, r] = hess[c, r] / (mass05[c] * mass05[r]);
                    }
                }
            }

            return(mwhess);
        }