///////////////////////////////////////////////////////////////////////////// /// mol2gif.cpp - with added features /// 22 Aug 2005 ///////////////////////////////////////////////////////////////////////////// #include "openeye.h" #include #include #include #include #include #include "oeplatform.h" #include "oesystem.h" #include "oechem.h" #include "oedepict.h" using namespace OEPlatform; using namespace OESystem; using namespace OEChem; using namespace OEDepict; using namespace std; #define FORMAT_GIF 0 #define FORMAT_BMP 1 #define FORMAT_EPS 2 #define PIXSCALE 25.0 static int WideOpt,HighOpt; static bool TransparentOpt; static bool AutoSizeOpt; static int FormatOpt; static int TitleOpt; static const char *inpname; static const char *outname; static string smarts=""; static bool chemtattoo=false; static bool showatommaps=false; static unsigned int verbose=0; static OEDepictView view; static void DefaultOptions(void) { inpname = (const char*)0; outname = (const char*)0; view.SetPixScale(PIXSCALE); view.SetShowHydrogens(false); FormatOpt = FORMAT_GIF; TransparentOpt = false; AutoSizeOpt = true; TitleOpt = 0; WideOpt = 0; HighOpt = 0; } static void DisplayUsage(void) { char *usage= "mol2gif [options] []\n" "\n" " options:\n" " -cob\n" " -bow\n" " -width W\n" " -height H\n" " -scale S\n" " -smarts SMARTS\n" " -[no]aromcirc\n" " -[no]aromdash\n" " -[no]title\n" " -titletop\n" " -titlebot\n" " -[no]trans[parent]\n" " -[no]super\n" " -[no]showh\n" " -[no]dative\n" " -chemtattoo\n" " -showatommaps\n" " -nologo\n" " -v ... verbose\n" " -vv ... very verbose\n" " -terse\n" " -gif\n" " -bmp\n" " -eps\n" ; OEThrow.Usage(usage); } static void ProcessCommandLine(int argc, const char *argv[]) { const char *ptr; int i, j; j = 0; for (i=1; i a; OESubSearch pat; OEIter m; OEGraphMol mol; oemolistream ims; unsigned int i; unsigned int p; char buff[100]; float modsim; string atomname; AtomStyle *astyle; char c; oeofstream ofs; oeofstream *optr; int transcolor; OEThrow.Info("Mol2GIF v2.2x Chemical Structure Renderer"); OEThrow.Info("OpenEye Scientific Software, October 2004\n"); DefaultOptions(); ProcessCommandLine(argc,argv); if (!ims.open(inpname)) OEThrow.Fatal("Cannot open input file!"); molformat=ims.GetFormat(); if (!OEReadMolecule(ims,mol)) OEThrow.Fatal("Cannot read input file!"); ims.close(); if (!smarts.empty()) { pat.Init(smarts.c_str()); for (m=pat.Match(mol);m;++m) { for (a=m->GetTargetAtoms();a;++a) a->SetBoolData("match",true); } } ///////////////////////////////////////////////////////////////////////////// /// The format of the chemtattoo input file is smiles where the "name" /// consists of a float molecule modal-similarity and then string of /// numatom [012]'s indicating atom similarity. ///////////////////////////////////////////////////////////////////////////// string name = mol.GetTitle(); chemtattoo&=(molformat==OEFormat::SMI || molformat==OEFormat::ISM); if (chemtattoo) { p=name.find_first_of(" "); if (p==0 || p>name.length()) OEThrow.Fatal("Chemtattoo error: \"%s\"",name.c_str()); modsim = atof(name.substr(0,p).c_str()); string ttoo = name.substr(p+1); if (ttoo.length()!=mol.NumAtoms()) { OEThrow.Warning("Chemtattoo \"%s\" length(%d)!=atomcount(%d)", ttoo.c_str(),ttoo.length(),mol.NumAtoms()); mol.SetTitle("ChemTattoo: error"); chemtattoo=false; } else { i=0; for (a=mol.GetAtoms();a;++a) { a->SetData("chemtattoo",ttoo[i++]); } sprintf(buff,"ChemTattooModal: %.3f",modsim); mol.SetTitle(buff); } } OESetDimensionFromCoords(mol); if (mol.GetDimension() != 2) { if (mol.GetDimension() == 3) { OEPerceiveChiral(mol); OE3DToBondStereo(mol); OE3DToAtomStereo(mol); } if (!chemtattoo) OESuppressHydrogens(mol); OEAddDepictionHydrogens(mol); OEDepictCoordinates(mol); OEMDLPerceiveBondStereo(mol); } if (showatommaps) { for (a=mol.GetAtoms();a;++a) { atomname=OEGetAtomicSymbol(a->GetAtomicNum()); if (a->GetMapIdx()) { sprintf(buff,":%d",a->GetMapIdx()); atomname+=buff; OESetAtomComment(a,atomname); a->SetData("atomname",atomname); } } } /// Done messing with mol. Now create view. if (TitleOpt==1) { view.SetTitleSize(12); } else if (TitleOpt==0) { const char *name = mol.GetTitle(); view.SetTitleSize((name && *name)? 12 : 0); } else view.SetTitleSize(0); view.SetMolecule(&mol.SCMol()); if (HighOpt && WideOpt && AutoSizeOpt) view.AdjustView(WideOpt,HighOpt); else view.DetermineView(WideOpt,HighOpt); if (!smarts.empty()) { view.GetBackColor(&r,&g,&b); for (a=mol.GetAtoms();a;++a) { AtomStyle *astyle = view.AStyle(a->GetIdx()); if (a->GetBoolData("match")) { if (!r && !g && !b) /// cob mode { astyle->r = 255; astyle->g = 255; astyle->b = 0; } else { astyle->r = 255; astyle->g = 0; astyle->b = 0; } } else { if (!r && !g && !b) /// cob mode { astyle->r = 127; astyle->g = 127; astyle->b = 127; } else { astyle->r = 0; astyle->g = 0; astyle->b = 0; } } } } if (chemtattoo) { for (a=mol.GetAtoms();a;++a) { astyle = view.AStyle(a->GetIdx()); c = a->GetData("chemtattoo"); switch (c) { case '0': astyle->r=255; astyle->g=0; astyle->b=0; break; case '1': astyle->r=127; astyle->g=0; astyle->b=127; break; case '2': astyle->r=0; astyle->g=0; astyle->b=255; break; default: astyle->r=0; astyle->g=0; astyle->b=0; } } } if (showatommaps) { for (a=mol.GetAtoms();a;++a) { astyle = view.AStyle(a->GetIdx()); atomname = a->GetData("atomname"); if (!atomname.empty()) { astyle->haslabel=true; } } } if (!outname) { switch (FormatOpt) { default: case FORMAT_GIF: oeout << "Content-type: image/gif" << oeendl; oeout << oeendl; break; case FORMAT_BMP: oeout << "Content-type: image/bmp" << oeendl; oeout << oeendl; break; case FORMAT_EPS: oeout << "Content-type: application/postscript" << oeendl; oeout << oeendl; break; } optr = &oeout; } else if (strcmp(outname,"-")) { if (!ofs.open(outname)) { OEThrow.Fatal("Unable to create output file!"); } optr = &ofs; } else optr = &oeout; OE8BitImage img(view.XRange(),view.YRange()); view.RenderImage(&img, true); // if (verbose>1) // { // OEThrow.Info("coordinates in final reference frame:"); // for (a=mol.GetAtoms();a;++a) // { // int xyz[3]; // atomname=OEGetAtomicSymbol(a->GetAtomicNum()); // view.GetViewCoords((OEMolBase *)&mol, a, xyz); // OEThrow.Info("\t%s\t%d,%d",atomname.c_str(),xyz[0],xyz[1]); // } // } view.GetForeColor(&r,&g,&b); img.DrawRect(0,0,view.XRange()-1,view.YRange()-1,img.Color(r,g,b)); if (TransparentOpt) { view.GetBackColor(&r, &g, &b); transcolor = img.Color(r,g,b); } else { transcolor = -1; } switch (FormatOpt) { default: case FORMAT_GIF: OEWriteGIF(*optr, img, transcolor); break; case FORMAT_BMP: OEWriteBMP(*optr, img); break; case FORMAT_EPS: OEWriteEPS(*optr, img); break; } optr->flush(); optr->close(); fflush(stdout); return 0; }