Added AMC allergy
[openemr.git] / library / standard_tables_capture.inc
blobc1a26fbbd666b31dc293bf5ea04d05e236a55dd0
1 <?php
2 /*******************************************************************/
3 // Copyright (C) 2011 Phyaura, LLC <info@phyaura.com>
4 //
5 // Authors:
6 //         Rohit Kumar <pandit.rohit@netsity.com>
7 //         Brady Miller <brady@sparmy.com>
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
13 /*******************************************************************/
14 // This library contains functions for importing the RxNorm and SNOMED
15 // databases.
16 /*******************************************************************/
18 // Function to copy a package to temp
19 // $type (rxnorm, snomed etc.)
20 function temp_copy($filename,$type) {
22     if(!file_exists($filename)) {
23         return false;
24     }
26     if(!mkdir($GLOBALS['temporary_files_dir']."/".$type, 0777, true)) {
27         return false;
28     }
30     if(copy($filename,$GLOBALS['temporary_files_dir']."/".$type."/".basename($filename))) {
31         return true;
32     }
33     else {
34         return false;
35     }
38 // Function to unarchive a package
39 // $type (rxnorm, snomed etc.)
40 function temp_unarchive($filename,$type) {
41     $filename = $GLOBALS['temporary_files_dir']."/".$type."/".basename($filename);
42     if(!file_exists($filename)) {
43         return false;
44     }
45     else {
46         $zip = new ZipArchive;
47         if ($zip->open($filename) === TRUE) {
48             if (!($zip->extractTo($GLOBALS['temporary_files_dir']."/".$type))) {
49                 return false;
50             }
51             $zip->close();
52             return true;
53         }
54         else {
55             return false;
56         }
57     }
61 // Function to import the rxnorm tables
62 // $is_windows_flag - pass the IS_WINDOWS constant
63 function rxnorm_import($is_windows_flag) {
65     $rx_info = array();
66     $rx_info['rxnatomarchive'] = array('title' => "Archive Data", 'dir' => "$dir", 'origin' => "RXNATOMARCHIVE.RRF", 'filename' => "RXNATOMARCHIVE.RRF", 'table' => "rxnatomarchive", 'required' => 0);
67     $rx_info['rxnconso'] = array('title' => "Concept Names and Sources", 'dir' => "$dir", 'origin' => "RXNCONSO.RRF", 'filename' => "RXNCONSO.RRF", 'table' => "rxnconso",  'required' => 1);
68     $rx_info['rxncui'] = array('title' => "Retired RXCUI Data", 'dir' => "$dir", 'origin' => "RXNCUI.RRF", 'filename' => "RXNCUI.RRF", 'table' => "rxncui", 'required' => 1);
69     $rx_info['rxncuichanges'] = array('title' => "Concept Changes", 'dir' => "$dir", 'origin' => "RXNCUICHANGES.RRF", 'filename' => "RXNCUICHANGES.RRF", 'table' => "rxncuichanges", 'required' => 1);
70     $rx_info['rxndoc'] = array('title' => "Documentation for Abbreviated Values", 'dir' => "$dir", 'origin' => "RXNDOC.RRF", 'filename' => "RXNDOC.RRF", 'table' => "rxndoc", 'required' => 1);
71     $rx_info['rxnrel'] = array('title' => "Relationships", 'dir' => "$dir", 'origin' => "RXNREL.RRF", 'filename' => "RXNREL.RRF", 'table' => "rxnrel", 'required' => 1);
72     $rx_info['rxnsab'] = array('title' => "Source Information", 'dir' => "$dir", 'origin' => "RXNSAB.RRF", 'filename' => "RXNSAB.RRF", 'table' => "rxnsab", 'required' => 0);
73     $rx_info['rxnsat'] = array('title' => "Simple Concept and Atom Attributes", 'dir' => "$dir", 'origin' => "RXNSAT.RRF", 'filename' => "RXNSAT.RRF", 'table' => "rxnsat", 'required' => 0);
74     $rx_info['rxnsty'] = array('title' => "Semantic Types ", 'dir' => "$dir", 'origin' => "RXNSTY.RRF", 'filename' => "RXNSTY.RRF", 'table' => "rxnsty", 'required' => 1);
76     // set paths
77     $dirScripts = $GLOBALS['temporary_files_dir']."/rxnorm/scripts/mysql";
78     $dir = $GLOBALS['temporary_files_dir']."/rxnorm/rrf";
80     // load scripts
81     $file_load = file_get_contents($dirScripts.'/Table_scripts_mysql_rxn.sql', true);
82     if ($is_windows_flag) {
83         $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_win.sql', true);
84     }
85     else {
86         $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_unix.sql', true);
87     }
88     $indexes_load = file_get_contents($dirScripts.'/Indexes_mysql_rxn.sql', true);
90     //
91     // Creating the structure for table and applying indexes
92     //
94     $file_array=explode(";",$file_load);
95     foreach($file_array as $val){
96         if(trim($val)!='')
97         {
98             sqlStatement($val);
99         }
100     }
102     $indexes_array=explode(";",$indexes_load);
104     foreach($indexes_array as $val1){
105         if(trim($val1)!='')
106         {
107             sqlStatement($val1);
108         }
109     }
111     $data=explode(";",$data_load);
112     foreach($data as $val)
113     {
114         foreach($rx_info as $key => $value)
115         {
116             $file_name= $value['origin'];
117             $replacement=$dir."/".$file_name;
119             $pattern='/'.$file_name.'/';
120             if(strpos($val,$file_name))     {
121                 $val1[]=       str_replace($file_name,$replacement,$val);
122             }
123         }
124     }
125     foreach($val1 as $val){
126         if(trim($val)!='')      {
127             sqlStatement($val);
128         }
129     }
131     return true;
134 // Function to import snomed tables
135 function snomed_import() {
137     // set up array
138     $table_array_for_snomed=array(
139         "sct_concepts_drop"=>"DROP TABLE IF EXISTS `sct_concepts`",
140         "sct_concepts_structure"=>"CREATE TABLE IF NOT EXISTS `sct_concepts` (
141             `ConceptId` bigint(20) NOT NULL,
142             `ConceptStatus` int(11) NOT NULL,
143             `FullySpecifiedName` varchar(255) NOT NULL,
144             `CTV3ID` varchar(5) NOT NULL,
145             `SNOMEDID` varchar(8) NOT NULL,
146             `IsPrimitive` tinyint(1) NOT NULL,
147             PRIMARY KEY (`ConceptId`)
148             ) ENGINE=MyISAM",
149         "sct_descriptions_drop"=>"DROP TABLE IF EXISTS `sct_descriptions`",
150         "sct_descriptions_structure"=>"CREATE TABLE IF NOT EXISTS `sct_descriptions` (
151             `DescriptionId` bigint(20) NOT NULL,
152             `DescriptionStatus` int(11) NOT NULL,
153             `ConceptId` bigint(20) NOT NULL,
154             `Term` varchar(255) NOT NULL,
155             `InitialCapitalStatus` tinyint(1) NOT NULL,
156             `DescriptionType` int(11) NOT NULL,
157             `LanguageCode` varchar(8) NOT NULL,
158             PRIMARY KEY (`DescriptionId`)
159             ) ENGINE=MyISAM",
160         "sct_relationships_drop"=>"DROP TABLE IF EXISTS `sct_relationships`",
161         "sct_relationships_structure"=>"CREATE TABLE IF NOT EXISTS `sct_relationships` (
162             `RelationshipId` bigint(20) NOT NULL,
163             `ConceptId1` bigint(20) NOT NULL,
164             `RelationshipType` bigint(20) NOT NULL,
165             `ConceptId2` bigint(20) NOT NULL,
166             `CharacteristicType` int(11) NOT NULL,
167             `Refinability` int(11) NOT NULL,
168             `RelationshipGroup` int(11) NOT NULL,
169             PRIMARY KEY (`RelationshipId`)
170             ) ENGINE=MyISAM"
173     // set up paths
174     $dir_snomed = $GLOBALS['temporary_files_dir']."/snomed/";
175     $sub_path="Terminology/Content/";
176     $dir=$dir_snomed;
178     // executing the create statement for tables, these are defined in snomed_capture.inc file
179     foreach($table_array_for_snomed as $val){
180         if(trim($val)!=''){
181             sqlStatement($val);
182         }
183     }
185     // reading the snomed directry and identifying the files to import and replacing the variables by originals values.
186     if( is_dir($dir) && $handle = opendir($dir)) {
187         while (false !== ($filename = readdir($handle))) {
188             if ($filename != "." && $filename != ".." && strpos($filename,"zip")==0) {
189                 $path=$dir."".$filename."/".$sub_path;
190                 if( is_dir($path) && $handle1 = opendir($path)) {
191                     while (false !== ($filename1 = readdir($handle1))) {
192                         $load_script="Load data local infile '#FILENAME#' into table #TABLE# fields terminated by '\\t' ESCAPED BY '' lines terminated by '\\n' ignore 1 lines   ";
193                         $array_replace=array("#FILENAME#","#TABLE#");
194                         if ($filename1 != "." && $filename1 != "..") {
195                             $file_replace=$path.$filename1;
196                             if(strpos($filename1,"Concepts")){
197                                 $new_str=str_replace($array_replace,array($file_replace,"sct_concepts"),$load_script);
198                             }   
199                             if(strpos($filename1,"Descriptions")){
200                                 $new_str=str_replace($array_replace,array($file_replace,"sct_descriptions"),$load_script);
201                             }
202                             if(strpos($filename1,"Relationships")){
203                                 $new_str=str_replace($array_replace,array($file_replace,"sct_relationships"),$load_script);
204                             }
205                             if($new_str!=''){
206                                 sqlStatement($new_str);
207                             }
208                         }
209                     }
210                 }
211                 closedir($handle1);
212             }
213         }
214         closedir($handle);
215     }
217     return true;
220 // Function to clean up temp files
221 // $type (rxnorm etc.)
222 function temp_dir_cleanup($type) {
223     if(is_dir($GLOBALS['temporary_files_dir']."/".$type)) {
224         rmdir_recursive($GLOBALS['temporary_files_dir']."/".$type);
225     }
228 // Function to update version tracker table if successful
229 // $type (rxnorm etc.)
230 function update_tracker_table($type,$revision) {
231     if ($type == 'rxnorm') {
232         sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`) VALUES (NOW(),'RXNORM',?)", array($revision) );
233         return true;
234     }
235     else { //$type == 'snomed'
236         sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`) VALUES (NOW(),'SNOMED',?)", array($revision) );
237         return true;
238     }
239     return false;
242 // Function to delete an entire directory
243 function rmdir_recursive($dir) {
244     $files = scandir($dir);
245     array_shift($files);    // remove '.' from array
246     array_shift($files);    // remove '..' from array
248     foreach ($files as $file) {
249         $file = $dir . '/' . $file;
250         if (is_dir($file)) {
251             rmdir_recursive($file);
252             rmdir($file);
253         } else {
254             unlink($file);
255         }
256     }
257     rmdir($dir);