2 /*******************************************************************/
3 // Copyright (C) 2011 Phyaura, LLC <info@phyaura.com>
6 // Rohit Kumar <pandit.rohit@netsity.com>
7 // Brady Miller <brady@sparmy.com>
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
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)) {
26 if(!mkdir($GLOBALS['temporary_files_dir']."/".$type, 0777, true)) {
30 if(copy($filename,$GLOBALS['temporary_files_dir']."/".$type."/".basename($filename))) {
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)) {
46 $zip = new ZipArchive;
47 if ($zip->open($filename) === TRUE) {
48 if (!($zip->extractTo($GLOBALS['temporary_files_dir']."/".$type))) {
61 // Function to import the rxnorm tables
62 // $is_windows_flag - pass the IS_WINDOWS constant
63 function rxnorm_import($is_windows_flag) {
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);
77 $dirScripts = $GLOBALS['temporary_files_dir']."/rxnorm/scripts/mysql";
78 $dir = $GLOBALS['temporary_files_dir']."/rxnorm/rrf";
79 $dir=str_replace('\\','/',$dir);
82 $file_load = file_get_contents($dirScripts.'/Table_scripts_mysql_rxn.sql', true);
83 if ($is_windows_flag) {
84 $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_win.sql', true);
87 $data_load = file_get_contents($dirScripts.'/Load_scripts_mysql_rxn_unix.sql', true);
89 $indexes_load = file_get_contents($dirScripts.'/Indexes_mysql_rxn.sql', true);
92 // Creating the structure for table and applying indexes
95 $file_array=explode(";",$file_load);
96 foreach($file_array as $val){
103 $indexes_array=explode(";",$indexes_load);
105 foreach($indexes_array as $val1){
112 $data=explode(";",$data_load);
113 foreach($data as $val)
115 foreach($rx_info as $key => $value)
117 $file_name= $value['origin'];
118 $replacement=$dir."/".$file_name;
120 $pattern='/'.$file_name.'/';
121 if(strpos($val,$file_name)) {
122 $val1[]= str_replace($file_name,$replacement,$val);
126 foreach($val1 as $val){
135 // Function to import snomed tables
136 function snomed_import() {
139 $table_array_for_snomed=array(
140 "sct_concepts_drop"=>"DROP TABLE IF EXISTS `sct_concepts`",
141 "sct_concepts_structure"=>"CREATE TABLE IF NOT EXISTS `sct_concepts` (
142 `ConceptId` bigint(20) NOT NULL,
143 `ConceptStatus` int(11) NOT NULL,
144 `FullySpecifiedName` varchar(255) NOT NULL,
145 `CTV3ID` varchar(5) NOT NULL,
146 `SNOMEDID` varchar(8) NOT NULL,
147 `IsPrimitive` tinyint(1) NOT NULL,
148 PRIMARY KEY (`ConceptId`)
150 "sct_descriptions_drop"=>"DROP TABLE IF EXISTS `sct_descriptions`",
151 "sct_descriptions_structure"=>"CREATE TABLE IF NOT EXISTS `sct_descriptions` (
152 `DescriptionId` bigint(20) NOT NULL,
153 `DescriptionStatus` int(11) NOT NULL,
154 `ConceptId` bigint(20) NOT NULL,
155 `Term` varchar(255) NOT NULL,
156 `InitialCapitalStatus` tinyint(1) NOT NULL,
157 `DescriptionType` int(11) NOT NULL,
158 `LanguageCode` varchar(8) NOT NULL,
159 PRIMARY KEY (`DescriptionId`)
161 "sct_relationships_drop"=>"DROP TABLE IF EXISTS `sct_relationships`",
162 "sct_relationships_structure"=>"CREATE TABLE IF NOT EXISTS `sct_relationships` (
163 `RelationshipId` bigint(20) NOT NULL,
164 `ConceptId1` bigint(20) NOT NULL,
165 `RelationshipType` bigint(20) NOT NULL,
166 `ConceptId2` bigint(20) NOT NULL,
167 `CharacteristicType` int(11) NOT NULL,
168 `Refinability` int(11) NOT NULL,
169 `RelationshipGroup` int(11) NOT NULL,
170 PRIMARY KEY (`RelationshipId`)
175 $dir_snomed = $GLOBALS['temporary_files_dir']."/snomed/";
176 $sub_path="Terminology/Content/";
178 $dir=str_replace('\\','/',$dir);
180 // executing the create statement for tables, these are defined in snomed_capture.inc file
181 foreach($table_array_for_snomed as $val){
187 // reading the snomed directry and identifying the files to import and replacing the variables by originals values.
188 if( is_dir($dir) && $handle = opendir($dir)) {
189 while (false !== ($filename = readdir($handle))) {
190 if ($filename != "." && $filename != ".." && strpos($filename,"zip")==0) {
191 $path=$dir."".$filename."/".$sub_path;
192 if (!(is_dir($path))) {
193 $path=$dir."".$filename."/RF1Release/".$sub_path;
195 if( is_dir($path) && $handle1 = opendir($path)) {
196 while (false !== ($filename1 = readdir($handle1))) {
197 $load_script="Load data local infile '#FILENAME#' into table #TABLE# fields terminated by '\\t' ESCAPED BY '' lines terminated by '\\n' ignore 1 lines ";
198 $array_replace=array("#FILENAME#","#TABLE#");
199 if ($filename1 != "." && $filename1 != "..") {
200 $file_replace=$path.$filename1;
201 if(strpos($filename1,"Concepts")){
202 $new_str=str_replace($array_replace,array($file_replace,"sct_concepts"),$load_script);
204 if(strpos($filename1,"Descriptions")){
205 $new_str=str_replace($array_replace,array($file_replace,"sct_descriptions"),$load_script);
207 if(strpos($filename1,"Relationships")){
208 $new_str=str_replace($array_replace,array($file_replace,"sct_relationships"),$load_script);
211 sqlStatement($new_str);
225 // Function to clean up temp files
226 // $type (rxnorm etc.)
227 function temp_dir_cleanup($type) {
228 if(is_dir($GLOBALS['temporary_files_dir']."/".$type)) {
229 rmdir_recursive($GLOBALS['temporary_files_dir']."/".$type);
233 // Function to update version tracker table if successful
234 // $type (rxnorm etc.)
235 function update_tracker_table($type,$revision) {
236 if ($type == 'rxnorm') {
237 sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`) VALUES (NOW(),'RXNORM',?)", array($revision) );
240 else { //$type == 'snomed'
241 sqlStatement("INSERT INTO `standardized_tables_track` (`imported_date`,`name`,`revision_date`) VALUES (NOW(),'SNOMED',?)", array($revision) );
247 // Function to delete an entire directory
248 function rmdir_recursive($dir) {
249 $files = scandir($dir);
250 array_shift($files); // remove '.' from array
251 array_shift($files); // remove '..' from array
253 foreach ($files as $file) {
254 $file = $dir . '/' . $file;
256 rmdir_recursive($file);