Esempio n. 1
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);
            }
Esempio n. 2
0
            public static void Align(IList <Vector> C1
                                     , IList <Anisou> anisou1
                                     , ref List <Vector> C2
                                     , HPack <List <Vector> > optMoveC2 = null
                                     , HPack <Trans3> outTrans          = null
                                     , HPack <double> optEnergy         = null
                                     )
            {
                Trans3 trans = GetTrans(C1, anisou1, C2, optEnergy: optEnergy);

                Vector[] nC2 = trans.GetTransformed(C2).ToArray();
                if (optMoveC2 != null)
                {
                    optMoveC2.value = new List <Vector>(nC2.Length);
                    for (int i = 0; i < nC2.Length; i++)
                    {
                        optMoveC2.value.Add(nC2[i] - C2[i]);
                    }
                }
                if (outTrans != null)
                {
                    outTrans.value = trans;
                }
                C2 = new List <Vector>(nC2);
            }
Esempio n. 3
0
            public static void Align(IList <Vector> C1
                                     , IList <Anisou> anisou1
                                     , ref IList <Vector> C2
                                     , ref IList <Anisou> anisou2
                                     , HPack <Trans3> outTrans  = null
                                     , HPack <double> optEnergy = null
                                     )
            {
                Trans3 trans = GetTrans(C1, anisou1, C2, anisou2, optEnergy: optEnergy);

                C2      = new List <Vector>(C2);
                anisou2 = new List <Anisou>(anisou2);

                Vector[] nC2 = trans.GetTransformed(C2).ToArray();
                for (int i = 0; i < C2.Count; i++)
                {
                    Vector p2i  = C2[i];
                    Anisou a2i  = anisou2[i];
                    Vector np2i = trans.DoTransform(p2i);
                    Anisou na2i = a2i.Clone();
                    na2i.eigvecs[0] = (trans.DoTransform(p2i + a2i.eigvecs[0]) - np2i).UnitVector();
                    na2i.eigvecs[1] = (trans.DoTransform(p2i + a2i.eigvecs[1]) - np2i).UnitVector();
                    na2i.eigvecs[2] = (trans.DoTransform(p2i + a2i.eigvecs[2]) - np2i).UnitVector();
                    C2[i]           = np2i;
                    anisou2[i]      = na2i;
                }
                if (outTrans != null)
                {
                    outTrans.value = trans;
                }
            }
Esempio n. 4
0
            public static void Align(IList <Vector> C1
                                     , ref List <Vector> C2
                                     , HPack <List <Vector> > optMoveC2 = null
                                     , HPack <Trans3> outTrans          = null
                                     , HPack <double> optRmsd           = null
                                     )
            {
                Trans3 trans = GetTrans(C1, C2);

                Vector[] nC2 = trans.GetTransformed(C2).ToArray();
                if (optMoveC2 != null)
                {
                    optMoveC2.value = new List <Vector>(nC2.Length);
                    for (int i = 0; i < nC2.Length; i++)
                    {
                        optMoveC2.value[i] = nC2[i] - C2[i];
                    }
                }
                if (optRmsd != null)
                {
                    optRmsd.value = 0;
                    for (int i = 0; i < nC2.Length; i++)
                    {
                        optRmsd.value += (nC2[i] - C1[i]).Dist2;
                    }
                    optRmsd.value /= nC2.Length;
                }
                C2 = new List <Vector>(nC2);
                if (outTrans != null)
                {
                    outTrans.value = trans.Clone();
                }
            }
