<commit_before>package org.nmdp.hmlfhirconverter.mapping.fhir;

/**
 * Created by <NAME>, Ph.D., <<EMAIL>>, on 4/25/17.
 * <p>
 * service-hmlFhirConverter
 * Copyright (c) 2012-2017 National Marrow Donor Program (NMDP)
 * <p>
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 * <p>
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * <p>
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library;  if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 * <p>
 * > http://www.fsf.org/licensing/licenses/lgpl.html
 * > http://www.opensource.org/licenses/lgpl-license.php
 */

import io.swagger.model.*;

import org.modelmapper.Converter;
import org.modelmapper.spi.MappingContext;

import org.nmdp.hmlfhirconverter.domain.fhir.BackboneElement;
import org.nmdp.hmlfhirconverter.domain.fhir.Identifier;
import org.nmdp.hmlfhirconverter.domain.fhir.ReferenceSequenceId;
import org.nmdp.hmlfhirconverter.domain.fhir.Sequence;

import java.util.ArrayList;
import java.util.List;

public class SequenceMap implements Converter<Hml, List<Sequence>> {

    @Override
    public List<Sequence> convert(MappingContext<Hml, List<Sequence>> context) {
        if (context.getSource() == null) {
            return null;
        }

        List<Sequence> sequences = new ArrayList<>();
        Hml hml = context.getSource();
        Sample sample = hml.getSamples().get(0);
        Typing typing = sample.getTyping();
        ConsensusSequence consensusSequence = typing.getConsensusSequence();
        ReferenceDatabase referenceDatabase = consensusSequence.getReferenceDatabase();
        ReferenceSequence referenceSequence = referenceDatabase.getReferenceSequence();
        List<ConsensusSequenceBlock> consensusSequenceBlock = consensusSequence.getConsensusSequenceBlocks();

        Sequence referenceSequenceFhir = new Sequence();
        BackboneElement backboneElement = new BackboneElement();
        ReferenceSequenceId referenceSequenceId = new ReferenceSequenceId();
        Identifier identifier = new Identifier();

        backboneElement.setWindowStart(referenceSequence.getStart());
        backboneElement.setWindowEnd(referenceSequence.getEnd());
        identifier.setValue(referenceSequence.getAccession());
        referenceSequenceId.setIdentifier(identifier);
        backboneElement.setReferenceSequenceId(referenceSequenceId);

        return sequences;
    }
}<commit_msg>Fix sequence mapping<commit_after>package org.nmdp.hmlfhirconverter.mapping.fhir;

/**
 * Created by <NAME>, Ph.D., <<EMAIL>>, on 4/25/17.
 * <p>
 * service-hmlFhirConverter
 * Copyright (c) 2012-2017 National Marrow Donor Program (NMDP)
 * <p>
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 3 of the License, or (at
 * your option) any later version.
 * <p>
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 * <p>
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library;  if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
 * <p>
 * > http://www.fsf.org/licensing/licenses/lgpl.html
 * > http://www.opensource.org/licenses/lgpl-license.php
 */

import io.swagger.model.*;

import org.modelmapper.Converter;
import org.modelmapper.spi.MappingContext;

import org.nmdp.hmlfhirconverter.domain.fhir.BackboneElement;
import org.nmdp.hmlfhirconverter.domain.fhir.Identifier;
import org.nmdp.hmlfhirconverter.domain.fhir.ReferenceSequenceId;
import org.nmdp.hmlfhirconverter.domain.fhir.Sequence;

import java.util.ArrayList;
import java.util.List;

public class SequenceMap implements Converter<Hml, List<Sequence>> {

    @Override
    public List<Sequence> convert(MappingContext<Hml, List<Sequence>> context) {
        if (context.getSource() == null) {
            return null;
        }

        Hml hml = context.getSource();
        Sample sample = hml.getSamples().get(0);
        Typing typing = sample.getTyping();
        ConsensusSequence consensusSequence = typing.getConsensusSequence();
        ReferenceDatabase referenceDatabase = consensusSequence.getReferenceDatabase();

        return createSequences(referenceDatabase);
    }

    private List<Sequence> createSequences(ReferenceDatabase element) {
        List<Sequence> sequences = new ArrayList<>();

        if (element == null) {
            return sequences;
        }

        Sequence nameSequence = createSequence(element.getName());
        Sequence descriptionSequence = createSequence(element.getDescription());
        Sequence versionSequence = createSequence(element.getVersion());
        Sequence uriSequence = createSequence(element.getUri());

        if (nameSequence != null) { sequences.add(nameSequence); }
        if (descriptionSequence != null) { sequences.add(descriptionSequence); }
        if (versionSequence != null) { sequences.add(versionSequence); }
        if (uriSequence != null) { sequences.add(uriSequence); }

        for (Sequence sequence : createSequences(element.getReferenceSequence())) {
            sequences.add(sequence);
        }

        return sequences;
    }

    private List<Sequence> createSequences(ReferenceSequence element) {
        List<Sequence> sequences = new ArrayList();

        if (element == null) {
            return sequences;
        }

        Sequence idSequence = createSequence(element.getReferenceSequenceId());
        Sequence nameSequence = createSequence(element.getName());
        Sequence descriptionSequence = createSequence(element.getDescription());
        Sequence uriSequence = createSequence(element.getUri());
        String strand = element.getStrand();

        if (strand != null && !strand.isEmpty()) {
            Sequence sequence = new Sequence();
            BackboneElement backboneElement = new BackboneElement();

            backboneElement.setStrand(Integer.parseInt(strand));
            sequence.setReferenceSeq(backboneElement);
            sequences.add(sequence);
        }

        if (idSequence != null) { sequences.add(idSequence); }
        if (nameSequence != null) { sequences.add(nameSequence); }
        if (descriptionSequence != null) { sequences.add(descriptionSequence); }
        if (uriSequence != null) { sequences.add(uriSequence); }

        return sequences;
    }

    private Sequence createSequence(String refSeqId) {
        if (refSeqId.isEmpty() || refSeqId == null) {
            return null;
        }

        Sequence sequence = new Sequence();
        BackboneElement backboneElement = new BackboneElement();
        ReferenceSequenceId referenceSequenceId = new ReferenceSequenceId();
        Identifier identifier = new Identifier();

        identifier.setValue(refSeqId);
        referenceSequenceId.setIdentifier(identifier);
        backboneElement.setReferenceSequenceId(referenceSequenceId);
        sequence.setReferenceSeq(backboneElement);

        return sequence;
    }
}
{'fraction_non_alphanumeric': 0.09763008758371973, 'fraction_numerical': 0.007470376094796497, 'mean_word_length': 3.5515826494724503, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '6988500', 'n_tokens_mistral': 2089, 'n_tokens_neox': 1983, 'n_words': 562}