コード例 #1
0
        // see SimulationResult.CalculateBestFitRotation
        public void CalculateBestFitRotation(Molecule mref, Molecule m)
        {
            int NAtomstotal = Math.Min(mref.NAtoms, m.NAtoms);

            // rotation
            Mapack.Matrix Rxt = new Mapack.Matrix(NAtomstotal, 3);
            Mapack.Matrix Ry  = new Mapack.Matrix(3, NAtomstotal);
            for (int j = 0; j < NAtomstotal; j++)
            {
                Rxt[j, 0] = m.XLocal[j]; Rxt[j, 1] = m.YLocal[j]; Rxt[j, 2] = m.ZLocal[j];
                Ry[0, j]  = mref.XLocal[j]; Ry[1, j] = mref.YLocal[j]; Ry[2, j] = mref.ZLocal[j];
            }
            // Kabsch solution
            Mapack.Matrix R = Ry * Rxt;
            Mapack.SingularValueDecomposition svdR = new Mapack.SingularValueDecomposition(R);
            Mapack.Matrix V  = svdR.VMatrix;
            Mapack.Matrix rS = new Mapack.Matrix(3, 3);
            rS[0, 0] = 1.0 / svdR.Diagonal[0];
            rS[1, 1] = 1.0 / svdR.Diagonal[1];
            rS[2, 2] = (R.Determinant > 0.0) ? 1.0 / svdR.Diagonal[2] : -1.0 / svdR.Diagonal[2];
            Mapack.Matrix Um = R * V * rS * V.Transpose();
            Matrix3       U  = new Matrix3(Um[0, 0], Um[0, 1], Um[0, 2],
                                           Um[1, 0], Um[1, 1], Um[1, 2], Um[2, 0], Um[2, 1], Um[2, 2]);

            this.BestFitRotation = Matrix3.RepairRotation(U);
        }
コード例 #2
0
        /// <summary>
        /// Best rotation to overlay with a reference structure
        /// See Kabsch papers for details: Kabsch W. Acta Cryst. A32 (1976) 922, A34 (1978) 827
        /// It is assumed that translations are already calculated in the same (weighted/unweighted) mode!!!
        /// </summary>
        /// <param name="refsr">reference structure</param>
        /// <param name="weighted">apply weights = mass</param>
        public void CalculateBestFitRotation(SimulationResult refsr, Boolean weighted)
        {
            if (Double.IsNaN(this.E) || Double.IsNaN(refsr.E))
            {
                this.BestFitRotation = Matrix3.E;
                return;
            }
            Vector3  r, r1, r2;
            Int32    NAtomstotal = 0, n;
            Molecule m;
            Double   w;

            for (Int32 i = 0; i < this.Molecules.Count; i++)
            {
                NAtomstotal += this.Molecules[i].NAtoms;
            }

            // rotation
            Mapack.Matrix Rxt = new Mapack.Matrix(NAtomstotal, 3);
            Mapack.Matrix Ry  = new Mapack.Matrix(3, NAtomstotal);
            n = 0;
            for (Int32 i = 0; i < this.Molecules.Count; i++)
            {
                m = this.Molecules[i];
                for (Int32 j = 0; j < m.NAtoms; j++)
                {
                    r         = new Vector3(m.XLocal[j], m.YLocal[j], m.ZLocal[j]);
                    r1        = this.Rotation[i] * r + this.Translation[i] + m.CM + this.BestFitTranslation;
                    r2        = refsr.Rotation[i] * r + refsr.Translation[i] + m.CM + refsr.BestFitTranslation;
                    w         = weighted ? m.AtomMass[j] : 1.0;
                    Rxt[n, 0] = r1.X; Rxt[n, 1] = r1.Y; Rxt[n, 2] = r1.Z;
                    Ry[0, n]  = r2.X * w; Ry[1, n] = r2.Y * w; Ry[2, n] = r2.Z * w;
                    n++;
                }
            }
            // Kabsch solution
            Mapack.Matrix R = Ry * Rxt;
            Mapack.SingularValueDecomposition svdR = new Mapack.SingularValueDecomposition(R);
            Mapack.Matrix V  = svdR.VMatrix;
            Mapack.Matrix rS = new Mapack.Matrix(3, 3);
            rS[0, 0] = 1.0 / svdR.Diagonal[0];
            rS[1, 1] = 1.0 / svdR.Diagonal[1];
            rS[2, 2] = (R.Determinant > 0.0) ? 1.0 / svdR.Diagonal[2] : -1.0 / svdR.Diagonal[2];
            Mapack.Matrix Um = R * V * rS * V.Transpose();
            Matrix3       U  = new Matrix3(Um[0, 0], Um[0, 1], Um[0, 2],
                                           Um[1, 0], Um[1, 1], Um[1, 2], Um[2, 0], Um[2, 1], Um[2, 2]);

            this.BestFitRotation = Matrix3.RepairRotation(U);
        }