Esempio n. 5
0
            public static Trans3 GetTrans(IList <Vector> C1, IList <Anisou> anisou1, IList <Vector> C2, HPack <double> optEnergy = null)
            {
                int size = C1.Count;

                HDebug.Assert(size == C1.Count, size == C2.Count);

                Vector[] C1s   = C1.ToArray().Clone(3);
                Vector[] C2s   = C2.ToArray().Clone(3);
                double[] W1s   = new double[size * 3];
                double[] enrgs = new double[size * 3];
                for (int i = 0; i < size; i++)
                {
                    HDebug.Assert(anisou1[i].eigvals[0] >= 0); W1s[i * 3 + 0] = (anisou1[i].eigvals[0] <= 0) ? 0 : 1 / anisou1[i].eigvals[0];
                    HDebug.Assert(anisou1[i].eigvals[1] >= 0); W1s[i * 3 + 1] = (anisou1[i].eigvals[1] <= 0) ? 0 : 1 / anisou1[i].eigvals[1];
                    HDebug.Assert(anisou1[i].eigvals[2] >= 0); W1s[i * 3 + 2] = (anisou1[i].eigvals[2] <= 0) ? 0 : 1 / anisou1[i].eigvals[2];
                    enrgs[i * 3 + 0] = enrgs[i * 3 + 1] = enrgs[i * 3 + 2] = double.NaN;
                }

                //Trans3 trans = ICP3.OptimalTransform(C2, C1);
                Trans3 trans    = new Trans3(new double[] { 0, 0, 0 }, 1, Quaternion.UnitRotation);// = transICP3.OptimalTransformWeighted(C2, C1, W1s);
                int    iter     = 0;
                int    iter_max = 1000;

                //double enrg = double.NaN;
                while (iter < iter_max)
                {
                    iter++;
                    Vector[] C2sUpdated = trans.GetTransformed(C2s);

                    //for(int i=0; i<size; i++)
                    System.Threading.Tasks.Parallel.For(0, size, delegate(int i)
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            Vector planeNormal = anisou1[i].axes[j];
                            Vector planeBase   = C1[i];
                            Vector query       = C2sUpdated[i * 3 + j];
                            Vector closest     = query - LinAlg.DotProd(planeNormal, query - planeBase) * planeNormal;
                            HDebug.AssertTolerance(0.00001, LinAlg.DotProd(closest - planeBase, planeNormal));
                            C1s[i * 3 + j]   = closest;
                            enrgs[i * 3 + j] = W1s[i * 3 + j] * (query - closest).Dist2;
                        }
                    });
                    Trans3 dtrans = ICP3.OptimalTransformWeighted(C2sUpdated, C1s, W1s);
                    trans = Trans3.AppendTrans(trans, dtrans);
                    double max_dtrans_matrix = (dtrans.TransformMatrix - LinAlg.Eye(4)).ToArray().HAbs().HMax();
                    if (max_dtrans_matrix < 0.0000001)
                    {
                        break;
                    }
                }

                if (optEnergy != null)
                {
                    optEnergy.value = enrgs.Sum() / size;
                }

                return(trans);
            }
Esempio n. 6
0
            public static void Align(List <Vector> coords1
                                     , ref List <Vector> coords2
                                     , int maxiteration = int.MaxValue
                                     , HPack <List <Trans3[]> > outTrajTrans = null
                                     )
            {
                List <Vector>[] ensemble = new List <Vector>[]
                {
                    coords1.HClone(),
           coords2.HClone()
                };
                Align(ref ensemble, maxiteration: maxiteration, outTrajTrans: outTrajTrans);
                Trans3 trans = MinRMSD.GetTrans(coords1, ensemble[0]);

                Vector[] ensemble0 = trans.GetTransformed(ensemble[0]).ToArray();
                Vector[] ensemble1 = trans.GetTransformed(ensemble[1]).ToArray();
                coords2 = new List <Vector>(ensemble1);
            }
Esempio n. 7
0
            public static void Align(List <Pdb.Atom> atoms1, ref List <Pdb.Atom> atoms2)
            {
                PdbStatic.SelectCommonAtoms(ref atoms1, ref atoms2);
                List <Vector> coords1 = atoms1.ListCoord();
                List <Vector> coords2 = atoms2.ListCoord();

                Trans3        trans    = GetTrans(coords1, coords2);
                List <Vector> coords2a = new List <Vector>(trans.GetTransformed(coords2));

                atoms2 = atoms2.CloneByUpdateCoord(coords2a);
            }
Esempio n. 8
0
            public static void Align(IList <Vector> C1
                                     , ref List <Vector> C2
                                     , IList <double> weight
                                     , HPack <Trans3> outTrans = null
                                     )
            {
                Trans3 trans = GetTrans(C1, C2, weight);

                Vector[] nC2 = trans.GetTransformed(C2).ToArray();
                C2 = new List <Vector>(nC2);
                if (outTrans != null)
                {
                    outTrans.value = trans.Clone();
                }
            }
Esempio n. 9
0
            public static Trans3 GetTrans(IList <Vector> C1, IList <Vector> C2, HPack <int> optIter = null, HPack <List <double> > optListWeightSum = null)
            {
                HDebug.Assert(C1.Count == C2.Count);
                int size = C1.Count;

                if (optIter != null)
                {
                    optIter.value = 0;
                }

                Trans3 trans0 = new Trans3(new double[3], 1, Quaternion.UnitRotation);
                Trans3 trans1 = Geometry.AlignPointPoint.MinRMSD.GetTrans(C2, C1);

                if (optListWeightSum != null)
                {
                    optListWeightSum.value = new List <double>();
                }
                while ((trans0.TransformMatrix - trans1.TransformMatrix).ToArray().HAbs().HMax() > 0.00000001)
                {
                    if (optIter != null)
                    {
                        optIter.value++;
                    }
                    Vector[] C2trans = trans1.GetTransformed(C2).ToArray();

                    double[] weight = new double[size];
                    double[] dist2s = new double[size];
                    for (int i = 0; i < size; i++)
                    {
                        double dist2 = (C1[i] - C2trans[i]).Dist2;
                        dist2     = HMath.Between(0.001, dist2, double.PositiveInfinity);
                        weight[i] = 1 / dist2;
                        dist2s[i] = dist2;
                    }
                    if (optListWeightSum != null)
                    {
                        optListWeightSum.value.Add(weight.Sum());
                    }

                    trans0 = trans1;
                    trans1 = Geometry.AlignPointPoint.MinRMSD.GetTrans(C2, C1, weight);
                }

                return(trans1);
            }
