public static void removeAtomAndConnectedElectronContainers(IChemModel chemModel, IAtom atom)
        {
            ICrystal crystal = chemModel.Crystal;

            if (crystal != null)
            {
                if (crystal.contains(atom))
                {
                    crystal.removeAtomAndConnectedElectronContainers(atom);
                }
                return;
            }
            ISetOfMolecules moleculeSet = chemModel.SetOfMolecules;

            if (moleculeSet != null)
            {
                SetOfMoleculesManipulator.removeAtomAndConnectedElectronContainers(moleculeSet, atom);
            }
            ISetOfReactions reactionSet = chemModel.SetOfReactions;

            if (reactionSet != null)
            {
                SetOfReactionsManipulator.removeAtomAndConnectedElectronContainers(reactionSet, atom);
            }
        }
Beispiel #2
0
        public override void TestClone()
        {
            ICrystal crystal = (ICrystal)NewChemObject();
            object   clone   = crystal.Clone();

            Assert.IsTrue(clone is ICrystal);
        }
Beispiel #3
0
        public override void TestAdd_IAtomContainer()
        {
            ICrystal crystal = (ICrystal)NewChemObject();

            IAtomContainer acetone = crystal.Builder.NewAtomContainer();
            IAtom          c1      = crystal.Builder.NewAtom("C");
            IAtom          c2      = crystal.Builder.NewAtom("C");
            IAtom          o       = crystal.Builder.NewAtom("O");
            IAtom          c3      = crystal.Builder.NewAtom("C");

            acetone.Atoms.Add(c1);
            acetone.Atoms.Add(c2);
            acetone.Atoms.Add(c3);
            acetone.Atoms.Add(o);
            IBond b1 = crystal.Builder.NewBond(c1, c2, BondOrder.Single);
            IBond b2 = crystal.Builder.NewBond(c1, o, BondOrder.Double);
            IBond b3 = crystal.Builder.NewBond(c1, c3, BondOrder.Single);

            acetone.Bonds.Add(b1);
            acetone.Bonds.Add(b2);
            acetone.Bonds.Add(b3);

            crystal.Add(acetone);
            Assert.AreEqual(4, crystal.Atoms.Count);
            Assert.AreEqual(3, crystal.Bonds.Count);
        }
        public static void removeElectronContainer(IChemModel chemModel, IElectronContainer electrons)
        {
            ICrystal crystal = chemModel.Crystal;

            if (crystal != null)
            {
                if (crystal.contains(electrons))
                {
                    crystal.removeElectronContainer(electrons);
                }
                return;
            }
            ISetOfMolecules moleculeSet = chemModel.SetOfMolecules;

            if (moleculeSet != null)
            {
                SetOfMoleculesManipulator.removeElectronContainer(moleculeSet, electrons);
            }
            ISetOfReactions reactionSet = chemModel.SetOfReactions;

            if (reactionSet != null)
            {
                SetOfReactionsManipulator.removeElectronContainer(reactionSet, electrons);
            }
        }
Beispiel #5
0
        public void TestNewCrystal_IAtomContainer()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            ICrystal           crystal = builder.NewCrystal(builder.NewAtomContainer());

            Assert.IsNotNull(crystal);
        }
