<commit_before>/*
 * Copyright 2013-{year} <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.antigenomics.higblast.genomic

import com.antigenomics.higblast.RuntimeInfo
import com.antigenomics.higblast.Util

class SegmentDatabase {
    private static final List<SegmentDatabase> DB_CACHE = new LinkedList<>()
    final String databaseTempPath
    final Map<String, Segment> segments = new HashMap<>()
    final boolean hasD
    final int vSegments, dSegments, jSegments, vSegmentsNoMarkup

    static final SPECIES_ALIAS =
            ["human"        : "HomoSapiens",
             "mouse"        : "MusMusculus",
             "rat"          : "RattusNorvegicus",
             "rabbit"       : "OryctolagusCuniculus",
             "rhesus_monkey": "MacacaMulatta"]

    SegmentDatabase(String dataBundlePath,
                    String species, List<String> genes,
                    boolean allAlleles, boolean useKabat) {
        genes = genes.unique()

        def speciesAlias = SPECIES_ALIAS[species]

        boolean hasD = false

        int vSegments = 0, dSegments = 0, jSegments = 0, vSegmentsNoMarkup = 0

        Util.getStream("segments.txt", true).splitEachLine("[\t ]+") { splitLine ->
            if (!splitLine[0].startsWith("#") &&
                    splitLine[0].startsWith(speciesAlias) &&
                    genes.contains(splitLine[1])) {

                def segmentName = splitLine[3]

                if (allAlleles || segmentName.endsWith("*01")) {
                    def seq = splitLine[5],
                        referencePoint = splitLine[4].toInteger(), segmentTypeStr = splitLine[2]

                    assert !segments.containsKey(segmentName)

                    if (segmentTypeStr.startsWith("V")) {
                        segments.put(segmentName, new Segment(SegmentType.V, segmentName, seq, referencePoint))
                        vSegments++
                    } else if (segmentTypeStr.startsWith("D")) {
                        segments.put(segmentName, new Segment(SegmentType.D, segmentName, seq, referencePoint))
                        hasD = true
                        dSegments++
                    } else if (segmentTypeStr.startsWith("J")) {
                        segments.put(segmentName, new Segment(SegmentType.J, segmentName, seq, referencePoint))
                        jSegments++
                    }
                }
            }
        }

        Util.report("Loaded database. #v=$vSegments,#d=$dSegments,#j=$jSegments.", 2)

        if (!hasD) {
            segments.put(Segment.DUMMY_D.name, Segment.DUMMY_D)
        }

        this.hasD = hasD
        this.databaseTempPath = dataBundlePath + "/database-" + UUID.randomUUID().toString()
        this.vSegments = vSegments
        this.dSegments = dSegments
        this.jSegments = jSegments
        this.vSegmentsNoMarkup = vSegmentsNoMarkup

        DB_CACHE.add(this)
    }

    void makeBlastDb() {
        new File(databaseTempPath).mkdir()

        new File("$databaseTempPath/v.fa").withPrintWriter { pwV ->
            new File("$databaseTempPath/d.fa").withPrintWriter { pwD ->
                new File("$databaseTempPath/j.fa").withPrintWriter { pwJ ->
                    segments.values().each {
                        switch (it.type) {
                            case SegmentType.V:<commit_msg>Segment database refactoring<commit_after>/*
 * Copyright 2013-{year} <NAME> (<EMAIL>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.antigenomics.higblast.genomic

import com.antigenomics.higblast.RuntimeInfo
import com.antigenomics.higblast.Util

class SegmentDatabase {
    private static final List<SegmentDatabase> DB_CACHE = new LinkedList<>()
    final String databaseTempPath
    final Map<String, Segment> segments = new HashMap<>()
    final boolean hasD
    final int vSegments, dSegments, jSegments, vSegmentsNoMarkup

    static final SPECIES_ALIAS =
            ["human"        : "HomoSapiens",
             "mouse"        : "MusMusculus",
             "rat"          : "RattusNorvegicus",
             "rabbit"       : "OryctolagusCuniculus",
             "rhesus_monkey": "MacacaMulatta"]

    SegmentDatabase(String dataBundlePath,
                    String species, List<String> genes,
                    boolean allAlleles = true) {
        genes = genes.unique()

        def speciesAlias = SPECIES_ALIAS[species]

        boolean hasD = false

        int vSegments = 0, dSegments = 0, jSegments = 0, vSegmentsNoMarkup = 0

        Util.getStream("segments.txt", true).splitEachLine("[\t ]+") { splitLine ->
            if (!splitLine[0].startsWith("#") &&
                    splitLine[0].startsWith(speciesAlias) &&
                    genes.contains(splitLine[1])) {

                def segmentName = splitLine[3]

                if (allAlleles || segmentName.endsWith("*01")) {
                    def seq = splitLine[5],
                        referencePoint = splitLine[4].toInteger(), segmentTypeStr = splitLine[2]

                    assert !segments.containsKey(segmentName)

                    if (segmentTypeStr.startsWith("V")) {
                        segments.put(segmentName, new Segment(SegmentType.V, segmentName, seq, referencePoint))
                        vSegments++
                    } else if (segmentTypeStr.startsWith("D")) {
                        segments.put(segmentName, new Segment(SegmentType.D, segmentName, seq, referencePoint))
                        hasD = true
                        dSegments++
                    } else if (segmentTypeStr.startsWith("J")) {
                        segments.put(segmentName, new Segment(SegmentType.J, segmentName, seq, referencePoint))
                        jSegments++
                    }
                }
            }
        }

        Util.report("Loaded database for $species:${genes.join(",")}. #v=$vSegments,#d=$dSegments,#j=$jSegments.", 2)

        if (!hasD) {
            segments.put(Segment.DUMMY_D.name, Segment.DUMMY_D)
        }

        this.hasD = hasD
        this.databaseTempPath = dataBundlePath + "/database-" + UUID.randomUUID().toString()
        this.vSegments = vSegments
        this.dSegments = dSegments
        this.jSegments = jSegments
        this.vSegmentsNoMarkup = vSegmentsNoMarkup

        DB_CACHE.add(this)
    }

    void makeBlastDb() {
        new File(databaseTempPath).mkdir()

        new File("$databaseTempPath/v.fa").withPrintWriter { pwV ->
            new File("$databaseTempPath/d.fa").withPrintWriter { pwD ->
                new File("$databaseTempPath/j.fa").withPrintWriter { pwJ ->
                    segments.values().each {
                        switch (it.type) {
                            case SegmentType.V:
{'fraction_non_alphanumeric': 0.10205391527599486, 'fraction_numerical': 0.005648267008985879, 'mean_word_length': 2.0291601866251945, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6722224', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2101, 'n_words': 502}