Esempio n. 10
0
            public static void GetEnergies(AlignData data1, IList <Vector> coords2
                                           , out double geo_rmsd, out double geo_enrg, out double geo_enrg_full, out double geo_enrg_anisou
                                           , Pdb pdb2, string pdb2outpath
                                           )
            {
                Trans3        trans        = GetTrans(data1.coords, coords2);
                List <Vector> coords2trans = new List <Vector>(trans.GetTransformed(coords2).ToArray());

                if (pdb2 != null && pdb2outpath != null)
                {
                    pdb2.ToFile(pdb2outpath, coords2);
                }

                geo_rmsd        = data1.GetRmsdFrom(coords2trans);
                geo_enrg        = data1.GetEnergyFromDiag(coords2trans);
                geo_enrg_full   = data1.GetEnergyFromFull(coords2trans);
                geo_enrg_anisou = data1.GetEnergyFromAnisou(coords2trans);
            }
Esempio n. 11
0
            //public static Trans3 GetTrans(IList<Vector> C1, Matrix[] anisou1, IList<Vector> C2)
            //{
            //    int size = C1.Count;
            //    Tuple<Vector[], double[]>[] eigs1 = new Tuple<Vector[], double[]>[size];
            //    {
            //        for(int i=0; i<size; i++)
            //        {
            //            Vector[] eigvec;
            //            double[] eigval;
            //            Debug.Verify(NumericSolver.Eig(anisou1[i], out eigvec, out eigval));
            //            {   // normalize eigval and eigvec
            //                double l;
            //                l = eigvec[0].Dist; eigvec[0] /= l; eigval[0] *= (l*l); eigval[0] = Math.Pow(eigval[0], 2); Debug.Assert(eigval[0] >= 0);
            //                l = eigvec[1].Dist; eigvec[1] /= l; eigval[1] *= (l*l); eigval[1] = Math.Pow(eigval[1], 2); Debug.Assert(eigval[1] >= 0);
            //                l = eigvec[2].Dist; eigvec[2] /= l; eigval[2] *= (l*l); eigval[2] = Math.Pow(eigval[2], 2); Debug.Assert(eigval[2] >= 0);
            //            }
            //            {
            //                eigval[0] = 1 / eigval[0];
            //                eigval[1] = 1 / eigval[1];
            //                eigval[2] = 1 / eigval[2];
            //            }
            //            eigs1[i] = new Tuple<Vector[], double[]>(eigvec, eigval);
            //        }
            //    }
            //    return GetTrans(C1, eigs1, C2);
            //}

            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();
                Trans3        trans        = GetTrans(data1.coords, anisous1, coords2);
                List <Vector> coords2trans = new List <Vector>(trans.GetTransformed(coords2));

                if (pdb2 != null && pdb2outpath != null)
                {
                    pdb2.ToFile(pdb2outpath, coords2trans);
                }
                //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);
            }
Esempio n. 12
0
            public static Trans3 GetTrans(IList <Vector> C1
                                          , IList <Vector> C2
                                          , IList <double> weight
                                          //, Pack<List<Vector>> C2new = null
                                          )
            {
                if (HDebug.Selftest())
                {
                    double[] tweight = new double[weight.Count];
                    for (int i = 0; i < tweight.Length; i++)
                    {
                        tweight[i] = 0.2;
                    }
                    Trans3 ttrans0 = GetTrans(C1, C2, tweight);
                    Trans3 ttrans1 = MinRMSD.GetTrans(C1, C2);
                    HDebug.AssertTolerance(0.0001, ttrans0.ds - ttrans1.ds);
                    HDebug.AssertTolerance(0.0001, ttrans0.dt - ttrans1.dt);
                    HDebug.AssertTolerance(0.0001, new Vector(ttrans0.dr.ToArray()) - ttrans1.dr.ToArray());
                    HDebug.AssertTolerance(0.0001, (ttrans0.TransformMatrix - ttrans1.TransformMatrix));
                }
                HDebug.Assert(C1.Count == C2.Count);
                HDebug.Assert(C1.Count == weight.Count);
                //Trans3 trans = ICP3.OptimalTransform(C2, C1);
                Trans3 trans = ICP3.OptimalTransformWeighted(C2, C1, weight);

                if (HDebug.IsDebuggerAttached)
                {
                    Vector[] C2updated = trans.GetTransformed(C2).ToArray();
                    double   RMSD0     = 0;
                    double   RMSD1     = 0;
                    for (int i = 0; i < C1.Count; i++)
                    {
                        RMSD0 += (C1[i] - C2[i]).Dist2;
                        RMSD1 += (C1[i] - C2updated[i]).Dist2;
                    }
                    //Debug.AssertTolerance(0.00000001, Math.Abs(RMSD1 - RMSD0));
                }
                return(trans);
            }