コード例 #3
0
        /// <summary>
        /// Obtain polynomial coefficients
        /// </summary>
        public void Convert()
        {
            if (_x == null || _x.Length < _order + 1 || _y == null || _y.Length < _order + 1)
            {
                _c = new Double[] { 0.0, 1.0 };
                return;
            }
            Int32 n = orderbox.Value + 1;
            Int32 m = _x.Length;

            Mapack.Matrix A = new Mapack.Matrix(m, n);
            Mapack.Matrix B = new Mapack.Matrix(m, 1);
            if (_inverse)
            {
                for (Int32 i = 0; i < m; i++)
                {
                    A[i, 0] = 1.0;
                    for (Int32 j = 1; j < n; j++)
                    {
                        A[i, j] = A[i, j - 1] * _y[i];
                    }
                    B[i, 0] = _x[i];
                }
            }
            else
            {
                for (Int32 i = 0; i < m; i++)
                {
                    A[i, 0] = 1.0;
                    for (Int32 j = 1; j < n; j++)
                    {
                        A[i, j] = A[i, j - 1] * _x[i];
                    }
                    B[i, 0] = _y[i];
                }
            }
            Mapack.Matrix Csolve = A.Solve(B);
            _c = new Double[n];
            for (Int32 j = 0; j < n; j++)
            {
                _c[j] = Csolve[j, 0];
            }
            coeftsarray.Value = _c;

            // error
            Mapack.Matrix E = A * Csolve - B;
            errorBoxDouble.Value = E.FrobeniusNorm / Math.Sqrt((Double)m);
        }
