File: export_linear_db.cc

package info (click to toggle)
openstructure 2.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 206,240 kB
  • sloc: cpp: 188,571; python: 36,686; ansic: 34,298; fortran: 3,275; sh: 312; xml: 146; makefile: 29
file content (238 lines) | stat: -rw-r--r-- 8,983 bytes parent folder | download | duplicates (4)
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
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2020 by the OpenStructure authors
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3.0 of the License, or (at your option)
// any later version.
// This library 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 Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//------------------------------------------------------------------------------


#include <boost/python.hpp>
#include <ost/db/linear_indexer.hh>
#include <ost/db/binary_container.hh>
#include <ost/db/extract_data_helper.hh>

using namespace boost::python;
using namespace ost::db;


namespace{

template<typename T>
void ListToVec(const list& l, std::vector<T>& v) {
  uint num_elements = len(l);
	v.resize(num_elements);
	for(uint i = 0; i < num_elements; ++i) {
		v[i] = extract<T>(l[i]);
	}
}


template<typename T>
void VecToList(const std::vector<T>& v, list& l) {
  l = list();
  for(typename std::vector<T>::const_iterator i = v.begin(); 
  	  i != v.end(); ++i) {
  	l.append(*i);
  }
}


void WrapAddAssembly(LinearIndexerPtr indexer,
	                   const String& name,
	                   const list& chain_names,
	                   const list& chain_lengths) {

  std::vector<String> v_chain_names;
  std::vector<uint> v_chain_lengths;
  ListToVec(chain_names, v_chain_names);
  ListToVec(chain_lengths, v_chain_lengths);
  indexer->AddAssembly(name, v_chain_names, v_chain_lengths);
}


list WrapGetAssemblies(LinearIndexerPtr indexer) {

  std::vector<String> entry_names = indexer->GetAssemblies();
  list return_list;
  VecToList(entry_names, return_list);
  return return_list;
}


list WrapGetChainNames(LinearIndexerPtr indexer, const String& name) {

  std::vector<String> chain_names = indexer->GetChainNames(name);
  list return_list;
  VecToList(chain_names, return_list);
  return return_list;
}


list WrapGetChainLengths(LinearIndexerPtr indexer, const String& name) {

  std::vector<uint> chain_lengths = indexer->GetChainLengths(name);
  list return_list;
  VecToList(chain_lengths, return_list);
  return return_list;
}


tuple WrapGetDataRangeAssembly(LinearIndexerPtr indexer,
	                             const String& name) {

  std::pair<uint64_t, uint64_t> range = indexer->GetDataRange(name);
  tuple return_tuple = make_tuple(range.first, range.second);
  return return_tuple;
}


tuple WrapGetDataRange(LinearIndexerPtr indexer,
	                     const String& name,
	                     const String& chain_name) {

  std::pair<uint64_t, uint64_t> range = indexer->GetDataRange(name, chain_name);
  tuple return_tuple = make_tuple(range.first, range.second);
  return return_tuple;
}

void WrapClearCharacterRange(LinearCharacterContainerPtr container,
                             tuple range) {

  std::pair<uint64_t, uint64_t> p_range;
  p_range.first = extract<uint64_t>(range[0]);
  p_range.second = extract<uint64_t>(range[1]);

  container->ClearRange(p_range);
}

String WrapGetCharacters(LinearCharacterContainerPtr container,
                         tuple& range) {

  std::pair<uint64_t, uint64_t> p_range;
  p_range.first = extract<uint64_t>(range[0]);
  p_range.second = extract<uint64_t>(range[1]);
  String return_string;
  container->GetCharacters(p_range, return_string);
  return return_string;
}

void WrapClearPositionRange(LinearPositionContainerPtr container,
                            tuple& range) {

  std::pair<uint64_t, uint64_t> p_range;
  p_range.first = extract<uint64_t>(range[0]);
  p_range.second = extract<uint64_t>(range[1]);
  container->ClearRange(p_range);
}

void WrapGetPositions(LinearPositionContainerPtr container,
                      tuple& range,
                      geom::Vec3List& positions) {

  std::pair<uint64_t, uint64_t> p_range;
  p_range.first = extract<uint64_t>(range[0]);
  p_range.second = extract<uint64_t>(range[1]);
  container->GetPositions(p_range, positions);  
}


tuple WrapExtractTemplateData(const String& entry_name, const String& chain_name,
                              const ost::seq::AlignmentHandle& aln,
                              LinearIndexer& indexer,
                              LinearCharacterContainer& seqres_container,
                              LinearCharacterContainer& atomseq_container,
                              LinearPositionContainer& position_container) {

  std::vector<int> v_residue_numbers;
  geom::Vec3List ca_positions;

  ost::db::ExtractTemplateData(entry_name, chain_name, aln, indexer, 
                               seqres_container, atomseq_container, 
                               position_container, v_residue_numbers,
                               ca_positions);

  list residue_numbers;
  VecToList(v_residue_numbers, residue_numbers);
  return boost::python::make_tuple(residue_numbers, ca_positions);
}   

}