Beispiel #6
0
        private static string F_ANGLE_FORMAT(double angle) => angle.ToString("F3", NumberFormatInfo.InvariantInfo).PadLeft(7);    //"%3.3f";

        public void WriteCrystal(ICrystal crystal)
        {
            try
            {
                WriteHeader();
                Vector3  a        = crystal.A;
                Vector3  b        = crystal.B;
                Vector3  c        = crystal.C;
                double[] ucParams = CrystalGeometryTools.CartesianToNotional(a, b, c);
                writer.Write("CRYST1 " + F_LENGTH_FORMAT(ucParams[0]));
                writer.Write(F_LENGTH_FORMAT(ucParams[1]));
                writer.Write(F_LENGTH_FORMAT(ucParams[2]));
                writer.Write(F_ANGLE_FORMAT(ucParams[3]));
                writer.Write(F_ANGLE_FORMAT(ucParams[4]));
                writer.Write('\n');

                // before saving the atoms, we need to create cartesian coordinates
                foreach (var atom in crystal.Atoms)
                {
                    //                Debug.WriteLine($"PDBWriter: atom -> {atom}");
                    // if it got 3D coordinates, use that. If not, try fractional coordinates
                    if (atom.Point3D == null && atom.FractionalPoint3D != null)
                    {
                        Vector3 frac = atom.FractionalPoint3D.Value;
                        Vector3 cart = CrystalGeometryTools.FractionalToCartesian(a, b, c, frac);
                        atom.Point3D = cart;
                    }
                }
                WriteMolecule(crystal.Builder.NewAtomContainer(crystal));
            }
            catch (IOException exception)
            {
                throw new CDKException("Error while writing file: " + exception.Message, exception);
            }
        }
Beispiel #7
0
        public void TestNewCrystal()
        {
            IChemObjectBuilder builder = RootObject.Builder;
            ICrystal           crystal = builder.NewCrystal();

            Assert.IsNotNull(crystal);
        }
Beispiel #8
0
 public void TestSetCrystal_ICrystal()
 {
     IChemModel chemModel = (IChemModel)NewChemObject();
     ICrystal crystal = chemModel.Builder.NewCrystal();
     chemModel.Crystal = crystal;
     Assert.AreEqual(crystal, chemModel.Crystal);
 }
Beispiel #9
0
        public ValidationReport ValidateChemModel(IChemModel subject)
        {
            Trace.TraceInformation($"Validating {nameof(IChemModel)}");
            var report = new ValidationReport();

            // apply validators
            foreach (var test in validators.Values)
            {
                report.Add(test.ValidateChemModel(subject));
            }
            // traverse into super class
            report.Add(ValidateChemObject(subject));
            // traverse into hierarchy
            ICrystal crystal = subject.Crystal;

            if (crystal != null)
            {
                report.Add(ValidateCrystal(crystal));
            }
            IReactionSet reactionSet = subject.ReactionSet;

            if (reactionSet != null)
            {
                report.Add(ValidateReactionSet(reactionSet));
            }
            var moleculeSet = subject.MoleculeSet;

            if (moleculeSet != null)
            {
                report.Add(ValidateMoleculeSet(moleculeSet));
            }
            return(report);
        }
Beispiel #10
0
        /// <summary>
        /// Remove an ElectronContainer from all AtomContainers
        /// inside an IChemModel.
        /// </summary>
        /// <param name="chemModel">The IChemModel object.</param>
        /// <param name="electrons">The ElectronContainer to remove.</param>
        public static void RemoveElectronContainer(IChemModel chemModel, IElectronContainer electrons)
        {
            ICrystal crystal = chemModel.Crystal;

            if (crystal != null)
            {
                if (crystal.Contains(electrons))
                {
                    crystal.Remove(electrons);
                }
                return;
            }
            IChemObjectSet <IAtomContainer> moleculeSet = chemModel.MoleculeSet;

            if (moleculeSet != null)
            {
                MoleculeSetManipulator.RemoveElectronContainer(moleculeSet, electrons);
            }
            IReactionSet reactionSet = chemModel.ReactionSet;

            if (reactionSet != null)
            {
                ReactionSetManipulator.RemoveElectronContainer(reactionSet, electrons);
            }
        }
Beispiel #11
0
        /// <summary>
        /// Remove an Atom and the connected ElectronContainers from all AtomContainers
        /// inside an IChemModel.
        /// </summary>
        /// <param name="chemModel">The IChemModel object.</param>
        /// <param name="atom">The Atom object to remove.</param>
        public static void RemoveAtomAndConnectedElectronContainers(IChemModel chemModel, IAtom atom)
        {
            ICrystal crystal = chemModel.Crystal;

            if (crystal != null)
            {
                if (crystal.Contains(atom))
                {
                    crystal.RemoveAtom(atom);
                }
                return;
            }
            IChemObjectSet <IAtomContainer> moleculeSet = chemModel.MoleculeSet;

            if (moleculeSet != null)
            {
                MoleculeSetManipulator.RemoveAtomAndConnectedElectronContainers(moleculeSet, atom);
            }
            IReactionSet reactionSet = chemModel.ReactionSet;

            if (reactionSet != null)
            {
                ReactionSetManipulator.RemoveAtomAndConnectedElectronContainers(reactionSet, atom);
            }
        }