コード例 #4
0
        // process a structure
        public double CalculateChi2(ref FilteringResult fr)
        {
            Molecule m;

            if (fr.MoleculeWeakReference == null || !fr.MoleculeWeakReference.TryGetTarget(out m))
            {
                m = new Molecule(fr.FullFileName);
            }
            fr.MoleculeWeakReference = new WeakReference <Molecule>(m);
            if (m.Error.Length > 0)
            {
                System.Windows.Forms.MessageBox.Show("Error reading file " + fr.FullFileName + ": " + m.Error, "Error",
                                                     System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return(MiscData.ENotCalculated);
            }
            AVEngine         av = new AVEngine(m, avparam);
            Int32            natom, ilp = 0, ilp1, ilp2, iref, nref_total = 0;
            List <Vector3[]> avcache = new List <Vector3[]>(labelingpos.Count);

            Vector3[]     t;
            Vector3[]     rmp = new Vector3[labelingpos.Count];
            Vector3       cr, cm; Matrix3 U;
            ReferenceAtom rr;
            Double        R06 = _filterParameters.R0 * _filterParameters.R0 * _filterParameters.R0 *
                                _filterParameters.R0 * _filterParameters.R0 * _filterParameters.R0,
                          dr, refrmsd_t = 0.0;

            fr.E        = 0.0;
            fr.InvalidR = 0;
            fr.RefRMSD  = 0.0;
            fr.Sigma1   = 0;
            fr.Sigma2   = 0;
            fr.Sigma3   = 0;

            foreach (LabelingPosition l in this.labelingpos)
            {
                // calculate AVs and mean positions
                if (l.AVData.AVType == AVSimlationType.SingleDyeR)
                {
                    natom = Array.BinarySearch <Int32>(m.OriginalAtomID, l.AVData.AtomID);
                    av.Calculate1R(l.AVData.L, l.AVData.W, l.AVData.R, natom);
                    t = new Vector3[av.R.Length];
                    Array.Copy(av.R, t, av.R.Length);
                    avcache.Add(t);
                    rmp[ilp++] = av.Rmp;
                }
                else if (l.AVData.AVType == AVSimlationType.ThreeDyeR)
                {
                    natom = Array.BinarySearch <Int32>(m.OriginalAtomID, l.AVData.AtomID);
                    av.Calculate3R(l.AVData.L, l.AVData.W, l.AVData.R1, l.AVData.R2, l.AVData.R3, natom);
                    t = new Vector3[av.R.Length];
                    Array.Copy(av.R, t, av.R.Length);
                    avcache.Add(t);
                    rmp[ilp++] = av.Rmp;
                }
                else if (l.AVData.AVType == AVSimlationType.None && referenceAtoms[ilp].Length > 0)
                {
                    // align reference atoms with the structure
                    // translation
                    cr = new Vector3();
                    cm = new Vector3();
                    for (iref = 0; iref < referenceAtoms[ilp].Length; iref++)
                    {
                        rr    = referenceAtoms[ilp][iref];
                        natom = rr.ConvertedN;
                        cr   += rr;
                        cm   += new Vector3(m.XLocal[natom] + m.CM.X, m.YLocal[natom] + m.CM.Y, m.ZLocal[natom] + m.CM.Z);
                    }
                    cr *= -1.0 / ((Double)referenceAtoms[ilp].Length);
                    cm *= -1.0 / ((Double)referenceAtoms[ilp].Length);

                    // rotation: see also SimulationResult.CalculateBestFitRotation
                    Mapack.Matrix Rxt = new Mapack.Matrix(referenceAtoms[ilp].Length, 3);
                    Mapack.Matrix Ry  = new Mapack.Matrix(3, referenceAtoms[ilp].Length);

                    for (iref = 0; iref < referenceAtoms[ilp].Length; iref++)
                    {
                        rr           = referenceAtoms[ilp][iref];
                        natom        = rr.ConvertedN;
                        Rxt[iref, 0] = rr.X + cr.X; Rxt[iref, 1] = rr.Y + cr.Y; Rxt[iref, 2] = rr.Z + cr.Z;
                        Ry[0, iref]  = m.XLocal[natom] + m.CM.X + cm.X;
                        Ry[1, iref]  = m.YLocal[natom] + m.CM.Y + cm.Y;
                        Ry[2, iref]  = m.ZLocal[natom] + m.CM.Z + cm.Z;
                    }

                    // Kabsch solution
                    Mapack.Matrix R = Ry * Rxt;
                    Mapack.SingularValueDecomposition svdR = new Mapack.SingularValueDecomposition(R);
                    Mapack.Matrix V  = svdR.VMatrix;
                    Mapack.Matrix rS = new Mapack.Matrix(3, 3);
                    rS[0, 0] = 1.0 / svdR.Diagonal[0];
                    rS[1, 1] = 1.0 / svdR.Diagonal[1];
                    rS[2, 2] = (R.Determinant > 0.0) ? 1.0 / svdR.Diagonal[2] : -1.0 / svdR.Diagonal[2];
                    Mapack.Matrix Um = R * V * rS * V.Transpose();
                    U = new Matrix3(Um[0, 0], Um[0, 1], Um[0, 2],
                                    Um[1, 0], Um[1, 1], Um[1, 2], Um[2, 0], Um[2, 1], Um[2, 2]);
                    U = Matrix3.RepairRotation(U);

                    // reference rmsd
                    for (iref = 0; iref < referenceAtoms[ilp].Length; iref++)
                    {
                        rr         = referenceAtoms[ilp][iref];
                        natom      = rr.ConvertedN;
                        refrmsd_t += Vector3.SquareNormDiff(U * (rr + cr),
                                                            new Vector3(m.XLocal[natom], m.YLocal[natom], m.ZLocal[natom]) + m.CM + cm);
                        nref_total++;
                    }

                    rmp[ilp++] = U * (l + cr) - cm;
                    avcache.Add(new Vector3[0]);
                }
                else
                {
                    rmp[ilp++] = l;
                    avcache.Add(new Vector3[0]);
                }
            }

            // calculate mp and FRET distances
            Distance d, dmp;
            Int32    activeR = 0;

            fr.RModel            = new DistanceList(dist.Count);
            fr.RmpModel          = new DistanceList(dist.Count);
            fr.RmpModel.DataType = dist.DataType;
            for (Int32 i = 0; i < dist.Count; i++)
            {
                dmp           = new Distance();
                d             = new Distance();
                dmp.Position1 = dist[i].Position1;
                dmp.Position2 = dist[i].Position2;
                d.Position1   = dist[i].Position1;
                d.Position2   = dist[i].Position2;
                ilp1          = labelingpos.FindIndex(d.Position1);
                ilp2          = labelingpos.FindIndex(d.Position2);
                dmp.R         = Vector3.Abs(rmp[ilp1] - rmp[ilp2]);
                if (dist.DataType == DistanceDataType.Rmp ||
                    labelingpos[ilp1].AVData.AVType == AVSimlationType.None || labelingpos[ilp2].AVData.AVType == AVSimlationType.None)
                {
                    d.R = dmp.R; // i.e. no clouds -> Rmp
                }
                else if (dist.DataType == DistanceDataType.RDAMeanE)
                {
                    if (avcache[ilp1].Length == 0 || avcache[ilp2].Length == 0)
                    {
                        d.R = Double.NaN;
                    }
                    else
                    {
                        d.R = FpsNativeWrapper.RdaMeanEFromAv(avcache[ilp1], avcache[ilp1].Length, avcache[ilp2], avcache[ilp2].Length,
                                                              avparam.ESamples, rnd.Next(), this._filterParameters.R0);
                    }
                }
                else if (dist.DataType == DistanceDataType.RDAMean)
                {
                    if (avcache[ilp1].Length == 0 || avcache[ilp2].Length == 0)
                    {
                        d.R = Double.NaN;
                    }
                    else
                    {
                        d.R = FpsNativeWrapper.RdaMeanFromAv(avcache[ilp1], avcache[ilp1].Length, avcache[ilp2], avcache[ilp2].Length,
                                                             avparam.ESamples, rnd.Next());
                    }
                }

                fr.RModel.Add(d);
                fr.RmpModel.Add(dmp);

                dr = d.R - dist[i].R;
                if (Double.IsNaN(d.R))
                {
                    fr.InvalidR++;
                }
                else if (!this._filterParameters.OptimizeSelected || dist[i].IsSelected)
                {
                    fr.E += dr > 0.0 ? dr * dr / dist[i].ErrPlus / dist[i].ErrPlus :
                            dr * dr / dist[i].ErrMinus / dist[i].ErrMinus;
                    activeR++;
                    if (dr > dist[i].ErrPlus)
                    {
                        fr.Sigma1++;
                    }
                    if (dr > 2.0 * dist[i].ErrPlus)
                    {
                        fr.Sigma2++;
                    }
                    if (dr > 3.0 * dist[i].ErrPlus)
                    {
                        fr.Sigma3++;
                    }
                    if (dr < -dist[i].ErrMinus)
                    {
                        fr.Sigma1++;
                    }
                    if (dr < -2.0 * dist[i].ErrMinus)
                    {
                        fr.Sigma2++;
                    }
                    if (dr < -3.0 * dist[i].ErrMinus)
                    {
                        fr.Sigma3++;
                    }
                }
            }

            fr.E       = fr.E / (Double)activeR;
            fr.RefRMSD = nref_total == 0 ? 0.0 : Math.Sqrt(refrmsd_t / (Double)nref_total);
            return(fr.E);
        }
コード例 #5
0
 public void SetDMatrix(Mapack.Matrix Mat)
 {
     ParamicsPuppetMaster.EditDemands EDM = new ParamicsPuppetMaster.EditDemands(TestC.ParamicsPath, Mat);
 }