Skip to content

Commit

Permalink
Convert coverage file to read_count dict
Browse files Browse the repository at this point in the history
  • Loading branch information
ThijsMaas committed Aug 2, 2024
1 parent e2bc0d8 commit 7427c24
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
17 changes: 17 additions & 0 deletions iss/abundance.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,3 +315,20 @@ def expand_draft_abundance(abundance_dic, draft, mode="abundance"):
elif mode == "coverage":
draft_dic[record.id] = abundance
return draft_dic


def to_readcount(coverage_dic, read_length, genome_file):
logger = logging.getLogger(__name__)
readcount_dic = {}
f = open(genome_file, "r") # re-opens the file
with f:
fasta_file = SeqIO.parse(f, "fasta")
for record in fasta_file:
try:
record_coverage = coverage_dic[record.id]
except KeyError as e:
logger.warning("Fasta record not found in coverage file: %s" % e)
continue
n_reads = int((record_coverage * len(record.seq)) / read_length)
readcount_dic[record.id] = n_reads
return readcount_dic
6 changes: 4 additions & 2 deletions iss/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,9 +555,11 @@ def load_readcount_or_abundance(
coverage_dic = abundance.parse_abundance_file(coverage_file)
complete_genomes_dic = {k: v for k, v in coverage_dic.items() if k not in draft}
draft_dic = abundance.expand_draft_abundance(coverage_dic, draft, mode="coverage")
abundance_dic = {**complete_genomes_dic, **draft_dic}
coverage_dic = {**complete_genomes_dic, **draft_dic}
else:
abundance_dic = abundance.parse_abundance_file(coverage_file)
coverage_dic = abundance.parse_abundance_file(coverage_file)
# generate new n_reads from coverage
readcount_dic = abundance.to_readcount(coverage_dic, error_model.read_length, genome_file)
elif coverage in abundance_dispatch:
# todo coverage distribution with --draft
logger.info("Using %s coverage distribution" % coverage)
Expand Down

0 comments on commit 7427c24

Please sign in to comment.