Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gov.nih.nlm.nls.metamap;
- import com.sun.deploy.util.StringUtils;
- import org.json.JSONArray;
- import org.json.JSONObject;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- /**
- * Created by Christine on 7/24/2017.
- */
- public class AbstractProcessor {
- // JDBC driver name and database URL
- static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://trec2017.ckdnt5fqmiqn.ap-northeast-2.rds.amazonaws.com:3306/trec2017";
- // Database credentials
- static final String USER = "trec2017";
- static final String PASS = "kaistknowtrec2017";
- static String serverhost = MetaMapApi.DEFAULT_SERVER_HOST;
- static int serverport = MetaMapApi.DEFAULT_SERVER_PORT; // default port
- static int timeout = -1; // use default timeout
- static MetaMapApi api = new MetaMapApiImpl(serverhost,serverport);
- static Connection conn = null;
- static Statement stmt = null;
- static List<String> semanticFilter = Arrays.asList("aapp","acab","amas","antb","bacs","bdsu",
- "bdsy","biof","blor","bpoc","celf","celc","cell","chem","chvf","chvs","clna",
- "clnd","comd","diap","drdd","dsyn","enzy","genf","gngm","hops","horm","imft",
- "lbpr","irda","lbtr","inch","lipd","mbrt","medd","menp","mobd","moft","mosq",
- "neop","nnon","nsba","orch","ortf","patf","phsu","phsf","sbst","sosy","topp",
- "virs","strd","tisu","vita","fndg"
- );
- public static void main(String args[]) throws Exception { //Receives file directory as argument
- //STEP 2: Register JDBC driver
- Class.forName("com.mysql.jdbc.Driver");
- //STEP 3: Open a connection
- System.out.println("Connecting to a selected database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- System.out.println("Connected database successfully...");
- api.setOptions("-Q 0");
- api.setOptions("--prune 30");
- ArrayList<String> years = new ArrayList<String>(
- Arrays.asList("2010","2011","2012","2013","2014","2015","2016","2017")); //todo: go back to 2009 later
- for(int i=0;i<years.size();i++){
- System.out.println("YEAR :"+years.get(i));
- dbOperations(conn,years.get(i));
- }
- api.disconnect();
- }
- public static void updateTables(Connection conn,ArrayList<String> params1) throws Exception{ //params1 is for clinical_trials_mapped, params2 is for clinical_trials
- // create the java mysql update preparedstatement
- String table_name = "extra_abstracts";
- String id = params1.get(0);
- String mesh_main_topics = params1.get(1);
- String other_topics = params1.get(2);
- String query1 = "update trec2017.extra_abstracts set mesh_main_topic = ?, other_topic = ? where id = ?;";//updating entries for clinical_trials_mapped
- PreparedStatement preparedStmt1 = conn.prepareStatement(query1);
- preparedStmt1.setString(1, mesh_main_topics);
- preparedStmt1.setString(2, other_topics);
- preparedStmt1.setString(3, id);
- preparedStmt1.executeUpdate();
- }
- public static void dbOperations(Connection con,String year)
- throws SQLException {
- Statement stmt = null;
- String query1 =
- "select * from trec2017.extra_abstracts where year="+year; //retrieving rows
- try {
- stmt = con.createStatement();
- ResultSet rs = stmt.executeQuery(query1);
- while (rs.next()) {
- ArrayList<String> params1 = new ArrayList<String>();
- String id= rs.getString("id");
- System.out.println(id);
- String title = rs.getString("title");
- String abs = rs.getString("abstract");
- //String diseaseCuis = processCondition(title,patient_condition);
- String mesh_main_topics = getCuis(title);
- System.out.println("Mesh Main Topics "+mesh_main_topics);
- String other_topics = getCuis(abs);
- params1.add(id);
- params1.add(mesh_main_topics);
- params1.add(other_topics);
- updateTables(con,params1);
- }
- } catch (SQLException e ) {
- e.printStackTrace();
- } catch (Exception e){
- e.printStackTrace();
- }
- // finally {
- // if (stmt != null) { stmt.close(); }
- // }
- }
- public static String getCuis(String title) throws Exception{ //Get the CUI of any disease mentioned in the title and condition
- ArrayList<String> cuis = new ArrayList<String>();
- List<Result> resultList = api.processCitationsFromString(title);
- for(Result result: resultList){
- for (Utterance utterance: result.getUtteranceList()) {
- for (PCM pcm: utterance.getPCMList()) {
- for (Mapping map : pcm.getMappingList()) {
- for (Ev mapEv : map.getEvList()) {
- String cui = mapEv.getConceptId();
- List<String> semanticTypes = mapEv.getSemanticTypes();
- int negation = mapEv.getNegationStatus();
- for(int sem=0; sem<semanticTypes.size();sem++){
- if(semanticFilter.contains(semanticTypes.get(sem))&&!cuis.contains(cui)&&negation!=1){
- cuis.add(cui);
- }
- }
- }
- }
- }
- }
- }
- return StringUtils.join(cuis,",");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement