View Javadoc

1   package uk.ac.ebi.intenz.tools.importer;
2   
3   import java.io.IOException;
4   import java.sql.Connection;
5   import java.sql.ResultSet;
6   import java.sql.SQLException;
7   import java.sql.Statement;
8   import java.util.ArrayList;
9   import java.util.Hashtable;
10  import java.util.Iterator;
11  import java.util.List;
12  import java.util.Set;
13  import java.util.SortedSet;
14  import java.util.TreeSet;
15  
16  import org.apache.log4j.Logger;
17  
18  import uk.ac.ebi.biobabel.util.db.OracleDatabaseInstance;
19  import uk.ac.ebi.intenz.domain.constants.EnzymeSourceConstant;
20  import uk.ac.ebi.intenz.domain.constants.EnzymeViewConstant;
21  import uk.ac.ebi.intenz.domain.constants.XrefDatabaseConstant;
22  import uk.ac.ebi.intenz.domain.enzyme.EnzymeEntry;
23  import uk.ac.ebi.intenz.domain.enzyme.EnzymeLink;
24  import uk.ac.ebi.intenz.mapper.EnzymeEntryMapper;
25  import uk.ac.ebi.intenz.mapper.EnzymeLinkMapper;
26  
27  /**
28   * Describe class <code>GoLinkImporter</code> here.
29   *
30   * @author <a href="mailto:rafalcan@ebi.ac.uk">Rafael Alcantara</a>
31   * @version 1.0
32   */
33  public class GoLinkImporter extends Importer {
34  
35      private Logger LOGGER = Logger.getLogger(GoLinkImporter.class);
36  
37      private Connection impCon = null, expCon = null;
38  
39      private List<EnzymeEntry> enzymeEntries;
40  
41      private static String EC2GO_QUERY = "SELECT x.db_id, t.go_id, t.name"
42          + " FROM go.terms t, go.xrefs x"
43          + " WHERE x.go_id = t.go_id AND x.db_code = 'EC' AND x.db_id NOT LIKE '%-'";
44  
45      public GoLinkImporter() throws IOException{
46  		super();
47      }
48  
49      /**
50       * Open database connections.
51       * @exception Exception if an error occurs
52       */
53      public final void setup() throws Exception {
54          Class.forName("oracle.jdbc.driver.OracleDriver");
55          impCon = OracleDatabaseInstance
56          	.getInstance(importerProps.getProperty("intenz.database"))
57          	.getConnection();
58          expCon = OracleDatabaseInstance
59          	.getInstance(importerProps.getProperty("go.database"))
60          	.getConnection();
61      }
62  
63      /**
64       * Gets the list of enzyme entries and the updated GO links.
65       * @exception Exception if an error occurs
66       */
67      public final void importData() throws Exception {
68          EnzymeEntryMapper mapper = new EnzymeEntryMapper();
69          enzymeEntries = mapper.findAll(impCon);
70          LOGGER.debug("Obtained enzymes to be updated.");
71  
72          Hashtable<String, SortedSet<EnzymeLink>> goLinksTable = getGoLinks();
73          LOGGER.debug("Obtained GO links.");
74  
75          Iterator<EnzymeEntry> iter = enzymeEntries.iterator();
76          while ( iter.hasNext() ) {
77             EnzymeEntry entry = (EnzymeEntry) iter.next();
78             String ec = entry.getEc().toString();
79             if (goLinksTable.containsKey(ec))
80                 entry.setLinks((SortedSet<EnzymeLink>) goLinksTable.get(ec));
81          }
82      }
83  
84      /**
85       * @return A table mapping EC numbers as Strings to Sets of GO links
86       * @throws SQLException
87       */
88      private final Hashtable<String, SortedSet<EnzymeLink>> getGoLinks() throws SQLException{
89          Statement stm = expCon.createStatement();
90          ResultSet rs = stm.executeQuery(EC2GO_QUERY);
91          Hashtable<String, SortedSet<EnzymeLink>> goLinksTable =
92          	new Hashtable<String, SortedSet<EnzymeLink>>();
93          while (rs.next()){
94              String ec = rs.getString(1);
95              EnzymeLink goLink = getGoLink(rs.getString(2), rs.getString(3));
96              if (!goLinksTable.containsKey(ec)){
97                  SortedSet<EnzymeLink> goLinksSet = new TreeSet<EnzymeLink>();
98                  goLinksSet.add(goLink);
99                  goLinksTable.put(ec, goLinksSet);
100             } else {
101                 ((Set<EnzymeLink>) goLinksTable.get(ec)).add(goLink);
102             }
103         }
104         rs.close();
105         stm.close();
106         return goLinksTable;
107     }
108 
109     private EnzymeLink getGoLink(String goId, String goName){
110       return EnzymeLink.valueOf(XrefDatabaseConstant.GO,
111           XrefDatabaseConstant.GO.getUrl(),
112           goId,
113           goName,
114           EnzymeSourceConstant.INTENZ,
115           EnzymeViewConstant.IUBMB_INTENZ);
116     }
117 
118     /**
119      * Write imported GO links to the database.
120      * @exception Exception if an error occurs
121      */
122     public final void loadData() throws Exception {
123         LOGGER.debug("Load data");
124         try {
125             EnzymeLinkMapper mapper = new EnzymeLinkMapper();
126             Iterator<EnzymeEntry> iter = enzymeEntries.iterator();
127             while (iter.hasNext()) {
128                EnzymeEntry entry = (EnzymeEntry) iter.next();
129                mapper.deleteByCodeXref(entry.getId(), XrefDatabaseConstant.GO.getDatabaseCode(), impCon);
130                mapper.insert(new ArrayList<EnzymeLink>(entry.getLinks()), entry.getId(), entry.getStatus(), impCon);
131             }
132             impCon.commit();
133         } catch (Exception e){
134             impCon.rollback();
135             throw e;
136         }
137     }
138 
139     /**
140      * Close database connections.
141      */
142     public final void destroy() {
143         if (impCon != null)
144             try {
145                 impCon.close();
146             } catch (SQLException e) {
147                 LOGGER.error("Unable to close import connection!", e);
148             }
149         if (expCon != null)
150             try {
151                 expCon.close();
152             } catch (SQLException e) {
153                 LOGGER.error("Unable to close export connection!", e);
154             }
155     }
156 
157 }