/* * * Copyright (c) 2011, Novartis Institutes for BioMedical Research Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are * met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of Novartis Institutes for BioMedical Research Inc. * nor the names of its contributors may be used to endorse or promote * products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include namespace { std::string getColor(int atNum){ std::string res="#000000"; switch(atNum){ case 7: res="#0000FF";break; case 8: res="#FF0000";break; case 9: res="#33CCCC";break; case 15: res="#FF7F00";break; case 16: res="#CCCC00";break; case 17: res="#00CC00";break; case 35: res="#7F4C19";break; case 0: res="#7F7F7F";break; default: res="#000000"; } return res; } void drawLine(std::vector::const_iterator &pos,std::ostringstream &sstr, unsigned int lineWidthMult){ int width=*pos++; width*=lineWidthMult; int dashed=*pos++; std::string dashString=""; if(dashed){ dashString=";stroke-dasharray:6, 6"; } int an1=*pos++; int an2=*pos++; std::string c1=getColor(an1); std::string c2=getColor(an2); if(c1==c2){ sstr<<"\n"; } else { int xp1 = *pos++; int yp1 = *pos++; int xp2 = *pos++; int yp2 = *pos++; int mx = xp1+(xp2-xp1)/2; int my = yp1+(yp2-yp1)/2; sstr<<"\n"; sstr<<"\n"; } } void drawAtom(std::vector::const_iterator &pos,std::ostringstream &sstr,unsigned int fontSz){ int atNum=*pos++; int xp=*pos++; int yp=*pos++; int slen=*pos++; std::string label=""; for(unsigned int i=0;i \n"; sstr<<""; sstr<<""; sstr<"; sstr<<""; sstr<<"\n"; } std::string DrawingToSVG(const std::vector &drawing, unsigned int lineWidthMult=2,unsigned int fontSize=50){ std::vector::const_iterator pos=drawing.begin()+2; if(*pos!= RDKit::Drawing::BOUNDS){ std::cerr<<"no bounds token found"<\n"; sstr << "\n"; sstr<<""; while(pos!=drawing.end()){ int token=*pos++; switch(token){ case RDKit::Drawing::LINE: drawLine(pos,sstr,lineWidthMult); break; case RDKit::Drawing::ATOM: drawAtom(pos,sstr,fontSize); break; default: std::cerr<<"unrecognized token: "<"; return sstr.str(); } } std::vector MolToDrawing(const RDKit::ROMol &mol,const std::vector *highlightAtoms=0){ RDKit::RWMol *cp = new RDKit::RWMol(mol); try{ RDKit::MolOps::Kekulize(*cp); } catch (...) { delete cp; cp = new RDKit::RWMol(mol); } if(!mol.getNumConformers()) { RDDepict::compute2DCoords(*cp); } std::vector drawing=RDKit::Drawing::DrawMol(*cp,-1,highlightAtoms); delete cp; return drawing; }