00001 <?php
00002
00003
00004
00028 function dbsf_init() {
00029 drupal_add_css(drupal_get_path('module', 'dbsf') .'/icbasic.css', 'module');
00030 }
00031
00035 function dbsf_perm() {
00036 $perms = array('Access dbsf', 'Access BLAST Server', 'Allow sequence retrieval', 'Administrate BLAST Server', 'Administrate dbsf');
00037 if (file_exists(drupal_get_path('module', 'dbsf') .'/dbsf_ssaha2.inc')) {
00038 $perms[] = 'Access SSAHA2 Server';
00039 $perms[] = 'Administrate SSAHA2 Server';
00040 }
00041 return $perms;
00042 }
00043
00047 function dbsf_theme() {
00048 $themes = array(
00049 'dbsf_blast_result_form' => array(
00050 'arguments' => array('form'),
00051 'function' => 'dbsf_theme_blast_result_form',
00052 ),
00053 );
00054 return ($themes);
00055 }
00056
00062 function dbsf_menu() {
00063 $items = array();
00064 $items['dbsf/blast_result'] = array(
00065 'title' => 'dbsf BLAST Results',
00066 'page callback' => 'dbsf_blast_result_page',
00067 'page arguments' => array(2),
00068 'access arguments' => array('Access BLAST Server'),
00069 'type' => MENU_CALLBACK,
00070 );
00071 $items['dbsf/get_sequences'] = array(
00072 'title' => 'dbsf Retrieve sequences',
00073 'page callback' => 'dbsf_get_sequence',
00074 'page arguments' => array(2),
00075 'access arguments' => array('Allow sequence retrieval'),
00076 'type' => MENU_CALLBACK,
00077 );
00078 $items['dbsf/software'] = array(
00079 'title' => 'Select dbsf software to use',
00080 'page callback' => 'dbsf_software_select_page',
00081 'access arguments' => array('Access dbsf'),
00082 'type' => MENU_CALLBACK,
00083 );
00084 $items['dbsf/blast'] = array(
00085 'title' => 'dbsf BLAST Server',
00086 'page callback' => 'dbsf_blast_page',
00087 'page arguments' => array(2),
00088 'access arguments' => array('Access BLAST Server'),
00089 'type' => MENU_CALLBACK,
00090 );
00091 $items['admin/settings/dbsf'] = array(
00092 'title' => 'Drupal Bioinformatic Server framework settings',
00093 'page callback' => 'dbsf_admin_page',
00094 'access arguments' => array('Administrate dbsf'),
00095 'description' => 'Configure the dbsf module',
00096
00097 'type' => MENU_NORMAL_ITEM,
00098 );
00099
00100 $items['admin/dbsf/software'] = array(
00101 'title' => 'Administrate Software for the dbsf',
00102 'page callback' => 'dbsf_admin_software_page',
00103 'access arguments' => array('Administrate dbsf'),
00104 'description' => 'Configure which software are made available via the dbsf',
00105 'type' => MENU_CALLBACK,
00106 );
00107 $items['admin/dbsf/dataset'] = array(
00108 'title' => 'Administrate datasets/db for the dbsf',
00109 'page callback' => 'dbsf_admin_dataset_page',
00110 'access arguments' => array('Administrate dbsf'),
00111 'description' => 'Configure which datasets/databases are loaded for dbsf',
00112 'type' => MENU_NORMAL_ITEM,
00113 );
00114
00115 $items['admin/dbsf/blast'] = array(
00116 'title' => 'BLAST Server administration',
00117 'page callback' => 'dbsf_admin_blast_page',
00118 'access arguments' => array('Administrate BLAST Server'),
00119 'description' => 'Configure BLASTALL specific settings',
00120 'type' => MENU_NORMAL_ITEM,
00121 );
00122
00123 if (file_exists(drupal_get_path('module', 'dbsf') .'/dbsf_ssaha2.inc')) {
00124 require_once (drupal_get_path('module', 'dbsf') .'/dbsf_ssaha2.inc');
00125 $new_items = dbsf_ssaha2_menu();
00126 $items = array_merge($items, $new_items);
00127 }
00128 return $items;
00129 }
00130
00136 function dbsf_software_select_page() {
00137 $software_avail = array();
00138 $software_res = db_query('SELECT uniquename,description from {ic_software} where active is true');
00139 while ($row = db_fetch_array($software_res)) {
00140 $software_avail[$row['uniquename']] = $row['description'];
00141 }
00142 if (empty($software_avail)) {
00143 drupal_set_message('Sorry, but the administrator has not activated any software', 'error');
00144 return ' ';
00145 }
00146 $links = '';
00147 if (!empty($software_avail['blastall'])) {
00148 $links .= "<dt>NCBI's ". l('BLASTALL', 'dbsf/blast') .'</dt><dd>'. $software_avail['blastall'] .'</dd>';
00149 }
00150
00151 if (!empty($software_avail['ssaha2'])) {
00152 $links .= "<dt>Sanger's ". l('SSAHA2', 'dbsf/ssaha2') .'</dt><dd>'. $software_avail['ssaha2'] .'</dd>';
00153 }
00154 if (empty($links)) {
00155 drupal_set_message($sorry, 'error');
00156 return ' ';
00157 }
00158 return '<dl>'. $links .'</dl>';
00159 }
00160
00166 function dbsf_admin_page() {
00167 $return_text = '<dl><dt>'. l('Setup the software available', 'admin/dbsf/software', array('attributes' => array('target' => '_blank'))) .'</dt><dd>Specify which software to activate for your search server</dd>';
00168 $return_text .= '<dt>'. l('Setup available reference datasets/databases', 'admin/dbsf/dataset', array('attributes' => array('target' => '_blank'))) .'</dt><dd>Load databases and datasets, specify which software and which users will have access to them.';
00169 $return_text .= '<dt>'. l('Setup the BLAST server', 'admin/dbsf/blast', array('attributes' => array('target' => '_blank'))) ."</dt><dd>Specify the necessary variables and databases to use with NCBI's BLAST</dd></dl>";
00170 if (file_exists(drupal_get_path('module', 'dbsf') .'/dbsf_ssaha2.inc')) {
00171 $return_text .= '<dt>'. l('Setup the SSAHA2 server', 'admin/dbsf/ssaha2', array('attributes' => array('target' => '_blank'))) ."</dt><dd>Specify the necessary variables and databases to use with Sangers's SSAHA2</dd></dl>";
00172 }
00173 return $return_text;
00174 }
00175
00185 function dbsf_check_software_active($software) {
00186 if (empty($software)) {
00187 return FALSE;
00188 }
00189 $active_sql = "SELECT active from {ic_software} where ";
00190 if (is_numeric($software)) {
00191 $active_sql .= " software_id=$software";
00192 }
00193 else {
00194 $active_sql .= " uniquename='$software'";
00195 }
00196 $res = db_fetch_array(db_query($active_sql));
00197 if ($res['active'] == 'f') {
00198 return FALSE;
00199 }
00200 else {
00201 return TRUE;
00202 }
00203 }
00204
00210 function dbsf_admin_dataset_page() {
00211 $add_dataset_variables = drupal_get_form('dbsf_admin_dataset_variables_form');
00212 $add_dataset_data = drupal_get_form('dbsf_admin_newdataset_form');
00213 $dataset_perm = drupal_get_form('dbsf_admin_dataset_perms_form');
00214 $link_dataset = drupal_get_form('dbsf_admin_link_dataset_software_form');
00215 $db_table = dbsf_admin_database_table();
00216 $file_table = dbsf_admin_database_files_table();
00217
00218 if (strlen($link_dataset) < 600) {
00219 $link_dataset = '';
00220 }
00221 if (strlen($add_dataset_data) < 600) {
00222 $add_dataset_data = '';
00223 drupal_set_message('You will need to define at least one group and one directory in </b>Dataset variables</b> before registering new datasets.', 'error');
00224 }
00225 if (strlen($dataset_perm) < 600) {
00226 $dataset_perm = '';
00227 }
00228 $dataset_perm .= $db_table;
00229 $link_dataset .= $db_table;
00230 $add_dataset_data .= $file_table;
00231
00232 $page_tabs = array(
00233 'dataset' => array(
00234 '#type' => 'tabset',
00235 'variables' => array(
00236 '#type' => 'tabpage',
00237 '#title' => 'Dataset variables',
00238 '#content' => $add_dataset_variables,
00239 '#weight' => 4,
00240 ),
00241 'add' => array(
00242 '#type' => 'tabpage',
00243 '#title' => 'Available datasets',
00244 '#content' => $add_dataset_data,
00245 '#weight' => 2,
00246 ),
00247 'perm' => array(
00248 '#type' => 'tabpage',
00249 '#title' => 'User permissions',
00250 '#content' => $dataset_perm,
00251 '#weight' => 3,
00252 ),
00253 'link' => array(
00254 '#type' => 'tabpage',
00255 '#title' => 'Link dataset w/ software',
00256 '#content' => $link_dataset,
00257 '#weight' => 4,
00258 ),
00259 ),
00260 );
00261 $return_text = tabs_render($page_tabs);
00262 return $return_text;
00263 }
00264
00273 function dbsf_admin_software_page() {
00274 $software_data = drupal_get_form('dbsf_software_form');
00275 $blast_data = drupal_get_form('dbsf_software_blast_core_settings_form');
00276 if (strlen($blast_data) < 600) {
00277 $blast_data = '';
00278 }
00279 $page_tabs = array(
00280 'software' => array(
00281 '#type' => 'tabset',
00282 'available' => array(
00283 '#type' => 'tabpage',
00284 '#title' => 'Available software',
00285 '#content' => $software_data,
00286 '#weight' => 0,
00287 ),
00288 'blast' => array(
00289 '#type' => 'tabpage',
00290 '#title' => 'BLASTALL',
00291 '#content' => $blast_data,
00292 '#weight' => 4,
00293 ),
00294 ),
00295 );
00296
00297
00298 $plugin_path = drupal_get_path('module', 'dbsf') .'/';
00299 $plugins = array();
00300 if (file_exists($plugin_path .'dbsf_ssaha2.inc')) {
00301 $plugins['ssaha2']['file'] = $plugin_path .'dbsf_ssaha2.inc';
00302 }
00303 foreach ($plugins as $plugin_name => $plugin_data) {
00304 require_once ($plugin_data['file']);
00305 $data = '';
00306 if (function_exists('dbsf_software_'. $plugin_name .'_core_settings_form')) {
00307 $data = drupal_get_form('dbsf_software_'. $plugin_name .'_core_settings_form');
00308 if (strlen($data) < 600) {
00309 $data = '';
00310 }
00311 if (!empty($data)) {
00312 $page_tabs['software'][$plugin_name] = array(
00313 '#type' => 'tabpage',
00314 '#title' => strtoupper($plugin_name),
00315 '#content' => $data,
00316 '#weight' => 5,
00317 );
00318 }
00319 }
00320 }
00321
00322 $return_text = tabs_render($page_tabs);
00323 return $return_text;
00324 }
00325
00336 function dbsf_software_form($form_state) {
00337 $software_array = array();
00338 $software_select = 'SELECT uniquename,description,active from {ic_software}';
00339 $res = db_query($software_select);
00340 while ($row = db_fetch_array($res)) {
00341 $software_array[$row['uniquename']]['description'] = $row['description'];
00342 $software_array[$row['uniquename']]['active'] = $row['active'];
00343 }
00344
00345 $form = array();
00346 $form['software'] = array(
00347 '#type' => 'fieldset',
00348 '#title' => t('Availabe software'),
00349 '#description' => t('Please specify which software will be active.'),
00350 '#tree' => TRUE,
00351 '#weight' => 1,
00352 );
00353 foreach ($software_array as $software => $value) {
00354 if ($value['active'] == 'f') {
00355 $value['active'] = FALSE;
00356 }
00357 else {
00358 $value['active'] = TRUE;
00359 }
00360 $form['software'][$software] = array(
00361 '#type' => 'checkbox',
00362 '#title' => t($software),
00363 '#description' => t($value['description']),
00364 '#default_value' => $value['active'],
00365 );
00366 }
00367 $form['buttons'] = array(
00368 '#type' => 'submit',
00369 '#value' => t('Update'),
00370 '#weight' => 2,
00371 );
00372 return $form;
00373 }
00374
00385 function dbsf_software_form_submit($form, &$form_state) {
00386
00387
00388 $options = $form_state['values']['software'];
00389 $sql_activate = "UPDATE {ic_software} set active=TRUE where uniquename='%s'";
00390 $sql_deactivate = "UPDATE {ic_software} set active=FALSE where uniquename='%s'";
00391 foreach ($options as $name => $active) {
00392 if ($active === 1) {
00393 db_query($sql_activate, $name);
00394 drupal_set_message("Software $name is active.", 'warning');
00395 }
00396 else {
00397 db_query($sql_deactivate, $name);
00398 drupal_set_message("Software $name is not active.", 'warning');
00399 }
00400 }
00401 }
00402
00412 function dbsf_admin_dataset_variables_form($form_state) {
00413 $form = array();
00414 $form['directories'] = array(
00415 '#type' => 'fieldset',
00416 '#title' => "Dataset/DB paths",
00417 'directory_name' => array(
00418 '#type' => 'textfield',
00419 '#description' => 'Here you can register a directory on the server that contains one or more databases for BLAST or other software.',
00420 '#size' => 50,
00421 '#title' => "Full path to directory containing your datasets",
00422 ),
00423 'directory_submit' => array(
00424 '#type' => 'submit',
00425 '#value' => t('Register directory'),
00426 ),
00427 );
00428
00429 $form['groups'] = array(
00430 '#type' => 'fieldset',
00431 '#title' => "File Groups",
00432 'group_name' => array(
00433 '#type' => 'textfield',
00434 '#description' => 'In order to help users when viewing a large number of datasets, you can create groups for them.',
00435 '#size' => 20,
00436 '#title' => "Group name",
00437 ),
00438 'group_submit' => array(
00439 '#type' => 'submit',
00440 '#value' => t('Create group'),
00441 ),
00442 );
00443 return $form;
00444 }
00445
00457 function dbsf_admin_dataset_variables_form_validate($form, &$form_state) {
00458 switch ($form_state['clicked_button']['#value']) {
00459 case 'Register directory':
00460 $directory_name = check_plain(trim($form_state['values']['directory_name']));
00461 if (empty($directory_name)) {
00462 form_set_error('directory_name', 'If you want to register a new directory, you must provide a name!');
00463 }
00464 if (!file_exists($directory_name)) {
00465 form_set_error('directory_name', "Cannot find directory $directory_name.");
00466 }
00467 elseif (substr($directory_name, -1) !== '/') {
00468 $directory_name .= '/';
00469 $form_state['values']['directory_name'] = $directory_name;
00470 }
00471 break;
00472
00473 case 'Create group':
00474 $group_name = check_plain(trim($form_state['values']['group_name']));
00475 if (empty($group_name)) {
00476 form_set_error('group_name', 'If you want to create a new group, you must provide a name!');
00477 }
00478 else {
00479 $form_state['values']['group_name'] = $group_name;
00480 }
00481 break;
00482 }
00483 }
00484
00496 function dbsf_admin_dataset_variables_form_submit($form, &$form_state) {
00497 switch ($form_state['clicked_button']['#value']) {
00498 case 'Register directory':
00499 $directory_name = $form_state['values']['directory_name'];
00500 if (empty($directory_name)) {
00501 return FALSE;
00502 }
00503 $res = icbasic_get_add_resource('directory', array('uniquename' => $directory_name, 'dbxref_name' => 'local resource'), 'ADD');
00504 if (!empty($res)) {
00505 drupal_set_message("Directory $directory_name saved.", 'warning');
00506 }
00507 break;
00508
00509 case 'Create group':
00510 $group_name = $form_state['values']['group_name'];
00511 if (empty($group_name)) {
00512 return FALSE;
00513 }
00514 icbasic_get_add_resource('dataset_group', array('uniquename' => $group_name, 'dbxref_name' => 'local resource'), 'ADD');
00515 if (!empty($res)) {
00516 drupal_set_message("Group $group_name saved.", 'warning');
00517 }
00518 break;
00519 }
00520 }
00521
00532 function dbsf_admin_newdataset_form($form_state) {
00533 $form = array();
00534
00535 $directory_array = icbasic_get_add_resource('directory');
00536 $group_array = icbasic_get_add_resource('dataset_group');
00537 $dbtypes_array = icbasic_get_add_cv_withnames('dataset_type', NULL, 'BOTH');
00538 if (empty($directory_array)) {
00539
00540 return $form;
00541 }
00542 if (empty($group_array)) {
00543
00544 return $form;
00545 }
00546
00547 $form['data'] = array(
00548 '#type' => 'fieldset',
00549 '#title' => 'Database settings',
00550 '#description' => 'You can register new datasets to the system by providing the filename and the directory on the server. In order to remove datasets, just provide the <b>friendly name</b> you gave before and click <b>remove DB</b>.',
00551 'friendly_name' => array(
00552 '#type' => 'textfield',
00553 '#size' => 50,
00554 '#title' => 'Friendly name',
00555 '#description' => 'This will be shown to users instead of the filename',
00556 '#weight' => 2,
00557 '#required' => TRUE,
00558 ),
00559 'filename' => array(
00560 '#weight' => 1,
00561 '#type' => 'textfield',
00562 '#size' => 100,
00563 '#title' => t('Filename'),
00564 '#description' => t('Please provide the basename of the database (e.g. as you would pass it to the -d option of BLASTALL).' . ' <b>Avoid any suffix created by a particular software (e.g. for BLAST, .pal/.nal can be created)</b>.'
00565 ),
00566 ),
00567 'db_type' => array(
00568 '#weight' => 2,
00569 '#type' => 'radios',
00570 '#title' => 'Molecule type',
00571 '#description' => 'Select what molecule types are supported by this basename. You can select <i>both</i> if, for example, a nucleotide and protein BLAST database share the same basename.',
00572 '#options' => $dbtypes_array,
00573 ),
00574 'directory_id' => array(
00575 '#weight' => 2,
00576 '#type' => 'select',
00577 '#size' => 1,
00578 '#title' => 'Directory',
00579 '#options' => $directory_array,
00580 ),
00581 'group_id' => array(
00582 '#weight' => 3,
00583 '#type' => 'select',
00584 '#size' => 1,
00585 '#title' => 'Group',
00586 '#options' => $group_array,
00587 ),
00588 );
00589
00590 $form['db_add'] = array(
00591 '#type' => 'submit',
00592 '#value' => t('Add DB'),
00593 '#weight' => 1,
00594 );
00595 $form['db_remove'] = array(
00596 '#type' => 'submit',
00597 '#value' => 'Remove DB',
00598 '#weight' => 2,
00599 );
00600 return $form;
00601 }
00602
00614 function dbsf_admin_newdataset_form_validate($form, &$form_state) {
00615
00616 $db_type = check_plain($form_state['values']['db_type']);
00617 $form_state['values']['db_type'] = $db_type;
00618 $filename = check_plain(trim($form_state['values']['filename']));
00619 $form_state['values']['filename'] = $filename;
00620 $fullname = check_plain(trim($form_state['values']['friendly_name']));
00621 $form_state['values']['friendly_name'] = $fullname;
00622 $directory_id = check_plain($form_state['values']['directory_id']);
00623 $form_state['values']['directory_id'] = $directory_id;
00624 $group_id = check_plain($form_state['values']['group_id']);
00625 $form_state['values']['group_id'] = $group_id;
00626 $blast_id = check_plain($form_state['values']['dataset']);
00627 $form_state['values']['dataset'] = $blast_id;
00628
00629 $sql_select = 'SELECT uniquename from {ic_resource} WHERE resource_id=%d';
00630
00631 switch ($form_state['clicked_button']['#value']) {
00632 case 'Add DB':
00633 if (!empty($filename)) {
00634 if (empty($fullname)) {
00635 form_set_error('friendly_name', 'Please give a friendly name for your database.');
00636 }
00637 $res = icbasic_get_add_resource('dataset', array('uniquename' => $fullname));
00638 if (!empty($res)) {
00639 form_set_error('friendly_name', "Name $fullname has been registered already.");
00640 }
00641 if (empty($group_id)) {
00642 form_set_error('group_id', 'You must specify a group.');
00643 }
00644 if (empty($directory_id)) {
00645 form_set_error('directory_id', 'You must specify a directory.');
00646 }
00647 if (empty($db_type)) {
00648 form_set_error('db_type', 'You must specify a type of molecule.');
00649 }
00650
00651 $dbdata = db_fetch_array(db_query($sql_select, $directory_id));
00652 $filename = $dbdata['uniquename'] . $filename;
00653 if ((file_exists($filename .'.nin') || file_exists($filename .'.nal'))
00654 && (file_exists($filename .'.pin') || file_exists($filename .'.pal'))
00655 ) {
00656 if ($db_type !== 'both') {
00657 form_set_error('filename', 'You asked for '. $db_type .' as molecule type but I found it formatted as ' . 'both.');
00658 }
00659 }
00660 elseif (file_exists($filename .'.nin') || file_exists($filename .'.nal')) {
00661 if ($db_type !== 'nucleotide') {
00662 form_set_error('filename', 'You asked for '. $dbtypes_array[$db_type] .' as molecule type but I found it formatted as ' . 'nucleotide.');
00663 }
00664 }
00665 elseif (file_exists($filename .'.pin') || file_exists($filename .'.pal')) {
00666 if ($db_type !== 'protein') {
00667 form_set_error('filename', 'You asked for '. $dbtypes_array[$db_type] .' as molecule type but I found it formatted as ' . 'protein.');
00668 }
00669 }
00670 elseif (file_exists($filename)) {
00671 dpm($db_type);
00672 drupal_set_message("I found $filename in your system but it does not seem to be a BLAST database formatted with formatdb. Perhaps you gave the formatdb suffix as well? Do ignore this warning if it is not meant to be used with BLAST.", 'warning');
00673 }
00674 else {
00675 form_set_error('filename', "Sorry, cannot find file $filename in your system.");
00676 }
00677 }
00678 else {
00679 form_set_error('filename', "Filename cannot be empty when you're trying to add a database.");
00680 }
00681 break;
00682
00683 case 'Remove DB':
00684 $res = icbasic_get_add_resource('dataset', array('uniquename' => $fullname));
00685 if (empty($res)) {
00686 form_set_error('name', "Database $fullname does not exist.");
00687 }
00688 break;
00689 }
00690 }
00691
00703 function dbsf_admin_newdataset_form_submit($form, &$form_state) {
00704
00705 $filename = $form_state['values']['filename'];
00706 $fullname = $form_state['values']['friendly_name'];
00707 $directory_id = $form_state['values']['directory_id'];
00708 $group_id = $form_state['values']['group_id'];
00709 $db_type = $form_state['values']['db_type'];
00710
00711
00712 switch ($form_state['clicked_button']['#value']) {
00713 case 'Add DB':
00714 if (empty($filename)) {
00715 return;
00716 }
00717
00718 $dataset_data = icbasic_get_add_resource('dataset', array('uniquename' => $fullname, 'dbxref_name' => 'BLAST database'), 'REPLACE', TRUE);
00719
00720 $filename_data = icbasic_get_add_resource('filename', array('uniquename' => $filename, 'dbxref_name' => 'BLAST database'), 'REPLACE', TRUE);
00721 icbasic_link_unlink_resources($dataset_data[$fullname], 'uses', $filename_data[$filename]);
00722
00723 icbasic_link_unlink_resources($dataset_data[$fullname], 'uses', $group_id);
00724 icbasic_link_unlink_resources($dataset_data[$fullname], 'uses', $directory_id);
00725
00726
00727 if ($db_type == 'both') {
00728 icbasic_populate_cvterms('ic_resource', $fullname, 'dataset_type', array('nucleotide', 'protein'));
00729 }
00730 else {
00731 icbasic_populate_cvterms('ic_resource', $fullname, 'dataset_type', array($db_type));
00732 }
00733
00734 $role_link = dbsf_link_unlink_resource_role($fullname, 'authenticated user');
00735 drupal_set_message("Dataset $filename saved.", 'warning');
00736 break;
00737
00738 case 'Remove DB':
00739 icbasic_link_unlink_resources($dataset_data[$fullname], 'uses', NULL, 'DELETE');
00740 $dataset_data = icbasic_get_add_resource('dataset',
00741 array('uniquename' => $fullname, 'dbxref_name' => 'BLAST database'),
00742 'DELETE'
00743 );
00744 drupal_set_message("BLASTDB $fullname removed.", 'warning');
00745 break;
00746 }
00747 }
00748
00761 function dbsf_admin_link_dataset_software_form($form_state, $software = NULL) {
00762 $form = array();
00763 $software_array = array();
00764 if (empty($software)) {
00765 $software_array = dbsf_get_software(array('active' => TRUE), 'BOTH');
00766 if (empty($software_array)) {
00767 drupal_set_message('Sorry, but you have not activated any software', 'error');
00768 return $form;
00769 }
00770 }
00771 $dataset_array = icbasic_get_add_resource('dataset', NULL, FALSE, 'BOTH');
00772 if (empty($dataset_array)) {
00773 return $form;
00774 }
00775 $form['dataset_link'] = array(
00776 '#type' => 'fieldset',
00777 '#title' => "Link datasets with software",
00778 '#description' => 'Here you can link datasets you have submitted earlier with activated software',
00779 'dataset' => array(
00780 '#options' => $dataset_array,
00781 '#size' => 5,
00782 '#title' => 'Dataset',
00783 '#description' => 'Which dataset/database to link.',
00784 '#required' => TRUE,
00785 ),
00786 'submit' => array(
00787 '#type' => 'submit',
00788 '#value' => t('Toggle link'),
00789 '#weight' => 10,
00790 ),
00791 );
00792
00793 if (empty($software)) {
00794 $form['dataset_link']['dataset']['#type'] = 'select';
00795 $form['dataset_link']['dataset']['#multiple'] = TRUE;
00796 $form['dataset_link']['software'] = array(
00797 '#type' => 'select',
00798 '#size' => 1,
00799 '#title' => 'Software',
00800 '#description' => 'These are the activated software',
00801 '#options' => $software_array,
00802 );
00803 }
00804 else {
00805 $form['dataset_link']['dataset']['#type'] = 'checkboxes';
00806 $form['dataset_link']['software'] = array(
00807 '#type' => 'value',
00808 '#value' => $software,
00809 );
00810 }
00811
00812 return $form;
00813 }
00814
00826 function dbsf_admin_link_dataset_software_form_validate($form, &$form_state) {
00827 $dataset_array = $form_state['values']['dataset'];
00828 $software = check_plain($form_state['values']['software']);
00829 if (empty($dataset_array) || empty($software)) {
00830 form_set_error();
00831 }
00832
00833 $suffix_array = array(
00834 'blastall' => array('suffix' => array('pin', 'nin', 'pal', 'nal'), 'switch' => 'ANY'),
00835 'ssaha2' => array('suffix' => array('head', 'body', 'base', 'name', 'size'), 'switch' => 'ALL'),
00836 );
00837 if (empty($suffix_array[$software])) {
00838 form_set_error('software', "Software $software does not allow dataset linking.");
00839 return FALSE;
00840 }
00841 foreach ($dataset_array as $key => $dataset) {
00842 $dataset = check_plain($dataset);
00843 $dataset_array[$key] = $dataset;
00844 if (empty($dataset)) {
00845 continue;
00846 }
00847 $format_check = 0;
00848 $fullpath = dbsf_get_dataset_path($dataset);
00849 $basename = basename($fullpath);
00850 $dirname = dirname($fullpath);
00851 $f = file_scan_directory($dirname, $basename .'\..+$', array('.', '..', 'CVS'), 0, FALSE, 'basename');
00852 foreach ($f as $fname => $obj) {
00853 $suffix = str_replace($basename .'.', '', $fname);
00854 if (in_array($suffix, $suffix_array[$software]['suffix'])) {
00855 $format_check++;
00856 }
00857 }
00858 switch ($suffix_array[$software]['switch']) {
00859 case 'ANY':
00860 if ($format_check < 1) {
00861 form_set_error('dataset', "It doesn't seem that $dataset is properly formatted for $software.");
00862 }
00863 break;
00864
00865 case 'ALL':
00866 if ($format_check < count($suffix_array[$software]['suffix'])) {
00867 form_set_error('dataset', "It doesn't seem that $dataset is properly formatted for $software.");
00868 }
00869 break;
00870 }
00871 }
00872 $form_state['values']['software'] = $software;
00873 $form_state['values']['dataset'] = $dataset_array;
00874 }
00875
00887 function dbsf_admin_link_dataset_software_form_submit($form, &$form_state) {
00888 $dataset_array = $form_state['values']['dataset'];
00889 $software = $form_state['values']['software'];
00890 if (empty($dataset_array) || empty($software)) {
00891 return FALSE;
00892 }
00893 foreach ($dataset_array as $dataset) {
00894 if (empty($dataset)) {
00895 continue;
00896 }
00897 $result = dbsf_link_unlink_software_resource($software, $dataset, 'TOGGLE');
00898 switch ($result) {
00899 case FALSE:
00900 drupal_set_message("Failed to change link of $dataset with $software", 'error');
00901 break;
00902
00903 case 'GRANTED':
00904 drupal_set_message("Dataset $dataset linked with $software.", 'warning');
00905 break;
00906
00907 case 'REVOKED':
00908 drupal_set_message("Dataset $dataset unlinked from $software.", 'warning');
00909 break;
00910 }
00911 }
00912 }
00913
00924 function dbsf_admin_dataset_perms_form($form_state) {
00925 $form = array();
00926
00927 $dataset_array = icbasic_get_add_resource('dataset');
00928 $role_array = dbsf_get_role();
00929 if (empty($dataset_array)) {
00930 return $form;
00931 }
00932 if (empty($role_array)) {
00933 drupal_set_message('This is very weird, it seems I cannot get the roles from your Drupal installation. Please file a bug report.');
00934 return $form;
00935 }
00936 $form['access'] = array(
00937 '#type' => 'fieldset',
00938 '#title' => "Data access settings",
00939 '#description' => 'If you wish, you can protect datasets (e.g. databases for BLAST or other software) by asking that they are accessible only to users belonging to specific user roles.' . ' By default, all new datasets are accessible only to logged in users (authenticated). If you wish for all users to be able to see the database, add permissions for the anonymous' . ' group too; alternatively, you may remove permissions from the authenticated group and add them to a new private one you created via Drupal.',
00940 'role' => array(
00941 '#type' => 'select',
00942 '#size' => 1,
00943 '#title' => 'Role',
00944 '#description' => 'These roles are defined by '. l("Drupal's role management", 'admin/user/roles'),
00945 '#options' => $role_array,
00946 ),
00947 'dataset' => array(
00948 '#type' => 'select',
00949 '#size' => 10,
00950 '#multiple' => TRUE,
00951 '#title' => 'Dataset',
00952 '#description' => 'Which dataset(s) to protect.',
00953 '#options' => $dataset_array,
00954 ),
00955 'access_submit' => array(
00956 '#type' => 'submit',
00957 '#value' => t('Toggle permission setting'),
00958 ),
00959 );
00960 return $form;
00961 }
00962
00974 function dbsf_admin_dataset_perms_form_submit($form, &$form_state) {
00975 $role_id = $form_state['values']['role'];
00976 $dataset_id_array = $form_state['values']['dataset'];
00977 if (empty($dataset_id_array) || empty($role_id)) {
00978 return FALSE;
00979 }
00980 foreach ($dataset_id_array as $dataset_id) {
00981 $result = dbsf_link_unlink_resource_role($dataset_id, $role_id, 'TOGGLE');
00982 switch ($result) {
00983 case FALSE:
00984 drupal_set_message('Failed to change dataset permissions', 'error');
00985 break;
00986
00987 case 'GRANTED':
00988 drupal_set_message("Permission granted.", 'warning');
00989 break;
00990
00991 case 'REVOKED':
00992 drupal_set_message("Permission revoked.", 'warning');
00993 break;
00994 }
00995 }
00996 }
00997
01009 function dbsf_form_clear($form, &$form_state) {
01010 unset($form_state['values']);
01011 unset($form_state['storage']);
01012 $form_state['rebuild'] = TRUE;
01013 }
01014
01024 function dbsf_admin_database_table($software = NULL) {
01025 $software_avail_array = dbsf_get_software(array('active' => TRUE), TRUE);
01026 if (!empty($software) && empty($software_avail_array[$software])) {
01027 drupal_set_message("It seems that $software is not active yet. Will show no databases for it.", 'error');
01028 return FALSE;
01029 }
01030 elseif (empty($software_avail_array)) {
01031 drupal_set_message("It seems that no software is not active yet. Will show no databases.", 'error');
01032 return FALSE;
01033 }
01034 $table_caption = 'Databases currently registered';
01035 $table_header = array('Linked with', 'Role permitted', 'File', 'Name shown to users', 'Grouping', 'Database type');
01036 $table_rows = array();
01037
01038 if (empty($software)) {
01039 $dataset_software = dbsf_get_software_resources(array('rcvterm.name' => 'dataset'), 'RESOURCE');
01040 $dataset_array = icbasic_get_add_resource('dataset');
01041 if (!empty($dataset_array)) {
01042 foreach ($dataset_array as $dataset_id => $dataset_name) {
01043 $linked_with = '';
01044 if (!empty($dataset_software) && !empty($dataset_software[$dataset_name])) {
01045 foreach ($dataset_software[$dataset_name] as $software_name => $d) {
01046 $linked_with .= $software_name .' ';
01047 }
01048 }
01049 $linked_with = trim($linked_with);
01050 if (empty($linked_with)) {
01051 $linked_with = 'none';
01052 }
01053 $to_add = array();
01054 $role_str = 'none';
01055 $role_data = dbsf_get_resource_roles($dataset_id);
01056 if (!empty($role_data)) {
01057 $role_str = implode(', ', $role_data);
01058 }
01059 $dataset_extra = icbasic_get_resource_cvterm_and_prop($dataset_id);
01060 $dbtype = implode(', ', $dataset_extra['cvterm']['dataset_type']);
01061 $linked_res_data = icbasic_get_linked_resources($dataset_id, NULL, TRUE);
01062 foreach ($linked_res_data as $object_name => $object_data) {
01063 $object_type = $object_data['type_name'];
01064 $to_add[$object_type] = $object_name;
01065 }
01066 $table_rows[] = array($linked_with, $role_str, $to_add['directory'] . $to_add['filename'], $dataset_name, $to_add['dataset_group'], $dbtype);
01067 }
01068 }
01069 }
01070 else {
01071 $table_caption .= " to $software";
01072 $table_header = array('Role permitted', 'File', 'Name shown to users', 'Grouping', 'Database type');
01073 foreach ($software_avail_array as $software_name => $software_id) {
01074 if (!empty($software) && $software_name !== $software) {
01075 continue;
01076 }
01077 $software_data = dbsf_get_software_resources(
01078 array('sr.software_id' => $software_id, 'rcvterm.name' => 'dataset'),
01079 FALSE
01080 );
01081 if (empty($software_data[$software_id])) {
01082 continue;
01083 }
01084 foreach ($software_data[$software_id] as $dataset_id => $dataset_data) {
01085 $to_add = array();
01086 $dataset_name = $dataset_data['uniquename'];
01087 $role_data = dbsf_get_resource_roles($dataset_id);
01088 if (!empty($role_data)) {
01089 $role_str = implode(', ', $role_data);
01090 }
01091 $dataset_extra = icbasic_get_resource_cvterm_and_prop($dataset_id);
01092 $dbtype = implode(', ', $dataset_extra['cvterm']['dataset_type']);
01093 $linked_res_data = icbasic_get_linked_resources($dataset_id, NULL, TRUE);
01094 foreach ($linked_res_data as $object_name => $object_data) {
01095 $object_type = $object_data['type_name'];
01096 $to_add[$object_type] = $object_name;
01097 }
01098 $table_rows[] = array($role_str, $to_add['directory'] . $to_add['filename'], $dataset_name, $to_add['dataset_group'], $dbtype);
01099 }
01100 }
01101 }
01102 $table = theme_table($table_header, $table_rows, array('width' => '750px'), $table_caption);
01103 return $table;
01104 }
01105
01112 function dbsf_admin_database_files_table() {
01113 $software_avail_array = dbsf_get_software(array('active' => TRUE), TRUE);
01114 if (empty($software_avail_array)) {
01115 drupal_set_message("It seems that no software is not active yet. Will show no databases.", 'error');
01116 return FALSE;
01117 }
01118 $directory_array = icbasic_get_add_resource('directory');
01119 if (empty($directory_array)) {
01120 drupal_set_message("It seems that no directory paths have been registered yet. Will show no databases.", 'error');
01121 return FALSE;
01122 }
01123
01124 $suffix_array = array(
01125 'blastall' => array('suffix' => array('pin', 'nin', 'pal', 'nal'), 'switch' => 'ANY'),
01126 'ssaha2' => array('suffix' => array('head', 'body', 'base', 'name', 'size'), 'switch' => 'ALL'),
01127 );
01128
01129 $table_caption = 'Files in your registered directories which could act as databases';
01130 $table_header = array('Directory', 'Filename', 'Formatted for', 'Suffixes found');
01131 $table_rows = array();
01132
01133
01134 foreach ($directory_array as $id => $name) {
01135 $name = rtrim($name, '/');
01136 foreach ($suffix_array as $software_name => $data) {
01137
01138 $files_array = array();
01139 foreach ($data['suffix'] as $suffix) {
01140 $f = file_scan_directory($name, '.+\.'. $suffix .'$', array('.', '..', 'CVS'), 0, FALSE, 'name');
01141 foreach ($f as $fname => $obj) {
01142 $files_array[$fname][] = $suffix;
01143 }
01144 }
01145 foreach ($files_array as $basename => $f_data) {
01146 $suffix_str = implode(', ', $f_data);
01147 $table_rows[] = array($name, $basename, $software_name, $suffix_str);
01148 }
01149 }
01150 }
01151 $table = theme_table($table_header, $table_rows, array('width' => '750px'), $table_caption);
01152 return $table;
01153 }
01154
01166 function dbsf_get_role($limit = NULL) {
01167 $role_array = array();
01168 $sql_select = "SELECT rid,name from {role}";
01169 if (!empty($limit)) {
01170 if (is_array($limit)) {
01171 $sql_select .= " WHERE $limit[0]=$limit[1]";
01172 }
01173 else {
01174 $sql_select .= " WHERE $limit";
01175 }
01176 }
01177 $db_results = db_query($sql_select);
01178 while ($dbdata = db_fetch_array($db_results)) {
01179 $role_array[$dbdata['rid']] = $dbdata['name'];
01180 }
01181 if (!empty($role_array)) {
01182 return $role_array;
01183 }
01184 else {
01185 return FALSE;
01186 }
01187 }
01188
01200 function _dbsf_generate_options($software_name) {
01201 if (empty($software_name)) {
01202 return FALSE;
01203 }
01204 $return_array = array();
01205 $resources = dbsf_get_software_resources(array('s.uniquename' => $software_name, 'rcvterm.name' => 'software option'), TRUE);
01206
01207 if (empty($resources[$software_name])) {
01208 drupal_set_message("Sorry, cannot find any options for $software_name stored in the database", 'error');
01209 return FALSE;
01210 }
01211 foreach ($resources[$software_name] as $id => $base_data) {
01212 $name = $base_data['uniquename'];
01213 $extra_data = icbasic_get_resource_cvterm_and_prop($id);
01214 if (empty($extra_data['cvterm']['input type'])) {
01215 continue;
01216
01217 }
01218
01219
01220 if ($name == 'query genetic code' || $name == 'DB genetic code') {
01221 $return_array[$name]['options'] = $extra_data['prop']['software option'];
01222 }
01223 elseif (!empty($extra_data['prop']['software option'])) {
01224 foreach ($extra_data['prop']['software option'] as $value) {
01225 $return_array[$name]['options'][$value] = $value;
01226 }
01227 }
01228 $return_array[$name]['type'] = implode('', $extra_data['cvterm']['input type']);
01229 $return_array[$name]['description'] = $base_data['description'];
01230 }
01231
01232 return $return_array;
01233 }
01234
01255 function dbsf_batch_upload_fasta($batch_file_data, &$context) {
01256
01257 $infilelist = $batch_file_data['infile'];
01258 $outfilelist = $batch_file_data['outfile'];
01259 $filetype = $batch_file_data['filetype'];
01260 $formatlist = $batch_file_data['format'];
01261 if (empty($infilelist) || empty($outfilelist) ||
01262 empty($filetype) || empty($formatlist)
01263 ) {
01264 dvm('Failed');
01265 return FALSE;
01266 }
01267 $max = count($infilelist);
01268 $progress = 0;
01269 foreach ($infilelist as $uid => $infile) {
01270 if (empty($infile)) {
01271 $progress++;
01272 continue;
01273 }
01274 icbasic_write_fastafile($infile, $outfilelist[$uid], TRUE);
01275 if ($formatlist[$uid] === TRUE) {
01276
01277 sleep(1);
01278 dbsf_formatdb_fastafile($outfilelist[$uid], $filetype[$uid]);
01279 $context['results'][$uid]['subject'][] = basename($outfilelist[$uid]);
01280 }
01281 $progress++;
01282 }
01283 $context['finished'] = $progress / $max;
01284 }
01285
01295 function dbsf_batch_save_data($data = NULL, &$context) {
01296 $context['results']['data'] = $data;
01297 }
01298
01317 function dbsf_parameter_daemon($uid, $dir, $par, $software = 'blastall', $other_data = NULL) {
01318 if (empty($uid) || empty($dir) || empty($par)) {
01319 return FALSE;
01320 }
01321
01322
01323 $data = "uid:$uid\nsoftware:$software\n";
01324 if (!empty($other_data)) {
01325 if (is_array($other_data)) {
01326 foreach ($other_data as $key => $value) {
01327 if (is_array($value)) {
01328 foreach ($value as $element) {
01329 if (!empty($element)) {
01330 $data .= $key .':'. $element ."\n";
01331 }
01332 }
01333 }
01334 else {
01335 if (!empty($value)) {
01336 $data .= $key .':'. $value ."\n";
01337 }
01338 }
01339 }
01340 }
01341 else {
01342 $data .= $other_data ."\n";
01343 }
01344 }
01345 $data .= "par:$par -i $uid.query -o $uid.output\n";
01346 $outfile = $dir .'/'. $uid .'.wait';
01347 if (!$outhandle = fopen($outfile, 'wt')) {
01348 drupal_set_message(t("Could not create $outfile."), 'error');
01349 return FALSE;
01350 }
01351 fwrite($outhandle, $data);
01352 fclose($outhandle);
01353 return TRUE;
01354 }
01355
01367 function dbsf_formatdb_fastafile($infile, $file_type = 'protein') {
01368 if (empty($infile) || !file_exists($infile)) {
01369 return FALSE;
01370 }
01371 $is_protein = 'T';
01372 if ($file_type == 'protein') {
01373 $is_protein = 'T';
01374 }
01375 else {
01376 $is_protein = 'F';
01377 }
01378 $select_software_path = 'SELECT value FROM {ic_softwareprop} where software_id=' . "(SELECT software_id from {ic_software} where uniquename='%s')" . ' AND rank=0 AND type_id=' . "(SELECT cvterm_id from {ic_cvterm} as cvterm JOIN {ic_cv} as cv on cv.cv_id=cvterm.cv_id where cv.name='software path' AND cvterm.name='%s')";
01379 $res = db_fetch_array(db_query($select_software_path, 'formatdb', 'executable'));
01380 $formatdb_exec = $res['value'];
01381 if (empty($formatdb_exec)) {
01382 drupal_set_message(t('The administrator has not activated the formatdb program yet.'), 'error');
01383 return FALSE;
01384 }
01385
01386 $args = " -p $is_protein -o T -i $infile -l /dev/stdout";
01387 $result = exec($formatdb_exec . $args);
01388 return $result;
01389 }
01390
01402 function dbsf_get_user_datasets($software_name) {
01403 if (empty($software_name)) {
01404 return FALSE;
01405 }
01406
01407 global $user;
01408
01409 $rid = $user->name;
01410
01411 $user_roles = $user->roles;
01412 $return_array = array();
01413
01414
01415
01416
01417 $dataset_array = dbsf_get_software_resources(array('s.uniquename' => $software_name, 'rcvterm.name' => 'dataset'), TRUE);
01418 if (empty($dataset_array) || empty($dataset_array[$software_name])) {
01419 return FALSE;
01420 }
01421 $dataset_array = $dataset_array[$software_name];
01422
01423 foreach ($dataset_array as $name => $db_data) {
01424 $id = $db_data['id'];
01425 $dbroles = dbsf_get_resource_roles($id);
01426 $security_check = array_intersect($user_roles, $dbroles);
01427 if (!empty($dbroles) && empty($security_check)) {
01428 continue;
01429 }
01430 $dbgroup = '';
01431 $data = icbasic_get_resource_cvterm_and_prop($id);
01432 if (empty($data['cvterm']['dataset_type'])) {
01433 continue;
01434 }
01435 $linked_data = icbasic_get_linked_resources($id, 'uses', FALSE, "rocvterm.name='dataset_group'");
01436 if (!empty($linked_data)) {
01437 foreach ($linked_data as $lid => $d) {
01438 $dbgroup = $d['uniquename'];
01439 }
01440 }
01441 $dbtype_array = $data['cvterm']['dataset_type'];
01442 foreach ($dbtype_array as $dbtype) {
01443 $return_array[$dbtype][$dbgroup][$id] = $name;
01444 }
01445 }
01446 if (!empty($return_array)) {
01447 return $return_array;
01448 }
01449 else {
01450 return FALSE;
01451 }
01452 }
01453
01468 function dbsf_get_sequence($uid = NULL, $database = NULL) {
01469 if (empty($uid) && empty($database)) {
01470 return 'No method to acquire the database was provided';
01471 }
01472
01473 $seqs_to_get_array = $_POST['features'];
01474 if (empty($seqs_to_get_array)) {
01475 return 'No sequences were requested for retrieval';
01476 }
01477 $seqs_to_get = '';
01478 foreach ($seqs_to_get_array as $seq) {
01479 $seqs_to_get .= escapeshellarg($seq) .' ';
01480 }
01481 $seqs_to_get = trim($seqs_to_get);
01482
01483 $operation = isset($_POST['op']) ? $_POST['op'] : 'FASTA';
01484
01485 $select_software_path = 'SELECT value FROM {ic_softwareprop} where software_id=' . "(SELECT software_id from {ic_software} where uniquename='%s')" . ' AND rank=0 AND type_id=' . "(SELECT cvterm_id from {ic_cvterm} as cvterm JOIN {ic_cv} as cv on cv.cv_id=cvterm.cv_id where cv.name='software path' AND cvterm.name='%s')";
01486 $res = db_fetch_array(db_query($select_software_path, 'fastacmd', 'executable'));
01487 $fastacmd_exec = $res['value'];
01488 if (empty($fastacmd_exec)) {
01489 drupal_set_message('Failed to find an executable for fastacmd', 'error');
01490 return ' ';
01491 }
01492
01493 $database_array = array();
01494
01495 if (!empty($uid)) {
01496
01497 $dirpath = file_create_path() .'/dbsf/';
01498 $infile = $dirpath . $uid .'.wait';
01499 if (!file_exists($infile)) {
01500 return 'Seems the UID you provided is invalid or expired';
01501 }
01502 if ($inhandle = fopen($infile, 'rb')) {
01503 while (!feof($inhandle)) {
01504 $line = trim(fgets($inhandle));
01505 if (!empty($line) && preg_match('/^database:/', $line)) {
01506 $line_data = explode(':', $line);
01507
01508 $database_array[trim(escapeshellarg($line_data[1]))] = 1;
01509 }
01510 }
01511 }
01512 else {
01513 dpm("Could not read $infile");
01514 }
01515 }
01516 elseif (!empty($database)) {
01517 if (is_array($database)) {
01518 foreach ($database as $db) {
01519 $database_array[$db] = 1;
01520 }
01521 }
01522 else {
01523 $database_array[$database] = 1;
01524 }
01525 }
01526
01527
01528
01529 $exec_output = array();
01530 $html_output = '';
01531 $fasta_data = '';
01532
01533
01534 foreach ($database_array as $database_str => $dummy) {
01535 $args = " -d '$database_str' -s '$seqs_to_get'";
01536
01537 $result = exec($fastacmd_exec . $args, $exec_output);
01538 }
01539
01540 if (empty($exec_output)) {
01541 dpm($fastacmd_exec . $args);
01542 return 'Sorry, cannot find your sequence entries in any database. If you want, you can '. l('contact', 'contact') .' the administrator.';
01543 }
01544 foreach ($exec_output as $line) {
01545 if (strpos($line, '>') !== FALSE) {
01546 $line = str_replace(' No definition line found', '', $line);
01547 $fasta_data .= $line ."\n";
01548 $html_output .= '</p><p>'. $line .'<br>';
01549 }
01550 else {
01551 $fasta_data .= $line ."\n";
01552 $html_output .= $line .'<br>';
01553 }
01554 }
01555 $return_text = '';
01556 $outfile = $infile . time() .'.get';
01557
01558 ini_set('auto_detect_line_endings', TRUE);
01559 if (!$outhandle = fopen($outfile, 'wt')) {
01560 drupal_set_message(t("Could not create output FASTA file $outfile."), 'error');
01561 }
01562 else {
01563 fwrite($outhandle, $fasta_data);
01564 fclose($outhandle);
01565 $return_text .= '<p>You can download a '. l('FASTA file', $outfile) .' or copy-paste it from below.</p>';
01566 }
01567
01568 $return_text .= '<pre>'. $html_output .'</pre>';
01569 return $return_text;
01570 }
01571
01577 function dbsf_cron() {
01578
01579 $dirpath = file_create_path() .'/dbsf';
01580 file_scan_directory($dirpath, '.+\.query$', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01581 file_scan_directory($dirpath, '.+\.subject.*', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01582 file_scan_directory($dirpath, '.+\.wait$', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01583 file_scan_directory($dirpath, '.+\.done$', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01584 file_scan_directory($dirpath, '.+\.parameter$', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01585 file_scan_directory($dirpath, '.+\.output.*', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01586 file_scan_directory($dirpath, '.+\.submission$', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01587 file_scan_directory($dirpath, '.+\.get$', array('.', '..', 'CVS'), 'dbsf_delete_temp', FALSE);
01588 }
01589
01599 function dbsf_delete_temp($file = NULL) {
01600 if (empty($file)) {
01601 return FALSE;
01602 }
01603
01604 $timestamp = time();
01605
01606 $age = filemtime($file);
01607 if (!empty($age) && !empty($timestamp)) {
01608 $diff = $timestamp - $age;
01609 if ($diff > 604800) {
01610 file_delete($file);
01611 }
01612 }
01613 }
01614
01629 function dbsf_link_unlink_resource_role($resource, $role, $switch = FALSE) {
01630 if (empty($resource) || empty($role)) {
01631 return FALSE;
01632 }
01633 $role = check_plain($role);
01634 $resource = check_plain($resource);
01635 $sql_check = '';
01636 $sql_insert = '';
01637 $sql_delete = '';
01638 if (is_numeric($role)) {
01639 $sql_check = "SELECT resource_role_id from {ic_resource_role} where role_id=%d AND resource_id=";
01640 $sql_insert = "INSERT INTO {ic_resource_role} (role_id,resource_id) VALUES (%d,";
01641 $sql_delete = "DELETE FROM {ic_resource_role} where role_id=%d AND resource_id=";
01642 }
01643 else {
01644 $sql_check = 'SELECT resource_role_id from {ic_resource_role} where role_id=' . "(SELECT rid from {role} where name='%s') AND resource_id=";
01645 $sql_insert = 'INSERT INTO {ic_resource_role} (role_id,resource_id) VALUES (' . "(SELECT rid from {role} where name='%s'),";
01646 $sql_delete = 'DELETE FROM {ic_resource_role} where role_id=' . "(SELECT rid from {role} where name='%s') AND resource_id=";
01647 }
01648 if (is_numeric($resource)) {
01649 $sql_check .= "%d";
01650 $sql_insert .= "%d)";
01651 $sql_delete .= "%d";
01652 }
01653 else {
01654 $sql_check .= "(SELECT resource_id from {ic_resource} where uniquename='%s')";
01655 $sql_insert .= "(SELECT resource_id from {ic_resource} where uniquename='%s'))";
01656 $sql_delete .= "(SELECT resource_id from {ic_resource} where uniquename='%s')";
01657 }
01658
01659 $res = db_fetch_array(db_query($sql_check));
01660 switch ($switch) {
01661 case FALSE:
01662 if (empty($res)) {
01663 db_query($sql_insert, array($role, $resource));
01664 return 'GRANTED';
01665 }
01666 else {
01667 return 'GRANTED';
01668 }
01669 break;
01670
01671 case 'TOGGLE':
01672 if (empty($res)) {
01673 db_query($sql_insert, array($role, $resource));
01674 return 'GRANTED';
01675 }
01676 else {
01677 db_query($sql_delete, array($role, $resource));
01678 $res = db_fetch_array(db_query($sql_check, array($role, $resource)));
01679 if (empty($res)) {
01680 return 'REVOKED';
01681 }
01682 else {
01683 dvm("Failed to delete previous link for role $role and resource $resource.");
01684 return FALSE;
01685 }
01686 }
01687 break;
01688
01689 case TRUE:
01690 if (empty($res)) {
01691 return 'REVOKED';
01692 }
01693 else {
01694 db_query($sql_delete, array($role, $resource));
01695 $res = db_fetch_array(db_query($sql_check, array($role, $resource)));
01696 if (empty($res)) {
01697 return 'REVOKED';
01698 }
01699 else {
01700 dvm("Failed to delete previous link for role $role and resource $resource.");
01701 return FALSE;
01702 }
01703 }
01704 break;
01705 }
01706 }
01707
01722 function dbsf_link_unlink_software_resource($software, $resource, $switch = FALSE) {
01723 if (empty($resource) || empty($software)) {
01724 return FALSE;
01725 }
01726 $software = check_plain($software);
01727 $resource = check_plain($resource);
01728 $sql_check = '';
01729 $sql_insert = '';
01730 $sql_delete = '';
01731 if (is_numeric($software)) {
01732 $sql_check = "SELECT software_resource_id from {ic_software_resource} where software_id=%d AND resource_id=";
01733 $sql_insert = "INSERT INTO {ic_software_resource} (software_id,resource_id) VALUES (%d,";
01734 $sql_delete = "DELETE FROM {ic_software_resource} where software_id=%d AND resource_id=";
01735 }
01736 else {
01737 $sql_check = 'SELECT software_resource_id from {ic_software_resource} where software_id=' . "(SELECT software_id from {ic_software} where uniquename='%s') AND resource_id=";
01738 $sql_insert = 'INSERT INTO {ic_software_resource} (software_id,resource_id) VALUES (' . "(SELECT software_id from {ic_software} where uniquename='%s'),";
01739 $sql_delete = 'DELETE FROM {ic_software_resource} where software_id=' . "(SELECT software_id from {ic_software} where uniquename='%s') AND resource_id=";
01740 }
01741 if (is_numeric($resource)) {
01742 $sql_check .= '%d';
01743 $sql_insert .= '%d' . ')';
01744 $sql_delete .= '%d';
01745 }
01746 else {
01747 $sql_check .= " (SELECT resource_id from {ic_resource} where uniquename='%s')";
01748 $sql_insert .= "(SELECT resource_id from {ic_resource} where uniquename='%s')" . ')';
01749 $sql_delete .= "(SELECT resource_id from {ic_resource} where uniquename='%s')";
01750 }
01751
01752
01753 $res = db_fetch_array(db_query($sql_check));
01754 switch ($switch) {
01755 case FALSE:
01756 if (empty($res)) {
01757 db_query($sql_insert, array($software, $resource));
01758 return 'GRANTED';
01759 }
01760 else {
01761 return 'GRANTED';
01762 }
01763 break;
01764
01765 case 'TOGGLE':
01766 if (empty($res)) {
01767 db_query($sql_insert, array($software, $resource));
01768 return 'GRANTED';
01769 }
01770 else {
01771 db_query($sql_delete);
01772 $res = db_fetch_array(db_query($sql_check, array($software, $resource)));
01773 if (empty($res)) {
01774 return 'REVOKED';
01775 }
01776 }
01777 break;
01778
01779 case TRUE:
01780 if (empty($res)) {
01781 return 'REVOKED';
01782 }
01783 else {
01784 db_query($sql_delete);
01785 $res = db_fetch_array(db_query($sql_check, array($software, $resource)));
01786 if (empty($res)) {
01787 return 'REVOKED';
01788 }
01789 else {
01790 dvm("Failed to delete previous link for software $software and resource $resource.");
01791 return FALSE;
01792 }
01793 }
01794 break;
01795 }
01796 }
01797
01812 function dbsf_get_softwareprop($limit = NULL, $name_as_key = FALSE) {
01813 $return_array = array();
01814 $sql_select = 'SELECT s.uniquename as software_name,s.software_id as software_id,' . ' cvterm.name as type_name,sp.value,sp.rank ' . ' from {ic_softwareprop} as sp ' . ' JOIN {ic_software} as s ON s.software_id=sp.software_id ' . ' JOIN {ic_cvterm as cvterm on sp.type_id=cvterm.cvterm_id ' . ' WHERE TRUE';
01815
01816 if (!empty($limit)) {
01817 if (is_array($limit)) {
01818 foreach ($limit as $column => $data) {
01819 $sql_select .= " AND $column='$data'";
01820 }
01821 }
01822 else {
01823 $sql_select .= " AND $limit";
01824 }
01825 }
01826 $res = db_query($sql_select);
01827 while ($row = db_fetch_array($res)) {
01828 if (!empty($name_as_key)) {
01829 $return_array[$row['software_name']][$row['type_name']][$row['rank']] = $row['value'];
01830 }
01831 else {
01832 $return_array[$row['software_id']][$row['type_name']][$row['rank']] = $row['value'];
01833 }
01834 }
01835 if (!empty($return_array)) {
01836 return $return_array;
01837 }
01838 else {
01839 return FALSE;
01840 }
01841 }
01842
01857 function dbsf_get_software_resources($limit = NULL, $name_as_key = FALSE) {
01858
01859 $return_array = array();
01860 $sql_select = 'SELECT sr.software_id as software_id,sr.resource_id as resource_id' . ',s.uniquename as software_name,r.uniquename as resource_name ' . ',rcvterm.name as rtype_name,r.description from {ic_software_resource} as sr ' . ' JOIN {ic_resource} as r on r.resource_id=sr.resource_id ' . ' JOIN {ic_software} as s on sr.software_id=s.software_id ' . ' JOIN {ic_cvterm as rcvterm on r.type_id=rcvterm.cvterm_id ' . ' WHERE TRUE';
01861 if (!empty($limit)) {
01862 if (is_array($limit)) {
01863 foreach ($limit as $column => $data) {
01864 $sql_select .= " AND $column='$data'";
01865 }
01866 }
01867 else {
01868 $sql_select .= " AND $limit";
01869 }
01870 }
01871
01872 $res = db_query($sql_select);
01873 while ($row = db_fetch_array($res)) {
01874 if (!empty($name_as_key) && $name_as_key === 'RESOURCE') {
01875 $return_array[$row['resource_name']][$row['software_name']] = array(
01876 'type_name' => $row['rtype_name'],
01877 'uniquename' => $row['software_name'],
01878 'id' => $row['software_id'],
01879 'description' => $row['description'],
01880 );
01881 }
01882 elseif (!empty($name_as_key)) {
01883 $return_array[$row['software_name']][$row['resource_name']] = array(
01884 'type_name' => $row['rtype_name'],
01885 'uniquename' => $row['resource_name'],
01886 'id' => $row['resource_id'],
01887 'description' => $row['description'],
01888 );
01889 }
01890 else {
01891 $return_array[$row['software_id']][$row['resource_id']] = array(
01892 'type_name' => $row['rtype_name'],
01893 'uniquename' => $row['resource_name'],
01894 'id' => $row['resource_id'],
01895 'description' => $row['description'],
01896 );
01897 }
01898 }
01899 if (!empty($return_array)) {
01900 return $return_array;
01901 }
01902 else {
01903 return FALSE;
01904 }
01905 }
01906
01919 function dbsf_get_software($limit = NULL, $name_as_key = NULL) {
01920 $return_array = array();
01921 $sql_select = "SELECT software_id,uniquename from {ic_software} WHERE TRUE ";
01922 if (!empty($limit)) {
01923 if (is_array($limit)) {
01924 foreach ($limit as $column => $data) {
01925 if ($column === 'active' && empty($data)) {
01926 $sql_select .= ' AND active is FALSE';
01927 }
01928 elseif ($column === 'active') {
01929 $sql_select .= ' AND active is TRUE';
01930 }
01931 else {
01932 $sql_select .= " AND $column='$data'";
01933 }
01934 }
01935 }
01936 else {
01937 $sql_select .= " AND $limit";
01938 }
01939 }
01940
01941 $res = db_query($sql_select);
01942 while ($row = db_fetch_array($res)) {
01943 if (!empty($name_as_key) && $name_as_key === 'BOTH') {
01944 $return_array[$row['uniquename']] = $row['uniquename'];
01945 }
01946 elseif (!empty($name_as_key)) {
01947 $return_array[$row['uniquename']] = $row['software_id'];
01948 }
01949 else {
01950 $return_array[$row['software_id']] = $row['uniquename'];
01951 }
01952 }
01953 if (!empty($return_array)) {
01954 return $return_array;
01955 }
01956 else {
01957 return FALSE;
01958 }
01959 }
01960
01973 function dbsf_get_resource_roles($resource, $name_as_key = NULL) {
01974 $return_array = array();
01975 if (empty($resource)) {
01976 return FALSE;
01977 }
01978 $resource = check_plain($resource);
01979 $sql_select = 'SELECT rr.role_id,r.name as role_name ' . 'from {ic_resource_role} as rr ' . ' JOIN {role} as r ON r.rid=rr.role_id ';
01980
01981 if (is_numeric($resource)) {
01982 $sql_select .= 'WHERE rr.resource_id=' . '%d';
01983 }
01984 else {
01985 $sql_select .= "WHERE rr.resource_id=(SELECT resource_id from {ic_resource} where uniquename='%s')";
01986 }
01987 $res = db_query($sql_select, $resource);
01988 while ($row = db_fetch_array($res)) {
01989 if (!empty($name_as_key)) {
01990 $return_array[$row['role_name']] = $row['role_id'];
01991 }
01992 else {
01993 $return_array[$row['role_id']] = $row['role_name'];
01994 }
01995 }
01996
01997 if (!empty($return_array)) {
01998 return $return_array;
01999 }
02000 else {
02001 return FALSE;
02002 }
02003 }
02004
02014 function dbsf_get_dataset_path($dataset) {
02015 if (empty($dataset)) {
02016 return FALSE;
02017 }
02018 $dataset = check_plain($dataset);
02019
02020 $linked_data = icbasic_get_linked_resources($dataset, 'uses');
02021 $dir = '';
02022 $basename = '';
02023 foreach ($linked_data as $d) {
02024 switch ($d['type_name']) {
02025 case 'filename':
02026 $basename = $d['uniquename'];
02027 break;
02028
02029 case 'directory':
02030 $dir = $d['uniquename'];
02031 break;
02032 }
02033 }
02034 return $dir . $basename;
02035 }
02036
02047 function dbsf_software_blast_core_settings_form($form_state) {
02048 $form = array();
02049 $active = dbsf_check_software_active('blastall');
02050 if ($active === FALSE) {
02051 return $form;
02052 }
02053 $select_software_path = 'SELECT value FROM {ic_softwareprop} where software_id=' . "(SELECT software_id from {ic_software} where uniquename='%s')" . ' AND rank=0 AND type_id=' . "(SELECT cvterm_id from {ic_cvterm} as cvterm JOIN {ic_cv} as cv on cv.cv_id=cvterm.cv_id where cv.name='software path' AND cvterm.name='%s')";
02054
02055 $core_exec = db_fetch_array(db_query($select_software_path, 'blastall', 'executable'));
02056 $data = db_fetch_array(db_query($select_software_path, 'blastall', 'data'));
02057 $format_exec = db_fetch_array(db_query($select_software_path, 'formatdb', 'executable'));
02058 $fastacmd_exec = db_fetch_array(db_query($select_software_path, 'fastacmd', 'executable'));
02059
02060 $form['settings'] = array(
02061 '#type' => 'fieldset',
02062 '#title' => t('BLAST core settings'),
02063 '#description' => t('Please set some important settings for the blastall program.'),
02064 'submit' => array(
02065 '#type' => 'submit',
02066 '#value' => 'Save core BLAST settings',
02067 '#weight' => 5,
02068 ),
02069 'blast_executable' => array(
02070 '#type' => 'textfield',
02071 '#title' => t('BLASTALL executable path'),
02072 '#description' => t('Please provide the <b>full path</b> to the <b>blastall</b> executable on the server, e.g. /usr/bin/blastall .'),
02073 '#required' => TRUE,
02074 '#default_value' => $core_exec['value'],
02075 ),
02076 'formatdb_executable' => array(
02077 '#type' => 'textfield',
02078 '#title' => t('FORMATDB executable path'),
02079 '#description' => t('Please provide the <b>full path</b> to the <b>formatdb</b> executable on the server, e.g. /usr/bin/formatdb .'),
02080 '#required' => TRUE,
02081 '#default_value' => $format_exec['value'],
02082 ),
02083 'fastacmd_executable' => array(
02084 '#type' => 'textfield',
02085 '#title' => t('FASTACMD executable path'),
02086 '#description' => t('Please provide the <b>full path</b> to the <b>fastacmd</b> executable on the server, e.g. /usr/bin/fastacmd .'),
02087 '#required' => TRUE,
02088 '#default_value' => $fastacmd_exec['value'],
02089 ),
02090 'blast_data' => array(
02091 '#type' => 'textfield',
02092 '#title' => t('Data directory'),
02093 '#description' => t('Please provide the <b>full path</b> to the <b>blastall data</b> directory on the server, e.g. /usr/share/ncbi/data/ . ' . 'This is the directory where the BLOSUM62 etc matrixes reside; if you do not know how to find it, try the command "locate BLOSUM62"'
02094 ),
02095 '#required' => TRUE,
02096 '#default_value' => $data['value'],
02097 ),
02098 );
02099 return $form;
02100 }
02101
02113 function dbsf_software_blast_core_settings_form_validate($form, &$form_state) {
02114 $data = escapeshellcmd(trim($form_state['values']['blast_data']));
02115 $core_exec = escapeshellcmd(trim($form_state['values']['blast_executable']));
02116 $format_exec = escapeshellcmd(trim($form_state['values']['formatdb_executable']));
02117 $fastacmd_exec = escapeshellcmd(trim($form_state['values']['fastacmd_executable']));
02118
02119 if (!empty($data) && substr($data, -1) !== '/') {
02120 $data .= '/';
02121 }
02122 if (!empty($core_exec)) {
02123 if (!file_exists($core_exec)) {
02124 form_set_error('blast_executable', "Cannot find executable $core_exec on the server.");
02125 }
02126 $form_state['values']['blast_executable'] = $core_exec;
02127 }
02128 if (!empty($data)) {
02129 if (!file_exists($data .'/BLOSUM62')) {
02130 form_set_error('blast_data', 'Cannot find matrix '. $data .'/BLOSUM62 on the server.');
02131 }
02132
02133 $form_state['values']['blast_data'] = $data;
02134 }
02135 if (!empty($format_exec)) {
02136 if (!file_exists($format_exec)) {
02137 form_set_error('formatdb_executable', "Cannot find executable $format_exec on the server.");
02138 }
02139 $form_state['values']['formatdb_executable'] = $format_exec;
02140 }
02141 if (!empty($fastacmd_exec)) {
02142 if (!file_exists($fastacmd_exec)) {
02143 form_set_error('fastacmd_executable', "Cannot find executable $fastacmd_exec on the server.");
02144 }
02145 $form_state['values']['fastacmd_executable'] = $fastacmd_exec;
02146 }
02147 }
02148
02160 function dbsf_software_blast_core_settings_form_submit($form, &$form_state) {
02161 $store_software_path = 'INSERT INTO {ic_softwareprop} (software_id,type_id,rank,value) VALUES (' . "(SELECT software_id from {ic_software} where uniquename='%s')" . ",(SELECT cvterm_id from {ic_cvterm} as cvterm JOIN {ic_cv} as cv on cv.cv_id=cvterm.cv_id where cv.name='software path' AND cvterm.name='%s')" . ",0,'%s')";
02162 $delete_software_path = 'DELETE FROM {ic_softwareprop} where software_id=' . "(SELECT software_id from {ic_software} where uniquename='%s')" . ' AND rank=0 AND type_id=' . "(SELECT cvterm_id from {ic_cvterm} as cvterm JOIN {ic_cv} as cv on cv.cv_id=cvterm.cv_id where cv.name='software path' AND cvterm.name='%s')";
02163 $select_software_path = 'SELECT value FROM {ic_softwareprop} where software_id=' . "(SELECT software_id from {ic_software} where uniquename='%s')" . ' AND rank=0 AND type_id=' . "(SELECT cvterm_id from {ic_cvterm} as cvterm JOIN {ic_cv} as cv on cv.cv_id=cvterm.cv_id where cv.name='software path' AND cvterm.name='%s')";
02164
02165 $core_exec = db_fetch_array(db_query($select_software_path, 'blastall', 'executable'));
02166 $format_exec = db_fetch_array(db_query($select_software_path, 'formatdb', 'executable'));
02167 $fastacmd_exec = db_fetch_array(db_query($select_software_path, 'fastacmd', 'executable'));
02168 $data = db_fetch_array(db_query($select_software_path, 'blastall', 'data'));
02169
02170 $user_bexec = $form_state['values']['blast_executable'];
02171 $user_fexec = $form_state['values']['formatdb_executable'];
02172 $user_fastacmd_exec = $form_state['values']['fastacmd_executable'];
02173
02174 $user_data = $form_state['values']['blast_data'];
02175 if (!empty($user_bexec) && $user_bexec != $core_exec['value']) {
02176 db_query($delete_software_path, 'blastall', 'executable');
02177 db_query($store_software_path, 'blastall', 'executable', $user_bexec);
02178 drupal_set_message("$user_bexec is now the blastall executable.", 'warning');
02179 }
02180 if (!empty($user_fexec) && $user_fexec != $format_exec['value']) {
02181 db_query($delete_software_path, 'formatdb', 'executable');
02182 db_query($store_software_path, 'formatdb', 'executable', $user_fexec);
02183 drupal_set_message("$user_fexec is now the formatdb executable.", 'warning');
02184 }
02185 if (!empty($user_fastacmd_exec) && $user_fastacmd_exec != $fastacmd_exec['value']) {
02186 db_query($delete_software_path, 'fastacmd', 'executable');
02187 db_query($store_software_path, 'fastacmd', 'executable', $user_fastacmd_exec);
02188 drupal_set_message("$user_fexec is now the fastacmd executable.", 'warning');
02189 }
02190 if (!empty($user_data) && $user_data != $data['value']) {
02191 db_query($delete_software_path, 'blastall', 'data');
02192 db_query($store_software_path, 'blastall', 'data', $user_data);
02193 drupal_set_message("$user_data is now the blastall data directory.", 'warning');
02194 }
02195 }
02196
02202 function dbsf_admin_blast_page() {
02203 $software = 'blastall';
02204 $check_active = dbsf_check_software_active($software);
02205 if (empty($check_active)) {
02206 drupal_set_message("I'm sorry, but $software does not seem to have been activated yet.<br>".
02207 l('See the software settings page', 'admin/dbsf/software'), 'error'
02208 );
02209 return FALSE;
02210 }
02211 $database_link_form = drupal_get_form('dbsf_admin_link_dataset_software_form', $software);
02212 if (strlen($database_link_form) < 600) {
02213 $database_link_form = "You have no datasets defined to link them to $software. Please see ". l('the dataset administration page', 'admin/dbsf/dataset');
02214 }
02215 $table = dbsf_admin_database_table($software);
02216
02217 $page_tabs = array(
02218 'settings' => array(
02219 '#type' => 'tabset',
02220 'variables' => array(
02221 '#type' => 'tabpage',
02222 '#title' => 'Link datasets to '. strtoupper($software),
02223 '#content' => $database_link_form . $table,
02224 '#weight' => 0,
02225 ),
02226 ),
02227 );
02228 $return_text = tabs_render($page_tabs);
02229 return $return_text;
02230 }
02231
02247 function dbsf_check_blast($software) {
02248 $check_active = dbsf_check_software_active($software);
02249 if (empty($check_active)) {
02250 return 'active';
02251 }
02252 $check = 0;
02253 $software_prop = dbsf_get_softwareprop(NULL, TRUE);
02254 if (!empty($software_prop)) {
02255 foreach ($software_prop as $name => $data) {
02256 if (!empty($data) && in_array($name, array('blastall', 'formatdb', 'fastacmd'))) {
02257 foreach ($data as $term_name => $v) {
02258 if ($term_name == 'executable' || $term_name == 'data') {
02259 $check++;
02260 }
02261 }
02262 }
02263 }
02264 }
02265 if ($check < 4) {
02266 return 'variables';
02267 }
02268 $sql = 'SELECT count(distinct rcv.cvterm_id) from {ic_resource_cvterm} rcv ' . ' JOIN {ic_software_resource} sr ON sr.resource_id=rcv.resource_id ' . " WHERE sr.software_id=(SELECT software_id from {ic_software} where uniquename='blastall') " . " AND rcv.cvterm_id IN (SELECT cvterm_id FROM {ic_cvterm} where cv_id=(SELECT cv_id from {ic_cv} where name='dataset_type'))";
02269 $count = db_fetch_array(db_query($sql));
02270 if ($count['count'] < 2) {
02271 return 'datasets';
02272 }
02273
02274 return TRUE;
02275 }
02276
02288 function dbsf_blast_page($sample_data = FALSE) {
02289 $setup = dbsf_check_blast('blastall');
02290 if ($setup === TRUE) {
02291 $return = '';
02292 if (empty($sample_data)) {
02293 $return = '<h3>Demonstration</h3><p> Click to '. l('load some sample data', 'dbsf/blast/demo');
02294 $return .= ' or you can download sample '. l('protein FASTA', drupal_get_path('module', 'dbsf') .'/test_prot.fsa') .' and '. l('nucleotide FASTA', drupal_get_path('module', 'dbsf') .'/test_nuc.fsa') .' files from the Arabidopsis thaliana database (TAIR), suitable for uploading as queries or databases.</p><br>';
02295 $return .= drupal_get_form('dbsf_blast_form');
02296 }
02297 else {
02298 $return = drupal_get_form('dbsf_blast_form', $sample_data);
02299 }
02300 return $return;
02301 }
02302 else {
02303 $return_text = "<p>I'm sorry but your blastall software has not been setup properly.</p><ul>";
02304 switch ($setup) {
02305 case 'datasets':
02306 $return_text .= '<li>Not enough datasets have been defined. Please define at least one protein and one nucleotide dataset at the '. l('dataset registration page', 'admin/dbsf/dataset', array('fragment' => 'available-datasets')) .' and then register them with BLAST at the '. l('BLAST database settings page', 'admin/dbsf/blast');
02307 break;
02308
02309 case 'active':
02310 $return_text .= '<li>BLAST has not been activated. Please activate it at the '. l('software settings page', 'admin/dbsf/software');
02311 break;
02312
02313 case 'variables':
02314 $return_text .= '<li>BLAST variable paths have not been defined. Please define them at at the '. l('BLASTALL settings page', 'admin/dbsf/software', array('fragment' => 'blastall'));
02315 break;
02316 }
02317 drupal_set_message($return_text .'</ul>', 'error');
02318 return ' ';
02319 }
02320 }
02321
02334 function dbsf_blast_form($form_state, $sample_data = FALSE) {
02335 $software_options = _dbsf_generate_options('blastall');
02336 $form = array();
02337 $dbs = dbsf_get_user_datasets('blastall');
02338
02339 $nucleotide_dbs = $dbs['nucleotide'];
02340 $protein_dbs = $dbs['protein'];
02341 $sample_seq_nuc = '';
02342 $sample_seq_prot = '';
02343 $sample_db_prot = '';
02344 $sample_db_nuc = '';
02345
02346 if ($sample_data) {
02347 $sample_data = TRUE;
02348 foreach ($nucleotide_dbs as $group) {
02349 foreach ($group as $n => $n2) {
02350 if ($n2 == 'Arabidopsis thaliana genes') {
02351 $sample_db_nuc = $n;
02352 $sample_seq_nuc = '>antiporter' . "\n". wordwrap('GTCATGGAATCCTTCATTCCCGAGTTTCAGGAGCTCCGCTGTTTCCGGACCAAAGTCATCACTGAAGCTGAACAGGTTTTTAAGAAACTGTGCAAGTACGAATCAGGAGCTTGTTGTTGATGGAGAAACCGGAAATGGGTCGATTTCGGAGCTCCAAGGAGATGCAGCAAATGGTTCGATTTCGCCGGTGGAAGTGGAAGCAGAAGTAGAAGAAGTGAAGGTAGATGATTTGGCGACTCAGAGCATTTGGGGACAGATGAAAGAGATCGTCATGTTTACCGGACCTGCCGCGGGATTGTGGCTATGTGGGCCGTTGATGAGTCTCATTGATACGGCGGTGATTGGTCAAGGAAGCTCACTCGAACTCGCTGCTTTAGGTCCTGCTACCGTCATCTGTGATTATTTGTGTTATACGTTCATGTTCCTCTCAGTTGCGACTTCAAATCTTGTTGCTACCTCTCTTGCTCGGCAGGATAAAGATGAAGTACAACATCAGATATCGATCTTGCTTTTCATTGGGTTGGCTTGTGGAGTCACGATGATGGTGTTGACAAGACTGTTTGGTTCCTGGGCACTAACTGCTTTTACAGGGGTAAAGAATGCCGACATTGTTCCAGCAGCTAATAAATATGTTCAGATTCGTGGTTTAGCATGGCCAGCTGTTCTCATTGGATGGGTTGCTCAAAGTGCAAGTCTTGGTATGAAAGACTCATGGGGACCTCTTAAGGCTTTGGCGGTTGCTAGTGCAATAAACGGTGTTGGTGATGTGGTCTTATGCACCTTTCTAGGATATGGTATAGCAGGTGCAGCTTGGGCAACTATGGTGTCACAAGTTGTTGCTGCTTATATGATGATGGACGCATTGAACAAGAAAGGATACAGCGCATTCTCATTCTGTGTTCCTTCTCCAAGTGAACTTTTGACGATTTTTGGACTCGCTGCCCCTGTCTTTATAACTATGATGTCAAAGGTTTTGTTCTATACGCTCCTTGTGTACTTTGCTACATCAATGGGTACAAATATCATAGCTGCTCATCAGGTTATGCTTCAGATATATACCATGAGTACGGTTTGGGGGGAGCCTCTCTCTCAAACTGCACAGTCCTTTATGCCTGAGCTTTTATTCGGAATCAATCGTAATTTGCCTAAAGCTAGGGTGCTTCTGAAGTCACTAGTTATCATCGGAGCTACGCTAGGAATAGTAGTCGGAACCATTGGCACAGCAGTTCCATGGCTGTTCCCTGGCATCTTCACACGTGACAAGGTTGTCACATCCGAGATGCACAAGGTCATAATACCGTATTTTCTTGCTTTATCCATCACTCCAAGTACTCACAGTCTTGAAGGCACCTTACTGGCTGGAAGAGATCTTAGATATATCAGCTTGTCAAT', 80, "\n", TRUE);
02353 break(2);
02354 }
02355 }
02356 }
02357 foreach ($protein_dbs as $group) {
02358 foreach ($group as $p => $p2) {
02359 if ($p2 == 'Arabidopsis thaliana proteins') {
02360 $sample_db_prot = $p;
02361 $sample_seq_prot = wordwrap('DFEPVKPYEVPMTAAGALQSYKLAAKAITRLQSLPSGSMERLCDTMVQEVFELTGYDRVMAYKFHEDDHGEVVSEVTKPGLEPYLGLHYPATDIPQAARFLFMKNKVRMIVDCNAKHARVLQDEKLSFDLTWCGSTLRAPHSCHLQYMANMDSIASLVMAVVVNEEDGEGDAPDATTQPQKRKRLWGLVVCHNTTPRFVPFPLRYACEFLAQVFAIHVNKEVELDNQMVEKNIXRTQTLLCDMLMRDAPLGIVSQSPNIMDLVKCDGAALLYKDKIWKLGTTPSEFHLQEIASWLCEYHMDLTGLSTDSLHDAGFPRALSLGDSVCGMAAVRISSKDMIFWFRSHTAGEVRWGGAKHDPDDRDDARRMHPTSSFKAFLEVVKTRSLPWKDYEMDAIHSLQLILRNAFKDSETTDVNTKVIYSKPNDLKIDGIQELEAVTSEMVRLIETATVPILAVDSDGLVNGWNTKIAELTGLSVDEAIGKHFLTLVEDSSVEIVKRMLENALEGTEEQNVQFEIKTHLSRADAGPISLVVNACASRDLHENVVGVCFVAHDLTGQKTVMDKFTRIEGDYKAIIQ', 80, "\n", TRUE);
02362 break(2);
02363 }
02364 }
02365 }
02366 }
02367
02368
02369 $option_form = array();
02370 if (empty($nucleotide_dbs) || empty($protein_dbs)) {
02371 global $user;
02372 $roles_str = implode(', ', $user->roles);
02373 drupal_set_message("I'm sorry, but the system administrator has not authorized your role ($roles_str) to have access to at least 1 protein and 1 nucleotide database. Please let them know.", 'error');
02374 return FALSE;
02375 }
02376
02377 foreach ($software_options as $name => $data) {
02378 $option_form[$name] = array(
02379 '#type' => $data['type'],
02380 '#title' => $name,
02381 '#description' => !empty($data['description']) ? $data['description'] : '',
02382 );
02383
02384 if ($data['type'] !== 'textfield' && !empty($data['options'])) {
02385
02386 $option_form[$name]['#options'] = $data['options'];
02387 }
02388 }
02389
02390 $form['program'] = array(
02391 '#type' => 'fieldset',
02392 '#title' => 'Available algorithms',
02393 '#tree' => TRUE,
02394 'blastn' => array(
02395 '#type' => 'fieldset',
02396 '#title' => 'blastn',
02397 '#collapsible' => TRUE,
02398 '#collapsed' => $sample_data ? FALSE : TRUE,
02399 'query_sequence' => array(
02400 '#type' => 'textarea',
02401 '#title' => t('Enter query sequence in simple text or FASTA format'),
02402 '#default_value' => $sample_seq_nuc ? $sample_seq_nuc : '',
02403 ),
02404 'query_file_blastn' => array(
02405 '#type' => 'file',
02406 '#title' => t('or upload query sequence in FASTA format'),
02407 '#description' => t('Please give a text file, not a MS-Word or other document'),
02408
02409 '#tree' => FALSE,
02410 ),
02411 'subject_db' => array(
02412 '#type' => 'select',
02413 '#title' => t('Dataset(s)'),
02414 '#description' => t('multiples allowed (use option/control keys; likewise to deselect).'),
02415 '#size' => 5,
02416 '#multiple' => '1',
02417 '#options' => $nucleotide_dbs,
02418 '#default_value' => $sample_db_nuc ? $sample_db_nuc : '',
02419 ),
02420 'subject_file_blastn' => array(
02421 '#type' => 'file',
02422 '#title' => t('or upload your own database'),
02423 '#description' => t('Please give a text file, not a MS-Word or other document'),
02424
02425 '#tree' => FALSE,
02426 ),
02427 'advanced parameters' => array(
02428 '#type' => 'fieldset',
02429 '#title' => 'Advance Search Parameters',
02430 '#collapsible' => TRUE,
02431 '#collapsed' => TRUE,
02432 $option_form,
02433 ),
02434 ),
02435 'blastp' => array(
02436 '#type' => 'fieldset',
02437 '#title' => 'blastp',
02438 '#collapsible' => TRUE,
02439 '#collapsed' => $sample_data ? FALSE : TRUE,
02440 '#options' => $result,
02441 'query_sequence' => array(
02442 '#type' => 'textarea',
02443 '#title' => t('Enter query sequence in simple text or FASTA format'),
02444 '#default_value' => $sample_seq_prot ? $sample_seq_prot : '',
02445 ),
02446 'query_file_blastp' => array(
02447 '#type' => 'file',
02448 '#title' => t('or upload query sequence in FASTA format'),
02449 '#description' => t('Please give a text file, not a MS-Word or other document'),
02450
02451 '#tree' => FALSE,
02452 ),
02453 'subject_db' => array(
02454 '#type' => 'select',
02455 '#title' => t('Dataset(s)'),
02456 '#description' => t('multiples allowed (use option/control keys; likewise to deselect).'),
02457 '#size' => 5,
02458 '#multiple' => '1',
02459 '#options' => $protein_dbs,
02460 '#default_value' => $sample_db_prot ? $sample_db_prot : '',
02461 ),
02462 'subject_file_blastp' => array(
02463 '#type' => 'file',
02464 '#title' => t('or upload your own database'),
02465
02466 '#tree' => FALSE,
02467 '#description' => t('Please give a text file, not a MS-Word or other document'),
02468 ),
02469 'advanced parameters' => array(
02470 '#type' => 'fieldset',
02471 '#title' => 'Advance Search Parameters',
02472 '#collapsible' => TRUE,
02473 '#collapsed' => TRUE,
02474 $option_form,
02475 ),
02476 ),
02477 'blastx' => array(
02478 '#type' => 'fieldset',
02479 '#title' => 'blastx',
02480 '#collapsible' => TRUE,
02481 '#collapsed' => $sample_data ? FALSE : TRUE,
02482 'query_sequence' => array(
02483 '#type' => 'textarea',
02484 '#title' => t('Enter query sequence in simple text or FASTA format'),
02485 '#default_value' => $sample_seq_nuc ? $sample_seq_nuc : '',
02486 ),
02487 'query_file_blastx' => array(
02488 '#type' => 'file',
02489 '#title' => t('or upload query sequence in FASTA format'),
02490 '#description' => t('Please give a text file, not a MS-Word or other document'),
02491
02492 '#tree' => FALSE,
02493 ),
02494 'subject_db' => array(
02495 '#type' => 'select',
02496 '#title' => t('Dataset(s)'),
02497 '#description' => t('multiples allowed (use option/control keys; likewise to deselect).'),
02498 '#size' => 5,
02499 '#multiple' => '1',
02500 '#options' => $protein_dbs,
02501 '#default_value' => $sample_db_prot ? $sample_db_prot : '',
02502 ),
02503 'subject_file_blastx' => array(
02504 '#type' => 'file',
02505 '#title' => t('or upload your own database'),
02506 '#description' => t('Please give a text file, not a MS-Word or other document'),
02507
02508 '#tree' => FALSE,
02509 ),
02510 'advanced parameters' => array(
02511 '#type' => 'fieldset',
02512 '#title' => 'Advance Search Parameters',
02513 '#collapsible' => TRUE,
02514 '#collapsed' => TRUE,
02515 $option_form,
02516 ),
02517 ),
02518 'tblastn' => array(
02519 '#type' => 'fieldset',
02520 '#title' => 'tblastn',
02521 '#collapsible' => TRUE,
02522 '#collapsed' => TRUE,
02523 'query_sequence' => array(
02524 '#type' => 'textarea',
02525 '#title' => t('Enter query sequence in simple text or FASTA format'),
02526 ),
02527 'query_file_tblastn' => array(
02528 '#type' => 'file',
02529 '#title' => t('or upload query sequence in FASTA format'),
02530 '#description' => t('Please give a text file, not a MS-Word or other document'),
02531
02532 '#tree' => FALSE,
02533 ),
02534 'subject_db' => array(
02535 '#type' => 'select',
02536 '#title' => t('Dataset(s)'),
02537 '#description' => t('multiples allowed (use option/control keys; likewise to deselect).'),
02538 '#size' => 5,
02539 '#multiple' => '1',
02540 '#options' => $nucleotide_dbs,
02541 ),
02542 'subject_file_tblastn' => array(
02543 '#type' => 'file',
02544 '#title' => t('or upload your own database'),
02545 '#description' => t('Please give a text file, not a MS-Word or other document'),
02546
02547 '#tree' => FALSE,
02548 ),
02549 'advanced parameters' => array(
02550 '#type' => 'fieldset',
02551 '#title' => 'Advance Search Parameters',
02552 '#collapsible' => TRUE,
02553 '#collapsed' => TRUE,
02554 $option_form,
02555 ),
02556 ),
02557 'tblastx' => array(
02558 '#type' => 'fieldset',
02559 '#title' => 'tblastx',
02560 '#collapsible' => TRUE,
02561 '#collapsed' => TRUE,
02562 'query_sequence' => array(
02563 '#type' => 'textarea',
02564 '#title' => t('Enter query sequence in simple text or in FASTA format'),
02565 ),
02566 'query_file_tblastx' => array(
02567 '#type' => 'file',
02568 '#title' => t('or upload query sequence in FASTA format'),
02569 '#description' => t('Please give a text file, not a MS-Word or other document'),
02570
02571 '#tree' => FALSE,
02572 ),
02573 'subject_db' => array(
02574 '#type' => 'select',
02575 '#title' => t('Dataset(s)'),
02576 '#description' => t('multiples allowed (use option/control keys; likewise to deselect).'),
02577 '#size' => 5,
02578 '#multiple' => '1',
02579 '#options' => $protein_dbs,
02580 ),
02581 'subject_file_tblastx' => array(
02582 '#type' => 'file',
02583 '#title' => t('or upload your own database'),
02584 '#description' => t('Please give a text file, not a MS-Word or other document'),
02585
02586 '#tree' => FALSE,
02587 ),
02588 'advanced parameters' => array(
02589 '#type' => 'fieldset',
02590 '#title' => 'Advance Search Parameters',
02591 '#collapsible' => TRUE,
02592 '#collapsed' => TRUE,
02593 $option_form,
02594 ),
02595 ),
02596 );
02597 $form['buttons'] = array(
02598 '#weight' => 10,
02599 'db_submit' => array(
02600 '#type' => 'submit',
02601 '#value' => t('Run search'),
02602 ),
02603 'clear' => array(
02604 '#attributes' => array('title' => t('Clear the form')),
02605 '#type' => 'submit',
02606 '#value' => 'Reset data',
02607 '#validate' => array('dbsf_form_clear'),
02608 '#weight' => 2,
02609 ),
02610 );
02611 $form['#attributes']['enctype'] = "multipart/form-data";
02612 return $form;
02613 }
02614
02626 function dbsf_blast_form_validate($form, &$form_state) {
02627 $algorithm_data = $form_state['values']['program'];
02628
02629
02630 foreach ($algorithm_data as $algorithm => $data) {
02631 if (!empty($data['query_sequence'])) {
02632 if (!empty($_FILES['files']['tmp_name']['query_file_'. $algorithm])) {
02633 form_set_error("program][$algorithm][query_sequence", "$algorithm: It seems you both typed a query sequence and uploaded one.");
02634 return FALSE;
02635 }
02636 if (empty($data['subject_db']) && empty($_FILES['files']['tmp_name']['subject_file_'. $algorithm])) {
02637 form_set_error("program][$algorithm][query_sequence", "$algorithm: It seems you provided a query sequence but no subject database (or file).");
02638 return FALSE;
02639 }
02640
02641 if ($algorithm == 'blastn' || $algorithm == 'blastx' || $algorithm == 'tblastx') {
02642 $result = icbasic_validate_seq_dna($data['query_sequence']);
02643 if ($result === FALSE || is_numeric($result)) {
02644 form_set_error("program][$algorithm][query_sequence", "$algorithm: Sorry your sequence does not seem to be a valid DNA sequence ($result errors).");
02645 return FALSE;
02646 }
02647 else {
02648 $form_state['values']['program'][$algorithm]['query_sequence'] = $result;
02649 }
02650 }
02651 elseif ($algorithm == 'blastp' || $algorithm == 'tblastn') {
02652 $result = icbasic_validate_seq_protein($data['query_sequence']);
02653 if ($result === FALSE || is_numeric($result)) {
02654 form_set_error("program][$algorithm][query_sequence", "$algorithm: Sorry your sequence does not seem to be a valid protein sequence ($result errors).");
02655 return FALSE;
02656 }
02657 else {
02658 $form_state['values']['program'][$algorithm]['query_sequence'] = $result;
02659 }
02660 }
02661 }
02662 elseif (!empty($_FILES['files']['tmp_name']['query_file_'. $algorithm])) {
02663
02664 if (empty($data['subject_db']) && empty($_FILES['files']['tmp_name']['subject_file_'. $algorithm])) {
02665 form_set_error("program][$algorithm][query_sequence", "$algorithm: It seems you provided a query but no database database (or file).");
02666 return FALSE;
02667 }
02668 }
02669
02670
02671
02672 if (!empty($data['subject_db'])) {
02673 if (empty($data['query_sequence']) && empty($_FILES['files']['tmp_name']['query_file_'. $algorithm])) {
02674
02675 form_set_error("program][$algorithm][subject_db", "$algorithm: It seems you provided a subject database but no query sequence (or file).");
02676 return FALSE;
02677 }
02678
02679 }
02680 elseif (!empty($_FILES['files']['tmp_name']['subject_file_'. $algorithm])) {
02681
02682 if (empty($data['query_sequence']) && empty($_FILES['files']['tmp_name']['query_file_'. $algorithm])) {
02683 form_set_error("program][$algorithm][subject_db", "$algorithm: It seems you provided a subject database but no query sequence (or file).");
02684 return FALSE;
02685 }
02686 }
02687 }
02688 }
02689
02701 function dbsf_blast_form_submit($form, &$form_state) {
02702
02703
02704 $validators_file = array('file_validate_size' => array(1000));
02705 $tmpdir = file_directory_temp();
02706
02707 $timestamp = time();
02708 $sessionid = session_id();
02709 $dirpath = file_create_path() .'/dbsf';
02710 $algorithm_data = $form_state['values']['program'];
02711
02712 $operations = array();
02713
02714 $save_array = array();
02715 $subject_dbs = '';
02716
02717 $form_uid = icbasic_create_uid($sessionid, $timestamp, 'blastall');
02718 $batch_file_data = array();
02719
02720
02721 foreach ($algorithm_data as $algorithm => $data) {
02722 $file_type = 'nucleotide';
02723 if ($algorithm == 'blastn' || $algorithm == 'blastx' || $algorithm == 'tblastx') {
02724 $file_type = 'nucleotide';
02725 }
02726 elseif ($algorithm == 'blastp' || $algorithm == 'tblastn') {
02727 $file_type = 'protein';
02728 }
02729 $verify = array();
02730 $uid = icbasic_create_uid($sessionid, $timestamp, $algorithm);
02731
02732 $par = ' -m 7 ';
02733
02734
02735 if (isset($_FILES['files']) && !empty($_FILES['files']['tmp_name']['query_file_'. $algorithm])) {
02736 $file = file_save_upload('query_file_'. $algorithm, $validators_file, $tmpdir, FILE_EXISTS_RENAME);
02737 if (empty($file)) {
02738 form_set_error('query_file_'. $algorithm, "Sorry your file for $algorithm was not saved. Maybe it is too large (>1 Mb)? Otherwise, ". l('contact', 'contact') ." the administrator (quote $uid).");
02739 }
02740 else {
02741 $verify['runq']++;
02742 file_set_status($file, FILE_STATUS_TEMPORARY);
02743 $batch_file_data['infile'][$uid] = $file->filepath;
02744 $batch_file_data['outfile'][$uid] = $dirpath .'/'. $uid .'.query';
02745 $batch_file_data['filetype'][$uid] = $file_type;
02746 $batch_file_data['format'][$uid] = FALSE;
02747 }
02748 }
02749 elseif (!empty($data['query_sequence'])) {
02750 $verify['runq']++;
02751
02752 $batch_file_data['infile'][$uid] = $data['query_sequence'];
02753 $batch_file_data['outfile'][$uid] = $dirpath .'/'. $uid .'.query';
02754 $batch_file_data['filetype'][$uid] = $file_type;
02755 $batch_file_data['format'][$uid] = FALSE;
02756 }
02757
02758
02759 $file_type = 'nucleotide';
02760 if ($algorithm == 'blastn' || $algorithm == 'tblastn' || $algorithm == 'tblastx') {
02761 $file_type = 'nucleotide';
02762 }
02763 elseif ($algorithm == 'blastp' || $algorithm == 'blastx') {
02764 $file_type = 'protein';
02765 }
02766
02767 if (isset($_FILES['files']) && !empty($_FILES['files']['tmp_name']['subject_file_'. $algorithm])) {
02768 $file = file_save_upload('subject_file_'. $algorithm, $validators_file, $tmpdir, FILE_EXISTS_RENAME);
02769 if (empty($file)) {
02770 form_set_error('subject_file_'. $algorithm, "Sorry your file for $algorithm was not saved. Maybe it is too large (>1 Mb)? Otherwise, ". l('contact', 'contact') ." the administrator (quote $uid).");
02771 }
02772 else {
02773 $verify['rundb']++;
02774 file_set_status($file, FILE_STATUS_TEMPORARY);
02775 $batch_file_data['infile'][$uid] = $file->filepath;
02776 $batch_file_data['outfile'][$uid] = $dirpath .'/'. $uid .'.subject';
02777 $batch_file_data['filetype'][$uid] = $file_type;
02778 $batch_file_data['format'][$uid] = TRUE;
02779 }
02780 }
02781 if (!empty($data['subject_db'])) {
02782
02783
02784 foreach ($data['subject_db'] as $db => $value) {
02785 $verify['rundb']++;
02786 $subject_dbs[] = dbsf_get_dataset_path($db);
02787 }
02788 }
02789
02790 if (!empty($verify['runq']) && $verify['runq'] == 1 && !empty($verify['rundb'])) {
02791 $par .= dbsf_blastall_software_pars($data['advanced parameters'][0], $algorithm);
02792 $save_array[$form_uid][$uid]['par'] = $par;
02793 $save_array[$form_uid][$uid]['sub'] = $subject_dbs;
02794 $save_array[$form_uid][$uid]['algorithm'] = $algorithm;
02795 }
02796 }
02797
02798 $operations[] = array('dbsf_batch_upload_fasta', array($batch_file_data));
02799 $operations[] = array('dbsf_batch_save_data', array($save_array));
02800 $batch = array(
02801 'title' => t('Preparing data needed for BLAST jobs...'),
02802 'operations' => $operations,
02803 'init_message' => t('Starting BLAST submission...'),
02804 'progress_message' => t('@remaining operations remaining...'),
02805 'error_message' => t('Your BLAST submission encountered an error.'),
02806 'finished' => 'dbsf_blast_batch_finished',
02807 );
02808 batch_set($batch);
02809
02810
02811
02812
02813 $form_state['redirect'] = array('dbsf/blast_result', "submission_uid=$form_uid");
02814 return "dbsf/blast_result?submission_uid=$form_uid";
02815 }
02816
02835 function dbsf_blastall_software_pars($data, $algorithm) {
02836 if (empty($data) || empty($algorithm)) {
02837 return FALSE;
02838 }
02839 $par = ' -p '. check_plain($algorithm);
02840 foreach ($data as $name => $option) {
02841 if (empty($option)) {
02842 continue;
02843 }
02844 switch ($name) {
02845 case 'Scoring matrix':
02846 $par .= ' -M '. check_plain($option);
02847 break;
02848
02849 case 'Filtering':
02850 if (!empty($option['Low complexity regions'])) {
02851 $par .= " -F T";
02852 }
02853 if (!empty($option['Lower-case letters'])) {
02854 $par .= " -U T";
02855 }
02856 break;
02857
02858 case 'E-value cutoff':
02859 $par .= ' -e '. check_plain($option);
02860 break;
02861
02862 case 'Advanced BLAST options':
02863 if (!empty($option['ungapped']) && $algorithm != 'tblastx') {
02864 $par .= " -g F";
02865 }
02866 if (!empty($option['megablast']) && $algorithm == 'blastn') {
02867 $par .= " -n T";
02868 }
02869 break;
02870
02871 case 'query genetic code':
02872 $par .= ' -Q '. check_plain($option);
02873 break;
02874
02875 case 'DB genetic code':
02876 $par .= ' -D '. check_plain($option);
02877 break;
02878
02879 case 'Frame shift penalty':
02880 $par .= ' -w '. check_plain($option);
02881 break;
02882
02883 case 'Number of results':
02884 $par .= ' -b '. check_plain($option) .' -v '. check_plain($option);
02885 break;
02886 }
02887 }
02888 return $par;
02889 }
02890
02906 function dbsf_blast_batch_finished($success, $results, $operations) {
02907
02908 $uid_array = array();
02909 $submission_uid = '';
02910 $data = array();
02911 $to_store = "submission_uid:$submission_uid\nsoftware:blastall\n";
02912 $dirpath = file_create_path() .'/dbsf';
02913 $message = '';
02914
02915 foreach ($results['data'] as $form_uid => $batch_data) {
02916
02917 $submission_uid = $form_uid;
02918 $data = $batch_data;
02919 foreach ($batch_data as $uid => $values) {
02920 $to_store .= 'algorithm;id='. $values['algorithm'] .";$uid\n";
02921 $uid_array[] = $uid;
02922 }
02923 }
02924
02925 if ($success) {
02926
02927
02928 $outfile = $dirpath .'/'. $submission_uid .'.submission';
02929 if (!$outhandle = fopen($outfile, 'wt')) {
02930 drupal_set_message(t("Could not create $outfile."), 'error');
02931 return FALSE;
02932 }
02933 fwrite($outhandle, $to_store);
02934 fclose($outhandle);
02935 foreach ($data as $uid => $values) {
02936 $subject_dbs = array_merge((array)$values['sub'], (array)$results[$uid]['subject']);
02937 $subject_dbs_str = trim(implode(' ', $subject_dbs));
02938 $par = $values['par'] ." -d '". $subject_dbs_str ."'";
02939 dbsf_parameter_daemon($uid, $dirpath, $par, 'blastall', array('database' => $subject_dbs));
02940 }
02941 $message = t('Your request has been submitted to the queue for processing...');
02942 }
02943 else {
02944 $error_operation = reset($operations);
02945 $uids = implode(' ', $uid_array);
02946 $message = t('An error occurred while processing your request. Please '. l('contact', 'contact') ." the administrator reporting: $uids.");
02947 dpm($error_operation);
02948 return FALSE;
02949 }
02950 drupal_set_message($message, 'warning');
02951 }
02952
02962 function dbsf_blast_result_xml($blastfile = NULL) {
02963 $result_array = array();
02964 if (file_exists($blastfile)) {
02965 $xml_in = simplexml_load_file($blastfile, 'SimpleXMLElement');
02966 $algorithm = (string)$xml_in->BlastOutput_program;
02967 $xml = $xml_in->BlastOutput_iterations;
02968
02969 $array_iterations = get_object_vars($xml);
02970 $array_iterations = $array_iterations['Iteration'];
02971 if (is_object($array_iterations)) {
02972 $array_iterations = array(0 => $array_iterations);
02973 }
02974 $query_rank;
02975 foreach ($array_iterations as $iteration_obj) {
02976 $query_rank++;
02977 $query_id = '';
02978 $name_of_hit = '';
02979 $accession_of_hit = '';
02980 $average_bit_score = '';
02981 $top_bit_score = '';
02982 $average_evalue = '';
02983 $lowest_evalue = '';
02984 $average_identity = '';
02985 $average_similarity = '';
02986
02987 $iteration_data_array = get_object_vars($iteration_obj);
02988
02989 $query_id = $iteration_data_array['Iteration_query-ID'] .' '. $iteration_data_array['Iteration_query-def'];
02990 $query_id = preg_replace('/\s*lcl\|\d+_\d+\s*/', '', $query_id);
02991 $query_id = str_replace('No definition line found', '', $query_id);
02992 if (empty($query_id)) {
02993 $query_id = "User query $query_rank";
02994 }
02995 if (!empty($iteration_data_array['Iteration_message'])) {
02996 drupal_set_message($algorithm .': '. $iteration_data_array['Iteration_message'] ." for $query_id", 'warning');
02997 }
02998 $hit_iteration_hits = $iteration_data_array['Iteration_hits'];
02999 if (empty($hit_iteration_hits)) {
03000 continue;
03001 }
03002
03003 $hits_array = get_object_vars($hit_iteration_hits);
03004
03005 $hits_array = $hits_array['Hit'];
03006 if (is_object($hits_array)) {
03007 $hits_array = array(0 => $hits_array);
03008 }
03009
03010
03011 foreach ($hits_array as $rank0 => $hit_object) {
03012
03013
03014 $name_of_hit = (string)$hit_object->Hit_id;
03015 $accession_of_hit = (string)$hit_object->Hit_accession;
03016 $description_of_hit = (string)$hit_object->Hit_def;
03017 if ($description_of_hit == 'No definition line found') {
03018 $description_of_hit = '';
03019 }
03020 $hsps_array = get_object_vars($hit_object->Hit_hsps);
03021
03022
03023 $hsps_array = $hsps_array['Hsp'];
03024
03025
03026 if (is_object($hsps_array)) {
03027
03028 $hsps_array = array(0 => $hsps_array);
03029 }
03030
03031 $total_bit_score = 0;
03032 $total_evalue = 0;
03033 $hsp_number = 0;
03034 $total_identity = 0;
03035 $total_similarity = 0;
03036 foreach ($hsps_array as $hsp_rank => $hsp_obj) {
03037 $hsp_data = get_object_vars($hsp_obj);
03038
03039 $bit_score = $hsp_data['Hsp_bit-score'];
03040 $aln_length = $hsp_data['Hsp_align-len'];
03041 $evalue = $hsp_data['Hsp_evalue'];
03042 if (!empty($aln_length)) {
03043 $identity = $hsp_data['Hsp_identity'] / $aln_length;
03044 $similarity = $hsp_data['Hsp_positive'] / $aln_length;
03045 }
03046 $total_bit_score += $bit_score;
03047 $total_evalue += $evalue;
03048 $total_identity += $identity;
03049 $total_similarity += $similarity;
03050 $hsp_number++;
03051 if (empty($lowest_evalue) || $evalue < $lowest_evalue) {
03052 $lowest_evalue = $evalue;
03053 }
03054 if (empty($top_bit_score) || $bit_score > $top_bit_score) {
03055 $top_bit_score = $bit_score;
03056 }
03057 }
03058 $lowest_evalue = sprintf('%.2e', $lowest_evalue);
03059 $average_bit_score = $hsp_number ? sprintf('%.2f', $total_bit_score / $hsp_number) : 'NA';
03060 $average_evalue = $hsp_number ? sprintf('%.2e', $total_evalue / $hsp_number) : 'NA';
03061 $top_bit_score = sprintf('%.2f', $top_bit_score);
03062 $average_identity = $hsp_number ? sprintf('%.2f %%', $total_identity * 100 / $hsp_number) : 'NA';
03063 $average_similarity = $hsp_number ? sprintf('%.2f %%', $total_similarity * 100 / $hsp_number) : 'NA';
03064 $result_array[$query_id][$rank0 + 1] = array(
03065 'name_of_hit' => $name_of_hit,
03066 'accession_of_hit' => $accession_of_hit,
03067 'average_bit_score' => $average_bit_score,
03068 'top_bit_score' => $top_bit_score,
03069 'average_evalue' => $average_evalue,
03070 'lowest_evalue' => $lowest_evalue,
03071 'average_identity' => $average_identity,
03072 'average_similarity' => $average_similarity,
03073 );
03074 }
03075 }
03076 return ($result_array);
03077 }
03078 else {
03079 drupal_set_message("Failed to parse $blastfile.", 'error');
03080 return FALSE;
03081 }
03082 }
03083
03097 function dbsf_blast_result_page($submission_uid = NULL) {
03098 if (empty($submission_uid)) {
03099 $submission_uid = $_GET['submission_uid'];
03100 if (empty($submission_uid)) {
03101 return 'No submission data provided';
03102 }
03103 }
03104 $basepath = file_create_path();
03105 $dirpath = $basepath .'/dbsf/';
03106 $data = array();
03107
03108
03109 $infile = $dirpath . $submission_uid .'.submission';
03110 if (file_exists($infile)) {
03111 if ($inhandle = fopen($infile, 'rb')) {
03112 while (!feof($inhandle)) {
03113 $line = trim(fgets($inhandle));
03114 if (!empty($line) && preg_match('/^algorithm;id=/', $line)) {
03115 $line_data = explode('=', $line);
03116 $alg_data = explode(';', $line_data[1]);
03117 $data[$alg_data[0]] = $alg_data[1];
03118 }
03119 }
03120 }
03121 }
03122 else {
03123 dpm("Did not find $infile");
03124 return 'Submission ID not found. Maybe it has expired or is invalid?';
03125 }
03126 if (empty($data)) {
03127 dpm("$infile has no data");
03128 return 'Not a valid submission ID or ID has expired.';
03129 }
03130
03131 $page_tabs = array(
03132 'blast_results' => array(
03133 '#type' => 'tabset',
03134 ),
03135 );
03136 $return_html = '';
03137 foreach ($data as $algorithm => $uid) {
03138 $blastfile = $dirpath . $uid .'.output';
03139 $graph = $blastfile .'.graph.png';
03140 $content = t('Your report is not ready yet. Please wait or ') . l('try again', 'dbsf/blast_result/'. $submission_uid
03141 ) . t(' in a few moments or bookmark this page.');
03142 if (file_exists($graph)) {
03143 $results = drupal_get_form('dbsf_blast_result_form', $uid, $blastfile);
03144 if (strlen($results) < 600) {
03145 $results = "No hits found for any of your queries using $algorithm. Perhaps you can try a different algorithm or database?";
03146 }
03147 $content = '<p>'. l(
03148 theme_image($graph, 'BLAST results of all queries', 'BLAST results graphical overview', array('width' => '100%'), FALSE)
03149 , $blastfile .'.html', array('html' => TRUE, 'alias' => TRUE, 'attributes' => array('target' => '_blank'))
03150 ) .'</p>'. $results;
03151 }
03152 else {
03153 drupal_set_html_head('<meta http-equiv="refresh" content="15;url=http://'. $_SERVER['SERVER_NAME'] . $basename .'/dbsf/blast_result/'. $submission_uid .'" />');
03154 }
03155 $page_tabs['blast_results'][$algorithm] = array(
03156 '#type' => 'tabpage',
03157 '#title' => 'Results for '. $algorithm,
03158 '#content' => $content,
03159 );
03160 }
03161 $return_text = '<br><p>If you want, you can bookmark this page: your results will be stored on the server at least until '. date("F j, Y, g:i a", time() + 604800) .'. Let us '. l('know', 'contact') .' if you would like to keep them for longer.</p>';
03162 $return_text .= tabs_render($page_tabs);
03163 return $return_text;
03164 }
03165
03182 function dbsf_blast_result_form($form_state, $uid, $blastfile) {
03183 $form = array();
03184 if (empty($blastfile)) {
03185 return $form;
03186 }
03187 $array_from_xml = array();
03188 $array_from_xml = dbsf_blast_result_xml($blastfile);
03189
03190
03191 $table_row = array();
03192
03193 $row_check_data = array();
03194 if (empty($array_from_xml) || is_array($array_from_xml) === FALSE) {
03195
03196 return $form;
03197 }
03198 $query_rank = 0;
03199 foreach ($array_from_xml as $query_id => $hit_array) {
03200 $query_rank++;
03201 foreach ($hit_array as $hit_rank => $data) {
03202 $hit_id = $data['accession_of_hit'] ? $data['accession_of_hit'] : $data['name_of_hit'];
03203 $bit_score = $data['average_bit_score'] .' ('. $data['top_bit_score'] .')';
03204 $evalue = $data['average_evalue'] .' ('. $data['lowest_evalue'] .')';
03205 $identity = $data['average_identity'] .' ('. $data['average_similarity'] .')';
03206 $links = ''. l(
03207 theme_image(drupal_get_path('module', 'dbsf') .'/images/xml.png', 'Report as XML', 'Report as XML (recommended for parsing with other software)'), $blastfile,
03208 array(
03209 'html' => TRUE,
03210 'alias' => TRUE,
03211 'attributes' => array('target' => '_blank'),
03212 )
03213 ) . l(
03214 theme_image(drupal_get_path('module', 'dbsf') .'/images/html.png', 'Report as HTML', 'Report as HTML'), "$blastfile.Query$query_rank.html",
03215 array(
03216 'html' => TRUE,
03217 'alias' => TRUE,
03218 'attributes' => array('target' => '_blank'),
03219 )
03220 ) . l(
03221 theme_image(drupal_get_path('module', 'dbsf') .'/images/txt.png', 'Report as text', 'Report as text'), "$blastfile.Query$query_rank.txt",
03222 array(
03223 'html' => TRUE,
03224 'alias' => TRUE,
03225 'attributes' => array('target' => '_blank'),
03226 )
03227 );
03228 $table_row[] = array($query_id, $hit_id, $bit_score, $evalue, $identity, $links);
03229
03230
03231 $row_check_data[$hit_id] = $query_id;
03232 }
03233 }
03234 $table_header = array('Query ID', 'Hit ID', 'Average bit score (top)', 'Average e-value (lowest)', 'Average identity (av. similarity)', 'Links');
03235 $table_attributes = array('class' => 'padded-table sortable');
03236 $table_caption = 'Overview of significant results';
03237 $table_array = array(
03238
03239 'header' => $table_header,
03240 'caption' => $table_caption,
03241 'attributes' => $table_attributes,
03242 'data' => $table_row,
03243 );
03244
03245 $form['#blastfile'] = array('#value' => $blastfile);
03246 $form['data'] = array('#value' => $table_array);
03247 $form['features'] = array(
03248 '#type' => 'checkboxes',
03249 '#options' => $row_check_data,
03250 );
03251 $form['buttons'] = array(
03252 '#type' => 'fieldset',
03253 '#title' => t('Download hits of selected results as'),
03254 '#description' => t('If the reference database has been indexed then you can download the hits. Warning, the server will refuse to serve you with very large hits (such as a whole chromosome of a genome).'),
03255 '#collapsible' => FALSE,
03256 '#weight' => 20,
03257 'FASTA' => array(
03258 '#type' => 'submit',
03259 '#value' => t('FASTA'),
03260 '#weight' => 15,
03261 ),
03262
03263
03264
03265
03266
03267 );
03268 $form['#action'] = url("dbsf/get_sequences/$uid");
03269 return $form;
03270 }
03271
03283 function dbsf_theme_blast_result_form($form) {
03284 $table_array = $form['data']['#value'];
03285
03286 drupal_add_js(drupal_get_path('module', 'icexplore') .'/sorttable.js');
03287
03288 $output = '';
03289
03290 $row_checked = array();
03291 $checkall = theme('table_select_header_cell');
03292 $table_array['header'][0] = array(
03293 'data' => $table_array['header'][0], 'class' => $checkall['class'] .' sortable',
03294 );
03295
03296 if (empty($table_array['data'])) {
03297
03298 return FALSE;
03299 }
03300 foreach ($table_array['data'] as $value) {
03301 $feature_toreplace = $value[1];
03302 $render = drupal_render($form['features'][$feature_toreplace]);
03303 $row_checked[] = array(
03304 'data' => array_merge((array)$render, array_slice($value, 1)),
03305 );
03306 }
03307 $output .= '<p>';
03308 if (!empty($table_array['#links']['previous'])) {
03309 $output .= $table_array['#links']['previous'];
03310 }
03311 if (!empty($table_array['#links']['next'])) {
03312 $output .= $table_array['#links']['next'];
03313 }
03314 if (!empty($table_array['#links']['bookmark'])) {
03315 $output .= $table_array['#links']['bookmark'];
03316 }
03317 $output .= '</p>';
03318 $themed_table = theme_table($table_array['header'], $row_checked, $table_array['attributes'], $table_array['caption']);
03319 $output .= $themed_table;
03320
03321 $output .= drupal_render($form['buttons']);
03322 return ($output);
03323 }
03324