humann/tools/split_table.py
def split_table_sample_columns(file_handle, header, line, output_dir, taxonomy_index, verbose):
    """
    Split a table where the abundances of genes are organized by sample in columns
    """
    
    samples=header.rstrip().split(GENE_TABLE_DELIMITER)    
    
    # if taxonomy is set the last column is not a sample but taxonomy
    # taxonomy_index can be set to zero
    if taxonomy_index != None:
        header_taxonomy=samples.pop()
    
    gene_table_data={}
    while line:
        # write the data to each of the new files
        data=line.rstrip().split(GENE_TABLE_DELIMITER)        
        gene=data.pop(0)
        # process the taxonomy
        # taxonomy_index can be set to zero
        if taxonomy_index != None:
            # taxonomy data is the last column
            taxonomy_data=data.pop().split(TAXONOMY_DELIMITER)
            try:
                gene=taxonomy_data[taxonomy_index]
            except IndexError:
                sys.exit("The taxonomy index provided is not valid: " + str(taxonomy_index))
                
                
        if gene in gene_table_data:
            # add the two gene lines
            original_gene_data=gene_table_data[gene]
            for i, data_point in enumerate(data):
                try:
                    float_data_point=float(data_point)
                except ValueError:
                    float_data_point=0
                try:
                    original_gene_data[i]=str(float(original_gene_data[i])+float_data_point)
                except ValueError:
                    sys.exit("ERROR: The format of the input file is not that expected. "+
                             "Please provide a gene table, output file from picrust predict_metagenomes.py,"+
                             " or output file from picrust metagenome_contributions.py. "+
                             "If this file is output from picrust metagenome_contributions.py, "+
                             "it does not include the expected header.")
            gene_table_data[gene]=original_gene_data
        else:
            gene_table_data[gene]=data

        line = file_handle.readline()
        
    file_handle.close()
    
    # write the genes to the files
    new_file_names=[]
    sample_names=samples[1:]
    subheader=samples[0]
        
    for i, sample in enumerate(sample_names):
        simple_sample_name=re.sub("[^a-zA-Z0-9_|-|.]|@|\\?|\\]|\\[|\\^","_",sample)
        try:
            new_file_name=os.path.join(output_dir,simple_sample_name+TSV_FILE_EXTENSION)
            new_file_names.append(new_file_name)
            if verbose:
                print("Creating file: " + new_file_name)
            
            new_file_handle=open(new_file_name,"w")
            
            # write the header
            new_file_handle.write(GENE_TABLE_DELIMITER.join([subheader,sample])+"\n")
            
            for gene in gene_table_data:
                new_file_handle.write(GENE_TABLE_DELIMITER.join([gene,gene_table_data[gene][i]])+"\n")
        
            new_file_handle.close()  
            
        except EnvironmentError:
            sys.exit("Unable to create split gene table files")
    
    return new_file_names

    Split a table where the abundances of genes are organized by sample in columns
    
