File: export_omf_io.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 (132 lines) | stat: -rw-r--r-- 4,994 bytes parent folder | download
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
//------------------------------------------------------------------------------
// This file is part of the OpenStructure project <www.openstructure.org>
//
// Copyright (C) 2008-2021 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>
using namespace boost::python;

#include <ost/io/mol/omf.hh>
#include <ost/mol/alg/biounit.hh>

using namespace ost;
using namespace ost::io;

namespace{

  template<typename T>
  boost::python::list VecToList(const std::vector<T>& vec){
    boost::python::list l;
    for(typename std::vector<T>::const_iterator it=vec.begin();
        it!=vec.end(); ++it){
      l.append(*it);
    }
    return l;
  }

  PyObject* wrap_to_bytes(OMFPtr omf) {
    String str = omf->ToString();
    return PyBytes_FromStringAndSize(str.c_str(), str.size());
  }
  
  OMFPtr wrap_from_bytes(boost::python::object obj) {
    String str(PyBytes_AsString(obj.ptr()), PyBytes_Size(obj.ptr()));
    return OMF::FromString(str);
  }

  boost::python::list wrap_get_chain_names(OMFPtr omf) {
    return VecToList<String>(omf->GetChainNames());
  }

  boost::python::list wrap_get_chain_types(OMFPtr omf) {

    boost::python::list chain_types;
    std::vector<String> chain_names = omf->GetChainNames();
    for(auto const &cname: chain_names){
      chain_types.append(omf->GetChainData(cname)->chain_type);
    }
    return chain_types;
  }

  OMFPtr wrap_to_assembly(OMFPtr omf,
                          const ost::mol::alg::BUInfo& bu_info) {
    return omf->ToAssembly(bu_info.GetAUChains(),
                           bu_info.GetTransformations(),
                           bu_info.GetBUChains());
  }

  tuple wrap_trace(OMFPtr omf,
                   const String& cname,
                   const String& aname) {
    std::vector<int> rnums;
    geom::Vec3List pos;
    omf->Trace(cname, aname, rnums, pos);
    return boost::python::make_tuple(VecToList(rnums), pos);
  }

  boost::python::list wrap_get_rnums(OMFPtr omf, const String& cname) {
    const std::vector<int>& rnums = omf->GetRNums(cname);
    return VecToList<int>(rnums);
  }

  boost::python::list wrap_get_start_pos(OMFPtr omf, const String& cname) {
    boost::python::list l;
    int i = 0;
    ChainDataPtr chain_data = omf->GetChainData(cname);
    const std::vector<ResidueDefinition>& res_defs = omf->GetResidueDefinitions();
    for(auto res_def_idx: chain_data->res_def_indices) {
      l.append(i);
      i += res_defs[res_def_idx].anames.size();
    }
    return l;
  }
}

void export_omf_io() {

  enum_<OMF::OMFOption>("OMFOption")
    .value("DEFAULT_PEPLIB", OMF::DEFAULT_PEPLIB)
    .value("AVG_BFACTORS", OMF::AVG_BFACTORS)
    .value("ROUND_BFACTORS", OMF::ROUND_BFACTORS)
    .value("SKIP_SS", OMF::SKIP_SS)
    .value("INFER_PEP_BONDS", OMF::INFER_PEP_BONDS)
  ;

  class_<OMF, OMFPtr>("OMF",no_init)
    .def("FromEntity", &OMF::FromEntity, (arg("ent"), arg("max_error")=0.0, arg("options")=0)).staticmethod("FromEntity")
    .def("FromFile", &OMF::FromFile).staticmethod("FromFile")
    .def("FromBytes", &wrap_from_bytes).staticmethod("FromBytes")
    .def("ToFile", &OMF::ToFile)
    .def("ToBytes", &wrap_to_bytes)
    .def("GetMaxError", &OMF::GetMaxError)
    .def("GetAU", &OMF::GetAU)
    .def("GetEntity", &OMF::GetEntity)
    .def("GetAUChain", &OMF::GetAUChain)
    .def("GetEntityChain", &OMF::GetEntityChain)
    .def("GetName", &OMF::GetName)
    .def("GetChainNames", &wrap_get_chain_names)
    .def("GetChainTypes", &wrap_get_chain_types)
    .def("GetPositions", &OMF::GetPositions, return_value_policy<reference_existing_object>(),(arg("cname")))
    .def("GetBFactors", &OMF::GetBFactors, return_value_policy<reference_existing_object>(),(arg("cname")))
    .def("GetOccupancies", &OMF::GetOccupancies, return_value_policy<reference_existing_object>(),(arg("cname")))
    .def("GetResStartPos", &wrap_get_start_pos, (arg("cname")))
    .def("GetAvgBFactors", &OMF::GetAvgBFactors, (arg("cname")))
    .def("GetSequence", &OMF::GetSequence, (arg("cname")))
    .def("GetRNums", &wrap_get_rnums, (arg("cname")))
    .def("ToAssembly", &wrap_to_assembly, (arg("bu_info")))
    .def("Trace", &wrap_trace, (arg("cname"), arg("aname")))
  ;
}