#! /usr/bin/env python3

import anndata, sys, argparse
    
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description="Print out info about metadata in an h5ad file")
    parser.add_argument('h5ad', type=str, help='an h5ad file')
    parser.add_argument('-f', '--fields', type=int, default=5, \
      help='number of values to print for a field')
    args = parser.parse_args()

    ad = anndata.read_h5ad(args.h5ad)

    # Prints out general info about the matrix/dataset
    print(ad,"\n")

    # Loop goes through each field in obs and prints out at least 5 values
    for annot in ad.obs_keys():
        # Get list of unique vals in obs annotation
        annot_vals = sorted(list(set(ad.obs[annot])))
        val_count = len(annot_vals)
        # Can't color on annots with >500 vals, so no need to display them here
        if val_count < 500:
            print(annot, "-", val_count, "unique values")
            for i in range(0,args.fields):
                # try/except here is because some annots have less than the 
                # amount we want to print, without this it crashes on these fields
                try:
                    print(annot_vals[i])
                except:
                    continue
            print()

if __name__ == "__main__":
    main()
