File: example.cpp

package info (click to toggle)
openbabel 2.4.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 58,308 kB
  • sloc: cpp: 459,210; ansic: 90,514; php: 13,963; python: 7,899; perl: 6,518; pascal: 793; sh: 179; xml: 97; ruby: 64; makefile: 46; java: 23; cs: 14
file content (171 lines) | stat: -rw-r--r-- 5,574 bytes parent folder | download | duplicates (8)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/**********************************************************************
example.cpp - example of interfacing of OpenBabel with Boost Graph Library
 
Copyright (C) 2007 by Gerde Menche
 
This file is part of the Open Babel project.
For more information, see <http://openbabel.org/>
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation version 2 of the License.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
***********************************************************************/

#include "babelconfig.h"
#include "openbabel/mol.h"
#include "openbabel/obconversion.h"

#include <iostream>
//#include <unistd.h>

#include "mol_graph.h"
#include "verbose_visitors.h"

#include <boost/graph/breadth_first_search.hpp>
#include <boost/graph/undirected_dfs.hpp>
#include <boost/pending/queue.hpp>

using namespace std;
using OpenBabel::OBMol;
using OpenBabel::OBAtom;
using OpenBabel::OBBond;
using OpenBabel::OBConversion;
using OpenBabel::OBFormat;

int main(int argc,char **argv)
{
    int c;
    char *FileIn = NULL;

    if (argc != 2)
    {
        cout << "give file name\n";
        return 1;
    }

    FileIn  = argv[1];

    // Find Input filetype
    OBConversion conv;
    OBFormat *format = conv.FormatFromExt(FileIn);
    
    if (!format || !conv.SetInAndOutFormats(format, format))
    {
        cerr << argv[0] << ": cannot read input format!" << endl;
        exit (-1);
    }

    ifstream ifs;

    // Read the file
    ifs.open(FileIn);
    if (!ifs)
    {
        cerr << argv[0] << ": cannot read input file!" << endl;
        exit (-1);
    }

    OBMol mol;

    for (c=0;;)
    {
        mol.Clear();
	conv.Read(&mol, &ifs);
        if (mol.Empty())
            break;
        OBAtom* atom = *(mol.BeginAtoms()); 

        // Examples using breadth first search

        std::cout << "\nBREADTH FIRST SEARCH\n";

        boost::queue< OBAtom* > buffer;
        boost::breadth_first_search( mol,atom,buffer, OpenBabel::OBVerboseBFSVisitor(cout),
            OBAtomMap< boost::default_color_type >(mol) );

        OBAtomMap< OBAtom* >predecessor_map_bfs(mol,(OBAtom*)0);
        OBAtomMap< int >distance_map_bfs(mol, 0);

        boost::breadth_first_search( mol,atom,buffer,
            boost::make_bfs_visitor( std::make_pair ( 
                boost::record_predecessors(predecessor_map_bfs,boost::on_tree_edge()),
                boost::record_distances(distance_map_bfs,boost::on_tree_edge()) )),
            OBAtomMap< boost::default_color_type >(mol) );

        for ( OpenBabel::OBAtomIterator beg = mol.BeginAtoms(); 
             beg != mol.EndAtoms(); ++beg )
        {
            OBAtom* parent = predecessor_map_bfs[*beg]; 
            if ( parent )
            {
                std::cout << (*beg)->GetIdx() - 1 << " has parent : " 
                          << parent->GetIdx() - 1 << "\t";
            }
            else
            {
                std::cout << (*beg)->GetIdx() - 1 << " has no parent\t\t";
            }
            std::cout << " graph theoretical distance : " << distance_map_bfs[*beg] << "\n"; 
        }

        std::vector< boost::graph_traits<OpenBabel::OBMol>::edge_descriptor > bv;
        boost::breadth_first_search( mol,atom,buffer,
            boost::make_bfs_visitor( 
                boost::record( std::back_inserter(bv), boost::on_gray_target() )),
            OBAtomMap< boost::default_color_type >(mol) );

        std::cout << "\nBACK EDGES\n";
        for ( std::vector< boost::graph_traits<OpenBabel::OBMol>::edge_descriptor >::iterator 
              beg = bv.begin(), end = bv.end();
              beg != end; ++beg )
        {
            std::cout << '(' << (*beg)->GetBeginAtom()->GetIdx() << ")--";
            std::cout << (*beg)->GetIdx() << "--(";
            std::cout << (*beg)->GetEndAtom()->GetIdx() << ")\n";
        }

        // Examples using depth first search

        std::cout << "\nDEPTH FIRST SEARCH\n";

        boost::undirected_dfs( mol, OpenBabel::OBVerboseDFSVisitor(cout),
            OBAtomMap< boost::default_color_type >(mol),
            OBBondMap< boost::default_color_type >(mol),
            atom );

        OBAtomMap< OBAtom* >predecessor_map_dfs(mol,(OBAtom*)0);
        OBAtomMap< int >distance_map_dfs(mol,0);
            
        boost::undirected_dfs( mol, 
            boost::make_dfs_visitor( std::make_pair ( 
                boost::record_predecessors(predecessor_map_dfs,boost::on_tree_edge()),
                boost::record_distances(distance_map_dfs,boost::on_tree_edge()) )),
            OBAtomMap< boost::default_color_type >(mol),
            OBBondMap< boost::default_color_type >(mol),
            atom );

        for ( OpenBabel::OBAtomIterator beg = mol.BeginAtoms(); 
              beg != mol.EndAtoms(); ++beg )
        {
            OBAtom* parent = predecessor_map_dfs[*beg]; 
            if ( parent )
            {
                std::cout << (*beg)->GetIdx() - 1 << " has parent : " 
                          << parent->GetIdx() - 1 << "\t";
            }
            else
            {
                std::cout << (*beg)->GetIdx() - 1 << " has no parent\t\t";
            }
            std::cout << " graph theoretical distance : " << distance_map_dfs[*beg] << "\n"; 
        }

    } // end for loop

    return(1);
}