def agent_texts_with_grounding(stmts):
    allag = all_agents(stmts)
    for ag in allag:
        pfam_def = ag.db_refs.get('PFAM-DEF')
        if pfam_def is not None:
            ag.db_refs['PFAM-DEF'] = tuple(pfam_def)
    refs = [tuple(ag.db_refs.items()) for ag in allag]
    refs_counter = Counter(refs)
    refs_counter_dict = [(dict(entry[0]), entry[1])
                         for entry in refs_counter.items()]
    refs_counter_dict.sort(key=lambda x: x[0].get('TEXT'))
    grouped_by_text = []
    for k, g in groupby(refs_counter_dict, key=lambda x: x[0].get('TEXT')):
        total = 0
        entry = [k]
        db_ref_list = []
        for db_refs, count in g:
            if list(db_refs.keys()) == ['TEXT']:
                db_ref_list.append((None, None, count))
            for db, db_id in db_refs.items():
                if db == 'TEXT':
                    continue
                else:
                    db_ref_list.append((db, db_id, count))
            total += count
        entry.append(tuple(sorted(db_ref_list, key=lambda x: x[2],
                     reverse=True)))
        entry.append(total)
        grouped_by_text.append(tuple(entry))
    grouped_by_text.sort(key=lambda x: x[2], reverse=True)
    return grouped_by_text