File: SmilesWrite.cpp

package info (click to toggle)
rdkit 201403-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 62,288 kB
  • ctags: 15,156
  • sloc: cpp: 125,376; python: 55,674; java: 4,831; ansic: 4,178; xml: 2,499; sql: 1,775; yacc: 1,551; lex: 1,051; makefile: 353; fortran: 183; sh: 148; cs: 93
file content (622 lines) | stat: -rw-r--r-- 22,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// $Id$
//
//  Copyright (C) 2002-2012 Greg Landrum and Rational Discovery LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//
#include "SmilesWrite.h"
#include <GraphMol/RDKitBase.h>
#include <RDGeneral/types.h>
#include <GraphMol/Canon.h>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#include <boost/dynamic_bitset.hpp>
#include <sstream>
#include <map>
#include <list>

//#define VERBOSE_CANON 1

namespace RDKit{

  namespace SmilesWrite{
    const int atomicSmiles[] = {5,6,7,8,9,15,16,17,35,53,-1};
    bool inOrganicSubset(int atomicNumber){
      unsigned int idx=0;
      while( atomicSmiles[idx]<atomicNumber &&
             atomicSmiles[idx]!=-1){
        ++idx;
      }
      if(atomicSmiles[idx]==atomicNumber){
        return true;
      }
      return false;
    }


    std::string GetAtomSmiles(const Atom *atom,bool doKekule,const Bond *bondIn){
      PRECONDITION(atom,"bad atom");
      INT_VECT atomicSmilesVect(atomicSmiles,
                                atomicSmiles+(sizeof(atomicSmiles)-1)/sizeof(atomicSmiles[0]));
      std::stringstream res;
      int fc = atom->getFormalCharge();
      int num = atom->getAtomicNum();
      int isotope = atom->getIsotope();

      bool needsBracket=false;
      std::string symb;
      if(atom->hasProp("smilesSymbol")){
        atom->getProp("smilesSymbol",symb);
      } else {
        symb=PeriodicTable::getTable()->getElementSymbol(num);
      }
      //symb = atom->getSymbol();
      if(inOrganicSubset(num)){
        // it's a member of the organic subset
        //if(!doKekule && atom->getIsAromatic() && symb[0] < 'a') symb[0] -= ('A'-'a');

        // -----
        // figure out if we need to put a bracket around the atom,
        // the conditions for this are:
        //   - formal charge specified
        //   - the atom has a nonstandard valence
        //   - chirality present and writing isomeric smiles
        //   - non-default isotope and writing isomeric smiles
        //   - atom-map information present
        const INT_VECT &defaultVs=PeriodicTable::getTable()->getValenceList(num);
        int totalValence= atom->getTotalValence();
        bool nonStandard=false;

        if(atom->getNumRadicalElectrons()){
          nonStandard=true;
        } else if((num==7||num==15) && atom->getIsAromatic() && atom->getNumExplicitHs()){
          // another type of "nonstandard" valence is an aromatic N or P with
          // explicit Hs indicated:
          nonStandard=true;
        } else {
          nonStandard = (totalValence!=defaultVs.front() && atom->getTotalNumHs());
        }

        if(fc || nonStandard){
          needsBracket=true;
        }
        if(atom->getOwningMol().hasProp("_doIsoSmiles")){
          if( atom->getChiralTag()!=Atom::CHI_UNSPECIFIED ){
            needsBracket = true;
          } else if(isotope){
            needsBracket=true;
          }
        }
        if(atom->hasProp("molAtomMapNumber")){
          needsBracket=true;
        }
      } else {
        needsBracket = true;
      }
      if( needsBracket ) res << "[";

      if(isotope && atom->getOwningMol().hasProp("_doIsoSmiles")){
        res <<isotope;
      }
      // this was originally only done for the organic subset,
      // applying it to other atom-types is a fix for Issue 3152751: 
      if(!doKekule && atom->getIsAromatic() && symb[0]>='A' && symb[0] <= 'Z'){
        symb[0] -= ('A'-'a');
      }
      res << symb;

      if(atom->getOwningMol().hasProp("_doIsoSmiles") &&
         atom->getChiralTag()!=Atom::CHI_UNSPECIFIED ){
        INT_LIST trueOrder;
        atom->getProp("_TraversalBondIndexOrder",trueOrder);
        int nSwaps=  atom->getPerturbationOrder(trueOrder);
        // if( !atom->hasProp("_CIPCode") && atom->hasProp("_CIPRank") &&
        //     !atom->getOwningMol().hasProp("_ringSteroWarning") ){
        //   BOOST_LOG(rdWarningLog)<<"Warning: ring stereochemistry detected. The output SMILES is not canonical."<<std::endl;
        //   atom->getOwningMol().setProp("_ringStereoWarning",true,true);
        // }

        if(atom->getDegree()==3 && !bondIn){
          // This is a special case. Here's an example:
          //   Our internal representation of a chiral center is equivalent to:
          //     [C@](F)(O)(C)[H]
          //   we'll be dumping it without the H, which entails a reordering:
          //     [C@@H](F)(O)C
          ++nSwaps;
        }
        //BOOST_LOG(rdErrorLog)<<">>>> "<<atom->getIdx()<<" "<<nSwaps<<" "<<atom->getChiralTag()<<std::endl;
        std::string atStr="";
        switch(atom->getChiralTag()){
        case Atom::CHI_TETRAHEDRAL_CW:
          if(!(nSwaps%2))
            atStr = "@@";
          else
            atStr = "@";
          break;
        case Atom::CHI_TETRAHEDRAL_CCW:
          if(!(nSwaps%2))
            atStr = "@";
          else
            atStr = "@@";
          break;
        default:
          break;
        }
        res << atStr;
      }

      if(needsBracket){
        unsigned int totNumHs=atom->getTotalNumHs();
        if(totNumHs > 0){
          res << "H";
          if(totNumHs > 1) res << totNumHs;
        }
        if(fc > 0){
          res << "+";
          if(fc > 1) res << fc;
        } else if(fc < 0) {
          res << "-";
          if(fc < -1) res << -fc;
        }
    
        if(atom->hasProp("molAtomMapNumber")){
          int mapNum;
          atom->getProp("molAtomMapNumber",mapNum);
          res<<":"<<mapNum;
        }
        res << "]";
      }

      // If the atom has this property, the contained string will
      // be inserted directly in the SMILES:
      if(atom->hasProp("_supplementalSmilesLabel")){
        std::string label;
        atom->getProp("_supplementalSmilesLabel",label);
        res << label;
      }

      return res.str();
    }

    std::string GetBondSmiles(const Bond *bond,int atomToLeftIdx,bool doKekule,bool allBondsExplicit){
      PRECONDITION(bond,"bad bond");
      if(atomToLeftIdx<0) atomToLeftIdx=bond->getBeginAtomIdx();

      std::stringstream res;
      bool aromatic=false;
      if( !doKekule &&
          (bond->getBondType() == Bond::SINGLE ||
           bond->getBondType() == Bond::DOUBLE ||
           bond->getBondType() == Bond::AROMATIC) ){
        Atom *a1,*a2;
        a1 = bond->getOwningMol().getAtomWithIdx(atomToLeftIdx);
        a2 = bond->getOwningMol().getAtomWithIdx(bond->getOtherAtomIdx(atomToLeftIdx));
        if((a1->getIsAromatic() && a2->getIsAromatic()) &&
           (a1->getAtomicNum()||a2->getAtomicNum())) aromatic=true;
      }

      Bond::BondDir dir= bond->getBondDir();

      if(bond->hasProp("_TraversalRingClosureBond")){
        //std::cerr<<"FLIP: "<<bond->getIdx()<<" "<<bond->getBeginAtomIdx()<<"-"<<bond->getEndAtomIdx()<<std::endl;
        //if(dir==Bond::ENDDOWNRIGHT) dir=Bond::ENDUPRIGHT;
        //else if(dir==Bond::ENDUPRIGHT) dir=Bond::ENDDOWNRIGHT;
        bond->clearProp("_TraversalRingClosureBond");
      }
  
      switch(bond->getBondType()){
      case Bond::SINGLE:
        if( dir != Bond::NONE && dir != Bond::UNKNOWN ){
          switch(dir){
          case Bond::ENDDOWNRIGHT:
            if(bond->getOwningMol().hasProp("_doIsoSmiles"))  res << "\\";
            break;
          case Bond::ENDUPRIGHT:
            if(bond->getOwningMol().hasProp("_doIsoSmiles"))  res << "/";
            break;
          default:
            break;
          }
        } else {
          // if the bond is marked as aromatic and the two atoms
          //  are aromatic, we need no marker (this arises in kekulized
          //  molecules).
          // FIX: we should be able to dump kekulized smiles
          //   currently this is possible by removing all
          //   isAromatic flags, but there should maybe be another way
          if(allBondsExplicit) res<<"-";
          else if( aromatic && !bond->getIsAromatic() ) res << "-";
        }
        break;
      case Bond::DOUBLE:
        // see note above
        if( !aromatic || !bond->getIsAromatic() ) res << "=";
        break;
      case Bond::TRIPLE: res << "#"; break;
      case Bond::AROMATIC:
        if ( dir != Bond::NONE && dir != Bond::UNKNOWN ){
          switch(dir){
          case Bond::ENDDOWNRIGHT:
            if(bond->getOwningMol().hasProp("_doIsoSmiles"))  res << "\\";
            break;
          case Bond::ENDUPRIGHT:
            if(bond->getOwningMol().hasProp("_doIsoSmiles"))  res << "/";
            break;
          default:
            break;
          }
        } else if(allBondsExplicit || !aromatic ){
          res << ":";
        }
        break;
      case Bond::DATIVE:
        if(atomToLeftIdx>=0 &&
           bond->getBeginAtomIdx()==static_cast<unsigned int>(atomToLeftIdx) ) res << ">";
        else res << "<";
        break;
      default:
        res << "~";
      }
      return res.str();
    }