void export_linear_db() {

  class_<LinearIndexer, LinearIndexerPtr>("LinearIndexer", init<>())  
    .def("Load", &LinearIndexer::Load, arg("filename")).staticmethod("Load")
    .def("Save", &LinearIndexer::Save, arg("filename"))
    .def("AddAssembly", &WrapAddAssembly, (arg("name"), 
    	                                     arg("chain_names"),
    	                                     arg("chain_lengths")))
    .def("RemoveAssembly", &LinearIndexer::RemoveAssembly, (arg("name")))
    .def("GetAssemblies", &WrapGetAssemblies)
    .def("GetChainNames", &WrapGetChainNames,arg("name"))
    .def("GetChainLengths", &WrapGetChainLengths,arg("name"))
    .def("GetDataRange", &WrapGetDataRangeAssembly,arg("name"))
    .def("GetDataRange", &WrapGetDataRange,(arg("name"),
    	                                      arg("chain_name")))
    .def("GetNumResidues", &LinearIndexer::GetNumResidues)
    .def("__eq__", &LinearIndexer::operator==)
    .def("__ne__", &LinearIndexer::operator!=)
  ;


  class_<LinearCharacterContainer, 
         LinearCharacterContainerPtr>("LinearCharacterContainer", init<>())  
    .def("Load", &LinearCharacterContainer::Load, arg("filename")).staticmethod("Load")
    .def("Save", &LinearCharacterContainer::Save, arg("filename"))
    .def("ClearRange", &WrapClearCharacterRange, arg("range"))
    .def("AddCharacters", &LinearCharacterContainer::AddCharacters, arg("characters"))
    .def("GetCharacter", &LinearCharacterContainer::GetCharacter, arg("idx"))
    .def("GetCharacters", &WrapGetCharacters, arg("range"))
    .def("GetNumElements", &LinearCharacterContainer::GetNumElements)
    .def("__eq__", &LinearCharacterContainer::operator==)
    .def("__ne__", &LinearCharacterContainer::operator!=)
  ;


  class_<LinearPositionContainer,
         LinearPositionContainerPtr>("LinearPositionContainer", init<>())
    .def("Load", &LinearPositionContainer::Load, arg("filename")).staticmethod("Load")
    .def("Save", &LinearPositionContainer::Save, arg("filename"))
    .def("ClearRange", &WrapClearPositionRange, arg("range"))
    .def("AddPositions", &LinearPositionContainer::AddPositions, arg("positions"))
    .def("GetPosition", &LinearPositionContainer::GetPosition, arg("idx"), arg("pos"))
    .def("GetPositions", &WrapGetPositions, (arg("range"),arg("positions")))
    .def("GetNumElements", &LinearPositionContainer::GetNumElements)
    .def("__eq__", &LinearPositionContainer::operator==)
    .def("__ne__", &LinearPositionContainer::operator!=)
  ;

  def("ExtractValidPositions", &ExtractValidPositions, (arg("entry_name"), 
                                                        arg("chain_name"),
                                                        arg("indexer"),
                                                        arg("atomseq_container"),
                                                        arg("position_container"),
                                                        arg("seq"), arg("positions")));

  def("ExtractTemplateData", &WrapExtractTemplateData, (arg("entry_name"),
                                                        arg("chain_name"),
                                                        arg("aln"),
                                                        arg("linear_indexer"),
                                                        arg("seqres_container"),
                                                        arg("atomseq_container"),
                                                        arg("position_container")));

}