package org.intermine.bio.dataconversion;

/*
 * Copyright (C) 2002-2012 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.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.biojava.bio.Annotation;
import org.biojava.bio.seq.Sequence;
import org.intermine.metadata.Model;
import org.intermine.model.FastPathObject;
import org.intermine.model.InterMineObject;
import org.intermine.model.bio.BioEntity;
import org.intermine.model.bio.Chromosome;
import org.intermine.model.bio.DataSet;
import org.intermine.model.bio.Location;
import org.intermine.model.bio.Organism;
import org.intermine.model.bio.SequenceFeature;
import org.intermine.objectstore.ObjectStore;
import org.intermine.objectstore.ObjectStoreException;
import org.intermine.util.DynamicUtil;

/**
 * A fasta loader that understand the headers of FlyBase fasta UTR fasta files and can make the
 * appropriate extra objects and references.
 * @author Kim Rutherford
 */
public class FlyBaseUTRFastaLoaderTask extends FlyBaseFeatureFastaLoaderTask
{

    Map<String, Chromosome> chrMap = new HashMap<String, Chromosome>();

    /**
     * {@inheritDoc}
     */
    @Override
    protected void extraProcessing(Sequence bioJavaSequence,
            @SuppressWarnings("unused") org.intermine.model.bio.Sequence flymineSequence,
            BioEntity bioEntity, Organism organism,
            @SuppressWarnings("unused") DataSet dataSet)
        throws ObjectStoreException {
        Annotation annotation = bioJavaSequence.getAnnotation();
        String mrnaIdentifier = bioJavaSequence.getName();

        ObjectStore os = getIntegrationWriter().getObjectStore();
        Model model = os.getModel();
        if (model.hasClassDescriptor(model.getPackageName() + ".UTR")) {
            Class<? extends FastPathObject> cdsCls =
                model.getClassDescriptorByName("UTR").getType();
            if (!DynamicUtil.isInstance(bioEntity, cdsCls)) {
                throw new RuntimeException("the InterMineObject passed to "
                        + "FlyBaseUTRFastaDataLoaderTask.extraProcessing() is not a "
                        + "UTR: " + bioEntity);
            }
            InterMineObject mrna = getMRNA(mrnaIdentifier, organism, model);
            if (mrna != null) {
                Set<? extends InterMineObject> mrnas = new HashSet(Collections.singleton(mrna));
                bioEntity.setFieldValue("transcripts", mrnas);
            }
            String header = (String) annotation.getProperty("description");
            Location loc = getLocationFromHeader(header, (SequenceFeature) bioEntity,
                    organism);
            getDirectDataLoader().store(loc);
        } else {
            throw new RuntimeException("Trying to load UTR sequence but UTR does not exist in the"
                    + " data model");
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getIdentifier(Sequence bioJavaSequence) {
        if (getClassName().endsWith(".FivePrimeUTR")) {
            return bioJavaSequence.getName() + "-5-prime-utr";
        }
        return bioJavaSequence.getName() + "-3-prime-utr";
    }
}

@@ -464,44 +464,73 @@
 et;%0A
-%0A
 import 
-org.biojava.bio.Annotatio
+java.util.regex.Matcher;%0Aimport java.util.regex.Patter
 n;%0A
+%0A
 impo
@@ -547,17 +547,31 @@
 java
-.bio.seq.
+3.core.sequence.Protein
 Sequ
@@ -907,16 +907,57 @@
 ganism;%0A
+import org.intermine.model.bio.Sequence;%0A
 import o
@@ -1551,16 +1551,23 @@
 cessing(
+Protein
 Sequence
@@ -1587,73 +1587,9 @@
 nce,
-%0A            @SuppressWarnings(%22unused%22) org.intermine.model.bio.
+ 
 Sequ
@@ -1665,48 +1665,8 @@
 ism,
-%0A            @SuppressWarnings(%22unused%22)
  Dat
@@ -1729,31 +1729,109 @@
     
-Annotation annotation =
+String header = bioJavaSequence.getOriginalHeader();%0A        // I don't know why this isn't working -
  bio
@@ -1847,25 +1847,32 @@
 nce.getA
-nnotation
+ccession().getID
 ();%0A    
@@ -1903,35 +1903,33 @@
 r = 
-bioJavaSequence.getName();%0A
+header.substring(0, 11); 
 %0A   
@@ -2804,71 +2804,8 @@
     
-String header = (String) annotation.getProperty(%22description%22);
 %0A   
@@ -3221,16 +3221,23 @@
 ntifier(
+Protein
 Sequence
@@ -3252,24 +3252,137 @@
 Sequence) %7B%0A
+        String header = bioJavaSequence.getOriginalHeader();%0A        String accession = header.substring(0, 11);%0A
         if (
@@ -3448,33 +3448,17 @@
 urn 
-bioJavaSequence.getName()
+accession
  + %22
@@ -3501,33 +3501,17 @@
 urn 
-bioJavaSequence.getName()
+accession
  + %22
@@ -3531,10 +3531,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

update UTR task