<commit_before>import os
import pymongo

import virtool.db.history
import virtool.db.indexes
import virtool.db.otus
import virtool.db.sync
import virtool.errors
import virtool.history
import virtool.jobs.job
import virtool.otus
import virtool.utils


class Job(virtool.jobs.job.Job):
    """
    Job object that builds a new Bowtie2 index for a given reference.

    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._stage_list = [
            self.mk_index_dir,
            self.write_fasta,
            self.bowtie_build,
            self.replace_old
        ]

    def check_db(self):
        """
        Get job information from the database.

        """
        self.params = dict(self.task_args)

        self.params["reference_path"] = os.path.join(
            self.settings["data_path"],
            "references",
            self.params["ref_id"]
        )

        self. params["index_path"] = os.path.join(
            self.params["reference_path"],
            self.params["index_id"]
        )

    def mk_index_dir(self):
        """
        Make dir for the new index at ``<data_path/references/<index_id>``.

        """
        try:
            os.makedirs(self.params["index_path"])
        except FileExistsError:
            pass

    def write_fasta(self):
        """
        Generates a FASTA file of all sequences in the reference database. The FASTA headers are
        the accession numbers.

        """
        fasta_dict = dict()

        for patch_id, patch_version in self.params["manifest"].items():
            document = self.db.otus.find_one(patch_id)

            if document["version"] == patch_version:
                joined = virtool.db.sync.join_otu(self.db, patch_id, document)
            else:
                _, joined, _ = virtool.db.sync.patch_otu_to_version(self.db, patch_id, patch_version)

            # Extract the list of sequences from the joined patched patch.
            sequences = virtool.otus.extract_default_sequences(joined)

            defaults = list()

            try:
                for sequence in sequences:
                    defaults.append(sequence["_id"])
                    fasta_dict[sequence["_id"]] = sequence["sequence"]

            except TypeError:
                raise

        fasta_path = os.path.join(self.params["index_path"], "ref.fa")

        write_fasta_dict_to_file(fasta_path, fasta_dict)

    def bowtie_build(self):<commit_msg>Write sequence_otu_map in new index builds<commit_after>import os
import pymongo

import virtool.db.history
import virtool.db.indexes
import virtool.db.otus
import virtool.db.sync
import virtool.errors
import virtool.history
import virtool.jobs.job
import virtool.otus
import virtool.utils


class Job(virtool.jobs.job.Job):
    """
    Job object that builds a new Bowtie2 index for a given reference.

    """
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._stage_list = [
            self.mk_index_dir,
            self.write_fasta,
            self.bowtie_build,
            self.replace_old
        ]

    def check_db(self):
        """
        Get job information from the database.

        """
        self.params = dict(self.task_args)

        self.params["reference_path"] = os.path.join(
            self.settings["data_path"],
            "references",
            self.params["ref_id"]
        )

        self. params["index_path"] = os.path.join(
            self.params["reference_path"],
            self.params["index_id"]
        )

    def mk_index_dir(self):
        """
        Make dir for the new index at ``<data_path/references/<index_id>``.

        """
        try:
            os.makedirs(self.params["index_path"])
        except FileExistsError:
            pass

    def write_fasta(self):
        """
        Generates a FASTA file of all sequences in the reference database. The FASTA headers are
        the accession numbers.

        """
        fasta_dict = dict()
        sequence_otu_map = dict()

        for patch_id, patch_version in self.params["manifest"].items():
            document = self.db.otus.find_one(patch_id)

            if document["version"] == patch_version:
                joined = virtool.db.sync.join_otu(self.db, patch_id, document)
            else:
                _, joined, _ = virtool.db.sync.patch_otu_to_version(self.db, patch_id, patch_version)

            for isolate in joined["isolates"]:
                for sequence in isolate["sequences"]:
                    sequence_otu_map[sequence["id"]] = patch_id

            # Extract the list of sequences from the joined patched patch.
            sequences = virtool.otus.extract_default_sequences(joined)

            defaults = list()

            try:
                for sequence in sequences:
                    defaults.append(sequence["_id"])
                    fasta_dict[sequence["_id"]] = sequence["sequence"]

            except TypeError:
                raise

        fasta_path = os.path.join(self.params["index_path"], "ref.fa")

        write_fasta_dict_to_file(fasta_path, fasta_dict)

        index_id = self.params["index_id"]

        self.db.indexes.update_one({"_id": index_id}, {
            "$set": {
                "sequence_otu_map": sequence_otu_map
            }
        })

        self.dispatch("indexes", "update", [index_id])

    def bowtie_build(self):
{'fraction_non_alphanumeric': 0.10484759456481821, 'fraction_numerical': 0.0003672420124862284, 'mean_word_length': 1.9895718990120745, 'pattern_counts': {'":': 3, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5188472', 'n_tokens_mistral': 1637, 'n_tokens_neox': 1553, 'n_words': 336}