/// <summary> Reads partial atomic charges and add the to the given ChemModel. /// /// </summary> /// <param name="model">Description of the Parameter /// </param> /// <throws> CDKException Description of the Exception </throws> /// <throws> IOException Description of the Exception </throws> private void readPartialCharges(IChemModel model) { //logger.info("Reading partial atomic charges"); ISetOfMolecules moleculeSet = model.SetOfMolecules; IMolecule molecule = moleculeSet.getMolecule(0); System.String line = input.ReadLine(); // skip first line after "Total atomic charges" while (input.Peek() != -1) { line = input.ReadLine(); //logger.debug("Read charge block line: " + line); if ((line == null) || (line.IndexOf("Sum of Mulliken charges") >= 0)) { //logger.debug("End of charge block found"); break; } System.IO.StringReader sr = new System.IO.StringReader(line); SupportClass.StreamTokenizerSupport tokenizer = new SupportClass.StreamTokenizerSupport(sr); if (tokenizer.NextToken() == SupportClass.StreamTokenizerSupport.TT_NUMBER) { //UPGRADE_WARNING: Data types in Visual C# might be different. Verify the accuracy of narrowing conversions. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1042'" int atomCounter = (int)tokenizer.nval; tokenizer.NextToken(); // ignore the symbol double charge; if (tokenizer.NextToken() == SupportClass.StreamTokenizerSupport.TT_NUMBER) { charge = tokenizer.nval; //logger.debug("Found charge for atom " + atomCounter + ": " + charge); } else { throw new CDKException("Error while reading charge: expected double."); } IAtom atom = molecule.getAtomAt(atomCounter - 1); atom.setCharge(charge); } } }
private IChemModel readChemModel(IChemModel model) { int[] atoms = new int[1]; double[] atomxs = new double[1]; double[] atomys = new double[1]; double[] atomzs = new double[1]; double[] atomcharges = new double[1]; int[] bondatomid1 = new int[1]; int[] bondatomid2 = new int[1]; int[] bondorder = new int[1]; int numberOfAtoms = 0; int numberOfBonds = 0; try { System.String line = input.ReadLine(); while (line != null) { SupportClass.Tokenizer st = new SupportClass.Tokenizer(line); System.String command = st.NextToken(); if ("!Header".Equals(command)) { //logger.warn("Ignoring header"); } else if ("!Info".Equals(command)) { //logger.warn("Ignoring info"); } else if ("!Atoms".Equals(command)) { //logger.info("Reading atom block"); // determine number of atoms to read try { numberOfAtoms = System.Int32.Parse(st.NextToken()); //logger.debug(" #atoms: " + numberOfAtoms); atoms = new int[numberOfAtoms]; atomxs = new double[numberOfAtoms]; atomys = new double[numberOfAtoms]; atomzs = new double[numberOfAtoms]; atomcharges = new double[numberOfAtoms]; for (int i = 0; i < numberOfAtoms; i++) { line = input.ReadLine(); SupportClass.Tokenizer atomInfoFields = new SupportClass.Tokenizer(line); int atomID = System.Int32.Parse(atomInfoFields.NextToken()); atoms[atomID] = System.Int32.Parse(atomInfoFields.NextToken()); //logger.debug("Set atomic number of atom (" + atomID + ") to: " + atoms[atomID]); } } catch (System.Exception exception) { //logger.error("Error while reading Atoms block"); //logger.debug(exception); } } else if ("!Bonds".Equals(command)) { //logger.info("Reading bond block"); try { // determine number of bonds to read numberOfBonds = System.Int32.Parse(st.NextToken()); bondatomid1 = new int[numberOfAtoms]; bondatomid2 = new int[numberOfAtoms]; bondorder = new int[numberOfAtoms]; for (int i = 0; i < numberOfBonds; i++) { line = input.ReadLine(); SupportClass.Tokenizer bondInfoFields = new SupportClass.Tokenizer(line); bondatomid1[i] = System.Int32.Parse(bondInfoFields.NextToken()); bondatomid2[i] = System.Int32.Parse(bondInfoFields.NextToken()); System.String order = bondInfoFields.NextToken(); if ("D".Equals(order)) { bondorder[i] = 2; } else if ("S".Equals(order)) { bondorder[i] = 1; } else if ("T".Equals(order)) { bondorder[i] = 3; } else { // ignore order, i.e. set to single //logger.warn("Unrecognized bond order, using single bond instead. Found: " + order); bondorder[i] = 1; } } } catch (System.Exception exception) { //logger.error("Error while reading Bonds block"); //logger.debug(exception); } } else if ("!Coord".Equals(command)) { //logger.info("Reading coordinate block"); try { for (int i = 0; i < numberOfAtoms; i++) { line = input.ReadLine(); SupportClass.Tokenizer atomInfoFields = new SupportClass.Tokenizer(line); int atomID = System.Int32.Parse(atomInfoFields.NextToken()); double x = System.Double.Parse(atomInfoFields.NextToken()); double y = System.Double.Parse(atomInfoFields.NextToken()); double z = System.Double.Parse(atomInfoFields.NextToken()); atomxs[atomID] = x; atomys[atomID] = y; atomzs[atomID] = z; } } catch (System.Exception exception) { //logger.error("Error while reading Coord block"); //logger.debug(exception); } } else if ("!Charges".Equals(command)) { //logger.info("Reading charges block"); try { for (int i = 0; i < numberOfAtoms; i++) { line = input.ReadLine(); SupportClass.Tokenizer atomInfoFields = new SupportClass.Tokenizer(line); int atomID = System.Int32.Parse(atomInfoFields.NextToken()); double charge = System.Double.Parse(atomInfoFields.NextToken()); atomcharges[atomID] = charge; } } catch (System.Exception exception) { //logger.error("Error while reading Charges block"); //logger.debug(exception); } } else if ("!End".Equals(command)) { //logger.info("Found end of file"); // Store atoms IAtomContainer container = model.Builder.newAtomContainer(); for (int i = 0; i < numberOfAtoms; i++) { try { IAtom atom = model.Builder.newAtom(IsotopeFactory.getInstance(container.Builder).getElementSymbol(atoms[i])); atom.AtomicNumber = atoms[i]; atom.setPoint3d(new Point3d(atomxs[i], atomys[i], atomzs[i])); atom.setCharge(atomcharges[i]); container.addAtom(atom); //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" //logger.debug("Stored atom: " + atom); } catch (System.Exception exception) { //logger.error("Cannot create an atom with atomic number: " + atoms[i]); //logger.debug(exception); } } // Store bonds for (int i = 0; i < numberOfBonds; i++) { container.addBond(bondatomid1[i], bondatomid2[i], bondorder[i]); } ISetOfMolecules moleculeSet = model.Builder.newSetOfMolecules(); moleculeSet.addMolecule(model.Builder.newMolecule(container)); model.SetOfMolecules = moleculeSet; return(model); } else { //logger.warn("Skipping line: " + line); } line = input.ReadLine(); } } catch (System.Exception exception) { //logger.error("Error while reading file"); //logger.debug(exception); } // this should not happen, file is lacking !End command return(null); }
/// <summary> Procedure required by the CDOInterface. This function is only /// supposed to be called by the JCFL library /// </summary> public virtual void setObjectProperty(System.String objectType, System.String propertyType, System.String propertyValue) { //logger.debug("objectType: " + objectType); //logger.debug("propType: " + propertyType); //logger.debug("property: " + propertyValue); if (objectType == null) { //logger.error("Cannot add property for null object"); return; } if (propertyType == null) { //logger.error("Cannot add property for null property type"); return; } if (propertyValue == null) { //logger.warn("Will not add null property"); return; } if (objectType.Equals("Molecule")) { if (propertyType.Equals("id")) { currentMolecule.ID = propertyValue; } else if (propertyType.Equals("inchi")) { currentMolecule.setProperty("iupac.nist.chemical.identifier", propertyValue); } else if (propertyType.Equals("pdb:residueName")) { currentMolecule.setProperty(new DictRef(propertyType, propertyValue), propertyValue); } else if (propertyType.Equals("pdb:oneLetterCode")) { currentMolecule.setProperty(new DictRef(propertyType, propertyValue), propertyValue); } else if (propertyType.Equals("pdb:id")) { currentMolecule.setProperty(new DictRef(propertyType, propertyValue), propertyValue); } else { //logger.warn("Not adding molecule property!"); } } else if (objectType.Equals("PseudoAtom")) { if (propertyType.Equals("label")) { if (!(currentAtom is IPseudoAtom)) { currentAtom = currentChemFile.Builder.newPseudoAtom(currentAtom); } ((IPseudoAtom)currentAtom).Label = propertyValue; } } else if (objectType.Equals("Atom")) { if (propertyType.Equals("type")) { if (propertyValue.Equals("R") && !(currentAtom is IPseudoAtom)) { currentAtom = currentChemFile.Builder.newPseudoAtom(currentAtom); } currentAtom.Symbol = propertyValue; } else if (propertyType.Equals("x2")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.X2d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("y2")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.Y2d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("x3")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.X3d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("y3")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.Y3d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("z3")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.Z3d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("xFract")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.FractX3d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("yFract")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.FractY3d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("zFract")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.FractZ3d = System.Double.Parse(propertyValue); } else if (propertyType.Equals("formalCharge")) { currentAtom.setFormalCharge(System.Int32.Parse(propertyValue)); } else if (propertyType.Equals("charge") || propertyType.Equals("partialCharge")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.setCharge(System.Double.Parse(propertyValue)); } else if (propertyType.Equals("hydrogenCount")) { currentAtom.setHydrogenCount(System.Int32.Parse(propertyValue)); } else if (propertyType.Equals("dictRef")) { currentAtom.setProperty("org.openscience.cdk.dict", propertyValue); } else if (propertyType.Equals("atomicNumber")) { currentAtom.AtomicNumber = System.Int32.Parse(propertyValue); } else if (propertyType.Equals("massNumber")) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" currentAtom.MassNumber = (int)(System.Double.Parse(propertyValue)); } else if (propertyType.Equals("spinMultiplicity")) { int unpairedElectrons = System.Int32.Parse(propertyValue) - 1; for (int i = 0; i < unpairedElectrons; i++) { currentMolecule.addElectronContainer(currentChemFile.Builder.newSingleElectron(currentAtom)); } } else if (propertyType.Equals("id")) { //logger.debug("id: ", propertyValue); currentAtom.ID = propertyValue; atomEnumeration[propertyValue] = (System.Int32)numberOfAtoms; } } else if (objectType.Equals("Bond")) { if (propertyType.Equals("atom1")) { bond_a1 = System.Int32.Parse(propertyValue); } else if (propertyType.Equals("atom2")) { bond_a2 = System.Int32.Parse(propertyValue); } else if (propertyType.Equals("id")) { //logger.debug("id: " + propertyValue); bond_id = propertyValue; } else if (propertyType.Equals("order")) { try { bond_order = System.Double.Parse(propertyValue); } catch (System.Exception e) { //logger.error("Cannot convert to double: " + propertyValue); bond_order = 1.0; } } else if (propertyType.Equals("stereo")) { if (propertyValue.Equals("H")) { bond_stereo = CDKConstants.STEREO_BOND_DOWN; } else if (propertyValue.Equals("W")) { bond_stereo = CDKConstants.STEREO_BOND_UP; } } } else if (objectType.Equals("Reaction")) { if (propertyType.Equals("id")) { currentReaction.ID = propertyValue; } } else if (objectType.Equals("SetOfReactions")) { if (propertyType.Equals("id")) { currentSetOfReactions.ID = propertyValue; } } else if (objectType.Equals("Reactant")) { if (propertyType.Equals("id")) { currentMolecule.ID = propertyValue; } } else if (objectType.Equals("Product")) { if (propertyType.Equals("id")) { currentMolecule.ID = propertyValue; } } else if (objectType.Equals("Crystal")) { // set these variables if (currentMolecule is ICrystal) { ICrystal current = (ICrystal)currentMolecule; if (propertyType.Equals("spacegroup")) { //logger.debug("Setting crystal spacegroup to: " + propertyValue); current.SpaceGroup = propertyValue; } else if (propertyType.Equals("z")) { try { //logger.debug("Setting z to: " + propertyValue); current.Z = System.Int32.Parse(propertyValue); } catch (System.FormatException exception) { //logger.error("Error in format of Z value"); } } } else { //logger.warn("Cannot add crystal cell parameters to a non " + "Crystal class!"); } } else if (objectType.Equals("a-axis") || objectType.Equals("b-axis") || objectType.Equals("c-axis")) { // set these variables if (currentMolecule is ICrystal) { //logger.debug("Setting axis (" + objectType + "): " + propertyValue); if (propertyType.Equals("x")) { crystal_axis_x = System.Double.Parse(propertyValue); } else if (propertyType.Equals("y")) { crystal_axis_y = System.Double.Parse(propertyValue); } else if (propertyType.Equals("z")) { crystal_axis_z = System.Double.Parse(propertyValue); } } else { //logger.warn("Cannot add crystal cell parameters to a non " + "Crystal class!"); } } //logger.debug("Object property set..."); }
// private procedures /// <summary> Private method that actually parses the input to read a ChemFile /// object. /// /// </summary> /// <returns> A ChemFile containing the data parsed from input. /// </returns> private IChemFile readChemFile(IChemFile file) { IChemSequence chemSequence = file.Builder.newChemSequence(); int number_of_atoms = 0; SupportClass.Tokenizer tokenizer; try { System.String line = input.ReadLine(); while (input.Peek() != -1 && line != null) { // parse frame by frame tokenizer = new SupportClass.Tokenizer(line, "\t ,;"); System.String token = tokenizer.NextToken(); number_of_atoms = System.Int32.Parse(token); System.String info = input.ReadLine(); IChemModel chemModel = file.Builder.newChemModel(); ISetOfMolecules setOfMolecules = file.Builder.newSetOfMolecules(); IMolecule m = file.Builder.newMolecule(); m.setProperty(CDKConstants.TITLE, info); for (int i = 0; i < number_of_atoms; i++) { line = input.ReadLine(); if (line == null) { break; } if (line.StartsWith("#") && line.Length > 1) { System.Object comment = m.getProperty(CDKConstants.COMMENT); if (comment == null) { comment = ""; } //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Object.toString' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" comment = comment.ToString() + line.Substring(1).Trim(); m.setProperty(CDKConstants.COMMENT, comment); //logger.debug("Found and set comment: ", comment); } else { double x = 0.0f, y = 0.0f, z = 0.0f; double charge = 0.0f; tokenizer = new SupportClass.Tokenizer(line, "\t ,;"); int fields = tokenizer.Count; if (fields < 4) { // this is an error but cannot throw exception } else { System.String atomtype = tokenizer.NextToken(); //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" x = (System.Double.Parse(tokenizer.NextToken())); //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" y = (System.Double.Parse(tokenizer.NextToken())); //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" z = (System.Double.Parse(tokenizer.NextToken())); if (fields == 8) { //UPGRADE_TODO: The differences in the format of parameters for constructor 'java.lang.Double.Double' may cause compilation errors. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1092'" charge = (System.Double.Parse(tokenizer.NextToken())); } IAtom atom = file.Builder.newAtom(atomtype, new Point3d(x, y, z)); atom.setCharge(charge); m.addAtom(atom); } } } setOfMolecules.addMolecule(m); chemModel.SetOfMolecules = setOfMolecules; chemSequence.addChemModel(chemModel); line = input.ReadLine(); } file.addChemSequence(chemSequence); } catch (System.IO.IOException e) { // should make some noise now file = null; //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" //logger.error("Error while reading file: ", e.Message); //logger.debug(e); } return(file); }
private IChemFile readChemFile(IChemFile file) { IChemSequence seq = file.Builder.newChemSequence(); IChemModel model = file.Builder.newChemModel(); ICrystal crystal = null; int lineNumber = 0; Vector3d a, b, c; try { System.String line = input.ReadLine(); while (input.Peek() != -1 && line != null) { //logger.debug((lineNumber++) + ": ", line); if (line.StartsWith("frame:")) { //logger.debug("found new frame"); model = file.Builder.newChemModel(); crystal = file.Builder.newCrystal(); // assume the file format is correct //logger.debug("reading spacegroup"); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); crystal.SpaceGroup = line; //logger.debug("reading unit cell axes"); Vector3d axis = new Vector3d(); //logger.debug("parsing A: "); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.x = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.y = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.z = FortranFormat.atof(line); crystal.A = axis; axis = new Vector3d(); //logger.debug("parsing B: "); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.x = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.y = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.z = FortranFormat.atof(line); crystal.B = axis; axis = new Vector3d(); //logger.debug("parsing C: "); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.x = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.y = FortranFormat.atof(line); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); axis.z = FortranFormat.atof(line); crystal.C = axis; //logger.debug("Crystal: ", crystal); a = crystal.A; b = crystal.B; c = crystal.C; //logger.debug("Reading number of atoms"); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); int atomsToRead = System.Int32.Parse(line); //logger.debug("Reading no molecules in assym unit cell"); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); int Z = System.Int32.Parse(line); crystal.Z = Z; System.String symbol; double charge; Point3d cart; for (int i = 1; i <= atomsToRead; i++) { cart = new Point3d(); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); symbol = line.Substring(0, (line.IndexOf(":")) - (0)); charge = System.Double.Parse(line.Substring(line.IndexOf(":") + 1)); line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); cart.x = System.Double.Parse(line); // x line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); cart.y = System.Double.Parse(line); // y line = input.ReadLine(); //logger.debug((lineNumber++) + ": ", line); cart.z = System.Double.Parse(line); // z IAtom atom = file.Builder.newAtom(symbol); atom.setCharge(charge); // convert cartesian coords to fractional Point3d frac = CrystalGeometryTools.cartesianToFractional(a, b, c, cart); atom.setFractionalPoint3d(frac); crystal.addAtom(atom); //logger.debug("Added atom: ", atom); } model.Crystal = crystal; seq.addChemModel(model); } else { //logger.debug("Format seems broken. Skipping these lines:"); while (!line.StartsWith("frame:") && input.Peek() != -1 && line != null) { line = input.ReadLine(); //logger.debug(lineNumber++ + ": ", line); } //logger.debug("Ok, resynched: found new frame"); } } file.addChemSequence(seq); } catch (System.Exception exception) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String message = "Error while parsing CrystClust file: " + exception.Message; //logger.error(message); //logger.debug(exception); throw new CDKException(message, exception); } return(file); }
/// <summary> Private method that actually parses the input to read a ChemFile /// object. In its current state it is able to read all the molecules /// (if more than one is present) in the specified HIN file. These are /// placed in a SetOfMolecules object which in turn is placed in a ChemModel /// which in turn is placed in a ChemSequence object and which is finally /// placed in a ChemFile object and returned to the user. /// /// </summary> /// <returns> A ChemFile containing the data parsed from input. /// </returns> private IChemFile readChemFile(IChemFile file) { IChemSequence chemSequence = file.Builder.newChemSequence(); IChemModel chemModel = file.Builder.newChemModel(); ISetOfMolecules setOfMolecules = file.Builder.newSetOfMolecules(); System.String info; SupportClass.Tokenizer tokenizer; try { System.String line; // read in header info while (true) { line = input.ReadLine(); if (line.IndexOf("mol ") == 0) { info = getMolName(line); break; } } // start the actual molecule data - may be multiple molecule line = input.ReadLine(); while (true) { if (line == null) { break; // end of file } if (line.IndexOf(';') == 0) { continue; // comment line } if (line.IndexOf("mol ") == 0) { info = getMolName(line); line = input.ReadLine(); } IMolecule m = file.Builder.newMolecule(); m.setProperty(CDKConstants.TITLE, info); // Each elemnt of cons is an ArrayList of length 3 which stores // the start and end indices and bond order of each bond // found in the HIN file. Before adding bonds we need to reduce // the number of bonds so as not to count the same bond twice System.Collections.ArrayList cons = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); // read data for current molecule int atomSerial = 0; while (true) { if (line.IndexOf("endmol ") >= 0) { break; } if (line.IndexOf(';') == 0) { continue; // comment line } tokenizer = new SupportClass.Tokenizer(line, " "); int ntoken = tokenizer.Count; System.String[] toks = new System.String[ntoken]; for (int i = 0; i < ntoken; i++) { toks[i] = tokenizer.NextToken(); } System.String sym = new System.Text.StringBuilder(toks[3]).ToString(); double charge = System.Double.Parse(toks[6]); double x = System.Double.Parse(toks[7]); double y = System.Double.Parse(toks[8]); double z = System.Double.Parse(toks[9]); int nbond = System.Int32.Parse(toks[10]); IAtom atom = file.Builder.newAtom(sym, new Point3d(x, y, z)); atom.setCharge(charge); for (int j = 11; j < (11 + nbond * 2); j += 2) { double bo = 1; int s = System.Int32.Parse(toks[j]) - 1; // since atoms start from 1 in the file char bt = toks[j + 1][0]; switch (bt) { case 's': bo = 1; break; case 'd': bo = 2; break; case 't': bo = 3; break; case 'a': bo = 1.5; break; } System.Collections.ArrayList ar = new System.Collections.ArrayList(3); ar.Add((System.Int32)atomSerial); ar.Add((System.Int32)s); ar.Add((double)bo); cons.Add(ar); } m.addAtom(atom); atomSerial++; line = input.ReadLine(); } // before storing the molecule lets include the connections // First we reduce the number of bonds stored, since we have // stored both, say, C1-H1 and H1-C1. System.Collections.ArrayList blist = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); for (int i = 0; i < cons.Count; i++) { System.Collections.ArrayList ar = (System.Collections.ArrayList)cons[i]; // make a reversed list System.Collections.ArrayList arev = new System.Collections.ArrayList(3); arev.Add(ar[1]); arev.Add(ar[0]); arev.Add(ar[2]); // Now see if ar or arev are already in blist if (blist.Contains(ar) || blist.Contains(arev)) { continue; } else { blist.Add(ar); } } // now just store all the bonds we have for (int i = 0; i < blist.Count; i++) { System.Collections.ArrayList ar = (System.Collections.ArrayList)blist[i]; int s = ((System.Int32)ar[0]); int e = ((System.Int32)ar[1]); double bo = ((System.Double)ar[2]); m.addBond(s, e, bo); } setOfMolecules.addMolecule(m); line = input.ReadLine(); // read in the 'mol N' } // got all the molecule in the HIN file (hopefully!) chemModel.SetOfMolecules = setOfMolecules; chemSequence.addChemModel(chemModel); file.addChemSequence(chemSequence); } catch (System.IO.IOException e) { // should make some noise now file = null; } return(file); }