From 3c55c8cfc610932d0a5e794c0dc7b0fd1c78e094 Mon Sep 17 00:00:00 2001
From: Valerio Mariani <valerio.mariani@unibas.ch>
Date: Thu, 5 Jul 2012 02:17:32 +0200
Subject: [PATCH] Molck missing files

---
 modules/conop/src/diag.cc        |  98 ++++++++++++++++++++
 modules/conop/src/diag.hh        | 134 +++++++++++++++++++++++++++
 modules/conop/src/model_check.cc | 150 +++++++++++++++++++++++++++++++
 modules/conop/src/model_check.hh |  30 +++++++
 4 files changed, 412 insertions(+)
 create mode 100644 modules/conop/src/diag.cc
 create mode 100644 modules/conop/src/diag.hh
 create mode 100644 modules/conop/src/model_check.cc
 create mode 100644 modules/conop/src/model_check.hh

diff --git a/modules/conop/src/diag.cc b/modules/conop/src/diag.cc
new file mode 100644
index 000000000..75245fc93
--- /dev/null
+++ b/modules/conop/src/diag.cc
@@ -0,0 +1,98 @@
+//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2011 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 "diag.hh"
+
+namespace ost { namespace conop {
+
+String Diag::Format(bool colored) const
+{
+  std::stringstream ss;
+  std::vector<String> strings;
+  for (std::vector<ArgDesc>::const_iterator
+       i=args_.begin(), e=args_.end(); i!=e; ++i) {
+   switch (i->type) {
+     case DIAG_ARG_TYPE_ATOM:
+       if (colored) {
+         strings.push_back("\033[0;30m"+
+                           atoms_[i->index].GetQualifiedName()+"\033[0m");
+       } else {
+         strings.push_back(atoms_[i->index].GetQualifiedName());
+       }
+       break;
+     case DIAG_ARG_TYPE_RESIDUE:
+        if (colored) {  
+          strings.push_back("\033[0;30m"+
+                            residues_[i->index].GetQualifiedName()+"\033[0m");
+       } else {
+          strings.push_back(residues_[i->index].GetQualifiedName());
+       }
+       break;
+     case DIAG_ARG_TYPE_CHAIN:
+        if (colored) {    
+          strings.push_back("\033[0;30m"+
+                            chains_[i->index].GetName()+"\033[0m");
+       } else {
+         strings.push_back(chains_[i->index].GetName());
+       }
+       break;
+     case DIAG_ARG_TYPE_STRING:
+       strings.push_back(strings_[i->index]);
+       break;
+     case DIAG_ARG_TYPE_INT:
+       ss << ints_[i->index];
+       strings.push_back(ss.str());
+       ss.str("");
+       break;
+   }
+  }
+  for (size_t i=0; i<format_.size(); ++i) {
+    if (format_[i]=='%') {
+      // scan for number
+      ++i;
+      assert(i<format_.size());
+      if (format_[i]=='%' ) {
+        ss << '%';
+        continue;
+      }
+      bool plural_s=false;
+      if (format_[i]=='s') {
+        plural_s=true;
+        ++i;
+      }
+      const char *start=&format_[i];
+      char *end=NULL;
+      long int id=strtol(start, &end, 10);
+      assert(start!=end);
+      assert(id>=0 && id<strings.size());
+      if (plural_s) {
+        if (ints_[args_[id].index]!=1) {
+          ss << "s";
+        }
+      } else {
+        ss << strings[id];
+      }
+      i+=end-start-1;
+      continue;
+    } 
+    ss << format_[i];
+  }
+  return ss.str();
+}
+
+}} /* ost::conop */
diff --git a/modules/conop/src/diag.hh b/modules/conop/src/diag.hh
new file mode 100644
index 000000000..58f476554
--- /dev/null
+++ b/modules/conop/src/diag.hh
@@ -0,0 +1,134 @@
+#//------------------------------------------------------------------------------
+// This file is part of the OpenStructure project <www.openstructure.org>
+//
+// Copyright (C) 2008-2011 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
+//------------------------------------------------------------------------------
+#ifndef OST_CONOP_DIAG_HH
+#define OST_CONOP_DIAG_HH
+#include <ost/mol/atom_handle.hh>
+#include <ost/mol/residue_handle.hh>
+#include <ost/mol/chain_handle.hh>
+#include <ost/conop/module_config.hh>
+
+namespace ost { namespace conop {
+
+typedef enum {
+	DIAG_ARG_TYPE_ATOM,
+	DIAG_ARG_TYPE_RESIDUE,
+	DIAG_ARG_TYPE_CHAIN,
+	DIAG_ARG_TYPE_STRING,
+	DIAG_ARG_TYPE_INT
+} DiagArgType;
+
+typedef enum {
+  DIAG_UNK_ATOM,
+  DIAG_UNK_RESIDUE,
+  DIAG_MISSING_ATOM,
+  DIAG_NONSTD_RESIDUE
+} DiagType;
+
+class DLLEXPORT_OST_CONOP Diag {
+public:
+	Diag(DiagType typ, const char* fmt): type_(typ), format_(fmt) {}
+  DiagType GetType() const { return type_; }
+	Diag& AddAtom(mol::AtomHandle atom)
+	{
+		atoms_.push_back(atom);
+		args_.push_back(ArgDesc(atoms_.size()-1, DIAG_ARG_TYPE_ATOM));
+		return *this;
+	}
+
+	Diag& AddResidue(mol::ResidueHandle res)
+	{
+		residues_.push_back(res);
+		args_.push_back(ArgDesc(residues_.size()-1, DIAG_ARG_TYPE_RESIDUE));
+		return *this;
+	}
+	Diag& AddChain(mol::ChainHandle chain)
+	{
+		chains_.push_back(chain);
+		args_.push_back(ArgDesc(chains_.size()-1, DIAG_ARG_TYPE_CHAIN));
+		return *this;
+	}
+	Diag& AddInt(int int_val)
+	{
+		ints_.push_back(int_val);
+		args_.push_back(ArgDesc(ints_.size()-1, DIAG_ARG_TYPE_INT));
+		return *this;
+	}
+	Diag& AddString(const String& str)
+	{
+		strings_.push_back(str);
+		args_.push_back(ArgDesc(strings_.size()-1, DIAG_ARG_TYPE_STRING));
+		return *this;
+	}
+  mol::AtomHandle GetAtom(size_t index) const
+  {
+    assert(index<args_.size());
+    return atoms_[args_[index].index];
+  }
+  mol::ResidueHandle GetResidue(size_t index) const
+  {
+    assert(index<args_.size());
+    return residues_[args_[index].index];
+  }
+  mol::ChainHandle GetChain(size_t index) const
+  {
+    assert(index<args_.size());
+    return chains_[args_[index].index];
+  }
+	String Format(bool colored=true) const;
+private:
+	struct ArgDesc {
+		ArgDesc(size_t i, DiagArgType t): index(i), type(t) { }
+		size_t      index;
+		DiagArgType type;
+	};
+	DiagType               type_;
+	String                 format_;
+	mol::AtomHandleList    atoms_;
+	mol::ResidueHandleList residues_;
+  mol::ChainHandleList   chains_;
+	std::vector<String>    strings_;
+	std::vector<int>       ints_;
+	std::vector<ArgDesc>   args_;
+};
+
+class DLLEXPORT_OST_CONOP DiagEngine {
+public:
+	DiagEngine() {}
+
+	~DiagEngine()
+	{
+		for(std::vector<Diag*>::iterator
+				i=diags_.begin(), e=diags_.end(); i!=e;++i) {
+		  delete *i;
+		}
+	}
+
+	Diag& AddDiag(DiagType type, const char* fmt)
+	{
+		diags_.push_back(new Diag(type, fmt));
+		return *diags_.back();
+	}
+
+	const std::vector<Diag*>& GetDiags() const { return diags_; }
+private:
+	std::vector<Diag*> diags_;
+};
+
+}} /* ost::conop */
+#endif
diff --git a/modules/conop/src/model_check.cc b/modules/conop/src/model_check.cc
new file mode 100644
index 000000000..cb5d710b6
--- /dev/null
+++ b/modules/conop/src/model_check.cc
@@ -0,0 +1,150 @@
+#include <ost/mol/iterator.hh>
+#include "model_check.hh"
+#include "amino_acids.hh"
+
+using namespace ost::mol;
+
+namespace ost { namespace conop {
+
+void Checker::CheckForCompleteness(bool require_hydrogens)
+{
+  for (ResidueHandleIter i=ent_.ResiduesBegin(), e=ent_.ResiduesEnd();
+    i!=e; ++i) {
+    ResidueHandle res=*i;
+    String anames="";
+    CompoundPtr compound=lib_->FindCompound(res.GetName(),Compound::PDB);
+    if (!compound) {
+      if (checked_unk_res_) {
+        continue;
+      }
+      diags_.AddDiag(DIAG_UNK_RESIDUE, "unknown residue %0")
+            .AddResidue(res);
+      continue;
+    }
+    int missing_atoms=0;
+    const AtomSpecList& atom_specs=compound->GetAtomSpecs();
+    for (AtomSpecList::const_iterator
+         j=atom_specs.begin(), e2=atom_specs.end(); j!=e2; ++j) {
+      if (j->is_leaving) {
+        continue;
+      }
+      if (!require_hydrogens && (j->element=="H" || j->element=="D")) {
+         continue;
+      }
+      if (!res.FindAtom(j->name) && !res.FindAtom(j->alt_name)) {
+        if (!anames.empty()) {
+          anames+=", ";
+        }
+        anames+="'"+j->name+"'";
+        missing_atoms+=1;
+      }
+    }
+    if (missing_atoms>0) {
+      diags_.AddDiag(DIAG_MISSING_ATOM, "residue %0 is missing %1 atom%s1: %2")
+            .AddResidue(res)
+            .AddInt(missing_atoms)
+            .AddString(anames);
+    }
+  }
+  checked_unk_res_=true;
+}
+
+mol::AtomHandleList Checker::GetHydrogens()
+{
+  AtomHandleList hydlist;
+  for (ResidueHandleIter i=ent_.ResiduesBegin(), e=ent_.ResiduesEnd();
+       i!=e; ++i) {
+    ResidueHandle res=*i;
+    CompoundPtr compound=lib_->FindCompound(res.GetName(),Compound::PDB);
+    if (!compound) {
+      continue;
+    }
+    AtomHandleList atoms=res.GetAtomList();
+    for (AtomHandleList::const_iterator
+         j=atoms.begin(), e2=atoms.end(); j!=e2; ++j) {
+      int specindx=compound->GetAtomSpecIndex(j->GetName());
+      if (specindx!=-1) {  
+        if (compound->GetAtomSpecs()[specindx].element=="H" || compound->GetAtomSpecs()[specindx].element=="D") {
+           hydlist.push_back(*j);
+        }
+      }
+    }
+  }
+  return hydlist;
+}
+
+mol::AtomHandleList Checker::GetZeroOccupancy()
+{
+  AtomHandleList zerolist;
+  for (ResidueHandleIter i=ent_.ResiduesBegin(), e=ent_.ResiduesEnd();
+       i!=e; ++i) {
+    ResidueHandle res=*i;
+    AtomHandleList atoms=res.GetAtomList();
+    for (AtomHandleList::const_iterator j=atoms.begin(), e2=atoms.end(); j!=e2; ++j) {
+      if (j->GetOccupancy()==0.0) {
+        zerolist.push_back(*j);
+      }
+    }
+  }
+  return zerolist;
+}
+
+void Checker::CheckForNonStandard()
+{
+  for (ResidueHandleIter i=ent_.ResiduesBegin(), e=ent_.ResiduesEnd();
+       i!=e; ++i) {
+    ResidueHandle res=*i;
+    CompoundPtr compound=lib_->FindCompound(res.GetName(),Compound::PDB);
+    if (!compound) {
+      if (checked_unk_res_) {
+        continue;
+      }
+      diags_.AddDiag(DIAG_UNK_RESIDUE, "unknown residue %0")            
+            .AddResidue(res);
+      continue;
+    }
+    if (ResidueToAminoAcid(res)==XXX) {
+      diags_.AddDiag(DIAG_NONSTD_RESIDUE, "%0 is not a standard amino acid")            
+            .AddResidue(res);
+    }
+  }
+  checked_unk_res_=true;
+}
+
+void Checker::CheckForUnknownAtoms()
+{
+  for (ResidueHandleIter i=ent_.ResiduesBegin(), e=ent_.ResiduesEnd();
+       i!=e; ++i) {
+    ResidueHandle res=*i;
+    String anames="";
+    CompoundPtr compound=lib_->FindCompound(res.GetName(),Compound::PDB);
+    if (!compound) {
+      if (checked_unk_res_) {
+        continue;
+      }
+      diags_.AddDiag(DIAG_UNK_RESIDUE, "unknown residue %0")            
+            .AddResidue(res);
+      continue;
+    }
+    AtomHandleList atoms=res.GetAtomList();
+    const AtomSpecList& atom_specs=compound->GetAtomSpecs();
+    for (AtomHandleList::const_iterator
+         j=atoms.begin(), e2=atoms.end(); j!=e2; ++j) {
+      bool found=false;
+      for (AtomSpecList::const_iterator
+           k=atom_specs.begin(), e3=atom_specs.end(); k!=e3; ++k) {
+       if (k->name==j->GetName() || k->alt_name==j->GetName()) {
+         found=true;
+         break;
+       }
+      }
+      if (!found) {
+        diags_.AddDiag(DIAG_UNK_ATOM, "residue %0 contains unknown atom %1")
+              .AddResidue(res)
+              .AddAtom(*j);
+      }
+    }
+  }
+  checked_unk_res_=true;
+}
+}} /* ost::conop */
diff --git a/modules/conop/src/model_check.hh b/modules/conop/src/model_check.hh
new file mode 100644
index 000000000..25bdfb88b
--- /dev/null
+++ b/modules/conop/src/model_check.hh
@@ -0,0 +1,30 @@
+#ifndef OST_CONOP_MODEL_CHECK_HH
+#define OST_CONOP_MODEL_CHECK_HH
+
+#include <ost/mol/entity_handle.hh>
+#include <ost/conop/diag.hh>
+#include <ost/conop/compound_lib.hh>
+namespace ost { namespace conop {
+
+class DLLEXPORT_OST_CONOP Checker {
+public:
+	Checker(CompoundLibPtr lib, const mol::EntityHandle& ent,
+			    DiagEngine& diags): lib_(lib), ent_(ent), diags_(diags),
+	                            checked_unk_res_(false)
+  {}
+  void CheckForUnknownAtoms();
+  void CheckForCompleteness(bool require_hydrogens=false);
+  void CheckForNonStandard();
+  mol::AtomHandleList GetHydrogens();
+  mol::AtomHandleList GetZeroOccupancy();
+  
+  const std::vector<Diag*>& GetDiags() const {  return diags_.GetDiags(); }
+private:
+	CompoundLibPtr      lib_;
+	mol::EntityHandle   ent_;
+	DiagEngine&         diags_;
+	bool                checked_unk_res_;
+};
+
+}} /* ost::conop */
+#endif
-- 
GitLab