LoginSignup
0
0

More than 5 years have passed since last update.

CDK(Chemical Development Toolkit)でSMILESを生成する

Last updated at Posted at 2019-03-17

はじめに

SDFファイルを読み込んでSMILESに吐き出すプログラムを作成した。併せてSMILES生成のオプションについても確認した。

環境

  • Windows 10
  • Java 11
  • CDK 2.2

ソースコード

以下のソースでできた。

Sdf2Smiles.java
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.smiles.SmiFlavor;
import org.openscience.cdk.smiles.SmilesGenerator;
import org.openscience.cdk.tools.manipulator.AtomContainerManipulator;

public class Sdf2Smiles {

    public static void main(String args[]){

        if(args.length!=1){
            System.err.println("Sdf2Smiles <sd-file>");
            System.exit(1);
        }
        FileInputStream fis = null;
        IteratingSDFReader isr = null;
        try{
            fis = new FileInputStream(new File(args[0]));
            isr = new IteratingSDFReader(fis, DefaultChemObjectBuilder.getInstance() );

            while( isr.hasNext() ) {
                IAtomContainer mol = (IAtomContainer) isr.next();
                String id = (String) mol.getProperty("cdk:Title");
                SmilesGenerator sg = new SmilesGenerator(SmiFlavor.Generic |SmiFlavor.UseAromaticSymbols);
                AtomContainerManipulator acm = new AtomContainerManipulator();
                try {
                    // sg.setUseAromaticityFlag(true);
                    String can = sg.create(acm.removeHydrogens(mol));
                    System.out.printf("%s\t%s\n", can, id);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(isr != null) {
                try {
                    isr.close();
                }catch(IOException e2) {
                    e2.printStackTrace();;
                }
            }
            if(fis != null) {
                try {
                    fis.close();
                }catch(IOException e2) {
                    e2.printStackTrace();
                }
            }
        }
    }
}

結果はこんな感じ。

CCCCC   n-pentane
C1CCCC1 cyclopentane
CCCCCC  n-hexane
CCCC(C)C    2-methylpentane
CCC(C)(C)C  2,2-dimethylbutane
C1CCCCC1    cyclohexane
C1CCCC1C    methylcyclopentane
CCCCCCC n-heptane
C1CCCCC1C   methylcyclohexane
CCCCCCCC    n-octane

SMILES生成オプションについて

SmilesGeneratorのコンストラクタの引数によって、SMILESの生成方法を指定できる。

オプション Canonical 同位体または立体性をエンコード
SmiFlavor.Generic × ×
SmiFlavor.Unique ×
SmiFlavor.Isomeric ×
SmiFlavor.Absolute

上は代表的なものであるが、SmiFlavor.Stereoや、SmiFlavor.AtomicMass等により詳細なオプションを指定することも可能である。さらに、SmiFlavor.UseAromaticSymbolsによってAromatic記号を使うかどうかも指定できる。もっとも各原子が芳香環に属するかどうかは、IAtom#setIsAromatic等によって明示的に指定する必要がありそうだ。(そこが最も面倒なところなのだが。。)

参考文献

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0