1 package uk.ac.ebi.intenz.tools.importer; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.SQLException; 6 import java.util.ArrayList; 7 import java.util.Iterator; 8 import java.util.List; 9 import java.util.SortedSet; 10 import java.util.TreeSet; 11 12 import org.apache.log4j.Logger; 13 14 import uk.ac.ebi.biobabel.util.db.OracleDatabaseInstance; 15 import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant; 16 import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant; 17 import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant; 18 import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry; 19 import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink; 20 import uk.ac.ebi.intenz.mapper.EnzymeEntryMapper; 21 import uk.ac.ebi.intenz.mapper.EnzymeLinkMapper; 22 import uk.ac.ebi.kraken.interfaces.uniprot.UniProtEntry; 23 import uk.ac.ebi.kraken.interfaces.uniprot.UniProtId; 24 import uk.ac.ebi.kraken.uuw.services.remoting.Attribute; 25 import uk.ac.ebi.kraken.uuw.services.remoting.AttributeIterator; 26 import uk.ac.ebi.kraken.uuw.services.remoting.Query; 27 import uk.ac.ebi.kraken.uuw.services.remoting.UniProtJAPI; 28 import uk.ac.ebi.kraken.uuw.services.remoting.UniProtQueryBuilder; 29 import uk.ac.ebi.kraken.uuw.services.remoting.UniProtQueryService; 30 31 public class KrakenLinkImporter extends Importer { 32 33 private static Logger LOGGER = Logger.getLogger(KrakenLinkImporter.class); 34 35 private Connection impCon; 36 private List<EnzymeEntry> enzymeEntries; 37 private UniProtQueryService uniProtQueryService; 38 39 protected KrakenLinkImporter() throws IOException { 40 super(); 41 } 42 43 @Override 44 protected void setup() throws Exception { 45 LOGGER.debug("Opening IntEnz import database connections"); 46 impCon = OracleDatabaseInstance 47 .getInstance(importerProps.getProperty("intenz.database")) 48 .getConnection(); 49 setupKraken(); 50 } 51 52 protected void setupKraken(){ 53 uniProtQueryService = UniProtJAPI.factory.getUniProtQueryService(); 54 } 55 56 @Override 57 protected void importData() throws Exception { 58 EnzymeEntryMapper mapper = new EnzymeEntryMapper(); 59 enzymeEntries = mapper.findAll(impCon); 60 LOGGER.debug("Obtained enzymes to be updated."); 61 62 Iterator<EnzymeEntry> iter = enzymeEntries.iterator(); 63 while ( iter.hasNext() ) { 64 EnzymeEntry entry = (EnzymeEntry) iter.next(); 65 String ec = entry.getEc().toString(); 66 entry.setLinks(getKrakenLinks(ec)); 67 } 68 } 69 70 protected SortedSet<EnzymeLink> getKrakenLinks(String ec) { 71 Query query = UniProtQueryBuilder.buildECNumberQuery(ec); 72 Query queryReviewed = UniProtQueryBuilder.setReviewedEntries(query); 73 AttributeIterator<UniProtEntry> it = 74 uniProtQueryService.getAttributes(queryReviewed, "ognl:uniProtId"); 75 SortedSet<EnzymeLink> updatedUniProtXrefs = new TreeSet<EnzymeLink>(); 76 for (Attribute attribute : it) { 77 String accession = attribute. getAccession(); 78 final UniProtId uniprotId = (UniProtId) attribute.getValue(); 79 EnzymeLink enzymeLink = EnzymeLink.valueOf(XrefDatabaseConstant.SWISSPROT , 80 XrefDatabaseConstant.SWISSPROT.getUrl(), 81 accession, 82 uniprotId.getValue(), 83 EnzymeSourceConstant.INTENZ, 84 EnzymeViewConstant.SIB_INTENZ); 85 updatedUniProtXrefs.add(enzymeLink); 86 } 87 return updatedUniProtXrefs; 88 } 89 90 @Override 91 protected void loadData() throws Exception { 92 LOGGER.debug("Load data"); 93 Iterator<EnzymeEntry> iter = enzymeEntries.iterator(); 94 EnzymeLinkMapper mapper = new EnzymeLinkMapper(); 95 while (iter.hasNext()) { 96 EnzymeEntry entry = (EnzymeEntry) iter.next(); 97 mapper.deleteByCodeXref(entry.getId(), XrefDatabaseConstant.SWISSPROT.getDatabaseCode(), impCon); 98 mapper.insert(new ArrayList<EnzymeLink>(entry.getLinks()), entry.getId(), entry.getStatus(), impCon); 99 } 100 } 101 102 @Override 103 protected void destroy() { 104 LOGGER.debug("Closing IntEnz import connection..."); 105 try { 106 impCon.close(); 107 } catch (SQLException e) { 108 LOGGER.error(e); 109 } 110 } 111 112 }