Example #1
0
        /*
         * The function below uses an interpolation method written by me :-)
         */
        public DensityPoint getCRSValue(Double c, Double r, Double s)
        {
            DensityPoint centre = new DensityPoint(c, r, s, 0, "CRS");
            DensityPoint vals   = getInterpolatedDensity(centre);

            return(vals);
        }
Example #2
0
        public DensityPoint getCRSFromXYZ(double x, double y, double z)
        {
            EdVector vXYZIn = new EdVector();
            EdVector vCRS   = new EdVector();

            vXYZIn.vector[0] = x;
            vXYZIn.vector[1] = y;
            vXYZIn.vector[2] = z;
            //If the axes are all orthogonal
            if (w14_CELLB_X == 90 && w15_CELLB_Y == 90 && w16_CELLB_Z == 90)
            {
                for (int i = 0; i < 3; ++i)
                {
                    double startVal = vXYZIn.vector[i] - _origin.vector[i];
                    startVal      /= _cellDims[i] / _axisSampling[i];
                    vCRS.vector[i] = startVal;
                }
            }
            else // they are not orthogonal
            {
                EdVector vFraction = _deOrthoMat.multiply(vXYZIn);
                for (int i = 0; i < 3; ++i)
                {
                    double val = vFraction.vector[i] * _axisSampling[i] - _crsStart[_map2xyz[i]];
                    vCRS.vector[i] = val;
                }
            }
            double c = Convert.ToSingle(Math.Round(vCRS.vector[_map2crs[0]], 4));
            double r = Convert.ToSingle(Math.Round(vCRS.vector[_map2crs[1]], 4));
            double s = Convert.ToSingle(Math.Round(vCRS.vector[_map2crs[2]], 4));

            DensityPoint dp = new DensityPoint(c, r, s, 0, "CRS");

            return(dp);
        }
Example #3
0
 public DensityPoint(DensityPoint copyPoint)
 {
     XYZ.x  = copyPoint.XYZ.x;
     XYZ.y  = copyPoint.XYZ.y;
     XYZ.z  = copyPoint.XYZ.z;
     CRS.x  = copyPoint.CRS.x;
     CRS.y  = copyPoint.CRS.y;
     CRS.z  = copyPoint.CRS.z;
     v      = copyPoint.v;
     values = copyPoint.values;
     valid  = copyPoint.valid;
 }
Example #4
0
        private Double getFraction(DensityPoint centre, DensityPoint p1, DensityPoint p2)
        {//Using the cosine rule, this interpolating within the crs matrix
            Double fraction = 0;
            Double a        = Math.Sqrt(Math.Pow(centre.C - p1.C, 2) + Math.Pow(centre.R - p1.R, 2) + Math.Pow(centre.S - p1.S, 2));
            Double b        = Math.Sqrt(Math.Pow(centre.C - p2.C, 2) + Math.Pow(centre.R - p2.R, 2) + Math.Pow(centre.S - p2.S, 2));
            Double c        = Math.Sqrt(Math.Pow(p1.C - p2.C, 2) + Math.Pow(p1.R - p2.R, 2) + Math.Pow(p1.S - p2.S, 2));

            if (c != 0)
            {
                Double x = (Math.Pow(a, 2) + Math.Pow(c, 2) - Math.Pow(b, 2)) / (2 * c);
                fraction = x / c;
            }
            return(fraction);
        }
Example #5
0
        public DensityPoint getDensityPointFromCRS(double c, double r, double s, bool calcValue)
        {
            DensityPoint dp  = new DensityPoint(c, r, s, 0, "CRS");
            DensityPoint dp2 = getXYZFromCRS(c, r, s);

            dp.setXYZ(dp2.X, dp2.Y, dp2.Z);
            if (calcValue)
            {
                DensityPoint v = getCRSValue(dp.C, dp.R, dp.S);
                dp.v      = v.v;
                dp.values = v.values;
            }
            return(dp);
        }
