Advertisement
Guest User

Untitled

a guest
May 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.76 KB | None | 0 0
  1. import java.sql.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. interface Constants
  6. {
  7.     public static final int NUM_ARGS = 2;
  8.     public static final int INPUT_FILE_ARG = 0;
  9.     public static final int OUTPUT_FILE_ARG = 1;
  10.     public static final String DB_USER = "root";
  11.     public static final String DB_PASS = "*_*lotuSMysql1";
  12.     public static final String DB_URL = "jdbc:mysql:///umls?socketFactory=com.mysql.jdbc.NamedPipeSocketFactory&namedPipePath=\\\\.\\Pipe\\mysql.sock ";
  13. }
  14.  
  15. I2B2Relation TeRP = new I2B2Relation();
  16. I2B2Relation TeCP = new I2B2Relation();
  17. I2B2Relation TrIP = new I2B2Relation();
  18. I2B2Relation TrWP = new I2B2Relation();
  19. I2B2Relation TrCP = new I2B2Relation();
  20. I2B2Relation TrAP = new I2B2Relation();
  21. I2B2Relation TrNAP = new I2B2Relation();
  22. I2B2Relation PIP = new I2B2Relation();
  23.  
  24. public class Connect implements Constants
  25. {
  26.  
  27.   public static void main (String[] args)
  28.   {
  29.     Connection conn = null;
  30.     String phrase;
  31.     ResultSet rs,rsConcept1,rsConcept2;
  32.  
  33.     PIP.UMLSInteresting.add("associated_with");
  34.     PIP.UMLSInteresting.add("co-occurs_with");
  35.     PIP.UMLSInteresting.add("diagnoses");
  36.     PIP.UMLSInteresting.add("result_of");
  37.     PIP.UMLSInteresting.add("manifestation_of");
  38.     PIP.UMLSInteresting.add("causes");
  39.  
  40.     TeCP.UMLSInteresting.add("associated_with");
  41.     TeCP.UMLSInteresting.add("assesses_effect_of");
  42.     TeCP.UMLSInteresting.add("measures");
  43.  
  44.     TeRP.UMLSInteresting.add("indicates");
  45.     TeRP.UMLSInteresting.add("diagnoses");
  46.  
  47.     TrIP.UMLSInteresting.add("treats");
  48.  
  49.     TrWP.UMLSInteresting.add("complicates");
  50.  
  51.     TrCP.UMLSInteresting.add("result_of");
  52.     TrCP.UMLSInteresting.add("causes");
  53.  
  54.     TrAP.UMLSInteresting.add("associated_with");
  55.  
  56.     TrNAP.UMLSInteresting.add("complicates");
  57.  
  58.         // args.length is equivalent to argc in C
  59.         if (args.length == NUM_ARGS)
  60.         {
  61.             try
  62.             {
  63.         // open record name file
  64.         FileInputStream fstreamIn = new FileInputStream(args[INPUT_FILE_ARG]);
  65.  
  66.         // open mapping result file
  67.                 FileOutputStream fstreamOut = new FileOutputStream(args[OUTPUT_FILE_ARG]);
  68.  
  69.                 // Convert to BufferedReader/Writer
  70.                 BufferedReader nameFile = new BufferedReader(new InputStreamReader(fstreamIn));
  71.                 BufferedWriter resultFile = new BufferedWriter(new OutputStreamWriter(fstreamOut));
  72.  
  73.         Class.forName ("com.mysql.jdbc.Driver").newInstance ();
  74.         conn = DriverManager.getConnection (DB_URL, DB_USER, DB_PASS);
  75.         System.out.println ("Database connection established");
  76.         Statement statement = conn.createStatement();
  77.       }
  78.       catch (Exception e)
  79.       {
  80.         System.err.println("Caught Exception: "
  81.                                  + e.getMessage());
  82.       }
  83.       // We are now connected and the record name file is open
  84.       // read in names of record files to be processed
  85.       while (nameFile.available() != 0)
  86.       { // each line is a record name
  87.         String recordName = nameFile.readLine();
  88.  
  89.         try
  90.         { // open this record file
  91.           FileInputStream fstreamIn = new FileInputStream(recordName);
  92.  
  93.           // get BufferedReader
  94.           BufferedReader recordFile = new BufferedReader(new InputStreamReader(fstreamIn));
  95.         }
  96.         catch (Exception e)
  97.         {
  98.           System.err.println("Caught Exception: "
  99.                                    + e.getMessage());
  100.         } // record open
  101.  
  102.         // read in pairs of concepts
  103.         while (recordFile.available() != 0)
  104.         { // each line is a concept pair
  105.           String relation = recordFile.readLine();
  106.           String[] relationParts = relation.split("||");
  107.  
  108.           // First Concept
  109.           String[] concept1 = relationParts[0].split("\\s");
  110.  
  111.           // I2B2 Relation
  112.           String I2B2Relation = relationParts[1];
  113.  
  114.           // Second Concept
  115.           String[] concept2 = relationParts[2].split("\\s");
  116.  
  117.           // this nested loop structure grabs adjacent words in the concept
  118.           // -- for example: "Her Left Arm Pain" might return nothing
  119.           // -- but: "Pain" would return 'Sign or Symptom' which is what we want
  120.           // -- Similarly, : "Blood Pressure High" might not return anything,
  121.           // -- but: "Blood Pressure" returns "Finding"
  122.           for (int i = 0; i < concept1.length; i++)
  123.           {
  124.             for (int p = 0; (p - i) <= 0; p++)
  125.             {
  126.               for (int c = p; c <= (concept1.length - (p + i)); c++)
  127.               {
  128.                 if (concept1[c].compareTo("") != 0)
  129.                 {
  130.                   phrase = phrase.concat(" ").concat(concept1[c]);
  131.                 }
  132.               }
  133.               // pull all unique CUIds for phrase
  134.               String CUIdQuery = "select distinct cui as type from mrxns_eng where mrxns_eng.nstr LIKE \""+phrase+"\"";
  135.               rs = statement.executeQuery(CUIdQuery);
  136.  
  137.               // this query pulls all unique semantic types for this phrase
  138.               // *Note* we use WHERE cui = "C###" OR cui = "C###" OR ...
  139.               // and then strip off the last 3 characters
  140.               String semanticTypeQuery = "select distinct sty as type from mrsty where";
  141.               while (rs.next())
  142.               {
  143.                 semanticTypeQuery.concat(" cui = ").concat(rs.getInt("cui")).concat(" OR");
  144.               }
  145.               semanticTypeQuery = semanticTypeQuery.substring(0,(semanticTypeQuery.length() - 3));
  146.              
  147.               // add all types to concept1 Types list
  148.               rsConcept1 = statement.executeQuery(CUIdQuery);
  149.             }
  150.           }
  151.  
  152.           // this nested loop structure grabs adjacent words in the concept
  153.           // -- for example: "Her Left Arm Pain" might return nothing
  154.           // -- but: "Pain" would return 'Sign or Symptom' which is what we want
  155.           // -- Similarly, : "Blood Pressure High" might not return anything,
  156.           // -- but: "Blood Pressure" returns "Finding"
  157.           for (int i = 0; i < concept2.length; i++)
  158.           {
  159.             for (int p = 0; (p - i) <= 0; p++)
  160.             {
  161.               for (int c = p; c <= (concept2.length - (p + i)); c++)
  162.               {
  163.                 if (concept2[c].compareTo("") != 0)
  164.                 {
  165.                   phrase = phrase.concat(" ").concat(concept2[c]);
  166.                 }
  167.               }
  168.               // pull all unique CUIds for phrase
  169.               String CUIdQuery = "select distinct cui as type from mrxns_eng where mrxns_eng.nstr LIKE \""+phrase+"\"";
  170.               rs = statement.executeQuery(CUIdQuery);
  171.  
  172.               // this query pulls all unique semantic types for this phrase
  173.               // *Note* we use WHERE cui = "C###" OR cui = "C###" OR ...
  174.               // and then strip off the last 3 characters
  175.               String semanticTypeQuery = "select distinct sty as type from mrsty where";
  176.               while (rs.next())
  177.               {
  178.                 semanticTypeQuery.concat(" cui = ").concat(rs.getInt("cui")).concat(" OR");
  179.               }
  180.               semanticTypeQuery = semanticTypeQuery.substring(0,(semanticTypeQuery.length() - 3));
  181.              
  182.               // add all types to concept2 Types list
  183.               rsConcept2 = statement.executeQuery(CUIdQuery);
  184.             }
  185.           }
  186.  
  187.           // Build query for UMLS relations
  188.           String relationQuery = "Select * from srstre2 where";
  189.           while (rsConcept1.next())
  190.           {
  191.             while (rsConcept2.next())
  192.             {
  193.               relationQuery.concat(" (STY1 = \""+ rsConcept1.getString("type") +"\"");
  194.               relationQuery.concat(" AND ");
  195.               relationQuery.concat("STY2 = \""+ rsConcept2.getString("type") +"\")");
  196.               relationQuery.concat(" OR ");
  197.               relationQuery.concat("(STY1 = \""+ rsConcept2.getString("type") +"\"");
  198.               relationQuery.concat(" AND ");
  199.               relationQuery.concat("STY2 = \""+ rsConcept1.getString("type") +"\")");
  200.               relationQuery.concat(" OR");
  201.             }
  202.             rsConcept2.beforeFirst();
  203.           }
  204.           relationQuery = relationQuery.substring(0,(relationQuery.length() - 3));
  205.  
  206.           // run query
  207.           rs = statement.executeQuery(relationQuery);
  208.           String UMLSRelation;
  209.           while (rs.next())
  210.           {
  211.             // build string from query result
  212.             UMLSRelation = rs.getString("STY1");
  213.             UMLSRelation.concat(rs.getString("RL"));
  214.             UMLSRelation.concat(rs.getString("STY2"));
  215.  
  216.             // add query to appropriate list
  217.             if (I2B2Relation.compareTo("TeRP") == 0)
  218.             {
  219.               if (TeRP.UMLSInteresting.contains(rs.getString("RL"))) {
  220.                 TeRP.UMLSRelationsList.add(UMLSRelation);
  221.               }
  222.             }
  223.             else if (I2B2Relation.compareTo("TeCP") == 0)
  224.             {
  225.               if (TeCP.UMLSInteresting.contains(rs.getString("RL"))) {
  226.                 TeCP.UMLSRelationsList.add(UMLSRelation);
  227.               }
  228.             }
  229.             else if (I2B2Relation.compareTo("TrIP") == 0)
  230.             {
  231.               if (TrIP.UMLSInteresting.contains(rs.getString("RL"))) {
  232.                 TrRP.UMLSRelationsList.add(UMLSRelation);
  233.               }
  234.             }
  235.             else if (I2B2Relation.compareTo("TrWP") == 0)
  236.             {
  237.               if (TrWP.UMLSInteresting.contains(rs.getString("RL"))) {
  238.                 TrWP.UMLSRelationsList.add(UMLSRelation);
  239.               }
  240.             }
  241.             else if (I2B2Relation.compareTo("TrAP") == 0)
  242.             {
  243.               if (TrCP.UMLSInteresting.contains(rs.getString("RL"))) {
  244.                 TrCP.UMLSRelationsList.add(UMLSRelation);
  245.               }
  246.             }
  247.             else if (I2B2Relation.compareTo("TrNAP") == 0)
  248.             {
  249.               if (TrAP.UMLSInteresting.contains(rs.getString("RL"))) {
  250.                 TraP.UMLSRelationsList.add(UMLSRelation);
  251.               }
  252.             }
  253.             else if (I2B2Relation.compareTo("TrCP") == 0)
  254.             {
  255.               if (TrNAP.UMLSInteresting.contains(rs.getString("RL"))) {
  256.                 TrNAP.UMLSRelationsLisst.add(UMLSRelation);
  257.               }
  258.             }
  259.             else if (I2B2Relation.compareTo("PIP") == 0)
  260.             {
  261.               if (PIP.contains(rs.getString("RL"))) {
  262.                 PIP.UMLSRelationsList.add(UMLSRelation);
  263.               }
  264.             }
  265.           } // end of record queries
  266.         } // end of recordFile
  267.  
  268.         recordFile.close();
  269.       }// end of nameFile
  270.  
  271.       in.close();
  272.  
  273.       out.write("TeRP:",0,5);
  274.       TeRP.printToWriter(out);
  275.       out.newLine();
  276.  
  277.       out.write("TeRP:",0,5);
  278.       TeCP.printToWriter(out);
  279.       out.newLine();
  280.  
  281.       out.write("TeRP:",0,5);
  282.       TrIP.printToWriter(out);
  283.       out.newLine();
  284.  
  285.       out.write("TeRP:",0,5);
  286.       TrWP.printToWriter(out);
  287.       out.newLine();
  288.  
  289.       out.write("TeRP:",0,5);
  290.       TrCP.printToWriter(out);
  291.       out.newLine();
  292.  
  293.       out.write("TeRP:",0,5);
  294.       TrAP.printToWriter(out);
  295.       out.newLine();
  296.  
  297.       out.write("TeRP:",0,5);
  298.       TrNAP.printToWriter(out);
  299.       out.newLine();
  300.  
  301.       out.write("TeRP:",0,5);
  302.       PIP.printToWriter(out);
  303.       out.newLine();
  304.  
  305.       out.close();
  306.       conn.close();
  307.         } else {
  308.       System.out.println("Invalid parameters");
  309.     }
  310.   }
  311. }
  312.  
  313.  
  314.  
  315. --- I2B2Relation.java ---
  316. import java.util.*;
  317. import java.io.*;
  318.  
  319. public class I2B2Relation {
  320.   private class UMLSRelationsList {
  321.     private class UMLSRelationsNode {
  322.       String name;
  323.       int count;
  324.       UMLSRelationsNode next;
  325.  
  326.       public UMLSRelationsNode(String n)
  327.       {
  328.         name = n;
  329.         count= 0;
  330.         next = null;
  331.       }
  332.      
  333.     }
  334.     UMLSRelationsNode head;
  335.     UMLSRelationsNode tail;
  336.  
  337.     public void add(String n)
  338.     {
  339.       // search list for string matching N
  340.       UMLSRelationsNode current = head;
  341.      
  342.       while (current != null)
  343.       {
  344.         // if string found, increment that node's count
  345.         if (current.name.compareTo(n) == 0) {
  346.           current.count++;
  347.           return;
  348.         }
  349.         current = current.next;
  350.       }
  351.  
  352.       // if string not found, add node to beginning of list
  353.       current = new UMLSRelationsNode(n);
  354.       current.next = head;
  355.       head = current;
  356.     }
  357.  
  358.     public void printToWriter(BufferedWriter out) throws IOException
  359.     {
  360.       UMLSRelationsNode current = head;
  361.  
  362.       while (current != null)
  363.       {
  364.         out.write(current.name,0,current.name.length());
  365.         out.write(" ",0,1);
  366.         out.write(current.count);
  367.         out.newLine();
  368.         current = current.next;
  369.       }
  370.  
  371.       out.flush();
  372.     }
  373.   }
  374.  
  375.   List<String> UMLSInteresting;
  376.   UMLSRelationsList UMLSRelations;
  377.  
  378.   public I2B2Relation ()
  379.   {
  380.     UMLSInteresting = null;
  381.     UMLSRelations = null;
  382.   }
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement