<commit_before>package psidev.psi.mi.jami.json.nary.elements;

import org.json.simple.JSONValue;
import psidev.psi.mi.jami.json.MIJsonUtils;
import psidev.psi.mi.jami.json.nary.IncrementalIdGenerator;
import psidev.psi.mi.jami.model.*;

import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;

/**
 * Json writer for participants
 *
 * @author <NAME> (<EMAIL>)
 * @version $Id$
 * @since <pre>18/07/14</pre>
 */

public class SimpleJsonInteractorWriter implements JsonElementWriter<Interactor>{

    private Writer writer;
    private JsonElementWriter<CvTerm> cvWriter;
    private JsonElementWriter<Xref> identifierWriter;
    private Map<String, Integer> processedInteractors;
    private IncrementalIdGenerator idGenerator;
    private JsonElementWriter<Organism> organismWriter;

    public SimpleJsonInteractorWriter(Writer writer,  Map<String, Integer>processedInteractors) {
        if (writer == null) {
            throw new IllegalArgumentException("The json interactor writer needs a non null Writer");
        }
        this.writer = writer;
        if (processedInteractors == null){
            throw new IllegalArgumentException("The json interactor writer needs a non null map of processed interactors");
        }
        this.processedInteractors = processedInteractors;
    }

    public SimpleJsonInteractorWriter(Writer writer,  Map<String, Integer>processedInteractors, IncrementalIdGenerator idGenerator) {
        if (writer == null) {
            throw new IllegalArgumentException("The json interactor writer needs a non null Writer");
        }
        this.writer = writer;
        if (processedInteractors == null){
            throw new IllegalArgumentException("The json interactor writer needs a non null map of processed interactors");
        }
        this.processedInteractors = processedInteractors;
        this.idGenerator = idGenerator;
    }

    public void write(Interactor object) throws IOException {
        String[] interactorIds = MIJsonUtils.extractInteractorId(object.getPreferredIdentifier(), object);
        String interactorKey = interactorIds[0]+"_"+interactorIds[1];
        // if the interactor has not yet been processed, we write the interactor
        if (!processedInteractors.containsKey(interactorKey)){
            int id = getIdGenerator().nextId();

            // when the interactor is not the first one, we write an element separator
            if (!processedInteractors.isEmpty()){
                MIJsonUtils.writeSeparator(writer);
            }
            processedInteractors.put(interactorKey, id);
            MIJsonUtils.writeStartObject(writer);
            MIJsonUtils.writeProperty("object", "interactor", writer);
            // write accession
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writeProperty("id", Integer.toString(id), writer);

            // write sequence if possible
            if (object instanceof Polymer){
                Polymer polymer = (Polymer) object;
                if (polymer.getSequence() != null){
                    MIJsonUtils.writeSeparator(writer);
                    MIJsonUtils.writeProperty("sequence", JSONValue.escape(polymer.getSequence()), writer);
                }
            }
            // write interactor type
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writePropertyKey("type", writer);
            getCvWriter().write(object.getInteractorType());

            // write organism
            if (object.getOrganism() != null){
                MIJsonUtils.writeSeparator(writer);
                MIJsonUtils.writePropertyKey("organism", writer);
                getOrganismWriter().write(object.getOrganism());
            }

            // write accession
            MIJsonUtils.writeSeparator(writer);
            if (object.getPreferredIdentifier() != null){
                MIJsonUtils.writePropertyKey("identifier", writer);
                getIdentifierWriter().write(object.getPreferredIdentifier());
            }

            if (object instanceof InteractorPool){
                InteractorPool pool = (InteractorPool)object;
                if (!pool.isEmpty()){
                    MIJsonUtils.writeSeparator(writer);
                    MIJsonUtils.writePropertyKey("setComponents", writer);
                    MIJsonUtils.writeOpenArray(writer);

                    Iterator<Interactor> interactorIterator = pool.iterator();
                    while (interactorIterator.hasNext()){
                        Interactor interactor = interactorIterator.next();
                        String[] interactorIds2 = MIJsonUtils.extractInteractorId(interactor.getPreferredIdentifier(), interactor);
                        String interactorKey2 = interactorIds2[0]+"_"+interactorIds2[1];
                        // if the interactor has not yet been processed, we write the interactor
                        if (!processedInteractors.containsKey(interactorKey2)){
                            write(interactor);
                        }
                        else{
                            MIJsonUtils.writeProperty("interactorRef",Integer.toString(this.processedInteractors.get(interactorKey)), writer);
                        }

                        if (interactorIterator.hasNext()){
                            MIJsonUtils.writeSeparator(writer);
                        }
                    }

                    MIJsonUtils.writeEndArray(writer);
                }
            }

            // write label
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writeProperty("label", JSONValue.escape(object.getShortName()), writer);
            MIJsonUtils.writeEndObject(writer);
        }
    }

