server.py 文件源码

python
阅读 31 收藏 0 点赞 0 评论 0

项目:kripodb 作者: 3D-e-Chem 项目源码 文件源码
def get_fragments(fragment_ids=None, pdb_codes=None):
    """Retrieve fragments based on their identifier or PDB code.

    Args:
        fragment_ids (List[str]): List of fragment identifiers
        pdb_codes (List[str]): List of PDB codes

    Returns:
        list[dict]: List of fragment information

    Raises:
        werkzeug.exceptions.NotFound: When one of the fragments_ids or pdb_code could not be found
    """
    fragments_db_filename = current_app.config['fragments']
    with FragmentsDb(fragments_db_filename) as fragmentsdb:
        fragments = []
        missing_ids = []
        if fragment_ids:
            for frag_id in fragment_ids:
                try:
                    fragments.append(fragmentsdb[frag_id])
                except LookupError:
                    missing_ids.append(frag_id)

        if pdb_codes:
            for pdb_code in pdb_codes:
                try:
                    for fragment in fragmentsdb.by_pdb_code(pdb_code):
                        fragments.append(fragment)
                except LookupError:
                    missing_ids.append(pdb_code)
        # TODO if fragment_ids and pdb_codes are both None then return paged list of all fragments
        if missing_ids:
            title = 'Not found'
            label = 'identifiers'
            if pdb_codes:
                label = 'PDB codes'
            description = 'Fragments with {1} \'{0}\' not found'.format(','.join(missing_ids), label)
            # connexion.problem is using json.dumps instead of flask custom json encoder, so performing convert myself
            # TODO remove mol2string conversion when https://github.com/zalando/connexion/issues/266 is fixed
            for fragment in fragments:
                if fragment['mol']:
                    fragment['mol'] = MolToMolBlock(fragment['mol'])
            ext = {'absent_identifiers': missing_ids, 'fragments': fragments}
            return connexion.problem(404, title, description, ext=ext)
        return fragments
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号