| 1 | /********************************************************************** | 
| 2 | Copyright (C) 2005 by Chris Morley | 
| 3 |  | 
| 4 | This program is free software; you can redistribute it and/or modify | 
| 5 | it under the terms of the GNU General Public License as published by | 
| 6 | the Free Software Foundation version 2 of the License. | 
| 7 |  | 
| 8 | This program is distributed in the hope that it will be useful, | 
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
| 11 | GNU General Public License for more details. | 
| 12 | ***********************************************************************/ | 
| 13 | #include "fingerprintformat.hpp" | 
| 14 |  | 
| 15 | using namespace std; | 
| 16 | namespace OpenBabel { | 
| 17 |  | 
| 18 | bool FingerprintFormat::WriteMolecule(OBBase* pOb, OBConversion* pConv) | 
| 19 | { | 
| 20 | ostream &ofs = *pConv->GetOutStream(); | 
| 21 |  | 
| 22 | OBFingerprint* pFP; | 
| 23 | string id; | 
| 24 | if(pConv->IsOption("F")) | 
| 25 | { | 
| 26 | while(OBFingerprint::GetNextFPrt(id, pFP)) | 
| 27 | ofs << id << " -- " << pFP->Description() << endl; | 
| 28 | return true; | 
| 29 | } | 
| 30 |  | 
| 31 |  | 
| 32 | bool hexoutput=false; | 
| 33 | if(pConv->IsOption("h") || (pConv->GetOutputIndex()==1 && pConv->IsLast())) | 
| 34 | hexoutput=true; | 
| 35 |  | 
| 36 | string fpid; | 
| 37 | int nbits=0; | 
| 38 | const char* p=pConv->IsOption("f"); | 
| 39 | if(p) | 
| 40 | { | 
| 41 | fpid=p; | 
| 42 | fpid = fpid.substr(0,fpid.find('"')); | 
| 43 | } | 
| 44 |  | 
| 45 | pFP = OBFingerprint::FindFingerprint(fpid); | 
| 46 | if(!pFP) | 
| 47 | { | 
| 48 | #ifdef HAVE_SSTREAM | 
| 49 | stringstream errorMsg; | 
| 50 | #else | 
| 51 | strstream errorMsg; | 
| 52 | #endif | 
| 53 | errorMsg << "Fingerprint type '" << fpid << "' not available" << endl; | 
| 54 | obErrorLog.ThrowError(__func__, errorMsg.str(), obError); | 
| 55 | return false; | 
| 56 | } | 
| 57 |  | 
| 58 | p=pConv->IsOption("N"); | 
| 59 | if(p) | 
| 60 | nbits = atoi(p); | 
| 61 |  | 
| 62 | vector<unsigned int> fptvec; | 
| 63 | if(!pFP->GetFingerprint(pOb, fptvec, nbits)) | 
| 64 | return false; | 
| 65 |  | 
| 66 | OBMol* pmol = dynamic_cast<OBMol*>(pOb); | 
| 67 | if(pmol) | 
| 68 | ofs << ">" << pmol->GetTitle(); | 
| 69 |  | 
| 70 | if(hexoutput) | 
| 71 | { | 
| 72 | int i, bitsset=0; | 
| 73 | for (i=0;i<fptvec.size();++i) | 
| 74 | { | 
| 75 | int wd = fptvec[i]; | 
| 76 | for(;wd;wd=wd<<1)//count bits set by shifting into sign bit until word==0 | 
| 77 | if(wd<0) ++bitsset; | 
| 78 | } | 
| 79 | ofs  << "   " << bitsset << " bits set. "; | 
| 80 | } | 
| 81 |  | 
| 82 | if(pConv->GetOutputIndex()==1) | 
| 83 | { | 
| 84 | //store the fingerprint and name of first molecule | 
| 85 | firstfp=fptvec; | 
| 86 | if(pmol) | 
| 87 | firstname=pmol->GetTitle(); | 
| 88 | if(firstname.empty()) | 
| 89 | firstname = "first mol"; | 
| 90 | } | 
| 91 | else | 
| 92 | { | 
| 93 | ofs << "   Tanimoto from " << firstname << " = " << OBFingerprint::Tanimoto(firstfp, fptvec); | 
| 94 | if(IsPossibleSubstructure(fptvec,firstfp)) | 
| 95 | ofs << "\nPossible superstructure of " << firstname; | 
| 96 | } | 
| 97 | ofs << endl; | 
| 98 |  | 
| 99 | int i; | 
| 100 |  | 
| 101 | if(hexoutput) | 
| 102 | { | 
| 103 | for(i=fptvec.size()-1;i>=0;i--) | 
| 104 | { | 
| 105 | ofs << hex << setfill('0') << setw(8) << fptvec[i] << " " ; | 
| 106 | if((fptvec.size()-i)%6==0) | 
| 107 | ofs <<endl; | 
| 108 | } | 
| 109 | ofs << dec << endl; | 
| 110 | } | 
| 111 | return true; | 
| 112 | } | 
| 113 |  | 
| 114 | bool FingerprintFormat::IsPossibleSubstructure(vector<unsigned int>Mol, vector<unsigned int>Frag) | 
| 115 | { | 
| 116 | //Returns false if Frag is definitely NOT a substructure of Mol | 
| 117 | int i; | 
| 118 | for (i=0;i<Mol.size();++i) | 
| 119 | if((Mol[i] & Frag[i]) ^ Frag[i]) return false; | 
| 120 | return true; | 
| 121 | } | 
| 122 |  | 
| 123 | } //namespace OpenBabel |