humann/tools/join_tables.py
def join_gene_tables(gene_tables,output,verbose=None):
    """
    Join the gene tables to a single gene table
    """
    
    gene_table_data={}
    start_column_id=""
    samples=[]
    file_basenames=[]
    index=0
    for gene_table in gene_tables:
        
        if verbose:
            print("Reading file: " + gene_table)
        
        lines=util.process_gene_table_with_header(gene_table, allow_for_missing_header=True)
        header=next(lines)
        
        # get the basename of the file
        file_basename='.'.join(os.path.basename(gene_table).split('.')[:-1])
        file_basenames.append(file_basename)
        
        if header:
            header_info=header.split(GENE_TABLE_DELIMITER)
            if not start_column_id:
                start_column_id=header_info[0]
            # allow for multiple samples
            sample_names=header_info[1:]
        else:
            # if there is no header in the file then use the file name as the sample name
            sample_names=[file_basename]
        
        for line in lines:
            data=line.split(GENE_TABLE_DELIMITER)
            try:
                gene=data[0]
                # if the header names multiple samples, merge all samples
                # this prevents extra columns from being included in some rows
                # this requires files containing multiple samples to include a header
                data_points=data[1:len(sample_names)+1]
            except IndexError:
                gene=""

            if gene:
                current_data=gene_table_data.get(gene,"")
                fill = index - current_data.count(GENE_TABLE_DELIMITER)
                if fill > 0:
                    # fill in zeros for samples without data then add data point
                    gene_table_data[gene]=current_data + GENE_TABLE_DELIMITER.join(["0"]*fill) + GENE_TABLE_DELIMITER + GENE_TABLE_DELIMITER.join(data_points) + GENE_TABLE_DELIMITER
                elif fill < 0:
                    # add data point to other data point from the same sample
                    current_data_points=current_data.split(GENE_TABLE_DELIMITER)
                    for i,point in enumerate(data_points):
                        store_index=len(data_points)*-1-1+i
                        current_data_points[store_index]=str(float(current_data_points[store_index])+float(point))
                    gene_table_data[gene] = GENE_TABLE_DELIMITER.join(current_data_points)
                else:
                    # add data point to end of list
                    gene_table_data[gene] = current_data + GENE_TABLE_DELIMITER.join(data_points) + GENE_TABLE_DELIMITER
            
        samples+=sample_names
        index+=len(sample_names)
    # if all of the header names for the files are the same
    # then use the file names as headers
    if samples.count(samples[0]) == len(samples):
        samples=file_basenames
                
    # write the joined gene table
    if not start_column_id:
        start_column_id="# header "
    sample_header=[start_column_id]+samples
    total_gene_tables=len(samples)
    sorted_gene_list=util.fsort(list(gene_table_data))
    try:
        file_handle=open(output,"w")
        file_handle.write(GENE_TABLE_DELIMITER.join(sample_header)+"\n")
    except EnvironmentError:
        sys.exit("Unable to write file: " + output)  
        
    for gene in sorted_gene_list:
        # extend gene data for any gene that is not included in all samples
        current_data=gene_table_data[gene]
        fill = total_gene_tables - current_data.count(GENE_TABLE_DELIMITER)
        if fill:
            current_data=current_data + GENE_TABLE_DELIMITER.join(["0"]*fill) + GENE_TABLE_DELIMITER
        file_handle.write(gene+GENE_TABLE_DELIMITER+current_data.rstrip(GENE_TABLE_DELIMITER)+"\n")
    
    file_handle.close()

    Join the gene tables to a single gene table
    
Join the gene tables to a single gene table
Join the gene tables to a single gene table
def join_gene_tables(gene_tables,output,verbose=None):
    gene_table_data={}
    start_column_id=""
    samples=[]
    file_basenames=[]
    index=0
    for gene_table in gene_tables:
        if verbose:
            print("Reading file: " + gene_table)
        lines=util.process_gene_table_with_header(gene_table, allow_for_missing_header=True)
        header=next(lines)
        file_basename='.'.join(os.path.basename(gene_table).split('.')[:-1])
        file_basenames.append(file_basename)
        if header:
            header_info=header.split(GENE_TABLE_DELIMITER)
            if not start_column_id:
                start_column_id=header_info[0]
            sample_names=header_info[1:]
        else:
            sample_names=[file_basename]
        for line in lines:
            data=line.split(GENE_TABLE_DELIMITER)
            try:
                gene=data[0]
                data_points=data[1:len(sample_names)+1]
            except IndexError:
                gene=""
            if gene:
                current_data=gene_table_data.get(gene,"")
                fill = index - current_data.count(GENE_TABLE_DELIMITER)
                if fill > 0:
                    gene_table_data[gene]=current_data + GENE_TABLE_DELIMITER.join(["0"]*fill) + GENE_TABLE_DELIMITER + GENE_TABLE_DELIMITER.join(data_points) + GENE_TABLE_DELIMITER
                elif fill < 0:
                    current_data_points=current_data.split(GENE_TABLE_DELIMITER)
                    for i,point in enumerate(data_points):
                        store_index=len(data_points)*-1-1+i
                        current_data_points[store_index]=str(float(current_data_points[store_index])+float(point))
                    gene_table_data[gene] = GENE_TABLE_DELIMITER.join(current_data_points)
                else:
                    gene_table_data[gene] = current_data + GENE_TABLE_DELIMITER.join(data_points) + GENE_TABLE_DELIMITER
        samples+=sample_names
        index+=len(sample_names)
    if samples.count(samples[0]) == len(samples):
        samples=file_basenames
    if not start_column_id:
        start_column_id="# header "
    sample_header=[start_column_id]+samples
    total_gene_tables=len(samples)
    sorted_gene_list=util.fsort(list(gene_table_data))
    try:
        file_handle=open(output,"w")
        file_handle.write(GENE_TABLE_DELIMITER.join(sample_header)+"\n")
    except EnvironmentError:
        sys.exit("Unable to write file: " + output)  
    for gene in sorted_gene_list:
        current_data=gene_table_data[gene]
        fill = total_gene_tables - current_data.count(GENE_TABLE_DELIMITER)
        if fill:
            current_data=current_data + GENE_TABLE_DELIMITER.join(["0"]*fill) + GENE_TABLE_DELIMITER
        file_handle.write(gene+GENE_TABLE_DELIMITER+current_data.rstrip(GENE_TABLE_DELIMITER)+"\n")
    file_handle.close()
def join_gene_tables ( gene_tables , output , verbose = None ) : gene_table_data = { } start_column_id = "" samples = [ ] file_basenames = [ ] index = 0 for gene_table in gene_tables : if verbose : print ( "Reading file: " + gene_table ) lines = util . process_gene_table_with_header ( gene_table , allow_for_missing_header = True ) header = next ( lines ) file_basename = '.' . join ( os . path . basename ( gene_table ) . split ( '.' ) [ : - 1 ] ) file_basenames . append ( file_basename ) if header : header_info = header . split ( GENE_TABLE_DELIMITER ) if not start_column_id : start_column_id = header_info [ 0 ] sample_names = header_info [ 1 : ] else : sample_names = [ file_basename ] for line in lines : data = line . split ( GENE_TABLE_DELIMITER ) try : gene = data [ 0 ] data_points = data [ 1 : len ( sample_names ) + 1 ] except IndexError : gene = "" if gene : current_data = gene_table_data . get ( gene , "" ) fill = index - current_data . count ( GENE_TABLE_DELIMITER ) if fill > 0 : gene_table_data [ gene ] = current_data + GENE_TABLE_DELIMITER . join ( [ "0" ] * fill ) + GENE_TABLE_DELIMITER + GENE_TABLE_DELIMITER . join ( data_points ) + GENE_TABLE_DELIMITER elif fill < 0 : current_data_points = current_data . split ( GENE_TABLE_DELIMITER ) for i , point in enumerate ( data_points ) : store_index = len ( data_points ) * - 1 - 1 + i current_data_points [ store_index ] = str ( float ( current_data_points [ store_index ] ) + float ( point ) ) gene_table_data [ gene ] = GENE_TABLE_DELIMITER . join ( current_data_points ) else : gene_table_data [ gene ] = current_data + GENE_TABLE_DELIMITER . join ( data_points ) + GENE_TABLE_DELIMITER samples += sample_names index += len ( sample_names ) if samples . count ( samples [ 0 ] ) == len ( samples ) : samples = file_basenames if not start_column_id : start_column_id = "# header " sample_header = [ start_column_id ] + samples total_gene_tables = len ( samples ) sorted_gene_list = util . fsort ( list ( gene_table_data ) ) try : file_handle = open ( output , "w" ) file_handle . write ( GENE_TABLE_DELIMITER . join ( sample_header ) + "\n" ) except EnvironmentError : sys . exit ( "Unable to write file: " + output ) for gene in sorted_gene_list : current_data = gene_table_data [ gene ] fill = total_gene_tables - current_data . count ( GENE_TABLE_DELIMITER ) if fill : current_data = current_data + GENE_TABLE_DELIMITER . join ( [ "0" ] * fill ) + GENE_TABLE_DELIMITER file_handle . write ( gene + GENE_TABLE_DELIMITER + current_data . rstrip ( GENE_TABLE_DELIMITER ) + "\n" ) file_handle . close ( )
Join the gene tables to a single gene table
Join the gene tables to a single gene table
"""
    Join the gene tables to a single gene table
    """ # get the basename of the file # allow for multiple samples # if there is no header in the file then use the file name as the sample name # if the header names multiple samples, merge all samples # this prevents extra columns from being included in some rows # this requires files containing multiple samples to include a header # fill in zeros for samples without data then add data point # add data point to other data point from the same sample # add data point to end of list # if all of the header names for the files are the same # then use the file names as headers # write the joined gene table # extend gene data for any gene that is not included in all samples
gene_tables
output
verbose