ww_evaluate_contact_map

Calculates contact map for a given protein structure

USAGE:
ww_evaluate_contact_map [ww_evaluate_contact_map.htm  [8003 [pdb_local_path]] ]

where the first optional argument is the HTML page file and 8003 is the port number (ww_evaluate_contact_map.htm and 8003 by default, respectively

Categories:

  • core::calc::structural::ContactMap

Program source:

  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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include <iostream>

#include <core/chemical/molecule_utils.hh>
#include <core/data/io/Pdb.hh>
#include <core/data/io/json_io.hh>
#include <core/data/structural/structure_selectors.hh>

#include <core/protocols/structure_calculation_protocols.hh>
#include <utils/exit.hh>
#include <utils/io_utils.hh>
#include <ui/www/WebServer.hh>
#include <utils/LogManager.hh>

std::string program_info = R"(

Calculates contact map for a given protein structure
USAGE:
    ww_evaluate_contact_map [ww_evaluate_contact_map.htm  [8003 [pdb_local_path]] ]

where the first optional argument is the HTML page file  and 8003 is the port
number (ww_evaluate_contact_map.htm and 8003 by default, respectively)
)";

std::string pdb_path = "";
std::string html_page = "";
std::string html_page_fname = "";
utils::Logger logs("ww_evaluate_contact_map");

/// Procedure that sends the HTML page of the app
void send_html(ui::www::Request *req, ui::www::Response *res) { 

  html_page = utils::load_text_file(html_page_fname);
  res->send(html_page); 
}

core::data::structural::AtomSelector_SP atom_selector(const std::string & atom_selection) {

  if(atom_selection=="cb") {
    logs << "CB atoms selected\n";
    return std::shared_ptr<core::data::structural::IsCB>();
  }
  if(atom_selection=="ca") {
    logs << "CA atoms selected\n";
    return std::shared_ptr<core::data::structural::IsCA>();
  }
  logs << "side chain atoms selected\n";
  return std::shared_ptr<core::data::structural::IsSC>();
}

size_t contactmap_to_json(const core::calc::structural::ContactMap &cm, core::data::io::JsonArray_SP json) {

  core::data::io::JsonArray_SP mx = std::make_shared<core::data::io::JsonArray>("");
  core::data::io::JsonArray_SP my = std::make_shared<core::data::io::JsonArray>("");
  size_t icnt = 0;
  for (core::index2 ires = 1; ires <= cm.max_index(); ++ires) {
    for (core::index2 jres = 0; jres < ires - 1; ++jres) {
      if (cm.has_element(ires, jres)) {
        mx->push_back(false, ires, "%d");
        my->push_back(false, jres, "%d");
        mx->push_back(false, jres, "%d");
        my->push_back(false, ires, "%d");
        ++icnt;
      }
    }
  }
  json->push_back(false, mx->string_value());
  json->push_back(false, my->string_value());

  return icnt;
}

void coordinates_to_json(const core::data::structural::Structure &strctr, core::data::io::JsonArray_SP xyz_json) {

  for (auto i_atm = strctr.first_const_atom();i_atm!=strctr.last_const_atom();++i_atm) {
    core::data::io::JsonArray_SP xyz = std::make_shared<core::data::io::JsonArray>("");
    xyz->push_back(false, (**i_atm).x, "%f");
    xyz->push_back(false, (**i_atm).y, "%f");
    xyz->push_back(false, (**i_atm).z, "%f");
    xyz->push_back(true, (**i_atm).atom_name());
    xyz_json->push_back(false, xyz->string_value());
  }
}


void atoms_by_residue_to_json(const core::data::structural::Structure &strctr, core::data::io::JsonArray_SP json) {

  for (auto i_res = strctr.first_const_residue();i_res!=strctr.last_const_residue();++i_res) {
    core::data::io::JsonArray_SP atom_ids = std::make_shared<core::data::io::JsonArray>("");
    for(const auto atom_sp: **i_res)
      atom_ids->push_back(false,(*atom_sp).id(),"%d");
    json->push_back(false, atom_ids->string_value());
  }
}