Beispiel #12
0
        public override void TestAddAtom_IAtom()
        {
            ICrystal crystal = (ICrystal)NewChemObject();
            IAtom    c1      = crystal.Builder.NewAtom("C");

            crystal.Atoms.Add(c1);
            Assert.AreEqual(1, crystal.Atoms.Count);
        }
Beispiel #13
0
        public virtual void TestSetZ_Integer()
        {
            ICrystal crystal = (ICrystal)NewChemObject();
            int      z       = 2;

            crystal.Z = z;
            Assert.AreEqual(z, crystal.Z.Value);
        }
Beispiel #14
0
        public virtual void TestSetSpaceGroup_String()
        {
            ICrystal crystal    = (ICrystal)NewChemObject();
            string   spacegroup = "P 2_1 2_1 2_1";

            crystal.SpaceGroup = spacegroup;
            Assert.AreEqual(spacegroup, crystal.SpaceGroup);
        }
Beispiel #15
0
        public virtual void TestSetZeroAxes()
        {
            ICrystal crystal = (ICrystal)NewChemObject();

            crystal.A = new Vector3(1, 2, 3);
            Vector3 a = crystal.A;

            Assert.IsNotNull(a);
        }
Beispiel #16
0
        public virtual void TestGetB()
        {
            ICrystal crystal = (ICrystal)NewChemObject();

            crystal.B = new Vector3(1, 2, 3);
            Vector3 a = crystal.B;

            Assert.IsNotNull(a);
        }
Beispiel #17
0
        public void Cod1100784AtomCount()
        {
            var       ins       = ResourceLoader.GetAsStream(GetType(), "1100784.cif");
            CIFReader cifReader = new CIFReader(ins);
            var       chemFile  = cifReader.Read(new ChemFile());
            ICrystal  crystal   = chemFile[0][0].Crystal;

            Assert.AreEqual(72, crystal.Atoms.Count);
            cifReader.Close();
        }
Beispiel #18
0
        public override void TestToString()
        {
            ICrystal crystal     = (ICrystal)NewChemObject();
            string   description = crystal.ToString();

            for (int i = 0; i < description.Length; i++)
            {
                Assert.IsTrue(description[i] != '\n');
                Assert.IsTrue(description[i] != '\r');
            }
        }
Beispiel #19
0
        public virtual void TestSetC_Vector3d()
        {
            ICrystal crystal = (ICrystal)NewChemObject();

            crystal.C = new Vector3(1, 2, 3);
            Vector3 c = crystal.C;

            Assert.AreEqual(1.0, c.X, 0.001);
            Assert.AreEqual(2.0, c.Y, 0.001);
            Assert.AreEqual(3.0, c.Z, 0.001);
        }
Beispiel #20
0
        /// <summary>
        /// Read the ShelX from input. Each ShelX document is expected to contain one crystal structure.
        /// </summary>
        /// <param name="file"></param>
        /// <returns>a ChemFile with the coordinates, charges, vectors, etc.</returns>
        private IChemFile ReadChemFile(IChemFile file)
        {
            IChemSequence seq     = file.Builder.NewChemSequence();
            IChemModel    model   = file.Builder.NewChemModel();
            ICrystal      crystal = ReadCrystal(file.Builder.NewCrystal());

            model.Crystal = crystal;
            seq.Add(model);
            file.Add(seq);
            return(file);
        }
Beispiel #21
0
        public virtual void TestSetA_Vector3d()
        {
            ICrystal crystal = (ICrystal)NewChemObject();

            crystal.A = new Vector3(1, 2, 3);
            Vector3 a = crystal.A;

            Assert.AreEqual(1.0, a.X, 0.001);
            Assert.AreEqual(2.0, a.Y, 0.001);
            Assert.AreEqual(3.0, a.Z, 0.001);
        }
Beispiel #22
0
        public virtual void TestSetB_Vector3d()
        {
            ICrystal crystal = (ICrystal)NewChemObject();

            crystal.B = new Vector3(1, 2, 3);
            Vector3 b = crystal.B;

            Assert.AreEqual(1.0, b.X, 0.001);
            Assert.AreEqual(2.0, b.Y, 0.001);
            Assert.AreEqual(3.0, b.Z, 0.001);
        }
Beispiel #23
0
        public void Cod1100784CellLengths()
        {
            var       ins       = ResourceLoader.GetAsStream(GetType(), "1100784.cif");
            CIFReader cifReader = new CIFReader(ins);
            var       chemFile  = cifReader.Read(new ChemFile());
            ICrystal  crystal   = chemFile[0][0].Crystal;

            Assert.IsTrue(Math.Abs(crystal.A.Length() - 10.9754) < 1E-5);
            Assert.IsTrue(Math.Abs(crystal.B.Length() - 11.4045) < 1E-5);
            Assert.IsTrue(Math.Abs(crystal.C.Length() - 12.9314) < 1E-5);
            cifReader.Close();
        }
Beispiel #24
0
        public void TestIsEmpty_Crystal()
        {
            var model = (IChemModel)NewChemObject();
            IChemObjectBuilder builder = model.Builder;

            ICrystal crystal = builder.NewCrystal();
            model.Crystal = crystal;
            Assert.IsTrue(model.IsEmpty());
            crystal.Atoms.Add(builder.NewAtom("C"));
            Assert.IsFalse(model.IsEmpty());
            model.Crystal = null;
            Assert.IsTrue(model.IsEmpty());
        }
Beispiel #25
0
        public virtual void TestClone_Axes()
        {
            ICrystal crystal1 = (ICrystal)NewChemObject();
            Vector3  axes     = new Vector3(1, 2, 3);

            crystal1.A = axes;
            ICrystal crystal2 = (ICrystal)crystal1.Clone();

            // test cloning of axes
            var cpy = crystal1.A; cpy.X = 5; crystal1.A = cpy; // NCDK's Vector3 is value type.

            Assert.AreEqual(1.0, crystal2.A.X, 0.001);
        }
Beispiel #26
0
        /// <summary>
        /// Writes a single frame to the Writer.
        /// </summary>
        /// <remarks>
        /// Format:
        /// <list type="table">
        /// <listheader>
        /// <term>line</term>
        /// <term>data</term>
        /// </listheader>
        /// <term><item>1</item><item>spacegroup</item></term>
        /// <term><item>2,3,4</item><item>cell parameter: a</item></term>
        /// <term><item>5,6,7</item><item>b</item></term>
        /// <term><item>8,9,10</item><item>c</item></term>
        /// <term><item>11</item><item>number of atoms</item></term>
        /// <term><item>12</item><item>number of asym. units</item></term>
        /// <term><item>13-16</item><item>atomtype: charge, atomcoord x, y, z</item></term>
        /// <term><item>17-20</item><item>idem second atom</item></term>
        /// <term><item>21-24</item><item>idem third atom etc</item></term>
        /// </list>
        /// </remarks>
        /// <param name="crystal">the Crystal to serialize</param>
        private void WriteCrystal(ICrystal crystal)
        {
            string sg = crystal.SpaceGroup;

            if (string.Equals("P 2_1 2_1 2_1", sg, StringComparison.Ordinal))
            {
                Writeln("P 21 21 21 (1)");
            }
            else
            {
                Writeln("P 1 (1)");
            }

            // output unit cell axes
            WriteVector3d(crystal.A);
            WriteVector3d(crystal.B);
            WriteVector3d(crystal.C);

            // output number of atoms
            int noatoms = crystal.Atoms.Count;

            Write(noatoms.ToString(NumberFormatInfo.InvariantInfo));
            Writeln("");

            // output number of asym. units (Z)
            if (string.Equals(sg, "P1", StringComparison.Ordinal))
            {
                Writeln("1");
            }
            else
            {
                // duno
                Writeln("1");
            }

            // output atoms
            for (int i = 0; i < noatoms; i++)
            {
                // output atom sumbol
                var atom = crystal.Atoms[i];
                Write(atom.Symbol);
                Write(":");
                // output atom charge
                Writeln(atom.Charge.ToString());
                // output coordinates
                Writeln(atom.Point3D.Value.X.ToString(NumberFormatInfo.InvariantInfo));
                Writeln(atom.Point3D.Value.Y.ToString(NumberFormatInfo.InvariantInfo));
                Writeln(atom.Point3D.Value.Z.ToString(NumberFormatInfo.InvariantInfo));
            }
        }