    public JsonElementWriter<CvTerm> getCvWriter() {
        if (this.cvWriter == null){
            this.cvWriter = new SimpleJsonCvTermWriter(writer);
        }
        return cvWriter;
    }

    public void setCvWriter(JsonElementWriter<CvTerm> cvWriter) {
        this.cvWriter = cvWriter;
    }

    public IncrementalIdGenerator getIdGenerator() {
        if (this.idGenerator == null){
            this.idGenerator = new IncrementalIdGenerator();
        }
        return idGenerator;
    }

    public void setIdGenerator(IncrementalIdGenerator idGenerator) {
        this.idGenerator = idGenerator;
    }<commit_msg>Fixed some bugs in interactor writer<commit_after>package psidev.psi.mi.jami.json.nary.elements;

import org.json.simple.JSONValue;
import psidev.psi.mi.jami.json.MIJsonUtils;
import psidev.psi.mi.jami.json.nary.IncrementalIdGenerator;
import psidev.psi.mi.jami.model.*;

import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.Map;

/**
 * Json writer for participants
 *
 * @author <NAME> (<EMAIL>)
 * @version $Id$
 * @since <pre>18/07/14</pre>
 */

public class SimpleJsonInteractorWriter implements JsonElementWriter<Interactor>{

    private Writer writer;
    private JsonElementWriter<CvTerm> cvWriter;
    private JsonElementWriter<Xref> identifierWriter;
    private Map<String, Integer> processedInteractors;
    private IncrementalIdGenerator idGenerator;
    private JsonElementWriter<Organism> organismWriter;

    public SimpleJsonInteractorWriter(Writer writer,  Map<String, Integer>processedInteractors) {
        if (writer == null) {
            throw new IllegalArgumentException("The json interactor writer needs a non null Writer");
        }
        this.writer = writer;
        if (processedInteractors == null){
            throw new IllegalArgumentException("The json interactor writer needs a non null map of processed interactors");
        }
        this.processedInteractors = processedInteractors;
    }

    public SimpleJsonInteractorWriter(Writer writer,  Map<String, Integer>processedInteractors, IncrementalIdGenerator idGenerator) {
        if (writer == null) {
            throw new IllegalArgumentException("The json interactor writer needs a non null Writer");
        }
        this.writer = writer;
        if (processedInteractors == null){
            throw new IllegalArgumentException("The json interactor writer needs a non null map of processed interactors");
        }
        this.processedInteractors = processedInteractors;
        this.idGenerator = idGenerator;
    }

