00001 <?php
00002
00003
00004
00027 function icbasic_init() {
00028 $css_file = drupal_get_path('module', 'icbasic') .'/icbasic.css';
00029 if (file_exists($css_file)) {
00030 drupal_add_css($css_file, 'module');
00031 }
00032 }
00033
00046 function icbasic_encode($variable) {
00047 if (!empty($variable)) {
00048 $encoded = strtr(base64_encode(addslashes(gzcompress(serialize($variable), 9))), '+/=', '-_,');
00049 return $encoded;
00050 }
00051 }
00052
00064 function icbasic_decode($encoded) {
00065 if (!empty($encoded)) {
00066 $string = unserialize(gzuncompress(stripslashes(base64_decode(strtr($encoded, '-_,', '+/=')))));
00067 return $string;
00068 }
00069 }
00070
00090 function icbasic_array2csv($array = NULL, $quote = NULL, $separator = ',', $key = FALSE) {
00091 if (empty($array)) {
00092 return FALSE;
00093 }
00094 $quotes = '';
00095 if (!empty($quote)) {
00096 if ($quote === TRUE) {
00097 $quotes = "'";
00098 }
00099 else {
00100 $quotes = $quote;
00101 }
00102 }
00103
00104 if (is_string($array) || is_int($array)) {
00105 return $quotes . $array . $quotes;
00106 }
00107
00108 $str = '';
00109 if (empty($key)) {
00110 foreach ($array as $ar) {
00111 $str .= $quotes . $ar . $quotes . $separator;
00112 }
00113 }
00114 else {
00115 foreach ($array as $key => $ar) {
00116 $str .= $quotes . $key . $quotes . $separator;
00117 }
00118 }
00119 $str = rtrim($str, $separator);
00120 return $str;
00121 }
00122
00137 function icbasic_db_execute($db = NULL, $function = NULL, $variables = NULL) {
00138 $return_array = array();
00139 if (!empty($db) && !empty($function)) {
00140 global $db_url;
00141 if (empty($db_url[$db])) {
00142 drupal_set_message("The public database for $db is unavailable at this time.", 'error', 'FALSE');
00143 return FALSE;
00144 }
00145 db_set_active($db);
00146 $return_array = $function($variables);
00147 if (!empty($return_array)) {
00148 db_set_active('default');
00149 return $return_array;
00150 }
00151 else {
00152 db_set_active('default');
00153 return 0;
00154 }
00155 }
00156
00157 elseif (!empty($db)) {
00158 global $db_url;
00159 if (!empty($db_url[$db])) {
00160 db_set_active($db);
00161 return 1;
00162 }
00163 else {
00164 drupal_set_message("The public database for $db is unavailable at this time.", 'error', 'FALSE');
00165 return FALSE;
00166 }
00167 }
00168 else {
00169
00170 db_set_active('default');
00171 return 1;
00172 }
00173 }
00174
00184 function icbasic_validate_ic_id($accession = NULL) {
00185 $accession = trim($accession);
00186 $accession = check_plain($accession);
00187 if (empty($accession)) {
00188 return FALSE;
00189 }
00190 preg_match("/^([A-Z]{2})(\d+)[A-Z][a-z]([A-Z][a-z]+)(\d+)/", $accession, $matches);
00191 if (isset($matches) && $matches[1] == "IC" && $matches[2] && $matches[3] && $matches[4]) {
00192 return $accession;
00193 }
00194 return FALSE;
00195 }
00196
00209 function icbasic_validate_seq_dna($seq = NULL) {
00210 $seq = trim($seq);
00211 if (empty($seq)) {
00212 return FALSE;
00213 }
00214 $seq_array = explode("\r", $seq);
00215 $seq_new = '';
00216 $error = 0;
00217 foreach ($seq_array as $line) {
00218 $line = trim($line);
00219 $line = str_replace("\n", '', $line);
00220 if (empty($line)) {
00221 continue;
00222 }
00223 $defline = strpos($line, '>');
00224 if ($defline !== FALSE) {
00225
00226 if ($defline > 0) {
00227 $seq_new .= substr($line, $defline) ."\n";
00228 }
00229 else {
00230 $seq_new .= $line ."\n";
00231 }
00232 }
00233 else {
00234 $line = str_replace(" ", '', $line);
00235 if (preg_match('/[^XatcgURMWYKBDHVSN]/i', $line)) {
00236 $error++;
00237 }
00238 $seq_new .= $line;
00239 }
00240 }
00241 if (!empty($error)) {
00242 return $error;
00243 }
00244 elseif (!empty($seq_new)) {
00245
00246 $seq_new = str_ireplace('U', 'T', $seq_new);
00247 return $seq_new;
00248 }
00249 else {
00250 return FALSE;
00251 }
00252 }
00253
00266 function icbasic_validate_seq_protein($seq = NULL) {
00267 $seq = trim($seq);
00268 if (empty($seq)) {
00269 return FALSE;
00270 }
00271 $seq_array = explode("\r", $seq);
00272 $seq_new = '';
00273 $error = 0;
00274 foreach ($seq_array as $line) {
00275 $line = str_ireplace("\n", '', $line);
00276 if (empty($line)) {
00277 continue;
00278 }
00279 if (preg_match('/>/', $line) == 0) {
00280 if (preg_match('/[^XacdefghiklmnpqrstvwyBZJ]/i', $line)) {
00281 $error++;
00282 }
00283 $seq_new .= $line;
00284 }
00285 }
00286 if (!empty($error)) {
00287 return $error;
00288 }
00289 elseif (!empty($seq_new)) {
00290 return $seq_new;
00291 }
00292 else {
00293 return FALSE;
00294 }
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313 function icbasic_chadoapi_get_single_table($table = NULL, $field = 'uniquename', $value = NULL) {
00314 if (empty($value) || empty($table)) {
00315 return FALSE;
00316 }
00317 $value = check_plain($value);
00318 $results = array();
00319 $sql = "SELECT * from %s where %s='%s'";
00320 $results = db_query($sql, $table, $field, $value);
00321 return $results;
00322 }
00323
00333 function icbasic_checkboxes_results2array($results) {
00334 if (empty($results)) {
00335 return FALSE;
00336 }
00337 $array = array();
00338 foreach ($results as $key => $value) {
00339 if (empty($value)) {
00340 continue;
00341 }
00342 $array[] = $key;
00343 }
00344 if (!empty($array)) {
00345 return $array;
00346 }
00347 }
00348
00360 function icbasic_add_cv($cv, $cvterm_array) {
00361 if (empty($cv) || empty($cvterm_array)) {
00362 return FALSE;
00363 }
00364 $sql_insert_cv = "INSERT INTO {ic_cv} (name) VALUES ('%s')";
00365 $sql_select_cv = "SELECT cv_id from {ic_cv} where name='%s'";
00366 $res = db_fetch_array(db_query($sql_select_cv, $cv));
00367 $cv_id = $res['cv_id'];
00368 if (empty($cv_id)) {
00369 db_query($sql_insert_cv, $cv);
00370 $res = db_fetch_array(db_query($sql_select_cv, $cv));
00371 $cv_id = $res['cv_id'];
00372 }
00373 if (empty($cv_id)) {
00374 drupal_set_message('There has been a problem inserting a new controlled vocabulary. Are you sure permissions are properly set?', 'error', FALSE);
00375 return FALSE;
00376 }
00377 $sql_check = "SELECT cvterm_id from {ic_cvterm} where name='%s' and cv_id=%d";
00378 $sql_insert_cvterm = "INSERT INTO {ic_cvterm} (name,cv_id) VALUES ('%s',%d)";
00379 foreach ($cvterm_array as $cvterm) {
00380 $res = db_fetch_array(db_query($sql_check, $cvterm, $cv_id));
00381 if (empty($res['cvterm_id'])) {
00382 db_query($sql_insert_cvterm, $cvterm, $cv_id);
00383 }
00384 }
00385 return $cv_id;
00386 }
00387
00404 function icbasic_populate_prop($table_type, $object_name, $cvterm_id, $property_array) {
00405 if (empty($table_type) || empty($object_name) || empty($cvterm_id) || empty($property_array)) {
00406 return FALSE;
00407 }
00408 $key = '';
00409 $table_pkey = '';
00410 $table_name = '';
00411 if ($table_type == 'ic_resource') {
00412 $key = 'resource_id';
00413 $table_pkey = 'resourceprop_id';
00414 $table_name = 'ic_resourceprop';
00415 }
00416 elseif ($table_type == 'ic_feature') {
00417 $key = 'feature_id';
00418 $table_pkey = 'featureprop_id';
00419 $table_name = 'ic_featureprop';
00420 }
00421 else {
00422 drupal_set_message('Currently only resource and feature table types are allowed', 'error', FALSE);
00423 return FALSE;
00424 }
00425
00426
00427 $object_select = "SELECT $key from {". $table_type ."} where uniquename='%s'";
00428 $res = db_fetch_array(db_query($object_select, $object_name));
00429 $object_id = $res[$key];
00430 if (empty($object_id)) {
00431 drupal_set_message("Failed to get $key for $object_name. Are you sure it exists?", 'error', FALSE);
00432 return FALSE;
00433 }
00434 $property_check = "SELECT $table_pkey from {". $table_name ."} WHERE $key=%d AND type_id=%d and value='%s'";
00435 $property_next_rank = "SELECT max(rank) from {". $table_name ."} WHERE $key=%d AND type_id=%d ";
00436 $property_insert = "INSERT INTO {". $table_name ."} ($key,type_id,value,rank) VALUES (%d,%d,'%s',%d)";
00437 foreach ($property_array as $value) {
00438 $res = db_fetch_array(db_query($property_check, $object_id, $cvterm_id, $value));
00439 if (empty($res[$table_pkey])) {
00440 $res_rank = db_fetch_array(db_query($property_next_rank, $object_id, $cvterm_id));
00441 $next_rank = $res_rank['max'] + 1;
00442 if (empty($next_rank)) {
00443 $next_rank = 0;
00444 }
00445 db_query($property_insert, $object_id, $cvterm_id, $value, $next_rank);
00446 $res = db_fetch_array(db_query($property_check, $object_id, $cvterm_id, $value));
00447 if (empty($res[$table_pkey])) {
00448 drupal_set_message("Failed to add $value into $table_name. Are you sure DB permissions allow INSERT operations?", 'error', FALSE);
00449 return FALSE;
00450 }
00451 }
00452 }
00453 return TRUE;
00454 }
00455
00474 function icbasic_populate_cvterms($table_type, $object_name, $cvname, $cvterm_array) {
00475 if (empty($table_type) || empty($object_name) || empty($cvname) || empty($cvterm_array)) {
00476 return FALSE;
00477 }
00478 $key = '';
00479 $table_pkey = '';
00480 $table_name = '';
00481 if ($table_type == 'ic_resource') {
00482 $key = 'resource_id';
00483 $table_pkey = 'resource_cvterm_id';
00484 $table_name = 'ic_resource_cvterm';
00485 }
00486 elseif ($table_type == 'ic_feature') {
00487 $key = 'feature_id';
00488 $table_pkey = 'feature_cvterm_id';
00489 $table_name = 'ic_feature_cvterm';
00490 }
00491 else {
00492 drupal_set_message('Currently only resource and feature table types are allowed', 'error', FALSE);
00493 return FALSE;
00494 }
00495
00496 $cv_id = icbasic_add_cv($cvname, $cvterm_array);
00497
00498 $object_select = "SELECT $key from {". $table_type ."} where uniquename='%s'";
00499 $res = db_fetch_array(db_query($object_select, $object_name));
00500 $object_id = $res[$key];
00501 if (empty($object_id)) {
00502 drupal_set_message("Failed to get $key for $object_name. Are you sure it exists?", 'error', FALSE);
00503 return FALSE;
00504 }
00505 $cvterm_check = "SELECT $table_pkey from {". $table_name ."} WHERE $key=%d AND " . "cvterm_id=(SELECT cvterm_id from {ic_cvterm} WHERE cv_id=%d and name='%s')";
00506 $cvterm_insert = "INSERT INTO {". $table_name ."} ($key,cvterm_id) VALUES (%d," . "(SELECT cvterm_id from {ic_cvterm} WHERE cv_id=%d and name='%s'))";
00507 foreach ($cvterm_array as $cvterm_name) {
00508 $res = db_fetch_array(db_query($cvterm_check, $object_id, $cv_id, $cvterm_name));
00509 if (empty($res[$table_pkey])) {
00510 db_query($cvterm_insert, $object_id, $cv_id, $cvterm_name);
00511 $res = db_fetch_array(db_query($cvterm_check, $object_id, $cv_id, $cvterm_name));
00512 if (empty($res[$table_pkey])) {
00513 drupal_set_message("Failed to add $cvterm_name into $table_name. Are you sure DB permissions allow INSERT operations?", 'error', FALSE);
00514 return FALSE;
00515 }
00516 }
00517 }
00518 return TRUE;
00519 }
00520
00543 function icbasic_get_add_cv_withnames($cv_name, $cvterm_name = NULL, $name_as_key = FALSE) {
00544 $return_array = array();
00545 if (empty($cv_name)) {
00546 return FALSE;
00547 }
00548 if (empty($cvterm_name)) {
00549
00550 $select_cvterms = 'SELECT cvterm_id,name from {ic_cvterm} as cvterm where cv_id=' . "(SELECT cv_id from {ic_cv} where name='%s')";
00551 $res = db_query($select_cvterms, $cv_name);
00552 if (empty($res)) {
00553 return FALSE;
00554 }
00555 while ($row = db_fetch_array($res)) {
00556 if (!empty($name_as_key) && $name_as_key === 'BOTH') {
00557 $return_array[$row['name']] = $row['name'];
00558 }
00559 elseif (!empty($name_as_key)) {
00560 $return_array[$row['name']] = $row['cvterm_id'];
00561 }
00562 else {
00563 $return_array[$row['cvterm_id']] = $row['name'];
00564 }
00565 }
00566 if (!empty($return_array)) {
00567 return $return_array;
00568 }
00569 else {
00570 return FALSE;
00571 }
00572 }
00573 $cvterm_id = '';
00574 $select_cvterm = 'SELECT cvterm_id from {ic_cvterm} as cvterm JOIN {ic_cv} as cv ON cvterm.cv_id=cv.cv_id ' . " WHERE cvterm.name='%s' AND cv.name='%s'";
00575 $res = db_fetch_array(db_query($select_cvterm, $cvterm_name, $cv_name));
00576 $cvterm_id = $res['cvterm_id'];
00577 if (!empty($cvterm_id)) {
00578 return $cvterm_id;
00579 }
00580 else {
00581
00582 $cv_id = '';
00583 $select_cv_id = "SELECT cv_id from {ic_cv} as cv where name='%s'";
00584 $res = db_fetch_array(db_query($select_cv_id, $cv_name));
00585 $cv_id = $res['cv_id'];
00586 if (empty($cv_id)) {
00587
00588 $insert_cv_id = "INSERT INTO {ic_cv} (name) VALUES ('%s')";
00589 db_query($insert_cv_id, $cv_name);
00590 $res = db_fetch_array(db_query($select_cv_id, $cv_name));
00591 $cv_id = $res['cv_id'];
00592 }
00593 if (empty($cv_id)) {
00594 drupal_set_message("I was not able to store the CV data for $cv_name.", 'error', FALSE);
00595 return FALSE;
00596 }
00597
00598 $insert_cvterm = "INSERT INTO {ic_cvterm} (name,cv_id) VALUES ('%s',%d)";
00599 db_query($insert_cvterm, $cvterm_name, $cv_id);
00600 $res = db_fetch_array(db_query($select_cvterm, $cvterm_name, $cv_id));
00601 $cvterm_id = $res['cvterm_id'];
00602 if (!empty($cvterm_id)) {
00603 return $cvterm_id;
00604 }
00605 else {
00606 drupal_set_message("I was not able to store the cvterm data for $cvterm_name.", 'error', FALSE);
00607 return FALSE;
00608 }
00609 }
00610 }
00611
00623 function icbasic_get_add_dbxref_withnames($dbname, $accession) {
00624 if (empty($dbname) || empty($accession)) {
00625 return FALSE;
00626 }
00627 $dbxref_id = '';
00628 $select_dbxref = 'SELECT dbxref_id from {ic_dbxref} as dbxref JOIN {ic_db} as db ON dbxref.db_id=db.db_id ' . " WHERE dbxref.accession='%s' AND db.name='%s'";
00629 $res = db_fetch_array(db_query($select_dbxref, $accession, $dbname));
00630 $dbxref_id = $res['dbxref_id'];
00631 if (!empty($dbxref_id)) {
00632 return ($dbxref_id);
00633 }
00634 else {
00635
00636 $db_id = '';
00637 $select_db_id = "SELECT db_id from {ic_db} as db where name='%s'";
00638 $res = db_fetch_array(db_query($select_db_id, $dbname));
00639 $db_id = $res['db_id'];
00640 if (empty($db_id)) {
00641
00642 $insert_db_id = "INSERT INTO {ic_db} (name) VALUES ('%s')";
00643 db_query($insert_db_id, $dbname);
00644 $res = db_fetch_array(db_query($select_db_id, $dbname));
00645 $db_id = $res['db_id'];
00646 }
00647 if (empty($db_id)) {
00648 drupal_set_message("I was not able to store the db data for $dbname.", 'error', FALSE);
00649 return FALSE;
00650 }
00651
00652 $insert_dbxref = "INSERT INTO {ic_dbxref} (accession,db_id) VALUES ('%s',%d)";
00653 db_query($insert_dbxref, $accession, $db_id);
00654 $res = db_fetch_array(db_query($select_dbxref, $accession, $db_id));
00655 $dbxref_id = $res['dbxref_id'];
00656 if (!empty($dbxref_id)) {
00657 return $dbxref_id;
00658 }
00659 else {
00660 drupal_set_message("I was not able to store the dbxref data for $accession.", 'error', FALSE);
00661 return FALSE;
00662 }
00663 }
00664 }
00665
00674 function icbasic_clear_session($var = NULL) {
00675 if (empty($var)) {
00676 $_SESSION = array();
00677 }
00678 else {
00679 if (is_array($var)) {
00680 foreach ($var as $element) {
00681 unset($_SESSION[$element]);
00682 }
00683 }
00684 else {
00685 unset($_SESSION[$var]);
00686 }
00687 }
00688 return TRUE;
00689 }
00690
00712 function icbasic_create_uid($sessionid = NULL, $timestamp = NULL, $string = NULL) {
00713 if (empty($sessionid)) {
00714 $sessionid = session_id();
00715 }
00716 if (empty($timestamp)) {
00717 $timestamp = time();
00718 }
00719
00720 $uid = md5($sessionid . $timestamp . $string);
00721 return $uid;
00722 }
00723
00736 function icbasic_get_resource_name_id($resource, $name_as_key = FALSE) {
00737 if (empty($resource)) {
00738 return FALSE;
00739 }
00740 $return_array = array();
00741 foreach ((array)$resource as $r) {
00742 $sql_select = 'SELECT resource_id as id,uniquename from {ic_resource} where ';
00743 if (is_numeric($r)) {
00744 $sql_select .= 'resource_id=%d';
00745 }
00746 else {
00747 $sql_select .= "uniquename='%s'";
00748 }
00749 $res = db_fetch_array(db_query($sql_select, $r));
00750 if (!empty($name_as_key)) {
00751 $return_array[$res['uniquename']] = $res['id'];
00752 }
00753 else {
00754 $return_array[$res['id']] = $res['uniquename'];
00755 }
00756 }
00757 }
00758
00778 function icbasic_get_add_resource($type_name, $data = NULL, $add = FALSE, $name_as_key = FALSE) {
00779 if (empty($type_name)) {
00780 return FALSE;
00781 }
00782 $return_array = array();
00783 $type_id = icbasic_get_add_cv_withnames('resource_type', $type_name);
00784 if (empty($type_id)) {
00785 drupal_set_message('Could not add cvterm for '. $type_name, 'error');
00786 return FALSE;
00787 }
00788
00789 $sql_select = "SELECT resource_id,uniquename from {ic_resource} where type_id=%d";
00790
00791
00792 if (!empty($add)) {
00793 $dbxref_id = icbasic_get_add_dbxref_withnames('local resource', $data['dbxref_name']);
00794 if (empty($dbxref_id)) {
00795 drupal_set_message('Could not add resource because could not get dbxref id for '. $data['dbxref_name'], 'error');
00796 return FALSE;
00797 }
00798 $name = $data['uniquename'];
00799 if (empty($name)) {
00800 drupal_set_message("Did not $add requested resource because name/id was missing or not given correctly", 'error');
00801 return FALSE;
00802 }
00803 $sql_delete = "DELETE from {ic_resource} where type_id=%d and uniquename='%s'";
00804 $sql_insert = "INSERT into {ic_resource} (type_id,uniquename,dbxref_id) VALUES (%d,'%s',%d)";
00805 $sql_check = "SELECT resource_id from {ic_resource} where uniquename='%s'";
00806
00807 switch ($add) {
00808 case 'REPLACE':
00809 $check = db_fetch_array(db_query($sql_check, $name));
00810 if (!empty($check)) {
00811 db_query($sql_delete, $type_id, $name);
00812 }
00813 db_query($sql_insert, $type_id, $name, $dbxref_id);
00814 break;
00815
00816 case 'ADD':
00817 $check = db_fetch_array(db_query($sql_check, $name));
00818 if (empty($check)) {
00819 db_query($sql_insert, $type_id, $name, $dbxref_id);
00820 }
00821 break;
00822
00823 case 'DELETE':
00824 $check = db_fetch_array(db_query($sql_check, $name));
00825 if (!empty($check)) {
00826 db_query($sql_delete, $type_id, $name);
00827 }
00828 return TRUE;
00829 }
00830 }
00831 else {
00832
00833 if (!empty($data)) {
00834 foreach ($data as $key => $value) {
00835 $sql_select .= " AND $key='$value'";
00836 }
00837 }
00838 }
00839 $db_results = db_query($sql_select, $type_id);
00840 while ($row = db_fetch_array($db_results)) {
00841 if (!empty($name_as_key) && $name_as_key === 'BOTH') {
00842 $return_array[$row['uniquename']] = $row['uniquename'];
00843 }
00844 elseif (empty($name_as_key)) {
00845 $return_array[$row['resource_id']] = $row['uniquename'];
00846 }
00847 else {
00848 $return_array[$row['uniquename']] = $row['resource_id'];
00849 }
00850 }
00851
00852 if (!empty($return_array)) {
00853 return $return_array;
00854 }
00855 else {
00856 return FALSE;
00857 }
00858 }
00859
00872 function icbasic_get_resource_cvterm_and_prop($resource) {
00873 $return_array = array();
00874 if (empty($resource)) {
00875 return FALSE;
00876 }
00877 $cvterm_select = 'SELECT cvterm.name as cvterm_name,cv.name as cv_name from {ic_resource_cvterm} as r ' . ' JOIN {ic_cvterm} as cvterm ON r.cvterm_id=cvterm.cvterm_id' . ' JOIN {ic_cv} as cv ON cv.cv_id=cvterm.cv_id ';
00878
00879 $prop_select = 'SELECT cvterm.name as cvterm_name,r.value,r.rank from {ic_resourceprop} as r ' . ' JOIN {ic_cvterm} as cvterm ON r.type_id=cvterm.cvterm_id ';
00880 if (is_numeric($resource)) {
00881 $cvterm_select .= ' WHERE r.resource_id=%d ';
00882 $prop_select .= ' WHERE r.resource_id=%d ';
00883 }
00884 else {
00885 $cvterm_select .= " WHERE r.resource_id=(SELECT resource_id from {ic_resource} where uniquename='%s')";
00886 $prop_select .= " WHERE r.resource_id=(SELECT resource_id from {ic_resource} where uniquename='%s')";
00887 }
00888
00889 $prop_select .= ' ORDER by r.rank';
00890 $cv_res = db_query($cvterm_select, $resource);
00891 while ($row = db_fetch_array($cv_res)) {
00892 $return_array['cvterm'][$row['cv_name']][] = $row['cvterm_name'];
00893 }
00894 $prop_res = db_query($prop_select, $resource);
00895 while ($row = db_fetch_array($prop_res)) {
00896 $return_array['prop'][$row['cvterm_name']][$row['rank']] = $row['value'];
00897 }
00898 if (!empty($return_array)) {
00899 return $return_array;
00900 }
00901 else {
00902 return FALSE;
00903 }
00904 }
00905
00923 function icbasic_link_unlink_resources($subject, $rel, $object = NULL, $replace = FALSE) {
00924 if (empty($subject) || empty($rel)) {
00925 return FALSE;
00926 }
00927 $type_id = icbasic_get_add_cv_withnames('relationship', $rel);
00928 if (empty($type_id)) {
00929 drupal_set_message('Could not add cvterm for '. $rel, 'error');
00930 return FALSE;
00931 }
00932 $object_grab = '';
00933 $subject_grab = "(SELECT resource_id from {ic_resource} where uniquename='%s')";
00934 if (!empty($object)) {
00935 $object_grab = "(SELECT resource_id from {ic_resource} where uniquename='%s')";
00936 }
00937 if (is_numeric($subject)) {
00938 $subject_grab = '%d';
00939 }
00940 if (!empty($object) && is_numeric($object)) {
00941 $object_grab = '%d';
00942 }
00943
00944 if (empty($object)) {
00945 if ($replace === 'DELETE') {
00946 $sql_delete_all = 'DELETE FROM {ic_resource_resource} where subject_id='. $subject_grab ."and type_id=%d";
00947 db_query($sql_delete_all, $subject, $type_id);
00948 return TRUE;
00949 }
00950 return FALSE;
00951 }
00952
00953 $sql_check = 'SELECT resource_resource_id from {ic_resource_resource} where subject_id='. $subject_grab .' AND object_id='. $object_grab;
00954 $sql_insert = 'INSERT INTO {ic_resource_resource} (subject_id,object_id,type_id) VALUES ('. $subject_grab .','. $object_grab .',%d)';
00955 $sql_delete = 'DELETE FROM {ic_resource_resource} where subject_id='. $subject_grab .' AND object_id='. $object_grab;
00956
00957 $res = db_fetch_array(db_query($sql_check, $subject, $object));
00958 switch ($replace) {
00959 case FALSE:
00960 if (empty($res)) {
00961 db_query($sql_insert, $subject, $object, $type_id);
00962
00963 return TRUE;
00964 }
00965 else {
00966
00967 return TRUE;
00968 }
00969 break;
00970
00971 case 'DELETE':
00972 if (empty($res)) {
00973 return TRUE;
00974 }
00975 else {
00976 db_query($sql_delete, $subject, $object);
00977 $res = db_fetch_array(db_query($sql_check, $subject, $object));
00978 if (empty($res)) {
00979
00980 return TRUE;
00981 }
00982 else {
00983 dvm("Failed to delete previous relationship for $subject $object.");
00984 return FALSE;
00985 }
00986 }
00987 break;
00988
00989 case TRUE:
00990 if (empty($res)) {
00991 db_query($sql_insert, $subject, $object, $type_id);
00992
00993 return TRUE;
00994 }
00995 else {
00996 db_query($sql_delete, $subject, $object);
00997 $res = db_fetch_array(db_query($sql_check, $subject, $object));
00998 if (empty($res)) {
00999 db_query($sql_insert, $subject, $object, $type_id);
01000
01001 return TRUE;
01002 }
01003 else {
01004 dvm("Failed to delete previous relationship for $subject $object.");
01005 return FALSE;
01006 }
01007 }
01008 break;
01009 }
01010 }
01011
01028 function icbasic_get_linked_resources($resource, $relationship_name = NULL, $name_as_key = FALSE, $limit = NULL) {
01029 $return_array = array();
01030 if (empty($resource)) {
01031 return FALSE;
01032 }
01033 $sql_select = 'SELECT rr.object_id,rocvterm.name as object_type,ro.uniquename as object_name ' . 'from {ic_resource_resource} as rr ' . ' JOIN {ic_resource} as ro ON ro.resource_id=rr.object_id ' . ' JOIN {ic_cvterm} as rocvterm ON rocvterm.cvterm_id=ro.type_id ';
01034
01035 if (is_numeric($resource)) {
01036 $sql_select .= 'WHERE rr.subject_id=%d';
01037 }
01038 else {
01039 $sql_select .= "WHERE rr.subject_id=(SELECT resource_id from {ic_resource} where uniquename='%s')";
01040 }
01041 if (!empty($relationship_name)) {
01042 $type_id = icbasic_get_add_cv_withnames('relationship', $relationship_name);
01043 $sql_select .= " AND rr.type_id=$type_id ";
01044 }
01045 if (!empty($limit)) {
01046 $sql_select .= " AND $limit ";
01047 }
01048 $res = db_query($sql_select, $resource);
01049 while ($row = db_fetch_array($res)) {
01050 if (!empty($name_as_key)) {
01051 $return_array[$row['object_name']] = array(
01052 'type_name' => $row['object_type'],
01053 'uniquename' => $row['object_name'],
01054 'id' => $row['object_id'],
01055 );
01056 }
01057 else {
01058 $return_array[$row['object_id']] = array(
01059 'type_name' => $row['object_type'],
01060 'uniquename' => $row['object_name'],
01061 'id' => $row['object_id'],
01062 );
01063 }
01064 }
01065
01066 if (!empty($return_array)) {
01067 return $return_array;
01068 }
01069 else {
01070 return FALSE;
01071 }
01072 }
01073
01089 function icbasic_write_fastafile($infile = NULL, $outfile = NULL, $delete = NULL) {
01090 if (empty($infile) || empty($outfile)) {
01091 return FALSE;
01092 }
01093
01094 ini_set('auto_detect_line_endings', TRUE);
01095
01096
01097
01098
01099
01100
01101
01102
01103
01104
01105
01106
01107
01108
01109
01110
01111
01112
01113 if (!$outhandle = fopen($outfile, 'wt')) {
01114 drupal_set_message(t("Could not create $outfile."), 'error');
01115 return FALSE;
01116 }
01117
01118 if (file_exists($infile)) {
01119 if ($inhandle = fopen($infile, 'rb')) {
01120 while (!feof($inhandle)) {
01121 $line = trim(fgets($inhandle));
01122 if (!empty($line)) {
01123 $defline = strpos($line, '>');
01124 if ($defline !== FALSE) {
01125
01126 if ($defline > 0) {
01127 $line = substr($line, $defline);
01128 }
01129 }
01130
01131
01132
01133 if (!empty($line)) {
01134
01135 $data = $line ."\n";
01136 fwrite($outhandle, $data);
01137 }
01138 }
01139 }
01140 fclose($inhandle);
01141 }
01142 }
01143 elseif (!empty($infile)) {
01144
01145 $seq_array = explode("\r", $infile);
01146 foreach ($seq_array as $line) {
01147 $line = trim($line);
01148 if (!empty($line)) {
01149 $defline = strpos($line, '>');
01150 if ($defline !== FALSE) {
01151 if ($defline > 0) {
01152 $line = substr($line, $defline);
01153 }
01154 }
01155
01156
01157
01158 if (!empty($line)) {
01159 $data = $line ."\n";
01160 fwrite($outhandle, $data);
01161 }
01162 }
01163 }
01164 }
01165 else {
01166 drupal_set_message(t("Could not find any input data for writing the FASTA."), 'error');
01167 }
01168
01169 fclose($outhandle);
01170 if (!empty($delete) && $delete === TRUE && file_exists($infile)) {
01171 file_delete($infile);
01172 }
01173 return TRUE;
01174 }
01175