package org.intermine.bio.dataconversion;

/*
 * Copyright (C) 2002-2010 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.HashSet;
import java.util.Iterator;
import java.util.Set;

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


/**
 * DataConverter to load WormBase gene identifiers from genes2molecular_names.txt.
 *
 * @author Richard Smith
 */
public class WormBaseIdentifiersConverter extends BioFileConverter
{
    protected Item worm;
    private Set<String> identifiers = new HashSet<String>();

    /**
     * Constructor
     * @param writer the ItemWriter used to handle the resultant items
     * @param model the Model
      */
    public WormBaseIdentifiersConverter(ItemWriter writer, Model model) {
        super(writer, model, "WormBase", "WormBase genes");
    }

    /**
     * Read each line from flat file, create genes and synonyms.
     *
     * {@inheritDoc}
     */
    public void process(Reader reader) throws Exception {
        Iterator<?> lineIter = FormattedTextParser.parseCsvDelimitedReader(reader);

        // data is in format:
        // primaryIdentifier | identifier | symbol

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

            if (line.length < 3 || line[0].startsWith("#")) {
                continue;
            }

//            if (line.length < 3) {
//                throw new RuntimeException("Line does not have enough elements: "
//                                           + Arrays.asList(line));
//            }
            String primaryidentifier = line[0];
            String identifier = line[2];
            String symbol = line[1];

            Item gene = createItem("Gene");
            if (!StringUtils.isEmpty(primaryidentifier)
                    && !identifiers.contains(primaryidentifier)) {
                gene.setAttribute("primaryIdentifier", primaryidentifier);
                identifiers.add(primaryidentifier);
            }
            if (!StringUtils.isEmpty(identifier) && !identifiers.contains(identifier)) {
                gene.setAttribute("secondaryIdentifier", identifier);
                identifiers.add(identifier);
            }
            if (!StringUtils.isEmpty(symbol) && !identifiers.contains(symbol)) {
                gene.setAttribute("symbol", symbol);
                // per Rachel.  We can't seem to get the gene names out of wormmart.
                gene.setAttribute("name", symbol);
                identifiers.add(symbol);
            }

            gene.setReference("organism", getOrganism("6239"));
            store(gene);
        }
    }
}

@@ -378,64 +378,16 @@
 til.
-HashSet;%0Aimport java.util.Iterator;%0Aimport java.util.Set
+Iterator
 ;%0A%0Ai
@@ -815,69 +815,8 @@
 orm;
-%0A    private Set%3CString%3E identifiers = new HashSet%3CString%3E();
 %0A%0A  
@@ -2058,72 +2058,8 @@
 ier)
-%0A                    && !identifiers.contains(primaryidentifier)
 ) %7B%0A
@@ -2137,60 +2137,8 @@
 r);%0A
-                identifiers.add(primaryidentifier);%0A
     
@@ -2199,45 +2199,8 @@
 ier)
- && !identifiers.contains(identifier)
 ) %7B%0A
@@ -2273,53 +2273,8 @@
 r);%0A
-                identifiers.add(identifier);%0A
     
@@ -2331,41 +2331,8 @@
 bol)
- && !identifiers.contains(symbol)
 ) %7B%0A
@@ -2524,49 +2524,8 @@
 l);%0A
-                identifiers.add(symbol);%0A
     

revert [23837]