new_contents: # -*- coding: utf-8 -*-
  #
  #  This file is part of Sequana software
  #
  #  Copyright (c) 2016 - Sequana Development Team
  #
  #  File author(s):
  #      Thomas Cokelaer <thomas.cokelaer@pasteur.fr>
  #      Dimitri Desvillechabrol <dimitri.desvillechabrol@pasteur.fr>,
  #          <d.desvillechabrol@gmail.com>
  #
  #  Distributed under the terms of the 3-clause BSD license.
  #  The full license is in the LICENSE file, distributed with this software.
  #
  #  website: https://github.com/sequana/sequana
  #  documentation: http://sequana.readthedocs.io
  #
  ##############################################################################
  """Handle experimental design files (adapters)"""
  import pandas as pd
  import shlex
  import io
  
  
  __all__ = ["ExpDesignAdapter"]
  
  class ExpDesignAdapter(object):
      """Generic Experimantal design class for adapters
  
  
      The :attr:`df` attribute contains Sample_ID, Index_Seq and Index_ID amongst
      other things. The other column may differ depending on the input.
  
      """
      def __init__(self, filename):
          try:
              self.df = ExpDesignMiSeq(filename).df
          except:
              self.df = ExpDesignHiSeq(filename).df
  
  
  class ExpDesignHiSeq(object):
      """Dedicated experimental design class for adapters (see details below)
  
  
      Columns are:
          - FCID
          - Lane
          - SampleID",
          - "SampleRef",
          - "Index Seq",
          - "Description",
          - "Control",
          - "Recipe",
          - "Operator"
      Design Exp for HiSeq data from 
  
      Index should appear in the name
  
      This layout is specific to the demultiplexing LIMS used at Biomics
  
      FCID = flowcell ID
  
      double indexing is stored in one column as index1-index2
  
      rename columns to a common format
      """
      def __init__(self, filename):
  
          self.df = pd.read_csv(filename, sep=",")
          self._expected_columns = ["FCID","Lane","SampleID","SampleRef","Index Seq",
              "Description","Control","Recipe","Operator"]
  
          if all(self._expected_columns == self.df.columns) is False:
              raise ValueError("Invalid header. Must be %s" % self._expected_columns)
  
          self.df.rename(columns={"SampleID":"Sample_ID", "Index Seq":"Index_Seq"},
                         inplace=True)
  
          indices = self.df.Index_Seq.apply(lambda x: x.split("-")).values
          index1_seq = [x[0] for x in indices]
          index2_seq = [None if len(x) == 1 else x[1] for x in indices]
          self.df['Index1_Seq'] = index1_seq
          self.df['Index2_Seq'] = index2_seq
          self.df.drop("Index_Seq", axis=1, inplace=True)
          self.df["Index1_Seq"] = index1_seq
          self.df["Index2_Seq"] = index2_seq
  
  
  class ExpDesignMiSeq(object):
      """
  
      MiSeq design file is generated by Illumina itself.
      The format is ::
  
          [Header]
  
          [Reads]
  
          [Settings]
  
          [Data]
  
      In Data, index are stored as NFXX where XX is the index ID.NF should be
      dropped.
      ::
  
          ff = ExpDesignMiSeq("test_expdesign_miseq_illumina.csv")
          ff.df
  
      """
      def __init__(self, filename):
          data = {}
          # shlex removes all white lines and split by return carriage
          # strip is also applied
          rawdata =shlex.split(open(filename, "r"))
          for line in rawdata:
              if line.startswith('[') and line.endswith(']'):
                  currentkey = line.replace("[", "").replace("]", "")
                  data[currentkey] = []
              else:
                  data[currentkey].append(line)
  
          for key in data.keys():
              data[key] = "\n".join(data[key])
  
          for this in ["Header", "Reads", "Settings", "Data"]:
              if this not in data.keys():
                  print("%s not found in the DesignExpMiSeq file" % this)
  
          self.data = data
          self.df = pd.read_csv(io.StringIO(data["Data"]))
  
          self.df.rename(columns={"I7_Index_ID":"Index1_ID", "index":"Index1_Seq"},
                         inplace=True)
  
          self.df["Index1_ID"] = self.df["Index1_ID"].apply(
              lambda x: float(x.replace("NF", "")))
          self.df["Index1_ID"] = self.df['Index1_ID'].astype(int)
  
  
  
  
  
subject: update designexp module to handle MiSeq and HiSeq sample sheet