Ejemplo n.º 1
0
        private static void ProcessChemFile(IChemFile chemFile, ISettings settings, FileUsage usage,
                                            MoleculeLoadingResults results, LoadingProgress progress)
        {
            MoleculeProcessingProgress molProcessing = new MoleculeProcessingProgress(progress);

            progress.MoveToNextProcess(molProcessing);

            // calc size
            int numModels = 0;

            for (int seq = 0; seq < chemFile.ChemSequenceCount; seq++)
            {
                IChemSequence chemSeq = chemFile.getChemSequence(seq);
                numModels += chemSeq.ChemModelCount;
            }
            float modelSz = 1.0f / (float)numModels;

            results.NumSequences = chemFile.ChemSequenceCount;
            for (int seq = 0; seq < chemFile.ChemSequenceCount; seq++)
            {
                IChemSequence chemSeq = chemFile.getChemSequence(seq);
                results.NumModels += chemSeq.ChemModelCount;
                for (int model = 0; model < chemSeq.ChemModelCount; model++)
                {
                    ProcessChemModel(chemSeq.getChemModel(model), settings, usage, results, molProcessing, modelSz);
                }
                molProcessing.Log("Processed Sequence", LogItem.ItemLevel.Success);
            }
            molProcessing.Log("Processed File", LogItem.ItemLevel.Success);
        }
Ejemplo n.º 2
0
        public void LoadFile(string file)
        {
            MoleculeLoadingResults results = null;

            try
            {
                renderSource = new ChemRenderingSource3D(MoleculeLoader.LoadFromFile(file, settings, MoleculeLoader.FileUsage.ThreeD, null, out results));
                if (renderContext != null)
                {
                    renderContext.ChemRenderSource = renderSource;
                    renderContext.OnResize(Width, Height);
                    renderContext.Render(null);
                }
            }
            catch (UserLevelException ule)
            {
                // post to user
                MessageBox.Show(ule.Message, "Loading File", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            UleDlg dlg = new UleDlg(results, null);

            dlg.ShowDialog();
            dlg.Dispose();
        }
Ejemplo n.º 3
0
        private static void ProcessChemModel(IChemModel chemModel, ISettings settings, FileUsage usage,
                                             MoleculeLoadingResults results, MoleculeProcessingProgress progress,
                                             float sectionSz)
        {
            //if (ChemModelManipulator.getAllInOneContainer(chemModel).getBondCount() == 0)
            //{
            //    return;
            //}

            // check for coordinates
            if ((GeometryTools.has2DCoordinatesNew(ChemModelManipulator.getAllInOneContainer(chemModel)) != 0))//
            {
                results.Num2DCoords++;
            }
            //    usage == FileUsage.TwoD)
            //{
            //    throw new UserLevelException("File has no 2D coords", UserLevelException.ExceptionType.FileLoading,
            //                                 typeof(MoleculeLoader), null);
            //}
            if ((GeometryTools.has2DCoordinatesNew(ChemModelManipulator.getAllInOneContainer(chemModel)) != 0))// &&
            {
                results.Num3DCoords++;
            }
            //    usage == FileUsage.ThreeD)
            //{
            //    throw new UserLevelException("File has no 3D coords", UserLevelException.ExceptionType.FileLoading,
            //                                 typeof(MoleculeLoader), null);
            //}

            ElementPTFactory elements = ElementPTFactory.Instance;

            // calc item sz
            int numItems = 0;

            if (chemModel.SetOfMolecules != null)
            {
                results.NumMolecules += chemModel.SetOfMolecules.MoleculeCount;
                for (int mol = 0; mol < chemModel.SetOfMolecules.MoleculeCount; mol++)
                {
                    numItems += chemModel.SetOfMolecules.Molecules[mol].Atoms.Length;
                    //numItems += chemModel.SetOfMolecules.Molecules[mol].Bonds.Length;
                }
            }
            float itemSz = sectionSz / (float)numItems;

            if (chemModel.SetOfMolecules != null)
            {
                results.NumMolecules += chemModel.SetOfMolecules.MoleculeCount;
                for (int mol = 0; mol < chemModel.SetOfMolecules.MoleculeCount; mol++)
                {
                    IMolecule molecule = chemModel.SetOfMolecules.Molecules[mol];
                    results.NumAtoms += molecule.Atoms.Length;
                    results.NumBonds += molecule.Bonds.Length;
                    foreach (IAtom atom in molecule.Atoms)
                    {
                        PeriodicTableElement pe = elements.getElement(atom.Symbol);
                        if (pe != null)
                        {
                            atom.AtomicNumber = pe.AtomicNumber;
                            atom.Properties["PeriodicTableElement"] = pe;
                            atom.Properties["Period"] = int.Parse(pe.Period);
                        }
                        else
                        {
                            progress.Log(string.Format("Failed to find periodic element: {0}", atom.Symbol), LogItem.ItemLevel.Failure);
                        }
                        progress.UpdateProgress(itemSz);
                    }
                    progress.Log(string.Format("Processed {0} atoms", molecule.Atoms.Length), LogItem.ItemLevel.Info);
                }
            }
            progress.Log("Processed Model", LogItem.ItemLevel.Success);
        }
Ejemplo n.º 4
0
        internal static IChemFile LoadFromFile(string filename, ISettings settings, FileUsage usage,
                                               LoadingProgress progress, out MoleculeLoadingResults results)
        {
            IChemObjectReader reader;

            results          = new MoleculeLoadingResults();
            results.Filename = filename;

            MoleculeLoadingProgress molLoading = new MoleculeLoadingProgress(Path.GetFileName(filename), progress);

            progress.MoveToNextProcess(molLoading);

            try
            {
                molLoading.Log("Reading file", LogItem.ItemLevel.StageInfo);

                StreamReader  file          = new StreamReader(filename, System.Text.Encoding.Default);
                ReaderFactory readerFactory = new ReaderFactory();

                molLoading.Log("Creating Reader", LogItem.ItemLevel.DebugInfo);

                reader = readerFactory.createReader(filename, file);
                file.Close();

                if (reader != null)
                {
                    reader.setReader(new StreamReader(filename, System.Text.Encoding.Default));
                }
                else
                {
                    throw new UserLevelException("Format not recognised", UserLevelException.ExceptionType.FileLoading,
                                                 typeof(MoleculeLoader), null);
                }
                results.FileFormat = (IChemFormat)reader.Format;

                molLoading.Log("Found File Format: " + reader.Format.FormatName, LogItem.ItemLevel.UserInfo);
            }
            catch (FileNotFoundException fnfe)
            {
                throw new UserLevelException("Target file no found", UserLevelException.ExceptionType.FileLoading,
                                             typeof(MoleculeLoader), fnfe);
            }
            molLoading.Log("Loaded Molecule File", LogItem.ItemLevel.Success);

            //catch (IOException ioe)
            //{
            //    throw new UserLevelException("Problem reading from file", UserLevelException.ExceptionType.FileLoading,
            //                                 typeof(MoleculeLoader), ioe);
            //}
            //catch (UserLevelException ule)
            //{
            //    results.Result = MoleculeLoadingResults.Results.Problems;
            //    throw ule;
            //}
            //catch (Exception e)
            //{
            //    results.Result = MoleculeLoadingResults.Results.Errors;
            //    throw new UserLevelException("Problem loading file", UserLevelException.ExceptionType.FileLoading,
            //                                 typeof(MoleculeLoader), e);
            //}

            IChemFile chemFile;
            ChemModel chemModel;

            if (reader.accepts(typeof(IChemFile)))
            {
                // try to read a ChemFile
                //try
                //{
                chemFile = (IChemFile)reader.read((IChemObject) new ChemFile());
                molLoading.UpdateProgress(1);
                if (chemFile != null)
                {
                    ProcessChemFile(chemFile, settings, usage, results, progress);
                }
                return(chemFile);
                //}
                //catch (UserLevelException ule)
                //{
                //    throw ule;
                //}
                //catch (Exception e)
                //{
                //    results.Result = MoleculeLoadingResults.Results.Errors;

                //    throw new UserLevelException("Problem reading/processing file", UserLevelException.ExceptionType.FileLoading,
                //                                 typeof(MoleculeLoader), e);
                //}
            }
            if (reader.accepts(typeof(ChemModel)))
            {
                // try to read a ChemModel
                //try
                //{
                chemModel = (ChemModel)reader.read((IChemObject) new ChemModel());
                molLoading.UpdateProgress(1);
                if (chemModel != null)
                {
                    MoleculeProcessingProgress molProcessing = new MoleculeProcessingProgress(progress);
                    progress.MoveToNextProcess(molProcessing);
                    ProcessChemModel(chemModel, settings, usage, results, molProcessing, 1);
                }
                //}
                //catch (UserLevelException ule)
                //{
                //    throw ule;
                //}
                //catch (Exception e)
                //{
                //    throw new UserLevelException("Problem reading/processing file", UserLevelException.ExceptionType.FileLoading,
                //                                 typeof(MoleculeLoader), e);
                //}
            }
            else
            {
                throw new UserLevelException("Unable to process reading", UserLevelException.ExceptionType.FileLoading,
                                             typeof(MoleculeLoader), null);
            }
            return(null);
        }