Beispiel #27
0
        /// <summary> Writes a single frame to the Writer.
        ///
        /// <p>Format:
        /// <pre>
        /// line      data
        /// -------    --------------------------
        /// 1       spacegroup
        /// 2,3,4     cell parameter: a
        /// 5,6,7                     b
        /// 8,9,10                    c
        /// 11       number of atoms
        /// 12       number of asym. units
        /// 13-16     atomtype: charge, atomcoord x, y, z
        /// 17-20     idem second atom
        /// 21-24     idem third atom etc
        /// </pre>
        ///
        /// </summary>
        /// <param name="crystal">the Crystal to serialize
        /// </param>
        //UPGRADE_NOTE: Access modifiers of method 'write' were changed to 'public'. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1204'"
        public void write(ICrystal crystal)
        {
            System.String sg = crystal.SpaceGroup;
            if ("P 2_1 2_1 2_1".Equals(sg))
            {
                write("P 21 21 21 (1)\n");
            }
            else
            {
                write("P 1 (1)\n");
            }

            // output unit cell axes
            writeVector3d(crystal.A);
            writeVector3d(crystal.B);
            writeVector3d(crystal.C);

            // output number of atoms
            int noatoms = crystal.AtomCount;

            write(((System.Int32)noatoms).ToString());
            write("\n");

            // output number of asym. units (Z)
            if (sg.Equals("P1"))
            {
                write("1\n");
            }
            else
            {
                // duno
                write("1\n");
            }

            // output atoms
            for (int i = 0; i < noatoms; i++)
            {
                // output atom sumbol
                IAtom atom = crystal.getAtomAt(i);
                write(atom.Symbol);
                write(":");
                // output atom charge
                write(((double)atom.getCharge()).ToString() + "\n");
                // output coordinates
                write(((double)atom.X3d).ToString() + "\n");
                write(((double)atom.Y3d).ToString() + "\n");
                write(((double)atom.Z3d).ToString() + "\n");
            }
        }
Beispiel #28
0
        public ValidationReport ValidateCrystal(ICrystal subject)
        {
            Trace.TraceInformation($"Validating {nameof(ICrystal)}");
            var report = new ValidationReport();

            // apply validators
            foreach (var test in validators.Values)
            {
                report.Add(test.ValidateCrystal(subject));
            }
            // traverse into super class
            report.Add(ValidateAtomContainer(subject));
            // traverse into hierarchy
            return(report);
        }
Beispiel #29
0
        /// <summary>
        /// Creates Cartesian coordinates for all Atoms in the Crystal.
        /// </summary>
        public static void FractionalToCartesian(ICrystal crystal)
        {
            Vector3 aAxis = crystal.A;
            Vector3 bAxis = crystal.B;
            Vector3 cAxis = crystal.C;

            foreach (var atom in crystal.Atoms)
            {
                Vector3?fracPoint = atom.FractionalPoint3D;
                if (fracPoint != null)
                {
                    atom.Point3D = FractionalToCartesian(aAxis, bAxis, cAxis, fracPoint.Value);
                }
            }
        }
Beispiel #30
0
        /// <summary> Creates cartesian coordinates for all Atoms in the Crystal.</summary>
        public static void fractionalToCartesian(ICrystal crystal)
        {
            IAtom[]  atoms = crystal.Atoms;
            Vector3d aAxis = crystal.A;
            Vector3d bAxis = crystal.B;
            Vector3d cAxis = crystal.C;

            for (int i = 0; i < atoms.Length; i++)
            {
                Point3d fracPoint = atoms[i].getFractionalPoint3d();
                if (fracPoint != null)
                {
                    atoms[i].setPoint3d(fractionalToCartesian(aAxis, bAxis, cAxis, fracPoint));
                }
            }
        }
Beispiel #31
0
 public virtual void createIDs(ICrystal crystal)
 {
     createIDs((IAtomContainer)crystal);
 }
 /// <summary> Creates cartesian coordinates for all Atoms in the Crystal.</summary>
 public static void fractionalToCartesian(ICrystal crystal)
 {
     IAtom[] atoms = crystal.Atoms;
     Vector3d aAxis = crystal.A;
     Vector3d bAxis = crystal.B;
     Vector3d cAxis = crystal.C;
     for (int i = 0; i < atoms.Length; i++)
     {
         Point3d fracPoint = atoms[i].getFractionalPoint3d();
         if (fracPoint != null)
         {
             atoms[i].setPoint3d(fractionalToCartesian(aAxis, bAxis, cAxis, fracPoint));
         }
     }
 }
Beispiel #33
0
        /// <summary> Read the ShelX from input. Each ShelX document is expected to contain
        /// one crystal structure.
        /// 
        /// </summary>
        /// <returns> a ChemFile with the coordinates, charges, vectors, etc.
        /// </returns>
        private IChemFile readChemFile(IChemFile file)
        {
            IChemSequence seq = file.Builder.newChemSequence();
            IChemModel model = file.Builder.newChemModel();
            crystal = file.Builder.newCrystal();

            System.String line = input.ReadLine();
            bool end_found = false;
            while (input.Peek() != -1 && line != null && !end_found)
            {
                if (line.StartsWith("#"))
                {
                    //logger.warn("Skipping comment: " + line);
                    // skip comment lines
                }
                else if (line.Length == 0)
                {
                    //logger.debug("Skipping empty line");
                    // skip empty lines
                }
                else if (!(line.StartsWith("_") || line.StartsWith("loop_")))
                {
                    //logger.warn("Skipping unrecognized line: " + line);
                    // skip line
                }
                else
                {

                    /* determine CIF command */
                    System.String command = "";
                    int spaceIndex = line.IndexOf(" ");
                    if (spaceIndex != -1)
                    {
                        // everything upto space is command
                        try
                        {
                            command = new System.Text.StringBuilder(line.Substring(0, (spaceIndex) - (0))).ToString();
                        }
                        catch (System.ArgumentOutOfRangeException sioobe)
                        {
                            // disregard this line
                            break;
                        }
                    }
                    else
                    {
                        // complete line is command
                        command = line;
                    }

                    //logger.debug("command: " + command);
                    if (command.StartsWith("_cell"))
                    {
                        processCellParameter(command, line);
                    }
                    else if (command.Equals("loop_"))
                    {
                        processLoopBlock();
                    }
                    else if (command.Equals("_symmetry_space_group_name_H-M"))
                    {
                        System.String value_Renamed = line.Substring(29).Trim();
                        crystal.SpaceGroup = value_Renamed;
                    }
                    else
                    {
                        // skip command
                        //logger.warn("Skipping command: " + command);
                        line = input.ReadLine();
                        if (line.StartsWith(";"))
                        {
                            //logger.debug("Skipping block content");
                            line = input.ReadLine().Trim();
                            while (!line.Equals(";"))
                            {
                                line = input.ReadLine().Trim();
                                //logger.debug("Skipping block line: " + line);
                            }
                            line = input.ReadLine();
                        }
                    }
                }
                line = input.ReadLine();
            }
            //logger.info("Adding crystal to file with #atoms: " + crystal.AtomCount);
            model.Crystal = crystal;
            seq.addChemModel(model);
            file.addChemSequence(seq);
            return file;
        }