Exemple #1
0
int main () {

    md_files::PDBSystem pdb ("test.pdb");
    for (Mol_it it = pdb.begin_mols(); it != pdb.end_mols(); it++) {
        static_cast<Water *>((*it))->H1()->Print();
    }

    return 0;

}
Exemple #2
0
int loadpdb(const char * fname, char ** _pdb)
{
  ifstream pdb(fname);
  // get file size
  pdb.seekg(0,ios::end);
  size_t n=pdb.tellg();
  if(n==-1)
    return 1;
  *_pdb = new char[n];
  pdb.seekg(0,ios::beg);
  pdb.read(*_pdb,n);
  pdb.close();

  cout<<"read pdb:"<<fname<<" n:"<<n<<endl;

  return pdb.bad();
}
Exemple #3
0
void LegacyUtils::UpdateFileToCurrentVersion(
        const char *file_path,
        FileVersionEnum file_version,
        const char *new_path,
        const Credentials &old_creds,
        const Credentials &new_creds,
        function<void(int, const QString &)> progress_cb)
{
    if(0 == strcmp(file_path, new_path))
        throw Exception<>("Refusing to update if source and dest are the same");

    QString progress_msg = QObject::tr("Parsing legacy database...");
    progress_cb(0, progress_msg);
    finally([&]{ progress_cb(100, QObject::tr("Updating database")); });

    // First make sure we can parse the input file
    std::string xml;
    std::string file_data;
    QFile f(file_path);
    f.open(QFile::ReadOnly);

    // Skip over the version info; we already know it
    if(Version1 != file_version){
        char c;
        while(f.getChar(&c) && c != '|');
    }

    progress_cb(3, progress_msg);

    try{
        QByteArray ba;

        switch(file_version)
        {
        case Version1:
            // In version 1 the whole file is encrypted, with no version identifier
            ba = f.readAll();
            file_data = string(ba.constData(), ba.length());
            file_data.append((char)0);  // Make sure it's null-terminated
            xml = V1::Encryption::DecryptString(file_data.data(), old_creds.Password);
            break;
        case Version2:
            ba = f.readAll();
            file_data = string(ba.constData(), ba.length());
            xml = V2::Encryption::DecryptString(file_data, old_creds.Password);
            break;
        case Version3:
        {
            // In version 3 we also need to know the length of the payload
            uint len = __read_four_chars_into_int(&f);
            ba = f.read(len);
            file_data = string(ba.constData(), ba.length());
            if(file_data.length() != len)
                throw Exception<>("Error while reading legacy file");
            xml = V3::Encryption::DecryptString(file_data, old_creds.Password);
        }
            break;
        default:
            GASSERT(false);
            break;
        }
    }
    catch(const ::CryptoPP::Exception &ex){
        throw AuthenticationException<>(ex.what());
    }

    progress_cb(7, progress_msg);

    // Parse the XML using the legacy object
    V3::Password_File pf;
    pf.readXML(QByteArray(xml.data(), xml.length()), file_version < 3);

    // Count the number of items so we know how much progress we're making
    int entry_cnt = __count_legacy_entries(pf.getContents());
    int entry_ctr = 0;

    progress_msg = QObject::tr("Populating new database...");
    progress_cb(10, progress_msg);
    
    // Remove the target path if it exists
    if(QFile::exists(new_path))
        QFile::remove(new_path);
    
    progress_cb(15, progress_msg);

    // Create the updated database
    PasswordDatabase pdb(new_path);
    pdb.Open(new_creds);

    // Iterate through the legacy object and populate the new object
    __add_children_to_database(pf.getContents(), pdb, EntryId::Null(), entry_ctr,
        [&](int entry_ctr){
            progress_msg = QString(QObject::tr("Populating new database (entry %1 of %2)..."))
                    .arg(entry_ctr)
                    .arg(entry_cnt);
            progress_cb(15 + 85.0 * ((float)entry_ctr / entry_cnt), progress_msg);
    });
}
Exemple #4
0
void multiple_common_substring(
    const std::vector<std::string> &strs,
    std::vector<std::string> &align)
{
    if (strs.size() == 1)
    {
        align.push_back(strs[0]);
        align.push_back(std::string(strs[0].length(), ' '));
        return;
    }
    //
    std::vector<char> seq;
    std::vector<id_type> ids;
    std::vector<word_t> offs;
    for (id_type id = 0; id != strs.size(); ++id)
    {
        offs.push_back(seq.size());
        std::copy(strs[id].begin(), strs[id].end(), std::back_inserter(seq));
        seq.push_back('\0');
        seq.push_back(id);
        ids.insert(ids.end(), strs[id].length() + 2, id);
    }
    offs.push_back(seq.size());
    //
    const word_t K = strs.size();
    //
    suffix_t suf(&seq[0], seq.size());
    for (word_t i = 0; i != seq.size() - 1; ++i)
        suf.push_back();
    //
    std::vector<word_t> h(suf.size() * 2);
    //
    {
        patl::lca_oracle<suffix_t> lca(suf);
        //
        std::vector<const_vertex> last_vtx(K);
        //
        for (suffix_t::const_iterator it = suf.begin(); it != suf.end(); ++it)
        {
            const word_t k = ids[*it - suf.keys()];
            const const_vertex &vtx = static_cast<const const_vertex&>(it);
            if (last_vtx[k].compact())
                ++h[suf.vertex_index_of(lca(last_vtx[k], vtx))];
            last_vtx[k] = vtx;
        }
    }
    //
#ifdef MULTI_ALIGN_DEBUG
    {
        std::ofstream fout("malign_suf.dot");
        patricia_dot_base<suffix_t> pdb(&ids, &h);
        patl::patricia_dot_creator<
            suffix_t,
            std::ofstream, patricia_dot_base<suffix_t> > dotcr(fout, pdb);
        dotcr.create(suf.root());
    }
#endif
    //
    std::vector<const_vertex> v(K + 1);
    std::vector<word_t> v_len(K + 1);
    //
    std::vector<word_t> s, u;
    const const_vertex root = suf.root();
    const suffix_t::const_postorder_iterator
        pit_beg = root.postorder_begin(),
        pit_end = root.postorder_end();
    for (suffix_t::const_postorder_iterator pit = pit_beg; pit != pit_end; ++pit)
    {
        if (pit->get_qtag())
        {
            s.push_back(1);
            u.push_back(0);
        }
        else
        {
            const word_t
                s_v = s.back() + s[s.size() - 2],
                u_v = u.back() + u[u.size() - 2] + h[suf.vertex_index_of(*pit)],
                c_v = s_v - u_v,
                idx = suf.index_of(*pit),
                len = patl::impl::get_min(
                    pit->next_skip() / 8,
                    patl::impl::max0(strs[ids[idx]].length() - (idx - offs[ids[idx]])));
            if (len > v_len[c_v])
            {
                v[c_v] = *pit;
                v_len[c_v] = len;
            }
            s.pop_back();
            s.back() = s_v;
            u.pop_back();
            u.back() = u_v;
        }
    }
    //
    for (word_t k = K - 1; k != 1; --k)
    {
        if (v_len[k] < v_len[k + 1])
        {
            v[k] = v[k + 1];
            v_len[k] = v_len[k + 1];
        }
    }
    // multiple common substrings
#ifdef MULTI_ALIGN_DEBUG
    printf("---\n");
    for (word_t k = 2; k != v.size(); ++k)
    {
        if (v[k].compact())
            printf("%u\t%u\t%s\n", k, v_len[k], std::string(v[k].key(), v_len[k]).c_str());
    }
#endif
    // эвристический выбор подход¤щего k
    word_t k_cur = v.size() - 1;
    for (; k_cur != 1 && v_len[k_cur] < 2; --k_cur) ;
    if (k_cur == 1)
    {
        for (k_cur = v.size() - 1; k_cur != 1 && !v_len[k_cur]; --k_cur) ;
        if (k_cur == 1)
        {
            word_t max_len = 0;
            for (id_type i = 0; i != strs.size(); ++i)
            {
                if (strs[i].length() > max_len)
                    max_len = strs[i].length();
            }
            for (id_type i = 0; i != strs.size(); ++i)
            {
                align.push_back(strs[i]);
                std::string &ref = align.back();
                while (ref.length() != max_len)
                    ref.push_back(' ');
            }
            align.push_back(std::string(max_len, ' '));
            return;
        }
    }
    //
    // std::pair<номер строки, смещение в строке>
    typedef std::map<id_type, word_t> motif_map;
    motif_map motifs; // номера строк с повтор¤ющимс¤ мотивом
    {
        const suffix_t::const_iterator it_end = v[k_cur].end();
        for (suffix_t::const_iterator it = v[k_cur].begin(); it != it_end; ++it)
        {
            const id_type id = ids[*it - suf.keys()];
            const word_t off = *it - suf.keys() - offs[id];
            motifs[id] = off;
        }
    }
    const word_t motif_len = v_len[k_cur];
    const std::string
        motif(v[k_cur].key(), motif_len),
        padding(motif_len, ' ');
    align.clear();
    for (id_type i = 0; i != K; ++i)
        align.push_back(motifs.find(i) != motifs.end() ? motif : padding);
    align.push_back(std::string(motif_len, '^'));
    word_t
        sum_left = 0,
        sum_right = 0;
    for (motif_map::const_iterator it = motifs.begin(); it != motifs.end(); ++it)
    {
        if (it->second)
            ++sum_left;
        if (strs[it->first].length() - it->second - motif_len)
            ++sum_right;
    }
    // left side
    if (sum_left)
    {
        std::vector<id_type> left_ids;
        std::vector<std::string> left_strs;
        for (id_type i = 0; i != K; ++i)
        {
            const motif_map::const_iterator it = motifs.find(i);
            if (it != motifs.end())
            {
                if (it->second)
                {
                    left_ids.push_back(it->first);
                    left_strs.push_back(strs[it->first].substr(0, it->second));
                }
            }
            else
            {
                left_ids.push_back(i);
                left_strs.push_back(strs[i]);
            }
        }
        std::vector<std::string> left_align;
        multiple_common_substring(left_strs, left_align);
        const word_t left_len = left_align[0].length();
        const std::string left_pad(left_len, ' ');
        for (id_type i = 0, j = 0; i != K; ++i)
        {
            align[i].insert(0, j != left_ids.size() && left_ids[j] == i
                ? left_align[j++]
                : left_pad);
        }
        align.back().insert(0, left_align.back());
    }
    // right side
    if (sum_right)
    {
        std::vector<id_type> right_ids;
        std::vector<std::string> right_strs;
        for (id_type i = 0; i != K; ++i)
        {
            const motif_map::const_iterator it = motifs.find(i);
            if (it != motifs.end())
            {
                if (strs[it->first].length() - it->second - motif_len)
                {
                    right_ids.push_back(it->first);
                    right_strs.push_back(strs[it->first].substr(it->second + motif_len));
                }
            }
            else if (!sum_left)
            {
                right_ids.push_back(i);
                right_strs.push_back(strs[i]);
            }
        }
        std::vector<std::string> right_align;
        multiple_common_substring(right_strs, right_align);
        const word_t right_len = right_align[0].length();
        const std::string right_pad(right_len, ' ');
        for (id_type i = 0, j = 0; i != K; ++i)
        {
            align[i].append(j != right_ids.size() && right_ids[j] == i
                ? right_align[j++]
                : right_pad);
        }
        align.back().append(right_align.back());
    }
    // neither left nor right
    if (!sum_left && !sum_right && K - k_cur)
    {
        std::vector<id_type> neither_ids;
        std::vector<std::string> neither_strs;
        for (id_type i = 0; i != K; ++i)
        {
            const motif_map::const_iterator it = motifs.find(i);
            if (it == motifs.end())
            {
                neither_ids.push_back(i);
                neither_strs.push_back(strs[i]);
            }
        }
        std::vector<std::string> neither_align;
        multiple_common_substring(neither_strs, neither_align);
        const word_t neither_len = neither_align[0].length();
        const std::string neither_pad(neither_len, ' ');
        for (id_type i = 0, j = 0; i != K; ++i)
        {
            align[i].append(j != neither_ids.size() && neither_ids[j] == i
                ? neither_align[j++]
                : neither_pad);
        }
        align.back().append(neither_align.back());
    }
}
Exemple #5
0
/**
 * BCMデータのロード
 * @param filename ファイルパス
 * @param generateBond 線primitiveとの接点作成
 * @retval true 成功
 * @retval false 失敗
 */
