Example #1
0
def join_tables(* args, **kwargs):
    """Join together tables into one table.
       Each argument is a tuple (table_i, key_i, cols_i)
       
       key_i is either a column name or a function that maps a 
       table row to a unique key
    """
    
    if len(args) == 0:
        return Table()
    
    # determine common keys
    tab, key, cols = args[0]
    if isinstance(key, str):
        keys = tab.cget(key)
        lookups = [tab.lookup(key)]        
    else:
        keys = map(key, tab)
        lookup = {}
        for row in tab:
            lookup[key(row)] = row
        lookups = [lookup]
        
    keyset = set(keys)
    

    for tab, key, cols in args[1:]:
        if isinstance(key, str):
            keyset = keyset & set(tab.cget(key))
            lookups.append(tab.lookup(key))            
        else:
            keyset = keyset & set(map(key, tab))
            lookup = {}
            for row in tab:
                lookup[key(row)] = row
            
            lookups.append(lookup)
    
    keys = filter(lambda x: x in keyset, keys)
    
    
    # build new table
    if "headers" not in kwargs:
        headers = util.concat(*util.cget(args, 2))
    else:
        headers = kwargs["headers"]
    tab = Table(headers=headers)
    
    for key in keys:
        row = {}
        for (tab2, key2, cols), lookup in zip(args, lookups):
            row.update(util.subdict(lookup[key], cols))
        tab.append(row)
    
    return tab
    def align(self, * names):
        if len(names) == 0:
            print "nothing to align"

        # get names from genes if they are not strings
        if type(names[0]) != str:
            names = [i.name for i in names]
        
        seqs2 = util.subdict(self.seqs, names)
        aln = muscle.muscle(seqs2)
        muscle.printAlign(aln)
Example #3
0
 def new(self, headers=None):
     """
     return a new table with the same info but no data
         
     headers - if specified, only a subset of the headers will be copied
     """
     
     if headers == None:
         headers = self.headers
     
     tab = type(self)(headers=headers)
     
     tab.types = util.subdict(self.types, headers)
     tab.defaults = util.subdict(self.defaults, headers)
     tab.comments = copy.copy(self.comments)
     tab.delim = self.delim
     tab.nheaders = self.nheaders
     
     tab._type_lookup = copy.copy(self._type_lookup)
     
     return tab
Example #4
0
    def new(self, headers=None):
        """
        Return a new table with the same info but no data.

        headers: if specified, only a subset of the headers will be copied.
        """
        if headers is None:
            headers = self.headers

        tab = type(self)(headers=headers)

        tab.types = util.subdict(self.types, headers)
        tab.comments = copy.copy(self.comments)
        tab.delim = self.delim
        tab.nheaders = self.nheaders

        return tab