Example #6
0
        public DensityPoint getXYZFromCRS(double c, double r, double s)
        {
            EdVector vXYZ   = new EdVector();
            EdVector vCRSIn = new EdVector();

            vCRSIn.vector[0] = c;
            vCRSIn.vector[1] = r;
            vCRSIn.vector[2] = s;
            //If the axes are all orthogonal
            if (w14_CELLB_X == 90 && w15_CELLB_Y == 90 && w16_CELLB_Z == 90)
            {
                for (int i = 0; i < 3; ++i)
                {
                    double startVal = vCRSIn.vector[_map2xyz[i]];
                    startVal      *= _cellDims[i] / _axisSampling[i];
                    startVal      += _origin.vector[i];
                    vXYZ.vector[i] = startVal;
                }
            }
            else // they are not orthogonal
            {
                EdVector vCRS = new EdVector();
                for (int i = 0; i < 3; ++i)
                {
                    Double startVal = 0;
                    if (w17_MAPC == i)
                    {
                        startVal = w05_NXSTART + c;
                    }
                    else if (w18_MAPR == i)
                    {
                        startVal = w06_NYSTART + r;
                    }
                    else
                    {
                        startVal = w07_NZSTART + s;
                    }

                    vCRS.vector[i] = startVal;
                }
                vCRS.vector[0] /= w08_MX;
                vCRS.vector[1] /= w09_MY;
                vCRS.vector[2] /= w10_MZ;
                vXYZ            = _orthoMat.multiply(vCRS);
            }
            DensityPoint dp = new DensityPoint(Convert.ToSingle(vXYZ.vector[0]), Convert.ToSingle(vXYZ.vector[1]), Convert.ToSingle(vXYZ.vector[2]), 0, "XYZ");

            return(dp);
        }
        static void Main(string[] args)
        {
            string ccp4Path = "F:\\Code\\ProteinDataFiles\\ccp4_data\\";
            Ccp4   ed       = new Ccp4("1ejg", ccp4Path, false);

            DensityPoint dp = ed.getCRSFromXYZ(0, 0, 0);

            Console.WriteLine(dp.C + "," + dp.R + "," + dp.S);

            DensityPoint dp2 = ed.getCRSValue(dp.C, dp.R, dp.S);

            Console.WriteLine(dp2.V);

            DensityPoint dp3 = ed.getXYZFromCRS(dp.C, dp.R, dp.S);

            Console.WriteLine(dp3.X + "," + dp.Y + "," + dp.Z);
        }
Example #8
0
        private double getFractionCosineRule(DensityPoint centre, DensityPoint p1, DensityPoint p2)
        {
            //The angle beta is found from the cosine rule
            //cos beta  equates x/a to (a^2 + c^2 - b^2) / 2ac
            double a2       = Math.Pow((centre.C - p1.C), 2) + Math.Pow((centre.R - p1.R), 2) + Math.Pow((centre.S - p1.S), 2);
            double b2       = Math.Pow((centre.C - p2.C), 2) + Math.Pow((centre.R - p2.R), 2) + Math.Pow((centre.S - p2.S), 2);
            double c2       = Math.Pow((p1.C - p2.C), 2) + Math.Pow((p1.R - p2.R), 2) + Math.Pow((p1.S - p2.S), 2);
            double a        = Math.Sqrt(a2);
            double b        = Math.Sqrt(b2);
            double c        = Math.Sqrt(c2);
            double fraction = 0;

            if (c != 0 && a != 0)
            {
                double cosBeta = (a2 + c2 - b2) / (2 * a * c);
                double length  = cosBeta * a;
                fraction = length / c;
            }
            return(fraction);
        }
Example #9
0
        public DensityPoint getSplinedDensity(List <DensityPoint> points, DensityPoint centre, List <int> diffs) // #recursive function
        {
            /*
             * RECURSIVE
             * List must be 2 ^ x long
             */
            int numPoints = 1 + 1;

            if (points.Count == 1) // # end of the recursion, return
            {
                foreach (int v in diffs)
                {
                    if (v == 0)
                    {
                        points[0].values[v] = points[0].v;
                    }
                    else
                    {
                        points[0].values[v] = 0;
                    }
                }

                return(points[0]);
            }
            else if (points.Count <= numPoints) // # end of the recursion, return
            {
                List <double> vs    = new List <double>();
                bool          valid = true;
                foreach (var point in points)
                {
                    double v = point.V;
                    if (point.valid)
                    {
                        vs.Add(v);
                    }
                    else
                    {
                        valid = false;
                    }
                }
                int          half = Convert.ToInt32(points.Count / 2);
                DensityPoint p1   = points[half - 1];
                DensityPoint p2   = points[half];
                double       fr   = getFractionCosineRule(centre, p1, p2);
                double       x    = p1.C + fr * (p2.C - p1.C);
                double       y    = p1.R + fr * (p2.R - p1.R);
                double       z    = p1.S + fr * (p2.S - p1.S);
                if (vs.Count == 0 || !valid)
                {
                    DensityPoint interped = new DensityPoint(x, y, z, -1000, "CRS");
                    foreach (int v in diffs)
                    {
                        interped.values[v] = -1000;
                        interped.valid     = false;
                    }
                    return(interped);
                }
                else
                {
                    Polynomial poly = new Polynomial(vs);
                    //the poly is a sequence so the value we want is a fraction along from the halfway markers
                    double        valPoint = half + fr;
                    List <double> finalvs  = poly.getValue(valPoint, diffs);
                    if (finalvs.Count > 0)
                    {
                        DensityPoint interped = new DensityPoint(x, y, z, finalvs[0], "CRS");
                        for (int i = 0; i < diffs.Count; ++i)
                        {
                            interped.values[diffs[i]] = finalvs[i];
                        }
                        return(interped);
                    }
                    else
                    {
                        DensityPoint interped = new DensityPoint(x, y, z, -1000, "CRS");
                        foreach (int v in diffs)
                        {
                            interped.values[v] = -1000;
                            interped.valid     = false;
                        }
                        return(interped);
                    }
                }
            }
            else//#split recursion down further
            {
                List <DensityPoint> ps    = new List <DensityPoint>();
                List <DensityPoint> tmpps = new List <DensityPoint>();
                for (int i = 0; i < points.Count; ++i)
                {
                    tmpps.Add(points[i]);
                    if (tmpps.Count == numPoints)
                    {
                        DensityPoint newA = getSplinedDensity(tmpps, centre, diffs);
                        ps.Add(newA);
                        tmpps.Clear();
                    }
                }
                return(getSplinedDensity(ps, centre, diffs));
            }
        }
Example #10
0
        private DensityPoint getInterpolatedDensity(DensityPoint centre)
        {
            //1. Build the points from which we will interpolate
            //Going to create the interpolated points from the perspective of 3 axes and average

            List <DensityPoint> pointsA = new List <DensityPoint>();
            List <DensityPoint> pointsB = new List <DensityPoint>();
            List <DensityPoint> pointsC = new List <DensityPoint>();

            int offset    = 1;
            int numPoints = 2 * offset;

            numPoints = 2;
            int clStart = Convert.ToInt16(Math.Floor(centre.C)) - offset + 1;
            int rlStart = Convert.ToInt16(Math.Floor(centre.R)) - offset + 1;
            int slStart = Convert.ToInt16(Math.Floor(centre.S)) - offset + 1;

            if (numPoints == 1)
            {
                clStart = Convert.ToInt16(Math.Round(centre.C));
                rlStart = Convert.ToInt16(Math.Round(centre.R));
                slStart = Convert.ToInt16(Math.Round(centre.S));
            }
            for (int i = 0; i < numPoints; ++i)
            {
                int cA = clStart + i;
                int rB = rlStart + i;
                int sC = slStart + i;
                for (int j = 0; j < numPoints; ++j)
                {
                    int rA = rlStart + j;
                    int sB = slStart + j;
                    int cC = clStart + j;
                    for (int k = 0; k < numPoints; ++k)
                    {
                        int sA = slStart + k;
                        int cB = clStart + k;
                        int rC = rlStart + k;
                        if (isValidCRS(cA, rA, sA))
                        {
                            Double       val = getCRSGridPoint(cA, rA, sA);
                            DensityPoint dp  = new DensityPoint(cA, rA, sA, val, "CRS");
                            pointsA.Add(dp);
                        }
                        else
                        {
                            DensityPoint dp = new DensityPoint(cA, rA, sA, MagicInvalid, "CRS");
                            dp.valid = false;
                            pointsA.Add(dp);
                        }
                    }
                }
            }
            MatrixInterpolator mi    = new MatrixInterpolator();
            List <int>         diffs = new List <int>();

            diffs.Add(0);
            DensityPoint A = mi.getSplinedDensity(pointsA, centre, diffs);

            List <DensityPoint> finals = new List <DensityPoint>();

            finals.Add(A);

            bool   isValid = true;
            double vAv     = 0;
            double xAv     = 0;
            double yAv     = 0;
            double zAv     = 0;
            double cAv     = 0;
            double rAv     = 0;
            double sAv     = 0;

            Dictionary <int, double> vals = new Dictionary <int, double>();

            foreach (DensityPoint dp in finals)
            {
                isValid = dp.valid ? isValid : false;
                vAv    += dp.V;
                xAv    += dp.X;
                yAv    += dp.Y;
                zAv    += dp.Z;
                cAv    += dp.C;
                rAv    += dp.R;
                sAv    += dp.S;

                foreach (var v in A.values)
                {
                    double vv = dp.values[v.Key]; // does a negative gradient mean anything for firsrt derivative from this perspective?
                    if (vals.ContainsKey(v.Key))
                    {
                        vals[v.Key] += vv;
                    }
                    else
                    {
                        vals[v.Key] = vv;
                    }
                }
            }
            vAv /= finals.Count;
            xAv /= finals.Count;
            yAv /= finals.Count;
            zAv /= finals.Count;
            cAv /= finals.Count;
            rAv /= finals.Count;
            sAv /= finals.Count;
            foreach (var v in A.values)
            {
                vals[v.Key] /= finals.Count;
            }

            if (!isValid)
            {
                DensityPoint interped = new DensityPoint(cAv, rAv, sAv, -1000, "CRS");
                interped.valid  = false;
                interped.values = A.values;
                return(interped);
            }
            else
            {
                A.v = vAv;
                A.setXYZ(xAv, yAv, zAv);
                A.setCRS(cAv, rAv, sAv);
                A.values = vals;
                return(A);
            }
        }
Example #11
0
 public bool isValidPoint(DensityPoint p)
 {
     return(isValidCRS((int)p.C, (int)p.R, (int)p.S));
 }