Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import java.io.*;
- import java.util.*;
- interface Constants
- {
- public static final int NUM_ARGS = 2;
- public static final int INPUT_FILE_ARG = 0;
- public static final int OUTPUT_FILE_ARG = 1;
- public static final String DB_USER = "root";
- public static final String DB_PASS = "*_*lotuSMysql1";
- public static final String DB_URL = "jdbc:mysql:///umls?socketFactory=com.mysql.jdbc.NamedPipeSocketFactory&namedPipePath=\\\\.\\Pipe\\mysql.sock ";
- }
- I2B2Relation TeRP = new I2B2Relation();
- I2B2Relation TeCP = new I2B2Relation();
- I2B2Relation TrIP = new I2B2Relation();
- I2B2Relation TrWP = new I2B2Relation();
- I2B2Relation TrCP = new I2B2Relation();
- I2B2Relation TrAP = new I2B2Relation();
- I2B2Relation TrNAP = new I2B2Relation();
- I2B2Relation PIP = new I2B2Relation();
- public class Connect implements Constants
- {
- public static void main (String[] args)
- {
- Connection conn = null;
- String phrase;
- ResultSet rs,rsConcept1,rsConcept2;
- PIP.UMLSInteresting.add("associated_with");
- PIP.UMLSInteresting.add("co-occurs_with");
- PIP.UMLSInteresting.add("diagnoses");
- PIP.UMLSInteresting.add("result_of");
- PIP.UMLSInteresting.add("manifestation_of");
- PIP.UMLSInteresting.add("causes");
- TeCP.UMLSInteresting.add("associated_with");
- TeCP.UMLSInteresting.add("assesses_effect_of");
- TeCP.UMLSInteresting.add("measures");
- TeRP.UMLSInteresting.add("indicates");
- TeRP.UMLSInteresting.add("diagnoses");
- TrIP.UMLSInteresting.add("treats");
- TrWP.UMLSInteresting.add("complicates");
- TrCP.UMLSInteresting.add("result_of");
- TrCP.UMLSInteresting.add("causes");
- TrAP.UMLSInteresting.add("associated_with");
- TrNAP.UMLSInteresting.add("complicates");
- // args.length is equivalent to argc in C
- if (args.length == NUM_ARGS)
- {
- try
- {
- // open record name file
- FileInputStream fstreamIn = new FileInputStream(args[INPUT_FILE_ARG]);
- // open mapping result file
- FileOutputStream fstreamOut = new FileOutputStream(args[OUTPUT_FILE_ARG]);
- // Convert to BufferedReader/Writer
- BufferedReader nameFile = new BufferedReader(new InputStreamReader(fstreamIn));
- BufferedWriter resultFile = new BufferedWriter(new OutputStreamWriter(fstreamOut));
- Class.forName ("com.mysql.jdbc.Driver").newInstance ();
- conn = DriverManager.getConnection (DB_URL, DB_USER, DB_PASS);
- System.out.println ("Database connection established");
- Statement statement = conn.createStatement();
- }
- catch (Exception e)
- {
- System.err.println("Caught Exception: "
- + e.getMessage());
- }
- // We are now connected and the record name file is open
- // read in names of record files to be processed
- while (nameFile.available() != 0)
- { // each line is a record name
- String recordName = nameFile.readLine();
- try
- { // open this record file
- FileInputStream fstreamIn = new FileInputStream(recordName);
- // get BufferedReader
- BufferedReader recordFile = new BufferedReader(new InputStreamReader(fstreamIn));
- }
- catch (Exception e)
- {
- System.err.println("Caught Exception: "
- + e.getMessage());
- } // record open
- // read in pairs of concepts
- while (recordFile.available() != 0)
- { // each line is a concept pair
- String relation = recordFile.readLine();
- String[] relationParts = relation.split("||");
- // First Concept
- String[] concept1 = relationParts[0].split("\\s");
- // I2B2 Relation
- String I2B2Relation = relationParts[1];
- // Second Concept
- String[] concept2 = relationParts[2].split("\\s");
- // this nested loop structure grabs adjacent words in the concept
- // -- for example: "Her Left Arm Pain" might return nothing
- // -- but: "Pain" would return 'Sign or Symptom' which is what we want
- // -- Similarly, : "Blood Pressure High" might not return anything,
- // -- but: "Blood Pressure" returns "Finding"
- for (int i = 0; i < concept1.length; i++)
- {
- for (int p = 0; (p - i) <= 0; p++)
- {
- for (int c = p; c <= (concept1.length - (p + i)); c++)
- {
- if (concept1[c].compareTo("") != 0)
- {
- phrase = phrase.concat(" ").concat(concept1[c]);
- }
- }
- // pull all unique CUIds for phrase
- String CUIdQuery = "select distinct cui as type from mrxns_eng where mrxns_eng.nstr LIKE \""+phrase+"\"";
- rs = statement.executeQuery(CUIdQuery);
- // this query pulls all unique semantic types for this phrase
- // *Note* we use WHERE cui = "C###" OR cui = "C###" OR ...
- // and then strip off the last 3 characters
- String semanticTypeQuery = "select distinct sty as type from mrsty where";
- while (rs.next())
- {
- semanticTypeQuery.concat(" cui = ").concat(rs.getInt("cui")).concat(" OR");
- }
- semanticTypeQuery = semanticTypeQuery.substring(0,(semanticTypeQuery.length() - 3));
- // add all types to concept1 Types list
- rsConcept1 = statement.executeQuery(CUIdQuery);
- }
- }
- // this nested loop structure grabs adjacent words in the concept
- // -- for example: "Her Left Arm Pain" might return nothing
- // -- but: "Pain" would return 'Sign or Symptom' which is what we want
- // -- Similarly, : "Blood Pressure High" might not return anything,
- // -- but: "Blood Pressure" returns "Finding"
- for (int i = 0; i < concept2.length; i++)
- {
- for (int p = 0; (p - i) <= 0; p++)
- {
- for (int c = p; c <= (concept2.length - (p + i)); c++)
- {
- if (concept2[c].compareTo("") != 0)
- {
- phrase = phrase.concat(" ").concat(concept2[c]);
- }
- }
- // pull all unique CUIds for phrase
- String CUIdQuery = "select distinct cui as type from mrxns_eng where mrxns_eng.nstr LIKE \""+phrase+"\"";
- rs = statement.executeQuery(CUIdQuery);
- // this query pulls all unique semantic types for this phrase
- // *Note* we use WHERE cui = "C###" OR cui = "C###" OR ...
- // and then strip off the last 3 characters
- String semanticTypeQuery = "select distinct sty as type from mrsty where";
- while (rs.next())
- {
- semanticTypeQuery.concat(" cui = ").concat(rs.getInt("cui")).concat(" OR");
- }
- semanticTypeQuery = semanticTypeQuery.substring(0,(semanticTypeQuery.length() - 3));
- // add all types to concept2 Types list
- rsConcept2 = statement.executeQuery(CUIdQuery);
- }
- }
- // Build query for UMLS relations
- String relationQuery = "Select * from srstre2 where";
- while (rsConcept1.next())
- {
- while (rsConcept2.next())
- {
- relationQuery.concat(" (STY1 = \""+ rsConcept1.getString("type") +"\"");
- relationQuery.concat(" AND ");
- relationQuery.concat("STY2 = \""+ rsConcept2.getString("type") +"\")");
- relationQuery.concat(" OR ");
- relationQuery.concat("(STY1 = \""+ rsConcept2.getString("type") +"\"");
- relationQuery.concat(" AND ");
- relationQuery.concat("STY2 = \""+ rsConcept1.getString("type") +"\")");
- relationQuery.concat(" OR");
- }
- rsConcept2.beforeFirst();
- }
- relationQuery = relationQuery.substring(0,(relationQuery.length() - 3));
- // run query
- rs = statement.executeQuery(relationQuery);
- String UMLSRelation;
- while (rs.next())
- {
- // build string from query result
- UMLSRelation = rs.getString("STY1");
- UMLSRelation.concat(rs.getString("RL"));
- UMLSRelation.concat(rs.getString("STY2"));
- // add query to appropriate list
- if (I2B2Relation.compareTo("TeRP") == 0)
- {
- if (TeRP.UMLSInteresting.contains(rs.getString("RL"))) {
- TeRP.UMLSRelationsList.add(UMLSRelation);
- }
- }
- else if (I2B2Relation.compareTo("TeCP") == 0)
- {
- if (TeCP.UMLSInteresting.contains(rs.getString("RL"))) {
- TeCP.UMLSRelationsList.add(UMLSRelation);
- }
- }
- else if (I2B2Relation.compareTo("TrIP") == 0)
- {
- if (TrIP.UMLSInteresting.contains(rs.getString("RL"))) {
- TrRP.UMLSRelationsList.add(UMLSRelation);
- }
- }
- else if (I2B2Relation.compareTo("TrWP") == 0)
- {
- if (TrWP.UMLSInteresting.contains(rs.getString("RL"))) {
- TrWP.UMLSRelationsList.add(UMLSRelation);
- }
- }
- else if (I2B2Relation.compareTo("TrAP") == 0)
- {
- if (TrCP.UMLSInteresting.contains(rs.getString("RL"))) {
- TrCP.UMLSRelationsList.add(UMLSRelation);
- }
- }
- else if (I2B2Relation.compareTo("TrNAP") == 0)
- {
- if (TrAP.UMLSInteresting.contains(rs.getString("RL"))) {
- TraP.UMLSRelationsList.add(UMLSRelation);
- }
- }
- else if (I2B2Relation.compareTo("TrCP") == 0)
- {
- if (TrNAP.UMLSInteresting.contains(rs.getString("RL"))) {
- TrNAP.UMLSRelationsLisst.add(UMLSRelation);
- }
- }
- else if (I2B2Relation.compareTo("PIP") == 0)
- {
- if (PIP.contains(rs.getString("RL"))) {
- PIP.UMLSRelationsList.add(UMLSRelation);
- }
- }
- } // end of record queries
- } // end of recordFile
- recordFile.close();
- }// end of nameFile
- in.close();
- out.write("TeRP:",0,5);
- TeRP.printToWriter(out);
- out.newLine();
- out.write("TeRP:",0,5);
- TeCP.printToWriter(out);
- out.newLine();
- out.write("TeRP:",0,5);
- TrIP.printToWriter(out);
- out.newLine();
- out.write("TeRP:",0,5);
- TrWP.printToWriter(out);
- out.newLine();
- out.write("TeRP:",0,5);
- TrCP.printToWriter(out);
- out.newLine();
- out.write("TeRP:",0,5);
- TrAP.printToWriter(out);
- out.newLine();
- out.write("TeRP:",0,5);
- TrNAP.printToWriter(out);
- out.newLine();
- out.write("TeRP:",0,5);
- PIP.printToWriter(out);
- out.newLine();
- out.close();
- conn.close();
- } else {
- System.out.println("Invalid parameters");
- }
- }
- }
- --- I2B2Relation.java ---
- import java.util.*;
- import java.io.*;
- public class I2B2Relation {
- private class UMLSRelationsList {
- private class UMLSRelationsNode {
- String name;
- int count;
- UMLSRelationsNode next;
- public UMLSRelationsNode(String n)
- {
- name = n;
- count= 0;
- next = null;
- }
- }
- UMLSRelationsNode head;
- UMLSRelationsNode tail;
- public void add(String n)
- {
- // search list for string matching N
- UMLSRelationsNode current = head;
- while (current != null)
- {
- // if string found, increment that node's count
- if (current.name.compareTo(n) == 0) {
- current.count++;
- return;
- }
- current = current.next;
- }
- // if string not found, add node to beginning of list
- current = new UMLSRelationsNode(n);
- current.next = head;
- head = current;
- }
- public void printToWriter(BufferedWriter out) throws IOException
- {
- UMLSRelationsNode current = head;
- while (current != null)
- {
- out.write(current.name,0,current.name.length());
- out.write(" ",0,1);
- out.write(current.count);
- out.newLine();
- current = current.next;
- }
- out.flush();
- }
- }
- List<String> UMLSInteresting;
- UMLSRelationsList UMLSRelations;
- public I2B2Relation ()
- {
- UMLSInteresting = null;
- UMLSRelations = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement