def parse_otu_map(otu_map_f, otu_ids_to_exclude=None, delim='_'):
 if (otu_ids_to_exclude is None):
  otu_ids_to_exclude = {}
 result = defaultdict(int)
 sample_ids = []
 sample_id_idx = {}
 otu_ids = []
 otu_count = 0
 sample_count = 0
 for line in otu_map_f:
  fields = line.strip().split(' DCTB ')
  otu_id = fields[0]
  if (otu_id in otu_ids_to_exclude):
   continue
  for seq_id in fields[1:]:
   sample_id = seq_id.split(delim)[0]
   try:
    sample_index = sample_id_idx[sample_id]
   except KeyError:
    sample_index = sample_count
    sample_id_idx[sample_id] = sample_index
    sample_count += 1
    sample_ids.append(sample_id)
   result[(otu_count, sample_index)] += 1
  otu_count += 1
  otu_ids.append(otu_id)
 return (result, sample_ids, otu_ids)

def parse_otu_map ( otu_map_f , otu_ids_to_exclude = None , delim = '_' ) : if ( otu_ids_to_exclude is None ) : otu_ids_to_exclude = { } result = defaultdict ( int ) sample_ids = [ ] sample_id_idx = { } otu_ids = [ ] otu_count = 0 sample_count = 0 for line in otu_map_f : fields = line . strip ( ) . split ( ' DCTB ' ) otu_id = fields [ 0 ] if ( otu_id in otu_ids_to_exclude ) : continue for seq_id in fields [ 1 : ] : sample_id = seq_id . split ( delim ) [ 0 ] try : sample_index = sample_id_idx [ sample_id ] except KeyError : sample_index = sample_count sample_id_idx [ sample_id ] = sample_index sample_count += 1 sample_ids . append ( sample_id ) result [ ( otu_count , sample_index ) ] += 1 otu_count += 1 otu_ids . append ( otu_id ) return ( result , sample_ids , otu_ids )
parse otu map file into a sparse dict {:count} this function is much more memory efficent than fields_to_dict and and the result dict is of the correct format to be passed to table_factory for creating otutable objects .