Esempio n. 13
0
            public static Trans3 GetTrans(IList <Vector> C1
                                          , IList <Vector> C2 // ref
                                                              //, Pack<List<Vector>> C2new = null
                                          )
            {
                HDebug.Assert(C1.Count == C2.Count);
                Trans3 trans = ICP3.OptimalTransform(C2, C1);

                if (HDebug.IsDebuggerAttached)
                {
                    Vector[] C2updated = trans.GetTransformed(C2).ToArray();
                    double   RMSD0     = 0;
                    double   RMSD1     = 0;
                    for (int i = 0; i < C1.Count; i++)
                    {
                        RMSD0 += (C1[i] - C2[i]).Dist2;
                        RMSD1 += (C1[i] - C2updated[i]).Dist2;
                    }
                    RMSD0 /= C1.Count;
                    RMSD1 /= C1.Count;
                    //HDebug.AssertTolerance(0.00000001, Math.Abs(RMSD1 - RMSD0));
                }
                return(trans);
            }
Esempio n. 14
0
            public static void Align(List <Vector> coords1, ref List <Vector> coords2)
            {
                Trans3 trans = GetTrans(coords1, coords2);

                coords2 = new List <Vector>(trans.GetTransformed(coords2));
            }
Esempio n. 15
0
            public static Trans3 GetTrans(IList <Vector> C1, IList <Vector> C2, HPack <int> optIter = null, HPack <List <double> > optListWeightSum = null)
            {
                HDebug.Assert(false);
                HDebug.Assert(C1.Count == C2.Count);
                int size = C1.Count;

                if (optIter != null)
                {
                    optIter.value = 0;
                }

                int    iter   = 0;
                Trans3 trans0 = new Trans3(new double[3], 1, Quaternion.UnitRotation);
                Trans3 trans1 = Geometry.AlignPointPoint.MinRMSD.GetTrans(C2, C1);

                if (optListWeightSum != null)
                {
                    optListWeightSum.value = new List <double>();
                }
                double maxdist = 0;

                for (int i = 0; i < size; i++)
                {
                    Vector diff = C1[i] - trans1.DoTransform(C2[i]); maxdist = Math.Max(maxdist, diff.Dist);
                }
                while ((trans0.TransformMatrix - trans1.TransformMatrix).ToArray().HAbs().HMax() > 0.00000001)
                {
                    iter++;
                    if (optIter != null)
                    {
                        optIter.value++;
                    }
                    Vector[] C2trans = trans1.GetTransformed(C2).ToArray();

                    Vector dist2s = new double[size];
                    Vector dists  = new double[size];
                    for (int i = 0; i < size; i++)
                    {
                        double dist2 = (C1[i] - C2trans[i]).Dist2;
                        dist2     = HMath.Between(0.00001, dist2, double.PositiveInfinity);
                        dist2s[i] = dist2;
                        dists[i]  = Math.Sqrt(dist2);
                    }
                    //int[] idxsort = dist2s.ToArray().IdxSorted().Reverse();
                    Vector weight = dists.Clone() / dists.ToArray().Max();
                    for (int i = 0; i < size; i++)
                    {
                        double w = weight[i];
                        //w = (w*2-1)*4;
                        w         = (w - 1) * 4;
                        w         = 1 / (1 + Math.Exp(-1 * w));
                        weight[i] = w;
                    }
                    //weight[idxsort[i]] = (i <= 6) ? 1 : 0;
                    if (optListWeightSum != null)
                    {
                        optListWeightSum.value.Add(weight.Sum());
                    }

                    trans0 = trans1;
                    trans1 = Geometry.AlignPointPoint.MinRMSD.GetTrans(C2, C1, weight.ToArray());

                    maxdist = 0;
                    for (int i = 0; i < size; i++)
                    {
                        Vector diff = C1[i] - trans1.DoTransform(C2[i]); maxdist = Math.Max(maxdist, diff.Dist);
                    }
                }

                return(trans1);
            }