def hsp2dict(hsp, num, hit_eval, domains_nr):
    """
    Turns Biopython HSP object into dictionary
    that can be easily serialized by json.

    :param hsp: Bio.SearchIO._model.HSP
    :param num: number in the ordered list of HSPs, same
                as for the hit representing given HSP in
                the hit list.
    :param hit_eval: General evalue for full length sequence
    :param domains_nr: Number of domains from hitlist
    :return:
    """

    hsp_json = {
        'num': num,
        'bias': hsp.bias,
        'bitscore': hsp.bitscore,
        'evalue': hsp.evalue,
        'full_evalue': hit_eval,
        'domain_obs_num': domains_nr,
        'acc_avg': hsp.acc_avg,
        'env_start': hsp.env_start,
        'env_end': hsp.env_end,
        'query_id': hsp.query.id,
        'query_description': hsp.query.description,
        'query_seq': str(hsp.query.seq),
        'query_start': hsp.query_start,
        'query_end': hsp.query_end,
        'hit_id': hsp.hit.id,
        'hit_description': hsp.hit.description,
        'hit_seq': str(hsp.hit.seq),
        'hit_start': hsp.hit_start,
        'hit_end': hsp.hit_end,
        'hit_len': hsp.hit_end - hsp.hit_start + 1,
        'aln_ann': hsp.aln_annotation
    }

    return hsp_json