void ResidIterator::first() { m_citer = m_pTarg->begin(); if (m_citer==m_pTarg->end()) return; // no chains in mol (error) MolChainPtr pchain = m_citer->second; m_riter = m_citer->second->begin(); if (m_riter==pchain->end()) { // pchain is empty (no residue) // --> try the next chain next(); return; } if (m_pSel.isnull()) return; // if (m_pSel->isSelectedResid(*m_riter)!=Selection::SEL_NONE) return; // OK // proceed to the next selected residue next(); }
void ResidIterator::next() { while (m_citer != m_pTarg->end()) { MolChainPtr pchain = m_citer->second; MB_ASSERT(!pchain.isnull()); if (m_riter != pchain->end()) { ++m_riter; } else { // the m_riter reached to the end of the chain ++m_citer; if (m_citer == m_pTarg->end()) break; // end of chain --> no more resid!! pchain = m_citer->second; m_riter = pchain->begin(); } if (m_riter == pchain->end()) { // the m_riter reached to the end of the chain // --> check the next residue continue; } if (m_pSel.isnull()) return; if (m_pSel->isSelectedResid(*m_riter)!=Selection::SEL_NONE) return; // OK // check the next residue } return; }
MolResiduePtr MolCoord::getResidue(const LString &chain, ResidIndex resid) const { MolChainPtr pChain = getChain(chain); if (pChain.isnull()) return MolResiduePtr(); return pChain->getResidue(resid); }
bool MolCoord::removeAtom(int atomid) { MolAtomPtr pAtom = getAtom(atomid); if (pAtom.isnull() || pAtom->getParentUID()!=getUID()) return false; m_atomPool.remove(atomid); // invalidate ID pAtom->setID(-1); const LString &aname = pAtom->getName(); char cConfID = pAtom->getConfID(); ResidIndex nresid = pAtom->getResIndex(); const LString &cname = pAtom->getChainName(); MolChainPtr pCh = getChain(cname); if (pCh.isnull()) return false; MolResiduePtr pRes = getResidue(cname, nresid); if (pRes.isnull()) return false; // remove atom if (!pRes->removeAtom(aname, cConfID)) return false; if (pRes->getAtomSize()>0) return true; // purge the empty residue if (!pCh->removeResidue(nresid)) return false; // delete pRes; if (pCh->getSize()>0) return true; // purge the empty chain if (!removeChain(cname)) return false; // delete pCh; return true; }
int SelCommand::isSelectedChain(MolChainPtr pCh) { if (m_pSelRoot==NULL) return true; MolChain::ResidCursor riter = pCh->begin(); int nsel=0, nresid=0; for ( ; riter!=pCh->end(); riter++) { MolResiduePtr pRes = *riter; if (pRes.isnull()) continue; if (isSelectedResid(pRes)) nsel++; nresid++; } if (nsel==nresid) return SEL_ALL; else if (nsel==0) return SEL_NONE; else return SEL_PART; }
// write PDB file to stream bool PDBFileWriter::write(qlib::OutStream &outs) { m_pMol = getTarget<MolCoord>(); if (m_pMol==NULL) { LOG_DPRINTLN("PDBWriter> MolCoord is not attached !!"); return false; } // check extension record handlers PDBFileReader::HndlrTab &htab = PDBFileReader::m_htab; bool bUseHndlr = htab.size()>0; MolCoord *pMol = m_pMol; qlib::PrintStream prs(outs); // // write header // //LString sbuf = pqsys->getVersion(); //prs.formatln("REMARK PDB File Generated by CueMol (ver %s)", sbuf.c_str()); prs.formatln("REMARK PDB File Generated by CueMol2"); // // write SSBOND record // writeSSBonds(prs); // // write LINK record // writeLinks(prs); writeSecstr(prs); // // write extension records (CRYST1) // if (bUseHndlr) { LString sbuf; PDBFileReader::HndlrTab::const_iterator iter = htab.begin(); for (; iter!=htab.end(); ++iter) { PDBFileReader::RecordHandler *ph = iter->second; if (ph!=NULL && ph->write(sbuf, pMol)) { prs.println(sbuf); } } } // // write body (ATOM/ANISOU records) // int nserial = 1; // Sort chain names by ASCII order // take care of '_' (empty) chain std::list<LString> chnames; { MolCoord::ChainIter iter = pMol->begin(); bool bHasEmptyChain = false; for (; iter!=pMol->end(); ++iter) { MolChainPtr pChn = iter->second; LString chnam = (pChn->getName().c_str()); if (chnam.equals("_")) { bHasEmptyChain = true; continue; } chnames.push_back(chnam); } chnames.sort(); if (bHasEmptyChain) chnames.push_back("_"); } std::list<LString>::const_iterator cniter = chnames.begin(); for (; cniter!=chnames.end(); ++cniter) { LString chnam = *cniter; MolChainPtr pChn = pMol->getChain(chnam); // format chain name char cch = convChainName(chnam); LString resnam; MolChain::ResidCursor2 riter = pChn->begin2(); // int nlastres = 0; for (; riter!=pChn->end2(); ++riter) { //MolResiduePtr pRes = *riter; MolResiduePtr pRes = riter->second; if (pRes.isnull()) continue; ResidIndex rindex = pRes->getIndex(); resnam = pRes->getName(); // format residue name // resnam = resnam.toUpperCase(); resnam = resnam.substr(0,3); // sort atom by AID std::list<int> atmlist; { MolResidue::AtomCursor aiter = pRes->atomBegin(); for (; aiter!=pRes->atomEnd(); ++aiter) { atmlist.push_back(aiter->second); } atmlist.sort(); } std::list<int>::const_iterator iter = atmlist.begin(); for (; iter!=atmlist.end(); ++iter) { int aid = *iter; MolAtomPtr pAtm = pMol->getAtom(aid); if (pAtm.isnull()) continue; if (!m_pSel.isnull()) { if (!m_pSel->isSelected(pAtm)) continue; // skip nonselected atom } writeAtomLine(nserial, rindex, resnam, cch, pAtm, prs); nserial++; } // nlastres = rindex.first; } // print TER record /*prs.formatln("TER " "%5d" " " "%3s " "%1s" "%4d" " ", nserial, resnam.c_str(), chnam.c_str(), nlastres);*/ prs.println("TER"); nserial ++; } //prs.println("END "); prs.println("END"); return true; }
bool MolCoord::appendChain(MolChainPtr pChain) { return m_chains.insert(ChainPool::value_type(pChain->getName(), pChain)).second; }
int MolCoord::appendAtom(MolAtomPtr pAtom) { pAtom->setParentUID(getUID()); const LString &cname = pAtom->getChainName(); const LString &rname = pAtom->getResName(); const LString &aname = pAtom->getName(); ResidIndex nresid = pAtom->getResIndex(); if (cname.length()<=0 || aname.length()<=0) { LString msg = LString::format("MolCoord> ERROR: appending atom with invalid properties" " (c:'%s' rn:'%s' ri:'%s' an:'%s')", cname.c_str(), rname.c_str(), nresid.toString().c_str(), aname.c_str()); MB_DPRINTLN(msg); MB_THROW(qlib::IllegalArgumentException, msg); return -1; } MolChainPtr pCh = getChain(cname); if (pCh.isnull()) { pCh = MolChainPtr(MB_NEW MolChain()); pCh->setParentUID(getUID()); pCh->setName(cname); appendChain(pCh); } MolResiduePtr pRes = pCh->getResidue(nresid); if (pRes.isnull()) { pRes = MolResiduePtr(MB_NEW MolResidue()); pRes->setParentUID(getUID()); pRes->setIndex(nresid); pRes->setName(rname); // pRes->setChainName(cname); pCh->appendResidue(pRes); } else { const LString &pre_rname = pRes->getName(); if (!pre_rname.equals(rname)) { MB_DPRINTLN("MolCoord> ERROR: appending an atom (%s %s%s %s) with inconsistent residue (%s)", cname.c_str(), rname.c_str(), nresid.toString().c_str(), aname.c_str(), pre_rname.c_str()); // TO DO: throw exception (???) // This is often the case, so is not an exception. // return -1; } } // // Append to the atompool --> assign the atom ID // int atomid = m_atomPool.put(pAtom); if (atomid<0) { // This isn't fail in normal situation. MB_THROW(qlib::RuntimeException, "append to the atompool failed"); return -1; } pAtom->setID(atomid); // MolResidue::appendAtom() must be called after setID(), // because MolResidue makes map from name to AID, which requires "AID". if (!pRes->appendAtom(pAtom)) { // This is often the case with malformed PDB files, so is not an exception. MB_DPRINTLN("MolCoord> ERROR: appending duplicated atom (%s %s%s %s)", cname.c_str(), rname.c_str(), nresid.toString().c_str(), aname.c_str()); // Remove the mis-appended atom from the pool. m_atomPool.remove(atomid); return -1; } // Update the cached xform matrix if required pAtom->resetXformMatrix(); if (!getXformMatrix().isIdent()) pAtom->setXformMatrix(getXformMatrix()); return atomid; }