Rosetta 3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
qsarOptFunc.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file src/protocols/qsar/qsarOptFunc.cc
11 /// @author Sam DeLuca
12 
13 
14 //This is a bit of a mess, probably refactor it some time
15 
17 #include <basic/database/sql_utils.hh>
18 #include <numeric/roc_curve.hh>
19 #include <utility/exit.hh>
20 
21 //External
22 #include <boost/uuid/uuid.hpp>
23 #include <boost/uuid/uuid_io.hpp>
24 
25 //Auto Headers
26 #include <utility/vector1.hh>
27 
28 namespace protocols {
29 namespace qsar {
30 
32  utility::sql_database::sessionOP db_session,
33  core::optimization::Multivec const & initial_values,
34  std::map<std::string,core::Size> const & grid_indices) : core::optimization::Multifunc(), initial_values_(initial_values), grid_indices_(grid_indices), cutoff_(0.0)
35 {
36  std::string value_string =
37  "SELECT job_string_real_data.data_value\n"
38  " FROM job_string_real_data\n"
39  " WHERE\n"
40  " job_string_real_data.data_key = ?\n"
41  " AND\n"
42  " job_string_real_data.struct_id = ?;";
43 
44  score_selection_ = basic::database::safely_prepare_statement(value_string,db_session);
45 
46  std::string struct_id_string = "SELECT structures.struct_id FROM structures;";
47 
48  struct_id_selection_ = basic::database::safely_prepare_statement(struct_id_string,db_session);
49 
50  std::string tag_activity_string =
51  "SELECT structures.tag, structure_activity.activity\n"
52  " FROM structures\n"
53  " INNER JOIN structure_activity ON structures.input_tag = structure_activity.input_tag\n"
54  " WHERE structures.struct_id = ?;";
55 
56  tag_activity_selection_ = basic::database::safely_prepare_statement(tag_activity_string,db_session);
57 
58 }
59 
61 {
62  data_map_.clear();
63 
64  cppdb::result struct_id_result(basic::database::safely_read_from_database(struct_id_selection_));
65  while(struct_id_result.next())
66  {
67  boost::uuids::uuid struct_id;
68  struct_id_result >> struct_id;
69  data_map_.push_back(get_struct_data(struct_id));
70  }
71 
72 }
73 
75 {
76  initial_values_ = initial_values;
77 }
78 
80 {
81  assert(vars.size() == grid_indices_.size());
82 
83  numeric::RocCurve roc_curve;
84 
85  for(std::list<qsarOptData>::const_iterator data_it = data_map_.begin(); data_it != data_map_.end();++data_it)
86  {
87 
88  core::Real total_score = 0.0;
89 
90  for(std::map<std::string,core::Real>::const_iterator score_it = data_it->score_map.begin(); score_it != data_it->score_map.end();++score_it)
91  {
92  core::Size vec_index = grid_indices_.find(score_it->first)->second;
93  core::Real initial_weight = initial_values_[vec_index];
94  core::Real current_weight = vars[vec_index];
95  core::Real component_score = score_it->second;
96 
97  total_score += (component_score/initial_weight)*current_weight;
98  }
99 
100  bool predicted(total_score < cutoff_);
101  roc_curve.insert_point(predicted,data_it->activity,data_it->tag,total_score);
102  }
103 
104  roc_curve.generate_roc_curve();
105  return roc_curve.calculate_auc();
106 
107 }
108 
110 {
111  utility_exit_with_message("haven't implemented dfunc sorry bye");
112 }
113 
115 {
116  utility_exit_with_message("haven't implemented dump sorry bye");
117 }
118 
119 qsarOptData qsarOptFunc::get_struct_data(boost::uuids::uuid const & struct_id )
120 {
121 
122  tag_activity_selection_.bind(1,struct_id);
123  std::string tag;
124  core::Size activity;
125  cppdb::result tag_activity_result(basic::database::safely_read_from_database(tag_activity_selection_));
126  if(tag_activity_result.next())
127  {
128  tag_activity_result >> tag >> activity;
129  }
130 
131  score_selection_.bind(2,struct_id);
132  std::map<std::string,core::Real> score_map;
133 
134  for(std::map<std::string,core::Size>::iterator grid_it = grid_indices_.begin(); grid_it != grid_indices_.end(); ++grid_it)
135  {
136  score_selection_.bind(1,grid_it->first+"_score_X");
137  cppdb::result score_result(basic::database::safely_read_from_database(score_selection_));
138  if(score_result.next())
139  {
140  core::Real component_score;
141  score_result >> component_score;
142  score_map.insert(std::make_pair(grid_it->first,component_score));
143 
144  }
145  }
146 
147 
148  qsarOptData new_point;
149  new_point.activity = static_cast<bool>(activity);
150  new_point.tag = tag;
151  new_point.score_map = score_map;
152 
153  return new_point;
154 }
155 
156 }
157 }