bool PDBLoader::Load(const char* filename, bool generateBond){
	Clear();

    tinypdb::TinyPDB pdb(filename);
    if (pdb.Parse(/* isBondGenerated = */ generateBond)) {
        fprintf(stderr,"[PDBLoader] PDB parsing failed: %s \n", filename);
        return false;
    }

	m_atoms = pdb.GetAtoms(); // copy

    int numAtoms = static_cast<int>(pdb.GetAtoms().size());

    {
        ball.Create(numAtoms);
        Vec3Buffer*  pos     = ball.Position();
        FloatBuffer* mat     = ball.Material();
        FloatBuffer* radius  = ball.Radius();

        printf("[PDBLoader] # of atoms: %ld\n", numAtoms);

        float* pp = pos->GetBuffer();
        for (size_t i = 0; i < numAtoms; i++) {
            pp[3*i+0] = pdb.GetAtoms()[i].GetX();
            pp[3*i+1] = pdb.GetAtoms()[i].GetY();
            pp[3*i+2] = pdb.GetAtoms()[i].GetZ();
        }

        // @fixme
        float* rad = radius->GetBuffer();
        for (int i = 0; i < numAtoms; ++i) {
            rad[i] = 0.25f;
        }

        // @todo
        memset(mat->GetBuffer(), 0, sizeof(float) * mat->GetNum());
    }


    if (generateBond) {

        // We reprent Bond as line primitives.
        std::vector<float> bondLines;
        for (unsigned int i = 0; i < numAtoms; i++) {

          tinypdb::Atom& atom = pdb.GetAtoms()[i];

          for (unsigned int j = 0; j < atom.GetBonds().size(); j++) {
            const tinypdb::Atom* dst = atom.GetBonds()[j];

            if (dst->Visited()) {
              continue;
            }

            bondLines.push_back(atom.GetX());
            bondLines.push_back(atom.GetY());
            bondLines.push_back(atom.GetZ());

            bondLines.push_back(dst->GetX());
            bondLines.push_back(dst->GetY());
            bondLines.push_back(dst->GetZ());

          }

          atom.SetVisited(true); 

        }

        size_t numBonds = bondLines.size() / 3 / 2;
        size_t numBondVertices = numBonds * 2;

        printf("[PDBLoader] # of bonds: %ld\n", numBonds);

        stick.Create(numBondVertices, /* index num = */0, /* use vrying radius */false);
        Vec3Buffer*  pos     = stick.Position();
        FloatBuffer* mat     = stick.Material();
        //FloatBuffer* radius  = stick.Radius();
        //UintBuffer*  index   = stick.Index();  // not used.

        float* pp = pos->GetBuffer();
        for (size_t i = 0; i < numBondVertices; i++) {
            pp[3*i+0] = bondLines[3*i+0];
            pp[3*i+1] = bondLines[3*i+1];
            pp[3*i+2] = bondLines[3*i+2];
        }

		// Don't use varying radius.
		//float* rad = radius->GetBuffer();
		//for (int i = 0; i < numBondVertices; ++i) {
		//	rad[i] = 1.0f;
		//}

		// @todo
		memset(mat->GetBuffer(), 0, sizeof(float) * mat->GetNum());

	}

	return true;
}