File size: 4,349 Bytes
ce4b573 | 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 27 28 29 30 31 32 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | '''
Created on Dec 6, 2012
@author: jonathanfriedman
'''
import numpy as np
from pandas.io.parsers import read_table
from Lineages import Lineages
#from pandas.util.decorators import Appender
#@Appender(read_table.__doc__)
def read_txt(file, T=True, lin=None, lin_label='lineage',
format='QIIME', verbose=True, **kwargs):
'''
Read general delimited file into DataFrame.
This a wrapper around pandas' read_table function which adds
optional parsing of lineage information, and sets some default
parameter values.
Note:
By default the data is transposed!
To avoid this behavior set the parameter 'T' to False.
Parameters
----------
file : string
Path to input file.
T : bool (default True)
Indicated whether the produced DataFrame will be transposed.
lin : bool/None (default None)
Indicated whether lineage information is given in the input file.
If None, read_txt tries to infer the presence of
lineage information automatically
lin_label : string (default 'lineage')
Label of the column containing the lineage information.
format : string (default 'QIIME')
Format of the lineage information.
This argument is passed to the Lineage object constructor.
verbose : bool (default True)
Indicated whether to print to screen the parsed table stats.
Returns
-------
table : DataFrame
Parsed table.
lins : Lineages (optional)
Parsed Lineages object.
Returned only if lineage information was parsed.
'''
kwargs.setdefault('index_col',0)
temp = read_table(file, engine='python', **kwargs)
# try to decide whether lineages are given, if not specified by user
if lin is None:
lin = False
lin_labels = ('lin','lins','lineage','lineages',
'taxon','taxa','rdp')
for c in temp.columns:
if hasattr(c, 'lower'):
if c.lower() in lin_labels:
lin = True
lin_label = c
if lin: # parse lins if needed
lins = Lineages.from_dict(temp[lin_label], format=format)
temp = temp.drop(lin_label,axis=1)
if T:
temp = temp.T
s = ['Finished parsing table.',
'Table dimensions: (%d,%d)' %temp.shape]
if T:
s += ['**** Data has been transposed! ****']
ncol = min(temp.shape[1],3)
nrow = min(temp.shape[0],3)
scol = tuple([ncol] + list(temp.columns[:ncol]))
srow = tuple([nrow] + list(temp.index[:nrow]))
s += [('First %d column labels are :'
+ ' ,'.join(['%s']*ncol)) %scol,
('First %d row labels are :'
+ ' ,'.join(['%s']*nrow)) %srow]
table = temp
if lin:
if verbose: print '\n'.join(s), '\n'
return table, lins
else:
if verbose: print '\n'.join(s), '\n'
return table
def write_txt(frame, file, T=True, lin=None, lin_label='lineage', **kwargs):
'''
Write frame to txt file.
This a wrapper around pandas' to_csv function which adds
optional writing of lineage information, and sets some default
parameter values.
Note:
By default the data is transposed!
To avoid this behavior set the parameter 'T' to False.
Parameters
----------
file : string
Path to input file.
T : bool (default True)
Indicated whether the produced DataFrame will be transposed.
lin : None/None (default None)
Lineages object to be included in the output file.
lin_label : string (default 'lineage')
Label of the column containing the lineage information.
'''
from pandas import Series
kwargs.setdefault('sep','\t')
if T: data = frame.T
else: data = frame
if lin is not None:
d = {}
for i in data.index:
if i in lin:
d[i] = lin[i].lin_str
else:
d[i] = None
t = Series(d, name=lin_label)
data = data.join(t)
data.to_csv(file,**kwargs)
if __name__ == '__main__':
file = 'demo/data/fake_data_lin.counts'
t, lin = read_txt(file)
# write_txt(t*1.1, 'temp.txt', lin=lin)
# print t
# print read_txt.__doc__
|