symbol, getDataSourceId("UniProt"));
symbol, dbId);
    protected Collection translateItem(Item srcItem)        throws ObjectStoreException, InterMineException {        // This Item should be an EntryType and only have proteins associated with one organism.        if (!(SRC_NS + "Entry").equals(srcItem.getClassName())) {            throw new IllegalArgumentException("Attempted to translate and Uniprot source Item that"                                               + " is not of class " + SRC_NS + "Entry");        }        // First things first: find out the taxonid of the organism of this entry.        int taxonId = 0;        List organismList = getItemsInCollection(srcItem.getCollection("organisms"));        Iterator organismIter = organismList.iterator();        while (organismIter.hasNext()) {            // Drosophila melanogaster = 7227            // Caenorhabditis elegans 6239            // Anopheles gambiae = 7165            Item organism = (Item) organismIter.next();            Iterator dbRefIter = getItemsInCollection(                                      organism.getCollection("dbReferences")).iterator();            while (dbRefIter.hasNext()) {                Item dbReference = (Item) dbRefIter.next();                String type = getAttributeValue(dbReference, "type");                if ("NCBI Taxonomy".equals(type)) {                    String taxonString = getAttributeValue(dbReference, "id");                    if (taxonId != 0) {                        throw new IllegalStateException("Attempting to set taxon id to "                                             + taxonString + " when it is already " + taxonId);                    }                    taxonId = Integer.parseInt(taxonString);                }            }        }        if (taxonId != 0) {            Set retval = new HashSet();            Reference organismReference = new Reference("organism", getOrganismId(taxonId));            // 1. create Protein, same for all organisms            // <entry>            Item protein = createItem(tgtNs + "Protein", "");            // find name and set all names as synonyms            List proteinNames = getItemsInCollection(srcItem.getCollection("names"));            String proteinName = (String) getAttributeValue((Item) proteinNames.get(0), "name");            protein.addAttribute(new Attribute("identifier", proteinName));            protein.addReference(organismReference);            retval.add(createSynonym(protein.getIdentifier(), "identifier",                                     proteinName, getDataSourceId("UniProt")));            // find primary accession and set others as synonyms <entry><accession>*            List srcAccessions = getItemsInCollection(srcItem.getCollection("accessions"));            if (srcAccessions.isEmpty()) {                // do not create store Protein if no primary accession                LOG.info("Entry " + proteinName + " does not have any accessions");                return Collections.EMPTY_SET;            } else {                // primary accession is first in list                Item srcPrimaryAccession = (Item) srcAccessions.get(0);                protein.addAttribute(new Attribute("primaryAccession", getAttributeValue(                                                           srcPrimaryAccession, "accession")));                // all accessions should be Synonyms                Iterator srcAccIter = srcAccessions.iterator();                while (srcAccIter.hasNext()) {                    Item srcAccession = (Item) srcAccIter.next();                    String srcAccessionString = getAttributeValue(srcAccession, "accession");                    retval.add(createSynonym(protein.getIdentifier(), "accession",                                             srcAccessionString, getDataSourceId("UniProt")));                }            }            // add UniProt Database to evidence collection            ReferenceList evidence = new ReferenceList("evidence", new ArrayList());            evidence.addRefId(getDataSourceId("UniProt"));            protein.addCollection(evidence);            // 2. first name should be protein name, all should be Synonyms            // <entry><protein><name>*            Item srcProtein = ItemHelper.convert(srcItemReader.getItemById(srcItem                                                  .getReference("protein").getRefId()));            if (srcProtein != null) {                List srcProteinNames = getItemsInCollection(srcProtein.getCollection("names"));                Iterator srcProtNameIter = srcProteinNames.iterator();                while (srcProtNameIter.hasNext()) {                    Item srcProteinName = (Item) srcProtNameIter.next();                    String srcProteinNameStr = getAttributeValue(srcProteinName, "name");                    if (!protein.hasAttribute("name")) {                        protein.setAttribute("name", srcProteinNameStr);                    }                    String srcProteinNameEvidence = getAttributeValue(srcProteinName,                                                                      "evidence");                    if (srcProteinNameEvidence != null) {                        srcProteinNameStr += " (Evidence " + srcProteinNameEvidence + ")";                    }                    retval.add(createSynonym(protein.getIdentifier(), "symbol",                                             srcProteinNameStr, getDataSourceId("UniProt")));                }            }            // 3. get collection of Comments            // <entry><comment>*            List srcComments = getItemsInCollection(srcItem.getCollection("comments"));            ReferenceList comments = new ReferenceList("comments", new ArrayList());            Iterator srcComIter = srcComments.iterator();            while (srcComIter.hasNext()) {                Item srcComment = (Item) srcComIter.next();                String srcCommentType = getAttributeValue(srcComment, "type");                String srcCommentText = getAttributeValue(srcComment, "text");                if ((srcCommentType != null) && (srcCommentText != null)) {                    Item comment = createItem(tgtNs + "Comment", "");                    comment.addAttribute(new Attribute("type", srcCommentType));                    comment.addAttribute(new Attribute("text", srcCommentText));                    comment.addReference(uniprotDataSetRef);                    comments.addRefId(comment.getIdentifier());                    retval.add(comment);                }            }            if (comments.getRefIds().size() > 0) {                protein.addCollection(comments);            }            // 4. create a collection of Publications related to this protein            // <entry><reference>*            List srcReferences = getItemsInCollection(srcItem.getCollection("references"));            ReferenceList publications = null;            Iterator srcRefIter = srcReferences.iterator();            while (srcRefIter.hasNext()) {                Item srcReference = (Item) srcRefIter.next();                Item srcCitation = ItemHelper.convert(srcItemReader.getItemById(srcReference                                                       .getReference("citation").getRefId()));                List srcDbReferences = getItemsInCollection(srcCitation                                                            .getCollection("dbReferences"));                Iterator srcDbRefIter = srcDbReferences.iterator();                while (srcDbRefIter.hasNext()) {                    Item srcDbReference = (Item) srcDbRefIter.next();                    String type = getAttributeValue(srcDbReference, "type");                    if ("PubMed".equals(type)) {                        String pubMedString = new String(getAttributeValue(srcDbReference,                                                                           "id"));                        String publicationId = (String) pubMedIdToPublicationId                            .get(pubMedString);                        pubLinkCount++;                        if (publicationId == null) {                            Item publication = createItem(tgtNs + "Publication", "");                            publication.addAttribute(new Attribute("pubMedId", pubMedString));                            retval.add(publication);                            publicationId = publication.getIdentifier();                            pubMedIdToPublicationId.put(pubMedString,                                                        publicationId);                            if (pubMedIdToPublicationId.size() % 100 == 0) {                                LOG.info("Processed " + pubMedIdToPublicationId.size()                                         + " publications, with " + pubLinkCount                                         + " references to publications");                            }                        }                        if (publications == null) {                            publications = new ReferenceList("publications", new ArrayList());                            protein.addCollection(publications);                        }                        publications.addRefId(publicationId);                    }                }            }            // 5. create a Sequence object and reference from Protein            // <entry><sequence>            Reference seqRef = srcItem.getReference("sequence");            if (seqRef != null) {                Item srcSeq = ItemHelper.convert(srcItemReader.getItemById(seqRef.getRefId()));                if (srcSeq != null) {                    Item sequence = createItem(tgtNs + "Sequence", "");                    String residues = srcSeq.getAttribute("sequence").getValue();                    sequence.addAttribute(new Attribute("residues", residues));                    sequence.addAttribute(new Attribute("length", "" + residues.length()));                    retval.add(sequence);                    protein.addReference(new Reference("sequence", sequence.getIdentifier()));                }            }            // TODO many dbReference types are present in uniprot records - e.g. Pfam, InterPro.            // Specifc code could be add to create additional objects            // 6. now try to create reference to gene, choice of identifier is organism specific            // <entry><gene>*            // <entry><dbReference>            // a protein can be related to multiple genes            List srcGenes = getItemsInCollection(srcItem.getCollection("genes"));            ReferenceList geneCollection = null;            Iterator srcGeneIter = srcGenes.iterator();            while (srcGeneIter.hasNext()) {                Item srcGene = (Item) srcGeneIter.next();                List srcGeneNames = getItemsInCollection(srcGene.getCollection("names"));                // Gene.identifier = <entry><gene><name type="ORF">                String geneIdentifier = null;                // Gene.name = <entry><gene><name type="primary">                String primaryGeneName = null;                Set geneNames = new HashSet();                {                    Iterator srcGeneNameIter = srcGeneNames.iterator();                    while (srcGeneNameIter.hasNext()) {                        Item srcGeneName = (Item) srcGeneNameIter.next();                        if ("primary".equals(getAttributeValue(srcGeneName, "type"))) {                            primaryGeneName = new String(getAttributeValue(srcGeneName,                                                                           "name"));                        } else if ("ORF".equals(getAttributeValue(srcGeneName, "type"))) {                            geneIdentifier = new String(getAttributeValue(srcGeneName, "name"));                        }                        geneNames.add(new String(getAttributeValue(srcGeneName, "name")));                    }                }                String dbId = null;                boolean createGene = false;                // geneOrganismDbId = <entry><dbReference><type="FlyBase/WormBase/..">                //             where designation = primary gene name                String geneOrganismDbId = null;                if (taxonId == 7227) { // D. melanogaster                    geneOrganismDbId = getDataSourceReferenceValue(srcItem, "FlyBase", geneNames);                    // For fly data use CGxxx as key instead of FBgnxxx                    if (geneIdentifier != null) {                        createGene = true;                        dbId = getDataSourceId("FlyBase");                    }                } else if (taxonId == 6239) { // C. elegans                    geneOrganismDbId = getDataSourceReferenceValue(srcItem, "WormBase", geneNames);                    if (geneOrganismDbId != null) {                        createGene = true;                        dbId = getDataSourceId("WormBase");                    }                } else if (taxonId == 180454) { // A. gambiae str. PEST                    // no organismDbId and no specific dbxref to enembl - assume that geneIdentifier                    // is always ensembl gene stable id and set organismDbId to be identifier                    if (geneIdentifier != null) {                        createGene = true;                        geneOrganismDbId = geneIdentifier;                        dbId = getDataSourceId("ensembl");                    }                }                // output gene identifier details                if (outputIdentifiers) {                    try {                        fw.write(taxonId + "\tprotein: " + proteinName + "\tname: "                                 + primaryGeneName + "\tidentifier: " + geneIdentifier                                 + "\tgeneOrganismDbId: " + geneOrganismDbId                                 + System.getProperty("line.separator"));                    } catch (IOException e) {                        throw new InterMineException(e);                    }                }                // uniprot data source has primary key of Gene.organismDbId                // only create gene if a value was found                if (createGene) {                    // may alrady have created this gene                    String geneItemId = (String) geneIdentifierToId.get(geneOrganismDbId);                    ReferenceList geneSynonyms = new ReferenceList("synonyms", new ArrayList());                    // UniProt sometimes has same identifier paired with two organismDbIds                    // causes problems merging other data sources.  Simple check to prevent                    // creating a gene with a duplicate identifier.                    // log problem genes                    boolean isDuplicateIdentifier = false;                    if ((geneItemId == null) && geneIdentifiers.contains(geneIdentifier)) {                        LOG.warn("already created a gene for identifier: " + geneIdentifier                                 + " with different organismDbId, discarding this one");                        isDuplicateIdentifier = true;                    }                    if ((geneItemId == null) && !isDuplicateIdentifier) {                        Item gene = createItem(tgtNs + "Gene", "");                        if (geneOrganismDbId != null) {                            if (geneOrganismDbId.equals("")) {                                LOG.info("geneOrganismDbId was empty string");                            }                            gene.addAttribute(new Attribute("organismDbId", geneOrganismDbId));                            Item synonym = createSynonym(gene.getIdentifier(), "identifier",                                                         geneOrganismDbId, dbId);                            geneSynonyms.addRefId(synonym.getIdentifier());                            retval.add(synonym);                        }                        if (geneIdentifier != null) {                            gene.addAttribute(new Attribute("identifier", geneIdentifier));                            // don't create duplicate synonym                            if (!geneIdentifier.equals(geneOrganismDbId)) {                                Item synonym = createSynonym(gene.getIdentifier(), "identifier",                                                             geneIdentifier, dbId);                                geneSynonyms.addRefId(synonym.getIdentifier());                                retval.add(synonym);                            }                            // keep a track of non-null gene identifiers                            geneIdentifiers.add(geneIdentifier);                        }                        // Problem with gene names for drosophila - ignore                        if (primaryGeneName != null && taxonId != 7227) {                            gene.addAttribute(new Attribute("symbol", primaryGeneName));                        }                        gene.addReference(organismReference);                        // add UniProt Database to evidence collection                        ReferenceList evidence1 = new ReferenceList("evidence", new ArrayList());                        evidence1.addRefId(getDataSourceId("UniProt"));                        gene.addCollection(evidence1);                        // everything in <entry><gene><name>* becomes a Synonym                        Iterator srcGeneNameIter = srcGeneNames.iterator();                        while (srcGeneNameIter.hasNext()) {                            Item srcGeneName = (Item) srcGeneNameIter.next();                            String type = getAttributeValue(srcGeneName, "type");                            String symbol = getAttributeValue(srcGeneName, "name");                            // synonym already created for ORF as identifer                            if (!type.equals("ORF")) {                                Item synonym = createSynonym(gene.getIdentifier(),                                    (type.equals("primary") || type.equals("synonym"))                                                             ? "symbol" : type,                                                             symbol, getDataSourceId("UniProt"));                                geneSynonyms.addRefId(synonym.getIdentifier());                                retval.add(synonym);                            }                        }                        gene.addCollection(geneSynonyms);                        geneItemId = gene.getIdentifier();                        geneIdentifierToId.put(geneOrganismDbId, geneItemId);                        retval.add(gene);                    }                    // TODO untidy - sould just do this check once                    if (!isDuplicateIdentifier) {                        if (geneCollection == null) {                            geneCollection = new ReferenceList("genes", new ArrayList());                            protein.addCollection(geneCollection);                        }                        geneCollection.addRefId(geneItemId);                    }                }            }            retval.add(protein);            return retval;        }        return Collections.EMPTY_SET;    }