Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. package gov.nih.nlm.nls.metamap;
  2.  
  3. import com.sun.deploy.util.StringUtils;
  4. import org.json.JSONArray;
  5. import org.json.JSONObject;
  6.  
  7. import java.sql.*;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.List;
  11.  
  12. /**
  13. * Created by Christine on 7/24/2017.
  14. */
  15. public class AbstractProcessor {
  16. // JDBC driver name and database URL
  17. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  18. static final String DB_URL = "jdbc:mysql://trec2017.ckdnt5fqmiqn.ap-northeast-2.rds.amazonaws.com:3306/trec2017";
  19.  
  20. // Database credentials
  21. static final String USER = "trec2017";
  22. static final String PASS = "kaistknowtrec2017";
  23.  
  24. static String serverhost = MetaMapApi.DEFAULT_SERVER_HOST;
  25. static int serverport = MetaMapApi.DEFAULT_SERVER_PORT; // default port
  26. static int timeout = -1; // use default timeout
  27.  
  28. static MetaMapApi api = new MetaMapApiImpl(serverhost,serverport);
  29.  
  30. static Connection conn = null;
  31. static Statement stmt = null;
  32.  
  33. static List<String> semanticFilter = Arrays.asList("aapp","acab","amas","antb","bacs","bdsu",
  34. "bdsy","biof","blor","bpoc","celf","celc","cell","chem","chvf","chvs","clna",
  35. "clnd","comd","diap","drdd","dsyn","enzy","genf","gngm","hops","horm","imft",
  36. "lbpr","irda","lbtr","inch","lipd","mbrt","medd","menp","mobd","moft","mosq",
  37. "neop","nnon","nsba","orch","ortf","patf","phsu","phsf","sbst","sosy","topp",
  38. "virs","strd","tisu","vita","fndg"
  39. );
  40.  
  41. public static void main(String args[]) throws Exception { //Receives file directory as argument
  42.  
  43. //STEP 2: Register JDBC driver
  44. Class.forName("com.mysql.jdbc.Driver");
  45.  
  46. //STEP 3: Open a connection
  47. System.out.println("Connecting to a selected database...");
  48. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  49. System.out.println("Connected database successfully...");
  50.  
  51. api.setOptions("-Q 0");
  52. api.setOptions("--prune 30");
  53.  
  54. ArrayList<String> years = new ArrayList<String>(
  55. Arrays.asList("2010","2011","2012","2013","2014","2015","2016","2017")); //todo: go back to 2009 later
  56.  
  57. for(int i=0;i<years.size();i++){
  58. System.out.println("YEAR :"+years.get(i));
  59. dbOperations(conn,years.get(i));
  60. }
  61.  
  62. api.disconnect();
  63. }
  64.  
  65. public static void updateTables(Connection conn,ArrayList<String> params1) throws Exception{ //params1 is for clinical_trials_mapped, params2 is for clinical_trials
  66. // create the java mysql update preparedstatement
  67.  
  68. String table_name = "extra_abstracts";
  69. String id = params1.get(0);
  70. String mesh_main_topics = params1.get(1);
  71. String other_topics = params1.get(2);
  72.  
  73. String query1 = "update trec2017.extra_abstracts set mesh_main_topic = ?, other_topic = ? where id = ?;";//updating entries for clinical_trials_mapped
  74.  
  75. PreparedStatement preparedStmt1 = conn.prepareStatement(query1);
  76. preparedStmt1.setString(1, mesh_main_topics);
  77. preparedStmt1.setString(2, other_topics);
  78. preparedStmt1.setString(3, id);
  79.  
  80. preparedStmt1.executeUpdate();
  81.  
  82. }
  83.  
  84. public static void dbOperations(Connection con,String year)
  85. throws SQLException {
  86.  
  87. Statement stmt = null;
  88. String query1 =
  89. "select * from trec2017.extra_abstracts where year="+year; //retrieving rows
  90.  
  91. try {
  92. stmt = con.createStatement();
  93. ResultSet rs = stmt.executeQuery(query1);
  94. while (rs.next()) {
  95.  
  96. ArrayList<String> params1 = new ArrayList<String>();
  97.  
  98. String id= rs.getString("id");
  99. System.out.println(id);
  100.  
  101. String title = rs.getString("title");
  102. String abs = rs.getString("abstract");
  103.  
  104. //String diseaseCuis = processCondition(title,patient_condition);
  105. String mesh_main_topics = getCuis(title);
  106. System.out.println("Mesh Main Topics "+mesh_main_topics);
  107.  
  108. String other_topics = getCuis(abs);
  109.  
  110. params1.add(id);
  111. params1.add(mesh_main_topics);
  112. params1.add(other_topics);
  113. updateTables(con,params1);
  114. }
  115.  
  116. } catch (SQLException e ) {
  117. e.printStackTrace();
  118. } catch (Exception e){
  119. e.printStackTrace();
  120. }
  121. // finally {
  122. // if (stmt != null) { stmt.close(); }
  123. // }
  124. }
  125.  
  126. public static String getCuis(String title) throws Exception{ //Get the CUI of any disease mentioned in the title and condition
  127. ArrayList<String> cuis = new ArrayList<String>();
  128.  
  129. List<Result> resultList = api.processCitationsFromString(title);
  130.  
  131. for(Result result: resultList){
  132. for (Utterance utterance: result.getUtteranceList()) {
  133. for (PCM pcm: utterance.getPCMList()) {
  134. for (Mapping map : pcm.getMappingList()) {
  135. for (Ev mapEv : map.getEvList()) {
  136. String cui = mapEv.getConceptId();
  137. List<String> semanticTypes = mapEv.getSemanticTypes();
  138. int negation = mapEv.getNegationStatus();
  139. for(int sem=0; sem<semanticTypes.size();sem++){
  140. if(semanticFilter.contains(semanticTypes.get(sem))&&!cuis.contains(cui)&&negation!=1){
  141. cuis.add(cui);
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }
  149.  
  150. return StringUtils.join(cuis,",");
  151.  
  152. }
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement