<commit_before>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.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.intermine.bio.dataconversion.IdResolver;
import org.intermine.bio.dataconversion.IdResolverService;
import org.intermine.bio.io.gff3.GFF3Record;
import org.intermine.metadata.Model;
import org.intermine.xml.full.Item;

/**
 * @author "<NAME>"
 */
public class MirandaGFF3RecordHandler extends GFF3RecordHandler
{
    private Map<String, Item> targets = new HashMap<String, Item>();
    private Map<String, Item> miRNAgenes = new HashMap<String, Item>();
    private Set<String> problems = new HashSet<String>();
    private IdResolver rslv;

    protected static final Logger LOG = Logger.getLogger(MirandaGFF3RecordHandler.class);

    /**
     * Create a new MirandaGFF3RecordHandler
     * @param tgtModel the model for which items will be created
     */
    public MirandaGFF3RecordHandler(Model tgtModel) {
        super(tgtModel);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void process(GFF3Record record) {
    	// Id resolver
    	if (rslv == null) {
            rslv = IdResolverService.getFlyIdResolver(Arrays.asList(new String[]{"gene", "mRNA"}));
        }
    	
        Item feature = getFeature();
        feature.setClassName("MiRNATarget");
        String geneName = record.getAttributes().get("Name").iterator().next();
        String targetName = record.getAttributes().get("target").iterator().next();
        feature.setAttribute("pvalue", record.getAttributes().get("pvalue").iterator().next());
        Item gene = getMiRNAGene(geneName);
        Item target = getTarget(targetName);
        if (gene != null) {
            feature.setReference("mirnagene", gene);
        }
        if (target != null) {
            feature.setReference("target", target);
        }
    }

    private Item getTarget(String targetName) {
        Item target = null;
        String primaryIdentifier = null;
        int resCount = rslv.countResolutions("7227", "mRNA", targetName);
        if (resCount == 1) {
            primaryIdentifier = rslv.resolveId("7227", "mRNA", targetName).iterator().next();
            target = targets.get(primaryIdentifier);
            if (target == null) {
                target = converter.createItem("MRNA");
                target.setAttribute("primaryIdentifier", primaryIdentifier);
                target.setReference("organism", getOrganism().getIdentifier());
                targets.put(primaryIdentifier, target);
                addEarlyItem(target);
            }
        } else {
            LOG.info("RESOLVER: failed to resolve mRNA to one identifier, ignoring mRNA: "<commit_msg>checkstyle<commit_after>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.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.intermine.bio.dataconversion.IdResolver;
import org.intermine.bio.dataconversion.IdResolverService;
import org.intermine.bio.io.gff3.GFF3Record;
import org.intermine.metadata.Model;
import org.intermine.xml.full.Item;

/**
 * @author "<NAME>"
 */
public class MirandaGFF3RecordHandler extends GFF3RecordHandler
{
    private Map<String, Item> targets = new HashMap<String, Item>();
    private Map<String, Item> miRNAgenes = new HashMap<String, Item>();
    private Set<String> problems = new HashSet<String>();
    private IdResolver rslv;

    protected static final Logger LOG = Logger.getLogger(MirandaGFF3RecordHandler.class);

    /**
     * Create a new MirandaGFF3RecordHandler
     * @param tgtModel the model for which items will be created
     */
    public MirandaGFF3RecordHandler(Model tgtModel) {
        super(tgtModel);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void process(GFF3Record record) {
        // Id resolver
        if (rslv == null) {
            rslv = IdResolverService.getFlyIdResolver(Arrays.asList(new String[]{"gene", "mRNA"}));
        }
        
        Item feature = getFeature();
        feature.setClassName("MiRNATarget");
        String geneName = record.getAttributes().get("Name").iterator().next();
        String targetName = record.getAttributes().get("target").iterator().next();
        feature.setAttribute("pvalue", record.getAttributes().get("pvalue").iterator().next());
        Item gene = getMiRNAGene(geneName);
        Item target = getTarget(targetName);
        if (gene != null) {
            feature.setReference("mirnagene", gene);
        }
        if (target != null) {
            feature.setReference("target", target);
        }
    }

    private Item getTarget(String targetName) {
        Item target = null;
        String primaryIdentifier = null;
        int resCount = rslv.countResolutions("7227", "mRNA", targetName);
        if (resCount == 1) {
            primaryIdentifier = rslv.resolveId("7227", "mRNA", targetName).iterator().next();
            target = targets.get(primaryIdentifier);
            if (target == null) {
                target = converter.createItem("MRNA");
                target.setAttribute("primaryIdentifier", primaryIdentifier);
                target.setReference("organism", getOrganism().getIdentifier());
                targets.put(primaryIdentifier, target);
                addEarlyItem(target);
            }
        } else {
            LOG.info("RESOLVER: failed to resolve mRNA to one identifier, ignoring mRNA: "
{'fraction_non_alphanumeric': 0.1192095885973437, 'fraction_numerical': 0.008422416585681892, 'mean_word_length': 2.9131812420785805, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '5401005', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1701, 'n_words': 410}