    public void write(Interactor object) throws IOException {
        String[] interactorIds = MIJsonUtils.extractInteractorId(object.getPreferredIdentifier(), object);
        String interactorKey = interactorIds[0]+"_"+interactorIds[1];
        // if the interactor has not yet been processed, we write the interactor
        if (!processedInteractors.containsKey(interactorKey)){
            int id = getIdGenerator().nextId();

            // when the interactor is not the first one, we write an element separator
            if (!processedInteractors.isEmpty()){
                MIJsonUtils.writeSeparator(writer);
            }
            processedInteractors.put(interactorKey, id);
            MIJsonUtils.writeStartObject(writer);
            MIJsonUtils.writeProperty("object", "interactor", writer);
            // write accession
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writeProperty("id", Integer.toString(id), writer);

            // write sequence if possible
            if (object instanceof Polymer){
                Polymer polymer = (Polymer) object;
                if (polymer.getSequence() != null){
                    MIJsonUtils.writeSeparator(writer);
                    MIJsonUtils.writeProperty("sequence", JSONValue.escape(polymer.getSequence()), writer);
                }
            }
            // write interactor type
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writePropertyKey("type", writer);
            getCvWriter().write(object.getInteractorType());

            // write organism
            if (object.getOrganism() != null){
                MIJsonUtils.writeSeparator(writer);
                MIJsonUtils.writePropertyKey("organism", writer);
                getOrganismWriter().write(object.getOrganism());
            }

            // write accession
            if (object.getPreferredIdentifier() != null){
                MIJsonUtils.writeSeparator(writer);
                MIJsonUtils.writePropertyKey("identifier", writer);
                getIdentifierWriter().write(object.getPreferredIdentifier());
            }

            if (object instanceof InteractorPool){
                InteractorPool pool = (InteractorPool)object;
                if (!pool.isEmpty()){
                    MIJsonUtils.writeSeparator(writer);
                    MIJsonUtils.writePropertyKey("setComponents", writer);
                    MIJsonUtils.writeOpenArray(writer);

                    Iterator<Interactor> interactorIterator = pool.iterator();
                    while (interactorIterator.hasNext()){
                        Interactor interactor = interactorIterator.next();
                        writeInteractorComponent(interactor);

                        if (interactorIterator.hasNext()){
                            MIJsonUtils.writeSeparator(writer);
                        }
                    }

                    MIJsonUtils.writeEndArray(writer);
                }
            }

            // write label
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writeProperty("label", JSONValue.escape(object.getShortName()), writer);
            MIJsonUtils.writeEndObject(writer);
        }
    }

    private void writeInteractorComponent(Interactor object) throws IOException {
        MIJsonUtils.writeStartObject(writer);
        // write accession
        MIJsonUtils.writeProperty("label", JSONValue.escape(object.getShortName()), writer);

        // write sequence if possible
        if (object instanceof Polymer){
            Polymer polymer = (Polymer) object;
            if (polymer.getSequence() != null){
                MIJsonUtils.writeSeparator(writer);
                MIJsonUtils.writeProperty("sequence", JSONValue.escape(polymer.getSequence()), writer);
            }
        }
        // write interactor type
        MIJsonUtils.writeSeparator(writer);
        MIJsonUtils.writePropertyKey("type", writer);
        getCvWriter().write(object.getInteractorType());

        // write organism
        if (object.getOrganism() != null){
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writePropertyKey("organism", writer);
            getOrganismWriter().write(object.getOrganism());
        }

        // write accession
        if (object.getPreferredIdentifier() != null){
            MIJsonUtils.writeSeparator(writer);
            MIJsonUtils.writePropertyKey("identifier", writer);
            getIdentifierWriter().write(object.getPreferredIdentifier());
        }

        // write end object
        MIJsonUtils.writeEndObject(writer);
    }

    public JsonElementWriter<CvTerm> getCvWriter() {
        if (this.cvWriter == null){
            this.cvWriter = new SimpleJsonCvTermWriter(writer);
        }
        return cvWriter;
    }

    public void setCvWriter(JsonElementWriter<CvTerm> cvWriter) {
        this.cvWriter = cvWriter;
    }

    public IncrementalIdGenerator getIdGenerator() {
        if (this.idGenerator == null){
            this.idGenerator = new IncrementalIdGenerator();
        }
        return idGenerator;
    }

    public void setIdGenerator(IncrementalIdGenerator idGenerator) {
        this.idGenerator = idGenerator;
    }
{'fraction_non_alphanumeric': 0.09199618516616535, 'fraction_numerical': 0.0016873303499376421, 'mean_word_length': 2.3063303419839922, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '7065445', 'n_tokens_mistral': 3403, 'n_tokens_neox': 3234, 'n_words': 701}