import operator
import math


def distance_kmer_vec(queryvec, targetvec):
    """Calculate the cosine similarity between two vectors
    
    Parameters
    ----------
    queryvec, targetvec: `list` with floats 
        corresponding to relative frequencies to compare
        
    Return
    ----------
    cos_sim: `float`
        cosine similarity
    """
    cos_sim = math.nan
    try:
        prod = sum(map(operator.mul, queryvec, targetvec))
        len1 = math.sqrt(sum(map(operator.mul, queryvec, queryvec)))
        len2 = math.sqrt(sum(map(operator.mul, targetvec, targetvec)))
        denom = (len1 * len2)
        if denom > 0:
            cos_sim = prod / denom
        else:
            raise RuntimeWarning
    except RuntimeWarning:
        pass
    return cos_sim