package org.intermine.bio.dataconversion;

/*
 * Copyright (C) 2002-2016 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.io.Reader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.intermine.dataconversion.ItemWriter;
import org.intermine.metadata.Model;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.util.FormattedTextParser;
import org.intermine.xml.full.Item;


/**
 * Load allele disease annotation
 *
 * @author Julie Sullivan
 */
public class FlybaseAllelesConverter extends BioFileConverter
{


    private static final String DATASET_TITLE = "FlyBase";
    private static final String DATA_SOURCE_NAME = "FlyBase Human disease model data set";
    private Map<String, String> alleles = new HashMap<String, String>();

    /**
     * Constructor
     * @param writer the ItemWriter used to handle the resultant items
     * @param model the Model
     */
    public FlybaseAllelesConverter(ItemWriter writer, Model model) {
        super(writer, model, DATA_SOURCE_NAME, DATASET_TITLE);
    }

    /**
    *
    *
    * {@inheritDoc}
    */
    public void process(Reader reader) throws Exception {
        Iterator<?> lineIter = FormattedTextParser.parseTabDelimitedReader(reader);

        while (lineIter.hasNext()) {
            String[] line = (String[]) lineIter.next();

            if (line.length < 6) {
                continue;
            }

            String alleleIdentifier = line[0];
            String symbol = line[1];
            String alleleRefId = getAllele(alleleIdentifier, symbol);

            String qualifier = line[2];
            //String diseaseName = line[3];
            String diseaseIdentifier = line[4];
            String evidence = line[5];

            Item term = createItem("DOTerm");
            if (StringUtils.isNotEmpty(diseaseIdentifier)) {
                term.setAttribute("identifier", diseaseIdentifier);
            }
            store(term);

            Item evidenceTerm = createItem("DOEvidence");
            if (StringUtils.isNotEmpty(evidence)) {
                evidenceTerm.setAttribute("evidence", evidence);
            }
            store(evidenceTerm);

            Item doAnnotation = createItem("DOAnnotation");
            doAnnotation.setReference("subject", alleleRefId);
            if (StringUtils.isNotEmpty(qualifier)) {
                doAnnotation.setAttribute("qualifier", qualifier);
            }
            doAnnotation.setReference("ontologyTerm", term);

        }
    }

    private String getAllele(String primaryIdentifier, String symbol) throws ObjectStoreException {
        String refId = alleles.get(primaryIdentifier);
        if (refId == null) {
            Item item = createItem("Allele");
            item.setAttribute("primaryIdentifier", primaryIdentifier);
            item.setAttribute("symbol", symbol);
            store(item);
            refId = item.getIdentifier();
            alleles.put(primaryIdentifier, refId);
        }
        return refId;
    }
}

@@ -1061,16 +1061,90 @@
 ring%3E();
+%0A    private Map%3CString, String%3E diseases = new HashMap%3CString, String%3E();
 %0A%0A    /*
@@ -2115,207 +2115,52 @@
     
-Item term = createItem(%22DOTerm%22);%0A            if (StringUtils.isNotEmpty(diseaseIdentifier)) %7B%0A                term.setAttribute(%22identifier%22, diseaseIdentifier);%0A            %7D%0A            store(term
+String doTerm = getDisease(diseaseIdentifier
 );%0A%0A
@@ -2693,17 +2693,19 @@
 yTerm%22, 
-t
+doT
 erm);%0A%0A 
@@ -3133,24 +3133,24 @@
 entifier();%0A
-
             
@@ -3225,11 +3225,422 @@
 d;%0A    %7D
+%0A%0A    private String getDisease(String identifier) throws ObjectStoreException %7B%0A        String refId = diseases.get(identifier);%0A        if (refId == null) %7B%0A            Item item = createItem(%22DOTerm%22);%0A            item.setAttribute(%22identifier%22, identifier);%0A            store(item);%0A            refId = item.getIdentifier();%0A            diseases.put(identifier, refId);%0A        %7D%0A        return refId;%0A    %7D
 %0A%7D%0A

remove duplicate terms. Refs #707