void residues_by_chain_to_json(const core::data::structural::Structure &strctr, core::data::io::JsonArray_SP json) {

  int last_res = -1;
  for (const auto & chain : strctr) {
    core::data::io::JsonArray_SP res_ids = std::make_shared<core::data::io::JsonArray>(utils::string_format("%c",chain->id()));
    res_ids->push_back(false, utils::string_format("%d", last_res + 1));
    res_ids->push_back(false, utils::string_format("%d", last_res + chain->size()));
    json->push_back(false,res_ids->string_value());
  }
}

void structure_to_json(const core::data::structural::Structure &strctr, core::data::io::JsonNode_SP structure_json) {

  std::string sequence, sec_str;
  core::data::io::JsonArray_SP seq_array = std::make_shared<core::data::io::JsonArray>("residues");
  size_t ires = 0;
  // --- Prepare sequence, secondary structure and residue data to be sent
  for(auto it_res=strctr.first_const_residue();it_res!=strctr.last_const_residue();++it_res) {
    sequence += (**it_res).residue_type().code1;
    sec_str += (**it_res).ss();
    const core::data::structural::Residue & r = **it_res;
    core::data::io::JsonArray_SP res_array = core::data::io::create_json_array("", "0011", ires, r.id(),
      r.residue_type().code3, r.owner()->id());
    seq_array->push_back(false,res_array->string_value());
    ++ires;
  }

  // --- Create a node for a sequence string
  core::data::io::JsonNode_SP seq_node = core::data::io::create_json_node<std::string>("sequence",sequence);
  seq_node->element->need_quotes(true);
  structure_json->add_branch(seq_node);

  // --- Create a node for a secondary structure string
  core::data::io::JsonNode_SP ss_node = core::data::io::create_json_node<std::string>("secondary",sec_str);
  ss_node->element->need_quotes(true);
  structure_json->add_branch(ss_node);

  // --- Create a node for residue data
  structure_json->add_branch(core::data::io::create_json_node(seq_array));

  // --- Create a node for atomic coordinates
  core::data::io::JsonArray_SP xyz = std::make_shared<core::data::io::JsonArray>("xyz");
  coordinates_to_json(strctr, xyz);
  structure_json->add_branch(core::data::io::create_json_node(xyz));

  // --- compile list of atom indexes for each residue
  core::data::io::JsonArray_SP atoms_for_residue = std::make_shared<core::data::io::JsonArray>("atoms_for_residue");
  atoms_by_residue_to_json(strctr, atoms_for_residue);
  structure_json->add_branch(core::data::io::create_json_node(atoms_for_residue));

  // --- compile list of residue ranges for each chain
  core::data::io::JsonArray_SP res_for_chain = std::make_shared<core::data::io::JsonArray>("residues_for_chain");
  residues_by_chain_to_json(strctr, res_for_chain);
  structure_json->add_branch(core::data::io::create_json_node(res_for_chain));

  core::data::io::JsonArray_SP bonds_json = std::make_shared<core::data::io::JsonArray>("bonds");
  std::vector<std::pair<core::index4,core::index4>> bonds;
  core::chemical::detect_bond_indexes(strctr.first_const_atom(),strctr.last_const_atom(),bonds);
  for(const std::pair<core::index4,core::index4> & b : bonds) {
    core::data::io::JsonArray_SP bi_json = std::make_shared<core::data::io::JsonArray>("");
    bi_json->push_back(false, b.first, "%d");
    bi_json->push_back(false, b.second, "%d");
    bonds_json->push_back(false, bi_json->string_value());
  }
  structure_json->add_branch(core::data::io::create_json_node(bonds_json));
}


/** Procedure that calculates contact map and sends them as JSON
 *
 * @param req - request object; must contain the following parameters:
 *      - pdb_id : four letter PDB code
 *      - type (string) : "ca", "cb" or "sc"
 *      - cutoff (string) : cutoff distance in Angstrom
 *      - model_id (string) : model ID starting from 1
 *
 * @param res - server response object
 */
void send_cmap(ui::www::Request *req, ui::www::Response *res) {

  using namespace core::calc::structural;

  const std::string & pdb_code = req->query["pdb_id"];
  if(! core::data::io::is_pdb_code(pdb_code)) {
    core::data::io::JsonValue error("error","Invalid PDB code");
    res->expose_stream() << "{"<<error<< "}\n";
    return;
  }

  logs << utils::LogLevel::HTTP  << "Downloading "<<pdb_code<<"\n";

  const std::string type = req->query["type"];
  double cutoff = atof(req->query["cutoff"].c_str());
  logs << utils::LogLevel::INFO << "Preparing " << type << " contact map up to " << cutoff << " Angstroms\n";

  std::stringstream sstr;
  if(pdb_path.size()==0)
    sstr << utils::download_pdb(pdb_code);
  else
    sstr << utils::download_pdb(pdb_code,pdb_path);
  core::data::io::Pdb reader(sstr, core::data::io::is_not_water);

  int model_id = (req->query.find("model_id") == req->query.end()) ? 0 : atoi(req->query["model_id"].c_str()) - 1;
  auto strstr_sp =  reader.create_structure(model_id);
  ContactMap cm(*strstr_sp, cutoff, atom_selector(type));

  core::data::io::JsonNode_SP sctr = std::make_shared<core::data::io::JsonNode>();
  structure_to_json(*strstr_sp,sctr);

  core::data::io::JsonNode_SP n_models =
    core::data::io::create_json_node<std::string>("n_models", utils::string_format("%d", reader.count_models()));
  sctr->add_branch(n_models);

  core::data::io::JsonArray_SP m0 = std::make_shared<core::data::io::JsonArray>(utils::string_format("M%d",model_id));
  size_t icnt = contactmap_to_json(cm, m0);

  sctr->add_branch(core::data::io::create_json_node(m0));

  logs << utils::LogLevel::INFO << int(icnt) << " contacts sent for model "<<model_id<<"\n";
  res->expose_stream()  <<  sctr<< "\n";

  return;
}

/** @brief Calculates contact map for the input protein structure
 *
 * CATEGORIES: core::calc::structural::ContactMap
 * KEYWORDS:   PDB input; structure properties; structure validation; contact map
 */
int main(const int argc, const char *argv[]) {

//  utils::LogManager::FINER();
  using namespace core::calc::structural;

  if (argc < 2) std::cout << program_info; // --- show the info just in case
  html_page_fname = (argc > 1) ? argv[1] : "ww_evaluate_contact_map.htm";
  int port = (argc > 2) ? atoi(argv[2]) : 8003;
  pdb_path = (argc > 3) ? argv[3] : "";
  try {
    logs << utils::LogLevel::HTTP << "Listening on port " << port << "\n";
    ui::www::WebServer server;
    // --- Register a function that responds to the call: http://localhost:8003/cmap/ and sends back the main HTML page
    server.register_method("/cmap/", ui::www::Method::BOTH, &send_html);
    // --- Register a function that responds to the call: http://localhost:8003/cmap/evaluate/ and sends back contact map data
    server.register_method("/cmap/evaluate/", ui::www::Method::BOTH, &send_cmap);
    // --- Register paths to VisuaLife directories
    server.register_path("/cmap/visualife/","visualife/");
    server.register_path("/cmap/","");
    server.register_path("/cmap/","visualife/");
    server.register_path("/cmap/","visualife/core/");
    server.register_path("/cmap/","visualife/data/");
    // --- start the web server
    server.start(port);
  } catch (std::runtime_error &e) {
    logs << utils::LogLevel::SEVERE  << e.what() << "\n";
  }

}
../_images/file_icon.png