


def log_metrics(logger, model, outmode='starting_uni'):
    
    
    G = len([g.id for g in model.genes])
    R = len([r.id for r in model.reactions if len(set([m.id.rsplit('_',1)[-1] for m in r.metabolites]))==1])
    T = len([r.id for r in model.reactions if len(set([m.id.rsplit('_',1)[-1] for m in r.metabolites]))!=1])
    M = len([m.id for m in model.metabolites])
    uM = len(set([m.id.rsplit('_',1)[0] for m in model.metabolites]))
    gr = len([gr.id for gr in model.groups])
    bP = len([m.id for m in model.reactions.get_by_id('Biomass').reactants])

    
    if   outmode == 'starting_uni':
        logger.info(f"Starting universe: [oG: {G}, R: {R}, T: {T}, uM: {uM}, bP: {bP}]")
    elif outmode == 'uni_features':
        logger.info(f"Universe features: [oG: {G}, R: {R}, T: {T}, uM: {uM}, bP: {bP}]")
    elif outmode == 'recon_model':
        logger.info(f"Resulting model: [G: {G}, R: {R}, T: {T}, uM: {uM}, bP: {bP}]")
    elif outmode == 'loaded_model':
        logger.info(f"Loaded model: [G: {G}, R: {R}, T: {T}, uM: {uM}, bP: {bP}]")
        
        
        
def show_contributions(logger, db):
    
    # create a counter for each author
    cnt = {author: 0 for author in db['authors']['username']}
    cnt_tot = 0
    for rid, row in db['R'].iterrows():
        for author in row['author'].split(';'):
            author = author.rstrip().strip()
            cnt[author] += 1
            cnt_tot += 1
    for rid, row in db['T'].iterrows():
        for author in row['author'].split(';'):
            author = author.rstrip().strip()
            cnt[author] += 1
            cnt_tot += 1
        
    # compute percentages:
    pct = {author: cnt[author]/cnt_tot*100 for author in cnt.keys()}
    # sort in descending order: 
    pct = dict(sorted(pct.items(), key=lambda item: item[1], reverse=True))
    # convert to string:
    pct = {author: f'{round(pct[author],2)}%' for author in pct.keys()}
    logger.debug(f"Contributions: {pct}.")