public void TestReactionCustomization() { var writer = new StringWriter(); var reaction = builder.NewReaction(); reaction.Id = "reaction1"; IAtomContainer reactant = reaction.Builder.NewAtomContainer(); reactant.Id = "react"; IAtomContainer product = reaction.Builder.NewAtomContainer(); product.Id = "product"; IAtomContainer agent = reaction.Builder.NewAtomContainer(); agent.Id = "agent"; reaction.Reactants.Add(reactant); reaction.Products.Add(product); reaction.Agents.Add(agent); var cmlWriter = new CMLWriter(writer); cmlWriter.Write(reaction); cmlWriter.Close(); string cmlContent = writer.ToString(); Debug.WriteLine("****************************** TestReactionCustomization()"); Debug.WriteLine(cmlContent); Debug.WriteLine("******************************"); Assert.IsTrue(cmlContent.IndexOf("<reaction id=\"reaction1") != -1); Assert.IsTrue(cmlContent.IndexOf("<molecule id=\"react") != -1); Assert.IsTrue(cmlContent.IndexOf("<molecule id=\"product") != -1); Assert.IsTrue(cmlContent.IndexOf("<molecule id=\"agent") != -1); }
public void TestNewReaction() { IChemObjectBuilder builder = RootObject.Builder; IReaction reaction = builder.NewReaction(); Assert.IsNotNull(reaction); }
public void SetUp() { molecule1 = builder.NewAtomContainer(); atomInMol1 = builder.NewAtom("Cl"); molecule1.Atoms.Add(atomInMol1); molecule1.Atoms.Add(builder.NewAtom("Cl")); bondInMol1 = builder.NewBond(atomInMol1, molecule1.Atoms[1]); molecule1.Bonds.Add(bondInMol1); molecule2 = builder.NewAtomContainer(); atomInMol2 = builder.NewAtom("O"); atomInMol2.ImplicitHydrogenCount = 2; molecule2.Atoms.Add(atomInMol2); moleculeSet = builder.NewChemObjectSet <IAtomContainer>(); moleculeSet.Add(molecule1); moleculeSet.Add(molecule2); reaction = builder.NewReaction(); reaction.Reactants.Add(molecule1); reaction.Products.Add(molecule2); reactionSet = builder.NewReactionSet(); reactionSet.Add(reaction); chemModel = builder.NewChemModel(); chemModel.MoleculeSet = moleculeSet; chemModel.ReactionSet = reactionSet; chemSequence1 = builder.NewChemSequence(); chemSequence1.Add(chemModel); chemSequence2 = builder.NewChemSequence(); chemFile = builder.NewChemFile(); chemFile.Add(chemSequence1); chemFile.Add(chemSequence2); }
public void TestGetBondLengthAverageIReaction() { var atom1 = builder.NewAtom("C"); atom1.Point2D = Vector2.Zero; var atom2 = builder.NewAtom("C"); atom2.Point2D = new Vector2(1, 0); var acont = builder.NewAtomContainer(); var reaction = builder.NewReaction(); reaction.Reactants.Add(acont); acont.Atoms.Add(atom1); acont.Atoms.Add(atom2); acont.AddBond(acont.Atoms[0], acont.Atoms[1], BondOrder.Single); Assert.AreEqual(1.0, GeometryUtil.GetBondLengthAverage(reaction), 0.0); }
public void TestReadReactions1() { string filename1 = "NCDK.Data.MDL.reaction-1.rxn"; Trace.TraceInformation("Testing: " + filename1); var ins1 = ResourceLoader.GetAsStream(filename1); MDLRXNReader reader1 = new MDLRXNReader(ins1); IReaction reaction1 = builder.NewReaction(); reaction1 = (IReaction)reader1.Read(reaction1); reader1.Close(); Assert.IsNotNull(reaction1); Assert.AreEqual(2, reaction1.Reactants.Count); Assert.AreEqual(1, reaction1.Products.Count); var educts = reaction1.Reactants; // Check Atom symbols of first educt string[] atomSymbolsOfEduct1 = { "C", "C", "O", "Cl" }; for (int i = 0; i < educts[0].Atoms.Count; i++) { Assert.AreEqual(atomSymbolsOfEduct1[i], educts[0].Atoms[i].Symbol); } // Check Atom symbols of second educt for (int i = 0; i < educts[1].Atoms.Count; i++) { Assert.AreEqual("C", educts[1].Atoms[i].Symbol); } // Check Atom symbols of first product var products = reaction1.Products; string[] atomSymbolsOfProduct1 = { "C", "C", "C", "C", "C", "C", "C", "O", "C" }; for (int i = 0; i < products[0].Atoms.Count; i++) { Assert.AreEqual(atomSymbolsOfProduct1[i], products[0].Atoms[i].Symbol); } }
public void TestAddReaction_IReaction_int() { ReactionChain chain = new ReactionChain(); IReaction reaction1 = builder.NewReaction(); reaction1.Id = "reaction1"; IReaction reaction2 = builder.NewReaction(); reaction1.Id = "reaction2"; IReaction reaction3 = builder.NewReaction(); reaction1.Id = "reaction3"; chain.Add(reaction1, 0); chain.Add(reaction2, 1); chain.Add(reaction3, 2); Assert.IsNotNull(chain); }
public void TestIsEmpty_ReactionSet() { var model = (IChemModel)NewChemObject(); IChemObjectBuilder builder = model.Builder; IAtomContainer molecule = builder.NewAtomContainer(); IReaction reaction = builder.NewReaction(); reaction.Reactants.Add(molecule); IReactionSet set = builder.NewReactionSet(); model.ReactionSet = set; Assert.IsTrue(model.IsEmpty(), "model has an empty reaction set and should be empty"); set.Add(reaction); Assert.IsFalse(model.IsEmpty(), "model has a reaction set and should not be empty"); model.ReactionSet = null; Assert.IsTrue(model.IsEmpty(), "model has no reaction set"); }
public void TestBug1851202() { string filename1 = "NCDK.Data.MDL.0002.stg01.rxn"; Trace.TraceInformation("Testing: " + filename1); var ins1 = ResourceLoader.GetAsStream(filename1); MDLRXNV2000Reader reader = new MDLRXNV2000Reader(ins1, ChemObjectReaderMode.Strict); IReaction reaction = (IReaction)reader.Read(builder.NewReaction()); Assert.IsNotNull(reaction); IAtomContainer reactant = reaction.Reactants[0]; IAtomContainer product = reaction.Products[0]; Fingerprinter fingerprinter = new Fingerprinter(64 * 26, 8); Assert.IsNotNull(fingerprinter.GetBitFingerprint(reactant)); Assert.IsNotNull(fingerprinter.GetBitFingerprint(product)); }
public void TestRoundtrip() { IReaction reaction = builder.NewReaction(); IAtomContainer hydroxide = builder.NewAtomContainer(); hydroxide.Atoms.Add(builder.NewAtom("O")); reaction.Reactants.Add(hydroxide); IAtomContainer proton = builder.NewAtomContainer(); proton.Atoms.Add(builder.NewAtom("H")); reaction.Reactants.Add(proton); IAtomContainer water = builder.NewAtomContainer(); water.Atoms.Add(builder.NewAtom("O")); reaction.Products.Add(water); reaction.Mappings.Add(new Mapping(hydroxide.Atoms[0], water.Atoms[0])); // now serialize to MDL RXN StringWriter writer = new StringWriter(); string file = ""; MDLRXNWriter mdlWriter = new MDLRXNWriter(writer); mdlWriter.Write(reaction); mdlWriter.Close(); file = writer.ToString(); Assert.IsTrue(file.Length > 0); // now deserialize the MDL RXN output IReaction reaction2 = builder.NewReaction(); MDLRXNReader reader = new MDLRXNReader(new StringReader(file)); reaction2 = (IReaction)reader.Read(reaction2); reader.Close(); Assert.AreEqual(2, reaction2.Reactants.Count); Assert.AreEqual(1, reaction2.Products.Count); Assert.AreEqual(1, reaction2.Mappings.Count); }
/// <summary> /// Parse a reaction SMILES. /// </summary> /// <param name="smiles">The SMILES string to parse</param> /// <returns>An instance of <see cref="IReaction"/></returns> /// <exception cref="InvalidSmilesException">if the string cannot be parsed</exception> /// <seealso cref="ParseSmiles(string)"/> public IReaction ParseReactionSmiles(string smiles) { if (!smiles.Contains(">")) { throw new InvalidSmilesException("Not a reaction SMILES: " + smiles); } var first = smiles.IndexOf('>'); var second = smiles.IndexOf('>', first + 1); if (second < 0) { throw new InvalidSmilesException("Invalid reaction SMILES:" + smiles); } var reactants = smiles.Substring(0, first); var agents = smiles.Substring(first + 1, second - (first + 1)); var products = smiles.Substring(second + 1, smiles.Length - (second + 1)); var reaction = builder.NewReaction(); // add reactants if (!(reactants.Count() == 0)) { var reactantContainer = ParseSmiles(reactants, true); var reactantSet = ConnectivityChecker.PartitionIntoMolecules(reactantContainer); foreach (var reactant in reactantSet) { reaction.Reactants.Add(reactant); } } // add agents if (!(agents.Count() == 0)) { var agentContainer = ParseSmiles(agents, true); var agentSet = ConnectivityChecker.PartitionIntoMolecules(agentContainer); foreach (var agent in agentSet) { reaction.Agents.Add(agent); } } string title = null; // add products if (!(products.Count() == 0)) { var productContainer = ParseSmiles(products, true); var productSet = ConnectivityChecker.PartitionIntoMolecules(productContainer); foreach (var product in productSet) { reaction.Products.Add(product); } reaction.SetProperty(CDKPropertyName.Title, title = productContainer.Title); } try { //CXSMILES layer ParseRxnCXSMILES(title, reaction); } catch (Exception e) { //e.StackTrace throw new InvalidSmilesException("Error parsing CXSMILES:" + e.Message); } return(reaction); }
/// <summary> /// Read a Reaction from a file in MDL RXN format /// </summary> /// <returns>The Reaction that was read from the MDL file.</returns> private IReaction ReadReaction(IChemObjectBuilder builder) { IReaction reaction = builder.NewReaction(); try { input.ReadLine(); // first line should be $RXN input.ReadLine(); // second line input.ReadLine(); // third line input.ReadLine(); // fourth line } catch (IOException exception) { Debug.WriteLine(exception); throw new CDKException("Error while reading header of RXN file", exception); } int reactantCount = 0; int productCount = 0; int agentCount = 0; try { string countsLine = input.ReadLine(); // this line contains the number of reactants and products var tokenizer = Strings.Tokenize(countsLine).GetEnumerator(); tokenizer.MoveNext(); reactantCount = int.Parse(tokenizer.Current, NumberFormatInfo.InvariantInfo); Trace.TraceInformation("Expecting " + reactantCount + " reactants in file"); tokenizer.MoveNext(); productCount = int.Parse(tokenizer.Current, NumberFormatInfo.InvariantInfo); if (tokenizer.MoveNext()) { agentCount = int.Parse(tokenizer.Current, NumberFormatInfo.InvariantInfo); // ChemAxon extension, technically BIOVIA now support this but // not documented yet if (ReaderMode == ChemObjectReaderMode.Strict && agentCount > 0) { throw new CDKException("RXN files uses agent count extension"); } } Trace.TraceInformation("Expecting " + productCount + " products in file"); } catch (Exception exception) { if (exception is IOException | exception is FormatException) { Debug.WriteLine(exception); throw new CDKException("Error while counts line of RXN file", exception); } throw; } // now read the reactants try { for (int i = 1; i <= reactantCount; i++) { var molFile = new StringBuilder(); input.ReadLine(); // announceMDLFileLine string molFileLine = ""; do { molFileLine = input.ReadLine(); molFile.Append(molFileLine); molFile.Append('\n'); } while (!string.Equals(molFileLine, "M END", StringComparison.Ordinal)); // read MDL molfile content // Changed this to mdlv2000 reader MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.ToString()), base.ReaderMode); IAtomContainer reactant = (IAtomContainer)reader.Read(builder.NewAtomContainer()); reader.Close(); // add reactant reaction.Reactants.Add(reactant); } } catch (CDKException) { // rethrow exception from MDLReader throw; } catch (Exception exception) { if (exception is IOException | exception is ArgumentException) { Debug.WriteLine(exception); throw new CDKException("Error while reading products", exception); } throw; } // now read the products try { for (int i = 1; i <= agentCount; i++) { var molFile = new StringBuilder(); input.ReadLine(); // String announceMDLFileLine = string molFileLine = ""; do { molFileLine = input.ReadLine(); molFile.Append(molFileLine); molFile.Append('\n'); } while (!string.Equals(molFileLine, "M END", StringComparison.Ordinal)); // read MDL molfile content MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.ToString())); IAtomContainer product = (IAtomContainer)reader.Read(builder.NewAtomContainer()); reader.Close(); // add reactant reaction.Agents.Add(product); } } catch (CDKException) { // rethrow exception from MDLReader throw; } catch (Exception exception) { if (exception is IOException | exception is ArgumentException) { Debug.WriteLine(exception); throw new CDKException("Error while reading reactant", exception); } throw; } // now read the products try { for (int i = 1; i <= productCount; i++) { var molFile = new StringBuilder(); input.ReadLine(); // string announceMDLFileLine = string molFileLine = ""; do { molFileLine = input.ReadLine(); molFile.Append(molFileLine); molFile.Append('\n'); } while (!string.Equals(molFileLine, "M END", StringComparison.Ordinal)); // read MDL molfile content MDLV2000Reader reader = new MDLV2000Reader(new StringReader(molFile.ToString())); IAtomContainer product = (IAtomContainer)reader.Read(builder.NewAtomContainer()); reader.Close(); // add reactant reaction.Products.Add(product); } } catch (CDKException) { // rethrow exception from MDLReader throw; } catch (Exception exception) { if (exception is IOException | exception is ArgumentException) { Debug.WriteLine(exception); throw new CDKException("Error while reading products", exception); } throw; } // now try to map things, if wanted Trace.TraceInformation("Reading atom-atom mapping from file"); // distribute all atoms over two AtomContainer's IAtomContainer reactingSide = builder.NewAtomContainer(); foreach (var molecule in reaction.Reactants) { reactingSide.Add(molecule); } IAtomContainer producedSide = builder.NewAtomContainer(); foreach (var molecule in reaction.Products) { producedSide.Add(molecule); } // map the atoms int mappingCount = 0; // IAtom[] reactantAtoms = reactingSide.GetAtoms(); // IAtom[] producedAtoms = producedSide.GetAtoms(); for (int i = 0; i < reactingSide.Atoms.Count; i++) { for (int j = 0; j < producedSide.Atoms.Count; j++) { IAtom eductAtom = reactingSide.Atoms[i]; IAtom productAtom = producedSide.Atoms[j]; if (eductAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping) != null && eductAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping).Equals( productAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping))) { reaction.Mappings.Add(builder.NewMapping(eductAtom, productAtom)); mappingCount++; break; } } } Trace.TraceInformation("Mapped atom pairs: " + mappingCount); return(reaction); }
private IReaction ReadReaction(IChemObjectBuilder builder) { IReaction reaction = builder.NewReaction(); ReadLine(); // first line should be $RXN ReadLine(); // second line ReadLine(); // third line ReadLine(); // fourth line int reactantCount = 0; int productCount = 0; bool foundCOUNTS = false; while (!foundCOUNTS) { string command = ReadCommand(); if (command.StartsWith("COUNTS", StringComparison.Ordinal)) { var tokenizer = Strings.Tokenize(command); try { reactantCount = int.Parse(tokenizer[1], NumberFormatInfo.InvariantInfo); Trace.TraceInformation($"Expecting {reactantCount} reactants in file"); productCount = int.Parse(tokenizer[2], NumberFormatInfo.InvariantInfo); Trace.TraceInformation($"Expecting {productCount } products in file"); } catch (Exception exception) { Debug.WriteLine(exception); throw new CDKException("Error while counts line of RXN file", exception); } foundCOUNTS = true; } else { Trace.TraceWarning("Waiting for COUNTS line, but found: " + command); } } // now read the reactants for (int i = 1; i <= reactantCount; i++) { var molFile = new StringBuilder(); string announceMDLFileLine = ReadCommand(); if (!string.Equals(announceMDLFileLine, "BEGIN REACTANT", StringComparison.Ordinal)) { string error = "Excepted start of reactant, but found: " + announceMDLFileLine; Trace.TraceError(error); throw new CDKException(error); } string molFileLine = ""; while (!molFileLine.EndsWith("END REACTANT", StringComparison.Ordinal)) { molFileLine = ReadLine(); molFile.Append(molFileLine); molFile.Append('\n'); } ; try { // read MDL molfile content MDLV3000Reader reader = new MDLV3000Reader(new StringReader(molFile.ToString()), base.ReaderMode); IAtomContainer reactant = (IAtomContainer)reader.Read(builder.NewAtomContainer()); reader.Close(); // add reactant reaction.Reactants.Add(reactant); } catch (Exception exception) { if (!(exception is ArgumentException || exception is CDKException || exception is IOException)) { throw; } string error = "Error while reading reactant: " + exception.Message; Trace.TraceError(error); Debug.WriteLine(exception); throw new CDKException(error, exception); } } // now read the products for (int i = 1; i <= productCount; i++) { var molFile = new StringBuilder(); string announceMDLFileLine = ReadCommand(); if (!string.Equals(announceMDLFileLine, "BEGIN PRODUCT", StringComparison.Ordinal)) { string error = "Excepted start of product, but found: " + announceMDLFileLine; Trace.TraceError(error); throw new CDKException(error); } string molFileLine = ""; while (!molFileLine.EndsWith("END PRODUCT", StringComparison.Ordinal)) { molFileLine = ReadLine(); molFile.Append(molFileLine); molFile.Append('\n'); } ; try { // read MDL molfile content MDLV3000Reader reader = new MDLV3000Reader(new StringReader(molFile.ToString())); IAtomContainer product = (IAtomContainer)reader.Read(builder.NewAtomContainer()); reader.Close(); // add product reaction.Products.Add(product); } catch (Exception exception) { if (!(exception is ArgumentException || exception is CDKException || exception is IOException)) { throw; } string error = "Error while reading product: " + exception.Message; Trace.TraceError(error); Debug.WriteLine(exception); throw new CDKException(error, exception); } } return(reaction); }
/// <summary> /// Read a Reaction from a file in MDL RXN format /// </summary> /// <returns>The Reaction that was read from the MDL file.</returns> private IReaction ReadReaction(IChemObjectBuilder builder) { IReaction reaction = builder.NewReaction(); try { input.ReadLine(); // first line should be $RXN input.ReadLine(); // second line input.ReadLine(); // third line input.ReadLine(); // fourth line } catch (IOException exception) { Debug.WriteLine(exception); throw new CDKException("Error while reading header of RXN file", exception); } int numReactans = 0; int numProducts = 0; int agentCount = 0; try { var countsLine = input.ReadLine(); // this line contains the number of reactants and products var tokenizer = Strings.Tokenize(countsLine).GetEnumerator(); tokenizer.MoveNext(); numReactans = int.Parse(tokenizer.Current, NumberFormatInfo.InvariantInfo); Trace.TraceInformation($"Expecting {numReactans} reactants in file"); tokenizer.MoveNext(); numProducts = int.Parse(tokenizer.Current, NumberFormatInfo.InvariantInfo); if (tokenizer.MoveNext()) { agentCount = int.Parse(tokenizer.Current, NumberFormatInfo.InvariantInfo); // ChemAxon extension, technically BIOVIA now support this but // not documented yet if (ReaderMode == ChemObjectReaderMode.Strict && agentCount > 0) { throw new CDKException("RXN files uses agent count extension"); } } Trace.TraceInformation($"Expecting {numProducts} products in file"); } catch (Exception exception) { if (exception is IOException | exception is FormatException) { Debug.WriteLine(exception); throw new CDKException("Error while counts line of RXN file", exception); } throw; } // now read the molecules try { var line = input.ReadLine(); if (line == null || !line.StartsWith("$MOL", StringComparison.Ordinal)) { throw new CDKException("Expected $MOL to start, was" + line); } var components = new List <IAtomContainer>(); var sb = new StringBuilder(); while ((line = input.ReadLine()) != null) { if (line.StartsWith("$MOL", StringComparison.Ordinal)) { ProcessMol(builder.NewAtomContainer(), components, sb); sb.Clear(); } else { sb.Append(line).Append('\n'); } } // last record if (sb.Length > 0) { ProcessMol(builder.NewAtomContainer(), components, sb); } foreach (var component in components.GetRange(0, numReactans)) { reaction.Reactants.Add(component); } foreach (var component in components.GetRange(numReactans, numProducts)) { reaction.Products.Add(component); } foreach (var component in components.GetRange(numReactans + numProducts, components.Count - (numReactans + numProducts))) { reaction.Agents.Add(component); } } catch (CDKException) { // rethrow exception from MDLReader throw; } catch (Exception exception) { if (exception is IOException | exception is ArgumentException) { Debug.WriteLine(exception); throw new CDKException("Error while reading reactant", exception); } throw; } // now try to map things, if wanted Trace.TraceInformation("Reading atom-atom mapping from file"); // distribute all atoms over two AtomContainer's var reactingSide = builder.NewAtomContainer(); foreach (var molecule in reaction.Reactants) { reactingSide.Add(molecule); } var producedSide = builder.NewAtomContainer(); foreach (var molecule in reaction.Products) { producedSide.Add(molecule); } // map the atoms int mappingCount = 0; for (int i = 0; i < reactingSide.Atoms.Count; i++) { for (int j = 0; j < producedSide.Atoms.Count; j++) { var eductAtom = reactingSide.Atoms[i]; var productAtom = producedSide.Atoms[j]; if (eductAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping) != null && eductAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping).Equals( productAtom.GetProperty <object>(CDKPropertyName.AtomAtomMapping))) { reaction.Mappings.Add(builder.NewMapping(eductAtom, productAtom)); mappingCount++; break; } } } Trace.TraceInformation("Mapped atom pairs: " + mappingCount); return(reaction); }
public void TestGetAllMolecules_IReactionSet() { IReactionSet reactionSet = builder.NewReactionSet(); reactionSet.Add(builder.NewReaction()); // 1 reactionSet.Add(builder.NewReaction()); // 2 Assert.AreEqual(0, ReactionSetManipulator.GetAllMolecules(reactionSet).Count); }
protected static IChemObject[] AcceptableChemObjects() { return(new IChemObject[] { builder.NewChemFile(), builder.NewChemModel(), builder.NewAtomContainer(), builder.NewReaction(), new RGroupQuery() }); }
public void TestGetAllMolecules_IReactionScheme() { IReactionScheme reactionScheme = builder.NewReactionScheme(); IReaction reaction1 = builder.NewReaction(); reaction1.Products.Add(builder.NewAtomContainer()); IReaction reaction2 = builder.NewReaction(); reaction2.Products.Add(builder.NewAtomContainer()); reactionScheme.Add(reaction1); // 1 reactionScheme.Add(reaction2); // 2 Assert.AreEqual(2, ReactionSchemeManipulator.GetAllAtomContainers(reactionScheme).Count); }