    std::string FragmentSmilesConstruct(ROMol &mol,int atomIdx,
                                        std::vector<Canon::AtomColors> &colors,
                                        INT_VECT &ranks,bool doKekule,bool canonical,
                                        bool allBondsExplicit,
                                        std::vector<unsigned int> &atomOrdering,
                                        const boost::dynamic_bitset<> *bondsInPlay=0,
                                        const std::vector<std::string> *atomSymbols=0,
                                        const std::vector<std::string> *bondSymbols=0
                                        ){
      PRECONDITION(!bondsInPlay||bondsInPlay->size()>=mol.getNumBonds(),"bad bondsInPlay");
      PRECONDITION(!atomSymbols||atomSymbols->size()>=mol.getNumAtoms(),"bad atomSymbols");
      PRECONDITION(!bondSymbols||bondSymbols->size()>=mol.getNumBonds(),"bad bondSymbols");
      Canon::MolStack molStack;
      // try to prevent excessive reallocation
      molStack.reserve(mol.getNumAtoms()+
                       mol.getNumBonds());
      std::stringstream res;

      std::map<int,int> ringClosureMap;
      int ringIdx,closureVal;
      if(!canonical) mol.setProp("_StereochemDone",1);
      std::list<unsigned int> ringClosuresToErase;

      Canon::canonicalizeFragment(mol,atomIdx,colors,ranks,
                                  molStack,bondsInPlay,bondSymbols);
      Bond *bond=0;
      BOOST_FOREACH(Canon::MolStackElem mSE,molStack){
        switch(mSE.type){
        case Canon::MOL_STACK_ATOM:
          if(!ringClosuresToErase.empty()){
            BOOST_FOREACH(unsigned int rclosure,ringClosuresToErase){
              ringClosureMap.erase(rclosure);
            }
            ringClosuresToErase.clear();
          }
          //std::cout<<"\t\tAtom: "<<mSE.obj.atom->getIdx()<<std::endl;
          if(!atomSymbols){
            res << GetAtomSmiles(mSE.obj.atom,doKekule,bond);
          } else {
            res << (*atomSymbols)[mSE.obj.atom->getIdx()];
          }
          atomOrdering.push_back(mSE.obj.atom->getIdx());
          
          break;
        case Canon::MOL_STACK_BOND:
          bond = mSE.obj.bond;
          //std::cout<<"\t\tBond: "<<bond->getIdx()<<std::endl;
          if(!bondSymbols){
            res << GetBondSmiles(bond,mSE.number,doKekule,allBondsExplicit);
          } else {
            res << (*bondSymbols)[bond->getIdx()];
          }
          break;
        case Canon::MOL_STACK_RING:
          ringIdx = mSE.number;
          //std::cout<<"\t\tRing: "<<ringIdx;
          if(ringClosureMap.count(ringIdx)){
            // the index is already in the map ->
            //   we're closing a ring, so grab
            //   the index and then delete the value:
            closureVal = ringClosureMap[ringIdx];
            //ringClosureMap.erase(ringIdx);
            ringClosuresToErase.push_back(ringIdx);
          } else {
            // we're opening a new ring, find the index for it:
            closureVal = 1;
            bool done=false;
            // EFF: there's got to be a more efficient way to do this
            while(!done){
              std::map<int,int>::iterator mapIt;
              for(mapIt=ringClosureMap.begin();
                  mapIt!=ringClosureMap.end();
                  mapIt++){
                if(mapIt->second==closureVal) break;
              }
              if(mapIt==ringClosureMap.end()){
                done=true;
              } else {
                closureVal+=1;
              }
            }
            ringClosureMap[ringIdx]=closureVal;
          }
          if(closureVal >= 10){
            res << "%";
          }
          //std::cout << " > " << closureVal <<std::endl;
          res << closureVal;
          break;
        case Canon::MOL_STACK_BRANCH_OPEN:
          res << "(";
          break;
        case Canon::MOL_STACK_BRANCH_CLOSE:
          res << ")";
          break;
        default:
          break;
        }
      }
      return res.str();
    }

  } // end of namespace SmilesWrite


  std::string MolToSmiles(const ROMol &mol,bool doIsomericSmiles,
                          bool doKekule,int rootedAtAtom,bool canonical,
                          bool allBondsExplicit){
    if(!mol.getNumAtoms()) return "";
    PRECONDITION(rootedAtAtom<0||static_cast<unsigned int>(rootedAtAtom)<mol.getNumAtoms(),
                 "rootedAtomAtom must be less than the number of atoms");

    ROMol tmol(mol,true);
    if(doIsomericSmiles){
      tmol.setProp("_doIsoSmiles",1);
    }
#if 0
    std::cout << "----------------------------" << std::endl;
    std::cout << "MolToSmiles:"<< std::endl;
    tmol.debugMol(std::cout);
    std::cout << "----------------------------" << std::endl;
#endif  
    std::string res;

    for(ROMol::AtomIterator atIt=tmol.beginAtoms();atIt!=tmol.endAtoms();atIt++){
      (*atIt)->updatePropertyCache(false);
    }

    unsigned int nAtoms=tmol.getNumAtoms();
    INT_VECT ranks(nAtoms,-1);

    std::vector<unsigned int> atomOrdering;

    // clean up the chirality on any atom that is marked as chiral,
    // but that should not be:
    if(doIsomericSmiles){
      if(!mol.hasProp("_StereochemDone")){
        MolOps::assignStereochemistry(tmol,true);
      } else {
        tmol.setProp("_StereochemDone",1);
        // we need the CIP codes:
        for(unsigned int aidx=0;aidx<tmol.getNumAtoms();++aidx){
          const Atom *oAt=mol.getAtomWithIdx(aidx);
          if(oAt->hasProp("_CIPCode")){
            std::string cipCode;
            oAt->getProp("_CIPCode",cipCode);
            tmol.getAtomWithIdx(aidx)->setProp("_CIPCode",cipCode);
          }
        }
      }
    }
    if(canonical){
      MolOps::rankAtoms(tmol,ranks,true,doIsomericSmiles,doIsomericSmiles);
    } else {
      for(unsigned int i=0;i<tmol.getNumAtoms();++i) ranks[i]=i;
    }
#ifdef VERBOSE_CANON
    for(unsigned int tmpI=0;tmpI<ranks.size();tmpI++){
      std::cout << tmpI << " " << ranks[tmpI] << " " << *(tmol.getAtomWithIdx(tmpI)) << std::endl;
    }
#endif

    std::vector<Canon::AtomColors> colors(nAtoms,Canon::WHITE_NODE);
    std::vector<Canon::AtomColors>::iterator colorIt;
    colorIt = colors.begin();
    // loop to deal with the possibility that there might be disconnected fragments
    while(colorIt != colors.end()){
      int nextAtomIdx=-1;
      std::string subSmi;

      // find the next atom for a traverse
      if(rootedAtAtom>=0){
        nextAtomIdx=rootedAtAtom;
        rootedAtAtom=-1;
      } else {
        int nextRank = nAtoms+1;
        for(unsigned int i=0;i<nAtoms;i++){
          if( colors[i] == Canon::WHITE_NODE && ranks[i] < nextRank ){
            nextRank = ranks[i];
            nextAtomIdx = i;
          }
        }
      }
      CHECK_INVARIANT(nextAtomIdx>=0,"no start atom found");

      subSmi = SmilesWrite::FragmentSmilesConstruct(tmol, nextAtomIdx, colors,
                                                    ranks,doKekule,canonical,allBondsExplicit,
                                                    atomOrdering);

      res += subSmi;
      colorIt = std::find(colors.begin(),colors.end(),Canon::WHITE_NODE);
      if(colorIt != colors.end()){
        res += ".";
      }
    }
    mol.setProp("_smilesAtomOutputOrder",atomOrdering,true);
    return res;
  } // end of MolToSmiles()

