def parse_cov(cov_table, scaffold2genome):
size   = {} <EOL>mapped = {} <EOL>for line in open(cov_table):<EOL><INDENT>line = line.strip().split('<STR_LIT:\t>')<EOL>if line[<NUM_LIT:0>].startswith('<STR_LIT:#>'):<EOL><INDENT>samples = line[<NUM_LIT:1>:]<EOL>samples = [i.rsplit('<STR_LIT:/>', <NUM_LIT:1>)[-<NUM_LIT:1>].split('<STR_LIT:.>', <NUM_LIT:1>)[<NUM_LIT:0>] for i in samples]<EOL>continue<EOL><DEDENT>scaffold, length = line[<NUM_LIT:0>].split('<STR_LIT>')<EOL>length = float(length)<EOL>covs  = [float(i) for i in line[<NUM_LIT:1>:]]<EOL>bases = [c * length for c in covs]<EOL>if scaffold not in scaffold2genome:<EOL><INDENT>continue<EOL><DEDENT>genome = scaffold2genome[scaffold]<EOL>if genome not in size:<EOL><INDENT>size[genome] = <NUM_LIT:0><EOL>mapped[genome] = {sample:<NUM_LIT:0> for sample in samples}<EOL><DEDENT>size[genome] += length<EOL>for sample, count in zip(samples, bases):<EOL><INDENT>mapped[genome][sample] += count<EOL><DEDENT><DEDENT>coverage = {'<STR_LIT>':[], '<STR_LIT>':[], '<STR_LIT>':[], '<STR_LIT>':[]}<EOL>for genome, length in size.items():<EOL><INDENT>for sample in samples:<EOL><INDENT>cov = mapped[genome][sample] / length<EOL>coverage['<STR_LIT>'].append(genome)<EOL>coverage['<STR_LIT>'].append(length)<EOL>coverage['<STR_LIT>'].append(sample)<EOL>coverage['<STR_LIT>'].append(cov)<EOL><DEDENT><DEDENT>return pd.DataFrame(coverage)<EOL>
calculate genome coverage from scaffold coverage table