Logo Search packages:      
Sourcecode: cdk version File versions  Download package

void org::openscience::cdk::io::MDLWriter::writeMolecule ( IMolecule  container  )  throws Exception [inline]

Writes a Molecule to an OutputStream in MDL sdf format.

Parameters:
container Molecule that is written to an OutputStream

Definition at line 248 of file MDLWriter.java.

References formatMDLFloat(), formatMDLInt(), formatMDLString(), org::openscience::cdk::interfaces::IBond::getAtom(), org::openscience::cdk::interfaces::IBond::getAtomCount(), org::openscience::cdk::interfaces::IChemObject::getBuilder(), org::openscience::cdk::interfaces::IAtomType::getFormalCharge(), org::openscience::cdk::interfaces::IIsotope::getMassNumber(), org::openscience::cdk::interfaces::IBond::getOrder(), org::openscience::cdk::interfaces::IAtom::getPoint2d(), org::openscience::cdk::interfaces::IAtom::getPoint3d(), org::openscience::cdk::interfaces::IBond::getStereo(), org::openscience::cdk::interfaces::IElement::getSymbol(), and org::openscience::cdk::tools::LoggingTool::warn().

Referenced by write(), and writeMoleculeSet().

                                                                    {
        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();
    }


Generated by  Doxygen 1.6.0   Back to index