def mapping_lines_from_collapsed_df(collapsed_df):
 lines = []
 lines.append(' DCTB '.join((['#SampleID', 'original-sample-ids'] + list(collapsed_df.columns)[1:])))
 for r in collapsed_df.iterrows():
  if isinstance(r[0], tuple):
   new_idx = '.'.join(map(str, r[0]))
  else:
   new_idx = str(r[0])
  new_values = []
  for e in r[1]:
   if (len(set(e)) == 1):
    new_values.append(str(e[0]))
   else:
    new_values.append(('(%s)' % ', '.join(map(str, e))))
  lines.append(' DCTB '.join(([new_idx] + new_values)))
 return lines

def mapping_lines_from_collapsed_df ( collapsed_df ) : lines = [ ] lines . append ( ' DCTB ' . join ( ( [ '#SampleID' , 'original-sample-ids' ] + list ( collapsed_df . columns ) [ 1 : ] ) ) ) for r in collapsed_df . iterrows ( ) : if isinstance ( r [ 0 ] , tuple ) : new_idx = '.' . join ( map ( str , r [ 0 ] ) ) else : new_idx = str ( r [ 0 ] ) new_values = [ ] for e in r [ 1 ] : if ( len ( set ( e ) ) == 1 ) : new_values . append ( str ( e [ 0 ] ) ) else : new_values . append ( ( '(%s)' % ', ' . join ( map ( str , e ) ) ) ) lines . append ( ' DCTB ' . join ( ( [ new_idx ] + new_values ) ) ) return lines
formats a multi-index dataframe as lines of a qiime mapping file parameters collapsed_df : pd .