Split a table where the abundances of genes are organized by sample in columns
Split a table where the abundances of genes are organized by sample in columns
def split_table_sample_columns(file_handle, header, line, output_dir, taxonomy_index, verbose):
    samples=header.rstrip().split(GENE_TABLE_DELIMITER)    
    if taxonomy_index != None:
        header_taxonomy=samples.pop()
    gene_table_data={}
    while line:
        data=line.rstrip().split(GENE_TABLE_DELIMITER)        
        gene=data.pop(0)
        if taxonomy_index != None:
            taxonomy_data=data.pop().split(TAXONOMY_DELIMITER)
            try:
                gene=taxonomy_data[taxonomy_index]
            except IndexError:
                sys.exit("The taxonomy index provided is not valid: " + str(taxonomy_index))
        if gene in gene_table_data:
            original_gene_data=gene_table_data[gene]
            for i, data_point in enumerate(data):
                try:
                    float_data_point=float(data_point)
                except ValueError:
                    float_data_point=0
                try:
                    original_gene_data[i]=str(float(original_gene_data[i])+float_data_point)
                except ValueError:
                    sys.exit("ERROR: The format of the input file is not that expected. "+
                             "Please provide a gene table, output file from picrust predict_metagenomes.py,"+
                             " or output file from picrust metagenome_contributions.py. "+
                             "If this file is output from picrust metagenome_contributions.py, "+
                             "it does not include the expected header.")
            gene_table_data[gene]=original_gene_data
        else:
            gene_table_data[gene]=data
        line = file_handle.readline()
    file_handle.close()
    new_file_names=[]
    sample_names=samples[1:]
    subheader=samples[0]
    for i, sample in enumerate(sample_names):
        simple_sample_name=re.sub("[^a-zA-Z0-9_|-|.]|@|\\?|\\]|\\[|\\^","_",sample)
        try:
            new_file_name=os.path.join(output_dir,simple_sample_name+TSV_FILE_EXTENSION)
            new_file_names.append(new_file_name)
            if verbose:
                print("Creating file: " + new_file_name)
            new_file_handle=open(new_file_name,"w")
            new_file_handle.write(GENE_TABLE_DELIMITER.join([subheader,sample])+"\n")
            for gene in gene_table_data:
                new_file_handle.write(GENE_TABLE_DELIMITER.join([gene,gene_table_data[gene][i]])+"\n")
            new_file_handle.close()  
        except EnvironmentError:
            sys.exit("Unable to create split gene table files")
    return new_file_names
def split_table_sample_columns ( file_handle , header , line , output_dir , taxonomy_index , verbose ) : samples = header . rstrip ( ) . split ( GENE_TABLE_DELIMITER ) if taxonomy_index != None : header_taxonomy = samples . pop ( ) gene_table_data = { } while line : data = line . rstrip ( ) . split ( GENE_TABLE_DELIMITER ) gene = data . pop ( 0 ) if taxonomy_index != None : taxonomy_data = data . pop ( ) . split ( TAXONOMY_DELIMITER ) try : gene = taxonomy_data [ taxonomy_index ] except IndexError : sys . exit ( "The taxonomy index provided is not valid: " + str ( taxonomy_index ) ) if gene in gene_table_data : original_gene_data = gene_table_data [ gene ] for i , data_point in enumerate ( data ) : try : float_data_point = float ( data_point ) except ValueError : float_data_point = 0 try : original_gene_data [ i ] = str ( float ( original_gene_data [ i ] ) + float_data_point ) except ValueError : sys . exit ( "ERROR: The format of the input file is not that expected. " + "Please provide a gene table, output file from picrust predict_metagenomes.py," + " or output file from picrust metagenome_contributions.py. " + "If this file is output from picrust metagenome_contributions.py, " + "it does not include the expected header." ) gene_table_data [ gene ] = original_gene_data else : gene_table_data [ gene ] = data line = file_handle . readline ( ) file_handle . close ( ) new_file_names = [ ] sample_names = samples [ 1 : ] subheader = samples [ 0 ] for i , sample in enumerate ( sample_names ) : simple_sample_name = re . sub ( "[^a-zA-Z0-9_|-|.]|@|\\?|\\]|\\[|\\^" , "_" , sample ) try : new_file_name = os . path . join ( output_dir , simple_sample_name + TSV_FILE_EXTENSION ) new_file_names . append ( new_file_name ) if verbose : print ( "Creating file: " + new_file_name ) new_file_handle = open ( new_file_name , "w" ) new_file_handle . write ( GENE_TABLE_DELIMITER . join ( [ subheader , sample ] ) + "\n" ) for gene in gene_table_data : new_file_handle . write ( GENE_TABLE_DELIMITER . join ( [ gene , gene_table_data [ gene ] [ i ] ] ) + "\n" ) new_file_handle . close ( ) except EnvironmentError : sys . exit ( "Unable to create split gene table files" ) return new_file_names
Split a table where the abundances of genes are organized by sample in columns
Split a table where the abundances of genes are organized by sample in columns
"""
    Split a table where the abundances of genes are organized by sample in columns
    """ # if taxonomy is set the last column is not a sample but taxonomy # taxonomy_index can be set to zero # write the data to each of the new files # process the taxonomy # taxonomy_index can be set to zero # taxonomy data is the last column # add the two gene lines # write the genes to the files # write the header
file_handle
header
line
output_dir
taxonomy_index
verbose