code: def parse_cov(cov_table, scaffold2genome):
      
      size   = {} # size[genome] = genome size
      mapped = {} # mapped[genome][sample] = mapped bases
      # parse coverage files
      for line in open(cov_table):
          line = line.strip().split('\t')
          if line[0].startswith('#'):
              samples = line[1:]
              samples = [i.rsplit('/', 1)[-1].split('.', 1)[0] for i in samples]
              continue
          scaffold, length = line[0].split(': ')
          length = float(length)
          covs  = [float(i) for i in line[1:]]
          bases = [c * length for c in covs]
          if scaffold not in scaffold2genome:
              continue
          genome = scaffold2genome[scaffold]
          if genome not in size:
              size[genome] = 0
              mapped[genome] = {sample:0 for sample in samples}
          # keep track of genome size
          size[genome] += length
          # keep track of number of mapped bases
          for sample, count in zip(samples, bases):
              mapped[genome][sample] += count
      # calculate coverage from base counts and genome size
      coverage = {'genome':[], 'genome size (bp)':[], 'sample':[], 'coverage':[]}
      for genome, length in size.items():
          for sample in samples:
              cov = mapped[genome][sample] / length
              coverage['genome'].append(genome)
              coverage['genome size (bp)'].append(length)
              coverage['sample'].append(sample)
              coverage['coverage'].append(cov)
      return pd.DataFrame(coverage)
docs: calculate genome coverage from scaffold coverage table