humann/tools/split_table.py
def split_table_sample_rows(file_handle, line, output_dir, verbose, taxonomy_level):
    """
    Split a table where the samples are indicated in each row
    """
    
    # create files for each sample
    new_file_names=[]
    
    # get the index to use for the taxonomy
    taxonomy_options=PICRUST_METAGENOME_HEADER.split("\t")
    taxonomy_index=taxonomy_options.index(taxonomy_level)-len(taxonomy_options)
    
    gene_table_data_by_sample_bug={}
    while line:
        # read in each line of data
        data=line.rstrip().split(GENE_TABLE_DELIMITER)        
        gene=data[PICRUST_METAGENOME_GENE_COLUMN]
        abundance=data[PICRUST_METAGENOME_ABUNDANCE_COLUMN]
        
        try:
            abundance=float(abundance)
        except ValueError:
            abundance=0
        
        sample=data[PICRUST_METAGENOME_SAMPLE_COLUMN]
        try:
            bug=data[taxonomy_index:]
        except IndexError:
            bug=[]
        
        # check for unclassified bugs (ie g__ ) and rename as "unclassified"
        renamed_bug=[]
        for taxon in bug:
            if not taxon.split("__")[-1]:
                taxon=taxon+"unclassified"
            renamed_bug.append(taxon)
            
        bug=".".join(renamed_bug)
        
        # sum the abundance data by sample and bug
        if not sample in gene_table_data_by_sample_bug:
            gene_table_data_by_sample_bug[sample]={}
            
        if not gene in gene_table_data_by_sample_bug[sample]:
            gene_table_data_by_sample_bug[sample][gene]={}
            
        gene_table_data_by_sample_bug[sample][gene][bug]=gene_table_data_by_sample_bug[sample][gene].get(bug,0)+abundance
        line = file_handle.readline()
        
    file_handle.close()
        
    # write the genes to the files
    for sample in gene_table_data_by_sample_bug:
        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)
            if verbose:
                print("Creating file: " + new_file_name)
            new_file_names.append(new_file_name)
            new_file_handle=open(new_file_name,"w")
            
            # write the header
            new_file_handle.write(GENE_TABLE_DELIMITER.join(["#OTU ID",sample])+"\n")
            
            # write all of the abundance data
            for gene in gene_table_data_by_sample_bug[sample]:
                total=0
                new_lines=[]
                for bug in gene_table_data_by_sample_bug[sample][gene]:
                    total+=gene_table_data_by_sample_bug[sample][gene][bug]
                    if bug:
                        new_lines.append(GENE_TABLE_DELIMITER.join([gene+"|"+bug,
                            str(gene_table_data_by_sample_bug[sample][gene][bug])])+"\n")
                    
                new_file_handle.write(GENE_TABLE_DELIMITER.join([gene,str(total)])+"\n")
                if new_lines:
                    new_file_handle.write("".join(new_lines))
                    
            new_file_handle.close()
            
        except EnvironmentError:
            sys.exit("Unable to create split gene table files") 
    
    return new_file_names

    Split a table where the samples are indicated in each row
    
Split a table where the samples are indicated in each row
Split a table where the samples are indicated in each row
def split_table_sample_rows(file_handle, line, output_dir, verbose, taxonomy_level):
    new_file_names=[]
    taxonomy_options=PICRUST_METAGENOME_HEADER.split("\t")
    taxonomy_index=taxonomy_options.index(taxonomy_level)-len(taxonomy_options)
    gene_table_data_by_sample_bug={}
    while line:
        data=line.rstrip().split(GENE_TABLE_DELIMITER)        
        gene=data[PICRUST_METAGENOME_GENE_COLUMN]
        abundance=data[PICRUST_METAGENOME_ABUNDANCE_COLUMN]
        try:
            abundance=float(abundance)
        except ValueError:
            abundance=0
        sample=data[PICRUST_METAGENOME_SAMPLE_COLUMN]
        try:
            bug=data[taxonomy_index:]
        except IndexError:
            bug=[]
        renamed_bug=[]
        for taxon in bug:
            if not taxon.split("__")[-1]:
                taxon=taxon+"unclassified"
            renamed_bug.append(taxon)
        bug=".".join(renamed_bug)
        if not sample in gene_table_data_by_sample_bug:
            gene_table_data_by_sample_bug[sample]={}
        if not gene in gene_table_data_by_sample_bug[sample]:
            gene_table_data_by_sample_bug[sample][gene]={}
        gene_table_data_by_sample_bug[sample][gene][bug]=gene_table_data_by_sample_bug[sample][gene].get(bug,0)+abundance
        line = file_handle.readline()
    file_handle.close()
    for sample in gene_table_data_by_sample_bug:
        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)
            if verbose:
                print("Creating file: " + new_file_name)
            new_file_names.append(new_file_name)
            new_file_handle=open(new_file_name,"w")
            new_file_handle.write(GENE_TABLE_DELIMITER.join(["#OTU ID",sample])+"\n")
            for gene in gene_table_data_by_sample_bug[sample]:
                total=0
                new_lines=[]
                for bug in gene_table_data_by_sample_bug[sample][gene]:
                    total+=gene_table_data_by_sample_bug[sample][gene][bug]
                    if bug:
                        new_lines.append(GENE_TABLE_DELIMITER.join([gene+"|"+bug,
                            str(gene_table_data_by_sample_bug[sample][gene][bug])])+"\n")
                new_file_handle.write(GENE_TABLE_DELIMITER.join([gene,str(total)])+"\n")
                if new_lines:
                    new_file_handle.write("".join(new_lines))
            new_file_handle.close()
        except EnvironmentError:
            sys.exit("Unable to create split gene table files") 
    return new_file_names
