#!/usr/local/bin/python

import sys, os

# read_signatures
# format_module
# form_get

SIGNATURE_PATH = "/opt/SIGNATURE"
sys.path = [SIGNATURE_PATH] + sys.path


def read_signatures(sigdb_path, desired_normalization, desired_tags):
    from genomicode import filelib
    
    opr = os.path.realpath
    opj = os.path.join

    filename = opj(sigdb_path, "signatures.txt")
    assert os.path.exists(filename), "Missing signatures.txt file."

    x = [x.upper() for x in desired_tags]
    desired_tags = {}.fromkeys(x)

    x = [x.upper() for x in desired_normalization]
    desired_normalization = {}.fromkeys(x)

    ds = []
    for d in filelib.read_row(filename, header=1):
        # Skip if not the right normalization.
        if d.Normalization.upper() not in desired_normalization:
            continue
        
        # Skip if the tags don't match.
        tags = [x.upper() for x in d.Tags.split()]
        for tag in tags:
            if tag in desired_tags:
                break
        else:
            # None of these tags matched any of the desired ones.
            continue
        
        # Skip if not all parameters supplied.
        if not d.Normalization: