public static PrimaryStructureTrajectory GetTrajectory(string filename, int startFrame, int numFrames, int frameFrequency)
        {
            PrimaryStructureTrajectory trajectory = new PrimaryStructureTrajectory();

            BinaryReader reader = null;

            try {
                reader = new BinaryReader(new FileStream(filename, FileMode.Open));

                for (int i = 0; i < startFrame; i++)
                {
                    discardFrame(reader);
                }

                int framesAdded  = 0;
                int currentFrame = 0;

                while (reader.BaseStream.Position != reader.BaseStream.Length && framesAdded < numFrames)
                {
                    currentFrame++;

                    if (currentFrame % frameFrequency == 0)
                    {
                        trajectory.AddFrame(getFrame(reader));
                        framesAdded++;
                    }
                    else
                    {
                        discardFrame(reader);
                    }
                }
            }
            catch (Exception e) {
                // handle end of file corruption gracefully.
                if (trajectory.FrameCount() > 0)
                {
                    return(trajectory);
                }
                else
                {
                    throw new FileParseException(e.Message);
                }
            }
            finally {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            return(trajectory);
        }
Exemplo n.º 2
0
        public static void GetTrajectoryColours(string filename, PrimaryStructure model, PrimaryStructureTrajectory trajectory, int startFrame, int numFrames, int frameFrequency)
        {
            Stopwatch watch = new Stopwatch();

            watch.Start();

            StreamReader reader = null;
            Regex        g      = new Regex(@"\s*(\d+)\s*(\d+\.?\d*)");

            try {
                reader = new StreamReader(filename);

                discardFrames(reader, startFrame);

                List <float[]> colourFrames      = new List <float[]>();
                int            colourFrameNumber = -1;

                int     atomIndex   = 0;
                float   colour      = 0;
                float[] colourFrame = null;
                string  line;

                while ((line = readLine(reader)) != null && colourFrames.Count < numFrames)
                {
                    if (line.Trim().Length == 0)   // is empty string?
                    {
                        continue;
                    }

                    // discard everything but matches
                    Match m = g.Match(line);
                    if (m.Success)
                    {
                        atomIndex = int.Parse(m.Groups[1].Value);
                        colour    = float.Parse(m.Groups[2].Value);

                        if (atomIndex == 1)
                        {
                            colourFrameNumber++;

                            // save colours
                            if (colourFrame != null)
                            {
                                colourFrames.Add(colourFrame);
                                colourFrame = null;
                            }

                            if (colourFrameNumber % frameFrequency == 0)
                            {
                                // initialise new colour arrary
                                colourFrame = new float[model.AtomCount()];
                                for (int i = 0; i < colourFrame.Length; i++)
                                {
                                    colourFrame[i] = DEFAULT_COLOUR_VALUE;
                                }
                            }
                        }

                        if (colourFrame != null)
                        {
                            if (atomIndex > 0 && atomIndex <= colourFrame.Length)
                            {
                                colourFrame[atomIndex - 1] = colour;
                            }
                        }
                    }
                }

                PrimaryStructureFrame currentFrame = null;
                int frameNumber = 0;

                // copy all colour frames to trajectory frames
                foreach (float[] frame in colourFrames)
                {
                    currentFrame         = trajectory.GetFrame(frameNumber);
                    currentFrame.Colours = frame;
                    frameNumber++;

                    // if we've run out of trajectory frames then stop copying
                    if (frameNumber >= trajectory.FrameCount())
                    {
                        break;
                    }
                }

                // if more trajectory frames than colour frames fill in rest of trajectory frames with default colours
                float[] colours = new float[model.AtomCount()];
                for (int i = 0; i < colours.Length; i++)
                {
                    colours[i] = 0f;
                }

                foreach (PrimaryStructureFrame frame in trajectory.GetFrames())
                {
                    if (frame.Colours == null)
                    {
                        frame.Colours = colours;
                    }
                }
            }
            catch (Exception e) {
                throw new FileParseException(e.Message);
            }
            finally {
                if (reader != null)
                {
                    reader.Close();
                }
            }
        }