def split_table_sample_rows ( file_handle , line , output_dir , verbose , taxonomy_level ) : new_file_names = [ ] taxonomy_options = PICRUST_METAGENOME_HEADER . split ( "\t" ) taxonomy_index = taxonomy_options . index ( taxonomy_level ) - len ( taxonomy_options ) gene_table_data_by_sample_bug = { } while line : data = line . rstrip ( ) . split ( GENE_TABLE_DELIMITER ) gene = data [ PICRUST_METAGENOME_GENE_COLUMN ] abundance = data [ PICRUST_METAGENOME_ABUNDANCE_COLUMN ] try : abundance = float ( abundance ) except ValueError : abundance = 0 sample = data [ PICRUST_METAGENOME_SAMPLE_COLUMN ] try : bug = data [ taxonomy_index : ] except IndexError : bug = [ ] renamed_bug = [ ] for taxon in bug : if not taxon . split ( "__" ) [ - 1 ] : taxon = taxon + "unclassified" renamed_bug . append ( taxon ) bug = "." . join ( renamed_bug ) if not sample in gene_table_data_by_sample_bug : gene_table_data_by_sample_bug [ sample ] = { } if not gene in gene_table_data_by_sample_bug [ sample ] : gene_table_data_by_sample_bug [ sample ] [ gene ] = { } gene_table_data_by_sample_bug [ sample ] [ gene ] [ bug ] = gene_table_data_by_sample_bug [ sample ] [ gene ] . get ( bug , 0 ) + abundance line = file_handle . readline ( ) file_handle . close ( ) for sample in gene_table_data_by_sample_bug : 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 ) if verbose : print ( "Creating file: " + new_file_name ) new_file_names . append ( new_file_name ) new_file_handle = open ( new_file_name , "w" ) new_file_handle . write ( GENE_TABLE_DELIMITER . join ( [ "#OTU ID" , sample ] ) + "\n" ) for gene in gene_table_data_by_sample_bug [ sample ] : total = 0 new_lines = [ ] for bug in gene_table_data_by_sample_bug [ sample ] [ gene ] : total += gene_table_data_by_sample_bug [ sample ] [ gene ] [ bug ] if bug : new_lines . append ( GENE_TABLE_DELIMITER . join ( [ gene + "|" + bug , str ( gene_table_data_by_sample_bug [ sample ] [ gene ] [ bug ] ) ] ) + "\n" ) new_file_handle . write ( GENE_TABLE_DELIMITER . join ( [ gene , str ( total ) ] ) + "\n" ) if new_lines : new_file_handle . write ( "" . join ( new_lines ) ) new_file_handle . close ( ) except EnvironmentError : sys . exit ( "Unable to create split gene table files" ) return new_file_names
Split a table where the samples are indicated in each row
Split a table where the samples are indicated in each row
"""
    Split a table where the samples are indicated in each row
    """ # create files for each sample # get the index to use for the taxonomy # read in each line of data # check for unclassified bugs (ie g__ ) and rename as "unclassified" # sum the abundance data by sample and bug # write the genes to the files # write the header # write all of the abundance data
file_handle
line
output_dir
verbose
taxonomy_level