Exemplo n.º 1
0
        private void PrepareSimulation()
        {
            // molecules
            Nmolecules = _molecules.Count;
            int maxatomsincluster = 0;

            for (int i = 0; i < Nmolecules; i++)
            {
                minmass           = Math.Min(minmass, _molecules[i].Mass);
                maxatomsincluster = Math.Max(maxatomsincluster, _molecules[i].MaxAtomsInCluster);
            }

            // labeling positions
            int im;

            labelingpos_cm = new LabelingPositionList();
            labelingpos_cm.AddRange(this._labelingpositions);
            lpossim = new Vector3[labelingpos_cm.Count];
            LtoM    = new int[labelingpos_cm.Count];
            // convert to local coordinates
            for (int i = 0; i < labelingpos_cm.Count; i++)
            {
                im                 = _molecules.FindIndex(labelingpos_cm[i].Molecule);
                LtoM[i]            = im;
                labelingpos_cm[i] -= _molecules[im].CM;
                lpossim[i]         = labelingpos_cm[i];
            }

            // distances
            Distance dist_i;

            DtoL1      = new int[_distances.Count];
            DtoM1      = new int[_distances.Count];
            DtoL2      = new int[_distances.Count];
            DtoM2      = new int[_distances.Count];
            kplus      = new double[_distances.Count];
            kminus     = new double[_distances.Count];
            drmaxplus  = new double[_distances.Count];
            drmaxminus = new double[_distances.Count];
            double minerror = double.MaxValue;

            for (int i = 0; i < _distances.Count; i++)
            {
                DtoL1[i] = labelingpos_cm.FindIndex(_distances[i].Position1);
                DtoM1[i] = _molecules.FindIndex(labelingpos_cm.Find(_distances[i].Position1).Molecule);
                DtoL2[i] = labelingpos_cm.FindIndex(_distances[i].Position2);
                DtoM2[i] = _molecules.FindIndex(labelingpos_cm.Find(_distances[i].Position2).Molecule);

                // check if bond
                dist_i        = _distances[i];
                dist_i.IsBond = (labelingpos_cm[DtoL1[i]].Dye == DyeType.Unknown && labelingpos_cm[DtoL1[i]].AVData.AtomID > 0 &&
                                 labelingpos_cm[DtoL1[i]].AVData.AVType == AVSimlationType.None && labelingpos_cm[DtoL2[i]].Dye == DyeType.Unknown &&
                                 labelingpos_cm[DtoL2[i]].AVData.AtomID > 0 && labelingpos_cm[DtoL2[i]].AVData.AVType == AVSimlationType.None);
                _distances[i] = dist_i;

                // if bond, set small vdW radii to "exclude" from clashing
                if (dist_i.IsBond)
                {
                    foreach (var lp in new[] { labelingpos_cm[DtoL1[i]], labelingpos_cm[DtoL2[i]] })
                    {
                        int m     = _molecules.FindIndex(lp.Molecule);
                        int natom = Array.BinarySearch <int>(_molecules[m].OriginalAtomID, lp.AVData.AtomID);
                        _molecules[m].vdWR[natom] = AtomData.vdWRNoClash;
                        int natom_cluster = Array.FindIndex <int>(_molecules[m].ClusteredAtomOriginalIndex, n => n == natom);
                        if (natom_cluster >= 0)
                        {
                            _molecules[m].ClusteredAtomvdwR[natom_cluster] = AtomData.vdWRNoClash;
                            System.Runtime.InteropServices.Marshal.Copy(new[] { (float)AtomData.vdWRNoClash }, 0,
                                                                        FpsNativeWrapper.Aligned16(_molecules[m].XYZvdwRVectorArray) + (4 * natom_cluster + 3) * sizeof(float), 1);
                        }
                    }
                }

                // "spring constants"
                kplus[i]  = 2.0 / _distances[i].ErrPlus / _distances[i].ErrPlus;
                kminus[i] = 2.0 / _distances[i].ErrMinus / _distances[i].ErrMinus;
                //Console.Out.WriteLine("distance#" + i + " k+= " + kplus[i] + " k-= " + kminus[i] + " err+ " + _distances[i].ErrPlus + " err- " + _distances[i].ErrMinus);

                drmaxplus[i]  = SimulationParameters.MaxForce / kplus[i];
                drmaxminus[i] = -SimulationParameters.MaxForce / kminus[i];
                minerror      = Math.Min(minerror, Math.Min(_distances[i].ErrPlus, _distances[i].ErrMinus));
            }

            // simulation parameters
            kclash     = 2.0 / SimulationParameters.ClashTolerance / SimulationParameters.ClashTolerance;
            drmaxclash = -SimulationParameters.MaxForce / kclash;
            dr4dt      = Math.Min(SimulationParameters.ClashTolerance * 0.5, minerror);
        }
Exemplo n.º 2
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);
        }