📄 mdlwriter.java
字号:
* @param container Molecule that is written to an OutputStream */ public void writeMolecule(IMolecule container) throws Exception { String line = ""; // taking care of the $$$$ signs: // we do not write such a sign at the end of the first molecule, thus we have to write on BEFORE the second molecule if(moleculeNumber == 2) { writer.write("$$$$"); writer.newLine(); } // write header block // lines get shortened to 80 chars, that's in the spec String title = (String)container.getProperty(CDKConstants.TITLE); if (title == null) title = ""; if(title.length()>80) title=title.substring(0,80); writer.write(title + "\n"); /* From CTX spec * This line has the format: * IIPPPPPPPPMMDDYYHHmmddSSssssssssssEEEEEEEEEEEERRRRRR * (FORTRAN: A2<--A8--><---A10-->A2I2<--F10.5-><---F12.5--><-I6-> ) * User's first and last initials (l), program name (P), * date/time (M/D/Y,H:m), dimensional codes (d), scaling factors (S, s), * energy (E) if modeling program input, internal registry number (R) * if input through MDL form. * A blank line can be substituted for line 2. */ writer.write(" CDK "); writer.write(new SimpleDateFormat("M/d/y,H:m",Locale.US).format( Calendar.getInstance(TimeZone.getDefault()).getTime()) ); writer.write('\n'); String comment = (String)container.getProperty(CDKConstants.REMARK); if (comment == null) comment = ""; if(comment.length()>80) comment=comment.substring(0,80); writer.write(comment + "\n"); // write Counts line line += formatMDLInt(container.getAtomCount(), 3); line += formatMDLInt(container.getBondCount(), 3); line += " 0 0 0 0 0 0 0 0999 V2000\n"; writer.write(line); // write Atom block for (int f = 0; f < container.getAtomCount(); f++) { IAtom atom = container.getAtom(f); line = ""; if (atom.getPoint3d() != null) { line += formatMDLFloat((float) atom.getPoint3d().x); line += formatMDLFloat((float) atom.getPoint3d().y); line += formatMDLFloat((float) atom.getPoint3d().z) + " "; } else if (atom.getPoint2d() != null) { line += formatMDLFloat((float) atom.getPoint2d().x); line += formatMDLFloat((float) atom.getPoint2d().y); line += " 0.0000 "; } else { // if no coordinates available, then output a number // of zeros line += formatMDLFloat((float)0.0); line += formatMDLFloat((float)0.0); line += formatMDLFloat((float)0.0) + " "; } if(container.getAtom(f) instanceof IPseudoAtom) line += formatMDLString(((IPseudoAtom) container.getAtom(f)).getLabel(), 3); else line += formatMDLString(container.getAtom(f).getSymbol(), 3); line += " 0 0 0 0 0 0 0 0 0 0 0 0"; writer.write(line); writer.newLine(); } // write Bond block Iterator bonds = container.bonds(); while (bonds.hasNext()) { IBond bond = (IBond) bonds.next(); if (bond.getAtomCount() != 2) { logger.warn("Skipping bond with more/less than two atoms: " + bond); } else { if (bond.getStereo() == CDKConstants.STEREO_BOND_UP_INV || bond.getStereo() == CDKConstants.STEREO_BOND_DOWN_INV) { // turn around atom coding to correct for inv stereo line = formatMDLInt(container.getAtomNumber(bond.getAtom(1)) + 1,3); line += formatMDLInt(container.getAtomNumber(bond.getAtom(0)) + 1,3); } else { line = formatMDLInt(container.getAtomNumber(bond.getAtom(0)) + 1,3); line += formatMDLInt(container.getAtomNumber(bond.getAtom(1)) + 1,3); } line += formatMDLInt((int)bond.getOrder(),3); line += " "; switch(bond.getStereo()){ case CDKConstants.STEREO_BOND_UP: line += "1"; break; case CDKConstants.STEREO_BOND_UP_INV: line += "1"; break; case CDKConstants.STEREO_BOND_DOWN: line += "6"; break; case CDKConstants.STEREO_BOND_DOWN_INV: line += "6"; break; default: line += "0"; } line += " 0 0 0 "; writer.write(line); writer.newLine(); } } // write formal atomic charges for (int i = 0; i < container.getAtomCount(); i++) { IAtom atom = container.getAtom(i); int charge = atom.getFormalCharge(); if (charge != 0) { writer.write("M CHG 1 "); writer.write(formatMDLInt(i+1,3)); writer.write(" "); writer.write(formatMDLInt(charge,3)); writer.newLine(); } } // write formal isotope information for (int i = 0; i < container.getAtomCount(); i++) { IAtom atom = container.getAtom(i); if (!(atom instanceof IPseudoAtom)) { int atomicMass = atom.getMassNumber(); int majorMass = IsotopeFactory.getInstance(atom.getBuilder()).getMajorIsotope(atom.getSymbol()).getMassNumber(); if (atomicMass != 0 && atomicMass != majorMass) { writer.write("M ISO 1 "); writer.write(formatMDLInt(i+1,3)); writer.write(" "); writer.write(formatMDLInt(atomicMass,3)); writer.newLine(); } } } // close molecule writer.write("M END"); writer.newLine(); //write sdfields, if any if(sdFields!=null){ Set set = sdFields.keySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Object element = iterator.next(); writer.write("> <"+(String)element+">"); writer.newLine(); writer.write(sdFields.get(element).toString()); writer.newLine(); writer.newLine(); } } // taking care of the $$$$ signs: // we write such a sign at the end of all except the first molecule if(moleculeNumber != 1) { writer.write("$$$$"); writer.newLine(); } moleculeNumber++; writer.flush(); } /** * Formats an int to fit into the connectiontable and changes it * to a String. * * @param i The int to be formated * @param l Length of the String * @return The String to be written into the connectiontable */ private String formatMDLInt(int i, int l) { String s = "", fs = ""; NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH); nf.setParseIntegerOnly(true); nf.setMinimumIntegerDigits(1); nf.setMaximumIntegerDigits(l); nf.setGroupingUsed(false); s = nf.format(i); l = l - s.length(); for (int f = 0; f < l; f++) fs += " "; fs += s; return fs; } /** * Formats a float to fit into the connectiontable and changes it * to a String. * * @param fl The float to be formated * @return The String to be written into the connectiontable */ private String formatMDLFloat(float fl) { String s = "", fs = ""; int l; NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH); nf.setMinimumIntegerDigits(1); nf.setMaximumIntegerDigits(4); nf.setMinimumFractionDigits(4); nf.setMaximumFractionDigits(4); nf.setGroupingUsed(false); s = nf.format(fl); l = 10 - s.length(); for (int f = 0; f < l; f++) fs += " "; fs += s; return fs; } /** * Formats a String to fit into the connectiontable. * * @param s The String to be formated * @param le The length of the String * @return The String to be written in the connectiontable */ private String formatMDLString(String s, int le) { s = s.trim(); if (s.length() > le) return s.substring(0, le); int l; l = le - s.length(); for (int f = 0; f < l; f++) s += " "; return s; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -