| [13a953] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * CheckAgainstAdjacencyFile.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Mar 3, 2011
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
 | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 21 | 
 | 
|---|
 | 22 | #include <iostream>
 | 
|---|
 | 23 | 
 | 
|---|
 | 24 | #include "CheckAgainstAdjacencyFile.hpp"
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | #include "atom.hpp"
 | 
|---|
 | 27 | #include "Bond/bond.hpp"
 | 
|---|
 | 28 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [255829] | 29 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 30 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
 | 31 | #include "Helpers/defs.hpp"
 | 
|---|
| [13a953] | 32 | 
 | 
|---|
 | 33 | CheckAgainstAdjacencyFile::CheckAgainstAdjacencyFile() :
 | 
|---|
 | 34 |   CurrentBonds(new int[MAXBONDS]),
 | 
|---|
 | 35 |   status(true),
 | 
|---|
 | 36 |   NonMatchNumber(0)
 | 
|---|
 | 37 | {
 | 
|---|
 | 38 |   for(int i=0;i<MAXBONDS;i++)
 | 
|---|
 | 39 |     CurrentBonds[i] = 0;
 | 
|---|
 | 40 | }
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | CheckAgainstAdjacencyFile::~CheckAgainstAdjacencyFile()
 | 
|---|
 | 43 | {
 | 
|---|
 | 44 |   delete[](CurrentBonds);
 | 
|---|
 | 45 | }
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | void CheckAgainstAdjacencyFile::CompareBonds(const atom *&Walker, size_t &CurrentBondsOfAtom, int AtomNr, std::map<int, atom*> &ListOfAtoms)
 | 
|---|
 | 48 | {
 | 
|---|
 | 49 |   size_t j = 0;
 | 
|---|
 | 50 |   int id = -1;
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 |   //Log() << Verbose(2) << "Walker is " << *Walker << ", bond partners: ";
 | 
|---|
 | 53 |   const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
 | 54 |   if (CurrentBondsOfAtom == ListOfBonds.size()) {
 | 
|---|
 | 55 |     for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
 | 56 |         Runner != ListOfBonds.end();
 | 
|---|
 | 57 |         ++Runner) {
 | 
|---|
 | 58 |       id = (*Runner)->GetOtherAtom(Walker)->getNr();
 | 
|---|
 | 59 |       j = 0;
 | 
|---|
 | 60 |       for (; (j < CurrentBondsOfAtom) && (CurrentBonds[j++] != id);)
 | 
|---|
 | 61 |         ; // check against all parsed bonds
 | 
|---|
 | 62 |       if (CurrentBonds[j - 1] != id) { // no match ? Then mark in ListOfAtoms
 | 
|---|
 | 63 |         ListOfAtoms[AtomNr] = NULL;
 | 
|---|
 | 64 |         NonMatchNumber++;
 | 
|---|
 | 65 |         status = false;
 | 
|---|
 | 66 |         ELOG(2, id << " can not be found in list." << endl);
 | 
|---|
 | 67 |       } else {
 | 
|---|
 | 68 |         //Log() << Verbose(0) << "[" << id << "]\t";
 | 
|---|
 | 69 |       }
 | 
|---|
 | 70 |     }
 | 
|---|
 | 71 |     //Log() << Verbose(0) << endl;
 | 
|---|
 | 72 |   } else {
 | 
|---|
 | 73 |     LOG(0, "STATUS: Number of bonds for Atom " << *Walker << " does not match, parsed " << CurrentBondsOfAtom << " against " << ListOfBonds.size() << ".");
 | 
|---|
 | 74 |     status = false;
 | 
|---|
 | 75 |   }
 | 
|---|
 | 76 | }
 | 
|---|
 | 77 | ;
 | 
|---|
 | 78 | 
 | 
|---|
 | 79 | /** Checks contents of adjacency file against bond structure in structure molecule.
 | 
|---|
 | 80 |  * \param File file to parser
 | 
|---|
 | 81 |  * \param ListOfAtoms map from int (index in file) to atom
 | 
|---|
 | 82 |  * \return true - structure is equal, false - not equivalence
 | 
|---|
 | 83 |  */
 | 
|---|
 | 84 | bool CheckAgainstAdjacencyFile::operator()(std::ifstream &File, std::map<int, atom*> ListOfAtoms)
 | 
|---|
 | 85 | {
 | 
|---|
 | 86 |   LOG(0, "STATUS: Looking at bond structure stored in adjacency file and comparing to present one ... ");
 | 
|---|
 | 87 |   if (File.fail()) {
 | 
|---|
 | 88 |     LOG(1, "STATUS: Adjacency file not found." << endl);
 | 
|---|
 | 89 |     return false;
 | 
|---|
 | 90 |   }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   char buffer[MAXSTRINGSIZE];
 | 
|---|
 | 93 |   int tmp;
 | 
|---|
 | 94 |   // Parse the file line by line and count the bonds
 | 
|---|
 | 95 |   while (!File.eof()) {
 | 
|---|
 | 96 |     File.getline(buffer, MAXSTRINGSIZE);
 | 
|---|
 | 97 |     stringstream line;
 | 
|---|
 | 98 |     line.str(buffer);
 | 
|---|
 | 99 |     int AtomNr = -1;
 | 
|---|
 | 100 |     line >> AtomNr;
 | 
|---|
 | 101 |     size_t CurrentBondsOfAtom = -1; // we count one too far due to line end
 | 
|---|
 | 102 |     // parse into structure
 | 
|---|
 | 103 |     if (AtomNr >= 0) {
 | 
|---|
 | 104 |       ASSERT(ListOfAtoms.count(AtomNr),
 | 
|---|
 | 105 |           "CheckAgainstAdjacencyFile::operator() - index "
 | 
|---|
 | 106 |           +toString(AtomNr)+" not present in ListOfAtoms.");
 | 
|---|
 | 107 |       const atom *Walker = ListOfAtoms[AtomNr];
 | 
|---|
 | 108 |       while (line >> ws >> tmp) {
 | 
|---|
 | 109 |         LOG(3, "INFO: Recognized bond partner " << tmp);
 | 
|---|
 | 110 |         CurrentBonds[++CurrentBondsOfAtom] = tmp;
 | 
|---|
 | 111 |         ASSERT(CurrentBondsOfAtom < MAXBONDS,
 | 
|---|
 | 112 |             "molecule::CheckAdjacencyFileAgainstMolecule() - encountered more bonds than allowed: "
 | 
|---|
 | 113 |             +toString(CurrentBondsOfAtom)+" >= "+toString(int(MAXBONDS))+"!");
 | 
|---|
 | 114 |       }
 | 
|---|
 | 115 |       // compare against present bonds
 | 
|---|
 | 116 |       CompareBonds(Walker, CurrentBondsOfAtom, AtomNr, ListOfAtoms);
 | 
|---|
 | 117 |     } else {
 | 
|---|
 | 118 |       if (AtomNr != -1)
 | 
|---|
 | 119 |         ELOG(2, AtomNr << " is negative.");
 | 
|---|
 | 120 |     }
 | 
|---|
 | 121 |   }
 | 
|---|
 | 122 | 
 | 
|---|
 | 123 |   if (status) { // if equal we parse the KeySetFile
 | 
|---|
 | 124 |     LOG(0, "STATUS: Equal.");
 | 
|---|
 | 125 |   } else
 | 
|---|
 | 126 |     LOG(0, "STATUS: Not equal by " << NonMatchNumber << " atoms.");
 | 
|---|
 | 127 |   return status;
 | 
|---|
 | 128 | }
 | 
|---|
 | 129 | ;
 | 
|---|