Files
rdkit/Docs/Book/C++Examples/example12.cpp
David Cosgrove fcc2e226ff Get started c++ (#1285)
* Start of GettingStartedInC++ documentation.

* Changed GettingStartedInC++ from rst to markdown format. Added some more text.

* Added Working with Molecules to GettingStartedInC++

* Expanded on behaviour of Kekulize wrt clearing aromatic flags on atoms and bonds.

* Added section Modifying Molecules.

* More plodding progress.

* A load more documentation.

* Undid change to global CMakeLists.txt

* Minor editing of docs.

* Changed examples so they use RDBASE to find test data rather than relative
path.

* Fixed use of -std=c++ in CMakeLists.txt. Extra waffling about memory.

* Modifications to examples and documentation as requested.

* Couple of minor changes.

* Change to example11.cpp and associated text in docs.
2017-03-30 04:50:53 +02:00

57 lines
1.7 KiB
C++

//
// 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;
}