  std::string MolFragmentToSmiles(const ROMol &mol,
                                  const std::vector<int> &atomsToUse,
                                  const std::vector<int> *bondsToUse,
                                  const std::vector<std::string> *atomSymbols,
                                  const std::vector<std::string> *bondSymbols,
                                  bool doIsomericSmiles,
                                  bool doKekule,
                                  int rootedAtAtom,
                                  bool canonical,
                                  bool allBondsExplicit){
    PRECONDITION(atomsToUse.size(),
                 "no atoms provided");
    PRECONDITION(rootedAtAtom<0||static_cast<unsigned int>(rootedAtAtom)<mol.getNumAtoms(),
                 "rootedAtomAtom must be less than the number of atoms");
    PRECONDITION(rootedAtAtom<0||std::find(atomsToUse.begin(),atomsToUse.end(),rootedAtAtom)!=atomsToUse.end(),
                 "rootedAtomAtom not found in atomsToUse");
    PRECONDITION(!atomSymbols || atomSymbols->size()>=mol.getNumAtoms(),
                 "bad atomSymbols vector");
    PRECONDITION(!bondSymbols || bondSymbols->size()>=mol.getNumBonds(),
                 "bad bondSymbols vector");
    if(!mol.getNumAtoms()) return "";

    ROMol tmol(mol,true);
    if(doIsomericSmiles){
      tmol.setProp("_doIsoSmiles",1);
    }
    std::string res;

    boost::dynamic_bitset<> atomsInPlay(mol.getNumAtoms(),0);
    BOOST_FOREACH(int aidx,atomsToUse){
      atomsInPlay.set(aidx);
    }
    // figure out which bonds are actually in play:
    boost::dynamic_bitset<> bondsInPlay(mol.getNumBonds(),0);
    if(bondsToUse){
      BOOST_FOREACH(int bidx,*bondsToUse){
        bondsInPlay.set(bidx);
      }
    } else {
      BOOST_FOREACH(int aidx,atomsToUse){
        ROMol::OEDGE_ITER beg,end;
        boost::tie(beg,end) = mol.getAtomBonds(mol.getAtomWithIdx(aidx));
        while(beg!=end){
          const BOND_SPTR bond=mol[*beg];
          if(atomsInPlay[bond->getOtherAtomIdx(aidx)])
            bondsInPlay.set(bond->getIdx());
          ++beg;
        }
        
      }
    }

    // copy over the rings that only involve atoms/bonds in this fragment:
    tmol.getRingInfo()->reset();
    tmol.getRingInfo()->initialize();
    for(unsigned int ridx=0;ridx<mol.getRingInfo()->numRings();++ridx){
      const INT_VECT &aring=mol.getRingInfo()->atomRings()[ridx];
      const INT_VECT &bring=mol.getRingInfo()->bondRings()[ridx];
      bool keepIt=true;
      BOOST_FOREACH(int aidx,aring){
        if(!atomsInPlay[aidx]){
          keepIt=false;
          break;
        }
      }
      if(keepIt){
        BOOST_FOREACH(int bidx,bring){
          if(!bondsInPlay[bidx]){
            keepIt=false;
            break;
          }
        }
      }
      if(keepIt){
        tmol.getRingInfo()->addRing(aring,bring);
      }
    }
    
    for(ROMol::AtomIterator atIt=tmol.beginAtoms();atIt!=tmol.endAtoms();atIt++){
      (*atIt)->updatePropertyCache(false);
    }

    INT_VECT ranks(tmol.getNumAtoms(),-1);

    std::vector<unsigned int> atomOrdering;

    // clean up the chirality on any atom that is marked as chiral,
    // but that should not be:
    if(doIsomericSmiles){
      if(!mol.hasProp("_StereochemDone")){
        MolOps::assignStereochemistry(tmol,true);
      } else {
        tmol.setProp("_StereochemDone",1);
        // we need the CIP codes:
        BOOST_FOREACH(int aidx,atomsToUse){
          const Atom *oAt=mol.getAtomWithIdx(aidx);
          if(oAt->hasProp("_CIPCode")){
            std::string cipCode;
            oAt->getProp("_CIPCode",cipCode);
            tmol.getAtomWithIdx(aidx)->setProp("_CIPCode",cipCode);
          }
        }
      }
    }
    if(canonical){
      MolOps::rankAtomsInFragment(tmol,ranks,atomsInPlay,bondsInPlay,atomSymbols,bondSymbols);
    } else {
      for(unsigned int i=0;i<tmol.getNumAtoms();++i) ranks[i]=i;
    }
#ifdef VERBOSE_CANON
    for(unsigned int tmpI=0;tmpI<ranks.size();tmpI++){
      std::cout << tmpI << " " << ranks[tmpI] << " " << *(tmol.getAtomWithIdx(tmpI)) << std::endl;
    }
#endif

    std::vector<Canon::AtomColors> colors(tmol.getNumAtoms(),Canon::BLACK_NODE);
    BOOST_FOREACH(int aidx,atomsToUse){
      colors[aidx]=Canon::WHITE_NODE;
    }
    std::vector<Canon::AtomColors>::iterator colorIt;
    colorIt = colors.begin();
    // loop to deal with the possibility that there might be disconnected fragments
    while(colorIt != colors.end()){
      int nextAtomIdx=-1;
      std::string subSmi;

      // find the next atom for a traverse
      if(rootedAtAtom>=0){
        nextAtomIdx=rootedAtAtom;
        rootedAtAtom=-1;
      } else {
        int nextRank = tmol.getNumAtoms()+1;
        BOOST_FOREACH(int i,atomsToUse){
          if( colors[i] == Canon::WHITE_NODE && ranks[i] < nextRank ){
            nextRank = ranks[i];
            nextAtomIdx = i;
          }
        }
      }
      CHECK_INVARIANT(nextAtomIdx>=0,"no start atom found");

      subSmi = SmilesWrite::FragmentSmilesConstruct(tmol, nextAtomIdx, colors,
                                                    ranks,doKekule,canonical,allBondsExplicit,
                                                    atomOrdering,
                                                    &bondsInPlay,
                                                    atomSymbols,bondSymbols);

      res += subSmi;
      colorIt = std::find(colors.begin(),colors.end(),Canon::WHITE_NODE);
      if(colorIt != colors.end()){
        res += ".";
      }
    }
    mol.setProp("_smilesAtomOutputOrder",atomOrdering,true);
    return res;
  } // end of MolFragmentToSmiles()
}