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>();
    private Map<String, String> diseases = 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];

            String doTerm = getDisease(diseaseIdentifier);

            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", doTerm);
            store(doAnnotation);
        }
    }

    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;
    }

    private String getDisease(String identifier) throws ObjectStoreException {
        String refId = diseases.get(identifier);
        if (refId == null) {
            Item item = createItem("DOTerm");
            item.setAttribute("identifier", identifier);
            store(item);
            refId = item.getIdentifier();
            diseases.put(identifier, refId);
        }
        return refId;
    }
}

@@ -1013,22 +1013,20 @@
 String, 
-String
+Item
 %3E allele
@@ -1045,30 +1045,28 @@
 Map%3CString, 
-String
+Item
 %3E();%0A    pri
@@ -1421,19 +1421,8 @@
 **%0A 
-   *%0A    *%0A
     
@@ -1437,16 +1437,330 @@
 ritDoc%7D%0A
+     */%0A    @Override%0A    public void close()  %7B%0A        for (Item allele : alleles.values()) %7B%0A            try %7B%0A                store(allele);%0A            %7D catch (ObjectStoreException e) %7B%0A                throw new RuntimeException(e);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     *%0A     *%0A     * %7B@inheritDoc%7D%0A 
     */%0A 
@@ -2171,26 +2171,19 @@
     
-String
+Item
  allele
-RefId
  = g
@@ -2793,13 +2793,8 @@
 lele
-RefId
 );%0A 
@@ -3047,30 +3047,28 @@
     private 
-String
+Item
  getAllele(S
@@ -3145,28 +3145,25 @@
         
-String refId
+Item item
  = allel
@@ -3193,37 +3193,36 @@
 r);%0A        if (
-refId
+item
  == null) %7B%0A    
@@ -3221,37 +3221,32 @@
 ) %7B%0A            
-Item 
 item = createIte
@@ -3382,75 +3382,8 @@
 l);%0A
-            store(item);%0A            refId = item.getIdentifier();%0A
     
@@ -3413,37 +3413,36 @@
 maryIdentifier, 
-refId
+item
 );%0A        %7D%0A   
@@ -3445,37 +3445,36 @@
 %0A        return 
-refId
+item
 ;%0A    %7D%0A%0A    pri

set all relationships Refs #707