utils.py 5.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

import collections


#### Utilities on dictionaries


def ordered(d, key=None):
    '''sorts a dictionary into an OrderedDict'''
    return collections.OrderedDict([(k, d[k]) for k in sorted(d, key=key)])


def concatenate_with_padding(d, 
                             d1, d1_size, 
                             d2, d2_size,
                             ignore_keys=[]):
    '''Concatenate two dictionaries d1 and d2 into d
    The dictionaries d1 and d2 store several values that are lists with d1_size and d2_size elements,
    and the resulting dictionary will store values that are lists with size d1_size + d2_size elements.
    Pads with lists [0, ... 0] data that appear in either only d1 or only d2.
    The values that are not lists are ignored (but this should not happen).
    
    >>> d = {}
    >>> d1 = { 'a': [1, 2], 'b': [11, 22], 'z':17 }
    >>> d2 = { 'a': [3, 4, 5], 'c': [333, 444, 555] } 
    >>> concatenate_with_padding(d, d1, 2, d2, 5, ['z'])
27 28 29 30 31 32
    >>> d['a']
    [1, 2, 3, 4, 5]
    >>> d['b']
    [11, 22, 0, 0, 0, 0, 0]
    >>> d['c']
    [0, 0, 333, 444, 555]
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    '''

    t1=[]
    t2=[]
        
    for i in range(d1_size):
        t1.append(0)
        
    for i in range(d2_size):
        t2.append(0)
    
    for key in d1:
        if key in ignore_keys:
            continue
        if type(d1[key]) is not list:
            continue

        d[key] = d1[key]
        if key not in d2 :
            d[key] += t2

    for key in d2:
        if key in ignore_keys:
            continue
        if type(d2[key]) is not list:
            continue

        if key not in d :
            d[key] = t1 + d2[key]
        else :
            d[key] = d[key] + d2[key]

                
class AccessedDict(dict):
    '''Dictionary providing a .not_accessed_keys() method
    Note that access with .get(key) are not tracked.

    >>> d = AccessedDict({1:11, 2:22, 3: 33, 4: 44})

    >>> d[1], d[3]
    (11, 33)

    >>> list(d.not_accessed_keys())
    [2, 4]
    '''

    def __init__(self, *args, **kwargs):
        dict.__init__(self, *args, **kwargs)
        self.accessed_keys = []

    def __getitem__(self, key):
        self.accessed_keys.append(key)
        return dict.__getitem__(self, key)

    def not_accessed_keys(self):
        for key in self.keys():
            if key in self.accessed_keys:
                continue
            yield key



                
###### Utilities on strings


def common_substring(l):
    '''Return the longest common substring among the strings in the list
    >>> common_substring(['abcdfffff', 'ghhhhhhhhhbcd'])
    'bcd'
    >>> common_substring(['abcdfffff', 'ghhhhhhhhh'])
    ''
    >>> common_substring(['b-abc-123', 'tuvwxyz-abc-321', 'tuvwxyz-abc-456', 'd-abc-789'])
    '-abc-'
    '''

    table = []
    for s in l:
        # adds in table all substrings of s - duplicate substrings in s are added only once
        table += set(s[j:k] for j in range(len(s)) for k in range(j+1, len(s)+1))

    # sort substrings by length (descending)
115
    table = sorted(table, key=lambda x:-len(x))
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    # get the position of duplicates and get the first one (longest)
    duplicates=[i for i, x in enumerate(table) if table.count(x) == len(l)]
    if len(duplicates) > 0:
        return table[duplicates[0]]
    else:
        return ""

    
def interesting_substrings(l, target_length=6, substring_replacement='-'):
    '''Return a list with intersting substrings.
    Now it removes common prefixes and suffixes, and then the longest 
    common substring. 
    But it stops removing once all lengths are at most 'target_length'.

    >>> interesting_substrings(['ec-3--bla', 'ec-512-bla', 'ec-47-bla'], target_length=0)
    ['3-', '512', '47']
    >>> interesting_substrings(['ec-A-seq-1-bla', 'ec-B-seq-512-bla', 'ec-C-seq-21-bla'], target_length=0, substring_replacement='')
    ['A1', 'B512', 'C21']
    >>> interesting_substrings(['ec-A-seq-1-bla', 'ec-B-seq-512-bla', 'ec-C-seq-21-bla'], target_length=0)
    ['A-1', 'B-512', 'C-21']
    >>> interesting_substrings(['ec-A-seq-1-bla', 'ec-B-seq-512-bla', 'ec-C-seq-21-bla'], target_length=9)
    ['A-seq-1', 'B-seq-512', 'C-seq-21']
    '''

    if not l:
        return {}

    if max(map (len, l)) <= target_length:
        return l

    min_length = min(map (len, l))

    ### Remove prefixes

    common_prefix = 0
    for i in range(min_length):
        if all(map(lambda x: x[i] == l[0][i], l)):
            common_prefix = i+1
        else:
            break

    substrings = [x[common_prefix:] for x in l]

    if max(map (len, substrings)) <= target_length:
        return substrings

    ### Remove suffixes

    common_suffix = 0
    for i in range(min_length - common_prefix):
        if all(map(lambda x: x[-(i+1)] == l[0][-(i+1)], l)):
            common_suffix = i
        else:
            break

    substrings = [x[common_prefix:-(common_suffix+1)] for x in l]            

    if max(map (len, substrings)) <= target_length:
        return substrings

    ### Remove the longest common substring
    
    #Have to replace '' by '_' if the removal have place between 2 substrings 

    common = common_substring(substrings)
    if common:
        substrings = [s.replace(common, substring_replacement) for s in substrings]

    return substrings
    
    # ### Build dict
    # substrings = {}
    # for x in l:
    #     substrings[x] = x[common_prefix:-(common_suffix+1)]
    # return substrings