File: example12.cpp

package info (click to toggle)
rdkit 201809.1%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,688 kB
  • sloc: cpp: 230,509; python: 70,501; java: 6,329; ansic: 5,427; sql: 1,899; yacc: 1,739; lex: 1,243; makefile: 445; xml: 229; fortran: 183; sh: 123; cs: 93
file content (56 lines) | stat: -rw-r--r-- 1,722 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//
// Preserving molecules - example12.cpp

#include <fstream>
#include <iostream>
#include <string>
#include <GraphMol/GraphMol.h>
#include <GraphMol/MolPickler.h>
#include <GraphMol/FileParsers/MolSupplier.h>
#include <GraphMol/SmilesParse/SmilesParse.h>
#include <GraphMol/SmilesParse/SmilesWrite.h>

int main( int argc , char **argv ) {

  RDKit::ROMOL_SPTR mol1( RDKit::SmilesToMol( "c1ccncc1" ) );
  std::string pickle;
  RDKit::MolPickler::pickleMol( *mol1 , pickle );
  RDKit::ROMol mol2;
  RDKit::MolPickler::molFromPickle( pickle , mol2 );
  std::cout << RDKit::MolToSmiles( mol2 ) << std::endl;

  // writing to pickle file
  std::string smi_file = getenv("RDBASE");
  smi_file += "/Code/GraphMol/test_data/canonSmiles.long.smi";
  std::string pkl_name = "canonSmiles.long.bin";
  
  // tab-delimited file, SMILES in column 0, name in 1, no title line
  RDKit::SmilesMolSupplier suppl( smi_file , "\t" , 0 , 1 , false );
  std::ofstream pickle_ostream( pkl_name.c_str() , std::ios_base::binary );
  int write_cnt = 0;
  while( !suppl.atEnd() ) {
    RDKit::ROMOL_SPTR mol( suppl.next() );
    RDKit::MolPickler::pickleMol( *mol , pickle_ostream );
    ++write_cnt;
  }
  pickle_ostream.close();  
  std::cout << "Wrote " << write_cnt << " molecules" << std::endl;
  
  // reading from pickle file
  std::ifstream pickle_istream( pkl_name.c_str() , std::ios_base::binary );
  int read_cnt = 0;
  while( !pickle_istream.eof() ) {
    RDKit::ROMol mol3;
    try {
      RDKit::MolPickler::molFromPickle( pickle_istream , mol3 );
    } catch( RDKit::MolPicklerException &e ) {
      break;
    }
    ++read_cnt;
  }
  pickle_istream.close();
  std::cout << "Read " << read_cnt << " molecules." << std::endl;
  
}