public void TestNewMapping_IChemObject_IChemObject() { IChemObjectBuilder builder = RootObject.Builder; IMapping mapping = builder.NewMapping(builder.NewChemObject(), builder.NewChemObject()); Assert.IsNotNull(mapping); }
/// <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); }
/// <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); }