helma / lazar-core

DEPRECATED (use the pure ruby implementation in conjunction with libfminer in opentox-algorithm and opentox-model), C++ implementation of various lazar algorithms

This URL has Read+Write access

lazar-core / feature-db.h
100644 180 lines (120 sloc) 4.615 kb
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
172
173
174
175
176
177
178
179
180
/* Copyright (C) 2005 Christoph Helma <helma@in-silico.de>
 
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; either version 2 of the License, or
(at your option) any later version.
 
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.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
*/
 
 
#include "lazmolvect.h"
 
#ifndef F_DB_H
#define F_DB_H
 
using namespace std;
 
//! compounds with features
template <class MolType, class FeatureType, class ActivityType>
class FeatMolVect: public MolVect<MolType,FeatureType,ActivityType> {
 
public:
 
typedef FeatMol < MolType, FeatureType, ActivityType > * MolRef ;
typedef Feature<FeatureType> * FeatRef;
 
private:
 
map<const string, FeatRef> feature_map; // lookup features by name
vector<FeatRef> features;
Out * out;
 
public:
 
FeatMolVect< MolType, FeatureType, ActivityType >(char * feat_file, char * structure_file, Out * out);
        ~FeatMolVect() {
            for (unsigned int i=0; i<features.size(); i++) {
                delete features[i];
            }
        }
 
//! read features from a file
void read_features(char * feat_file);
 
void add_feature(MolRef s, string name);
 
void copy_level(vector<Feature<FeatureType> *> * level, MolRef test_comp);
 
vector<FeatRef> * get_features() { return(&features); };
};
 
// read a feature file
template <class MolType, class FeatureType, class ActivityType>
FeatMolVect<MolType, FeatureType, ActivityType>::FeatMolVect(char * feat_file, char * structure_file, Out * out): MolVect< MolType, FeatureType, ActivityType >(structure_file,out), out(out) {
 
ifstream input;
input.open(feat_file);
 
if (!input) {
*out << "Cannot open " << feat_file << endl;
out->print_err();
exit(1);
}
 
string line;
string tmp_field;
int line_nr =1;
FeatRef feat_ptr;
 
*out << "Reading features from " << feat_file << endl;
out->print_err();
while (getline(input, line)) {
 
istringstream iss(line);
int i =0;
 
while(getline(iss, tmp_field, '\t') && i < 2) { // split at tab and read exactly 2 fields
remove_dos_cr(&tmp_field);
 
if (i==0) { // SMARTS
 
feat_ptr = new Feature<FeatureType>(tmp_field); // initialize Feature with smarts
feature_map[tmp_field] = feat_ptr;
features.push_back(feat_ptr);
 
}
 
else { // MATCHES
 
size_t startpos = 0, endpos = 0;
vector<string> tokens;
 
for (;;) {
 
startpos = tmp_field.find_first_not_of(" \t\n",startpos);
endpos = tmp_field.find_first_of(" \t\n",startpos);
 
if (endpos < tmp_field.size() && startpos <= tmp_field.size())
tokens.push_back(tmp_field.substr(startpos,endpos-startpos));
else
break;
 
startpos = endpos + 1;
 
}
 
for (unsigned int i = 0 ; i < tokens.size() ; i++ ) {
 
if ( tokens[i] == "[" )
continue;
else if ( tokens[i] == "]" )
break;
 
int comp_nr = atoi(tokens[i].c_str());
feat_ptr->add_match(comp_nr); // comp_nr is line number
this->get_compound(comp_nr)->add_feature(feat_ptr);
 
}
 
}
 
i++;
 
}
 
line_nr++;
 
}
 
input.close();
 
};
 
template <class MolType, class FeatureType, class ActivityType>
void FeatMolVect<MolType, FeatureType, ActivityType>::add_feature(MolRef s, string name) {
 
typename map<const string, FeatRef>::iterator pos;
pos = feature_map.find(name);
 
if (pos != feature_map.end())
s->add_feature(pos->second);
};
 
template <class MolType, class FeatureType, class ActivityType>
void FeatMolVect<MolType, FeatureType, ActivityType>::copy_level(vector<Feature<FeatureType> *> * level, MolRef test_comp) {
 
typename vector<Feature<FeatureType> *>::iterator frag;
typename map<const string, Feature<FeatureType> *>::iterator pos;
string name;
 
for (frag = level->begin(); frag != level->end(); frag++) {
 
name = (*frag)->get_name();
 
if ((*frag)->nr_matches() == 0) {
test_comp->add_unknown(name);
delete *frag; // free memory
*frag = NULL;
level->erase(frag);
frag--; // set the iterator back to avoid skipping the next compound
}
 
else // search in the feature_map
this->add_feature(test_comp,name);
 
}
 
};
#endif // F_DB_H