Commit ff940d3d authored by Frédéric Escudié's avatar Frédéric Escudié
Browse files

Prevent bug with default parameter values in python.

parent 0344aea1
......@@ -138,8 +138,9 @@ class DenseData( list ):
class SparseData( dict ):
def __init__( self, list=list() ):
for data in list:
def __init__( self, list=None ):
ini_list = list if list is not None else list()
for data in ini_list:
if not self.has_key( data[0] ):
self[data[0]] = dict()
self[data[0]][data[1]] = data[2]
......@@ -299,22 +300,35 @@ class Biom:
"""
def __init__( self, id=None, format="Biological Observation Matrix 1.0.0-dev",
format_url="http://biom-format.org", type="OTU table", generated_by=None,
date=time.strftime('%y-%m-%dT%H:%M:%S',time.localtime()), rows=list(),
columns=list(), matrix_type="dense", matrix_element_type="int",
data=list() ):
date=None, rows=None, columns=None, matrix_type="dense", matrix_element_type="int",
data=None ):
"""
@param id : [int]
@param format : [str]
@param format_url : [str]
@param type : [str]
@param generated_by : [str]
@param date : [str]
@param rows : [list]
@param columns : [list]
@param matrix_type : [str]
@param matrix_element_type : [str]
@param data : [list]
"""
self.id = id
self.format = format
self.format_url = format_url
self.type = type
self.generated_by = generated_by
self.date = date
self.rows = rows
self.columns = columns
self.date = date if date is not None else time.strftime('%y-%m-%dT%H:%M:%S',time.localtime())
self.rows = rows if rows is not None else list()
self.columns = columns if columns is not None else list()
self.matrix_element_type = matrix_element_type
ini_data = data if data is not None else list()
if matrix_type == "dense":
self.data = DenseData( data )
self.data = DenseData( ini_data )
else:
self.data = SparseData( data )
self.data = SparseData( ini_data )
def __str__(self):
return str( self.__dict__ )
......@@ -535,38 +549,40 @@ class Biom:
col_idx = self.find_idx( self.columns, sample_name )
self.data.add( row_idx, col_idx, value )
def add_observation( self, observation_name, metadata=dict() ):
def add_observation( self, observation_name, metadata=None ):
"""
@summary : Add one observation in biom.
@param observation_name : [str] The observation name.
@param metadata : [dict] The metadata (keys : metadata names ; values : metadata values).
"""
ini_metadata = metadata if metadata is not None else dict()
try:
self.find_idx( self.rows, observation_name )
# Observation doesn't exist
except ValueError:
self.rows.append( {'id':observation_name, 'metadata':None } )
self.data.add_row()
for metadata_name in metadata.keys():
self.add_metadata( observation_name, metadata_name, metadata[metadata_name], "observation" )
for metadata_name in ini_metadata.keys():
self.add_metadata( observation_name, metadata_name, ini_metadata[metadata_name], "observation" )
# Observation already exists
else:
raise ValueError( "The observation '" + observation_name + "' already exists." )
def add_sample( self, sample_name, metadata=dict() ):
def add_sample( self, sample_name, metadata=None ):
"""
@summary : Add one sample in biom.
@param sample_name : [str] The sample name.
@param metadata : [dict] The metadata (keys : metadata names ; values : metadata values).
"""
ini_metadata = metadata if metadata is not None else dict()
try:
self.find_idx( self.columns, sample_name )
# Sample doesn't exist
except ValueError:
self.columns.append( {'id':sample_name, 'metadata':None } )
self.data.add_column()
for metadata_name in metadata.keys():
self.add_metadata( sample_name, metadata_name, metadata[metadata_name], "sample" )
for metadata_name in ini_metadata.keys():
self.add_metadata( sample_name, metadata_name, ini_metadata[metadata_name], "sample" )
# Sample already exists
else:
raise ValueError( "The sample '" + sample_name + "' already exists." )
......@@ -817,7 +833,7 @@ class BiomIO:
out_fh.close()
@staticmethod
def load_metadata( biom, metadata_file, subject_type="sample", types={}, list_sep={} ):
def load_metadata( biom, metadata_file, subject_type="sample", types=None, list_sep=None ):
"""
@summary : Add to biom several metadata from metadata file.
@param biom : [Biom] The Biom object to update.
......@@ -840,6 +856,8 @@ class BiomIO:
'environnement' : '/' # Sea/Ocean
}
"""
ini_types = types if types is not None else dict()
ini_list_sep = list_sep if list_sep is not None else dict()
metadata_fh = open( metadata_file )
metadata = list()
# Names and type of metadata
......@@ -847,11 +865,11 @@ class BiomIO:
title_fields = title_line.split()
for metadata_name in title_fields[1:]:
metadata_type = "str"
if types.has_key( metadata_name ):
metadata_type = types[metadata_name]
if ini_types.has_key( metadata_name ):
metadata_type = ini_types[metadata_name]
metadata_list_sep = None
if list_sep.has_key( metadata_name ):
metadata_list_sep = list_sep[metadata_name]
if ini_list_sep.has_key( metadata_name ):
metadata_list_sep = ini_list_sep[metadata_name]
metadata.append( {
'name' : metadata_name,
'type' : metadata_type,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment