code: def prepare(args):
      
      p = OptionParser(prepare.__doc__)
      opts, args = p.parse_args(args)
  
      if len(args) != 2:
          sys.exit(not p.print_help())
  
      counts, families = args
      countfiles = glob(op.join(counts, "*.count"))
      countsdb = defaultdict(list)
      for c in countfiles:
          rs = RiceSample(c)
          countsdb[(rs.tissue, rs.ind)].append(rs)
  
      # Merge duplicates - data sequenced in different batches
      key = lambda x: (x.label, x.rep)
      for (tissue, ind), rs in sorted(countsdb.items()):
          rs.sort(key=key)
          nrs = len(rs)
          for i in xrange(nrs):
              ri = rs[i]
              if not ri.working:
                  continue
              for j in xrange(i + 1, nrs):
                  rj = rs[j]
                  if key(ri) != key(rj):
                      continue
                  ri.merge(rj)
                  rj.working = False
          countsdb[(tissue, ind)] = [x for x in rs if x.working]
  
      # Group into families
      mkdir("families")
      for (tissue, ind), r in sorted(countsdb.items()):
          r = list(r)
          if r[0].label != "F1":
              continue
          P1, P2 = r[0].P1, r[0].P2
          P1, P2 = countsdb[(tissue, P1)], countsdb[(tissue, P2)]
          rs = P1 + P2 + r
          groups = [1] * len(P1) + [2] * len(P2) + [3] * len(r)
          assert len(rs) == len(groups)
  
          outfile = "-".join((tissue, ind))
          merge_counts(rs, op.join(families, outfile))
          groupsfile = outfile + ".groups"
          fw = open(op.join(families, groupsfile), "w")
          print(",".join(str(x) for x in groups), file=fw)
          fw.close()
docs: %prog prepare countfolder families
  
      Parse list of count files and group per family into families folder.