123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542 |
- <?php
- require('tripal_library.api.inc');
- function tripal_library_help($path, $arg) {
- $output = '';
- switch ($path) {
- case "admin/help#tripal_library":
- $output = '<p>'.
- t("Displays links to nodes created on this date").
- '</p>';
- break;
- }
- return $output;
- }
- function tripal_library_node_info() {
- $nodes = array();
- $nodes['chado_library'] = array(
- 'name' => t('Library'),
- 'module' => 'chado_library',
- 'description' => t('A library from the chado database'),
- 'has_title' => FALSE,
- 'title_label' => t('Library'),
- 'has_body' => FALSE,
- 'body_label' => t('Library Description'),
- 'locked' => TRUE
- );
- return $nodes;
- }
- function tripal_library_perm(){
- return array(
- 'access chado_library content',
- 'create chado_library content',
- 'delete chado_library content',
- 'edit chado_library content',
- );
- }
- function chado_library_access($op, $node, $account) {
- if ($op == 'create') {
- return user_access('create chado_library content', $account);
- }
- if ($op == 'update') {
- if (user_access('edit chado_library content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'delete') {
- if (user_access('delete chado_library content', $account)) {
- return TRUE;
- }
- }
- if ($op == 'view') {
- if (user_access('access chado_library content', $account)) {
- return TRUE;
- }
- }
- return FALSE;
- }
- function tripal_library_menu() {
- $items = array();
-
- $items['admin/tripal/tripal_library'] = array(
- 'title' => 'Libraries',
- 'description' => 'Basic Description of Tripal Library Module Functionality',
- 'page callback' => 'tripal_library_module_description_page',
- 'access arguments' => array('access administration pages'),
- 'type' => MENU_NORMAL_ITEM,
- );
-
- $items['admin/tripal/tripal_library/configuration'] = array(
- 'title' => 'Configuration',
- 'description' => 'Manage integration of Chado libraries including associated features.',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('tripal_library_admin'),
- 'access arguments' => array('access administration pages'),
- 'type' => MENU_NORMAL_ITEM,
- );
-
-
- $items['chado_sync_libraries'] = array(
- 'title' => t('Sync Data'),
- 'page callback' => 'tripal_library_sync_libraries',
- 'access arguments' => array('access administration pages'),
- 'type' => MENU_CALLBACK
- );
-
- $items['libraries'] = array(
- 'menu_name' => ('primary-links'),
- 'title' => t('DNA Libraries'),
- 'page callback' => 'tripal_library_show_libraries',
- 'access arguments' => array('access chado_library content'),
- 'type' => MENU_NORMAL_ITEM
- );
- return $items;
- }
-
- function tripal_library_views_api() {
- return array(
- 'api' => 2.0,
- );
- }
- function tripal_library_module_description_page() {
- $text = '';
-
- $text .= '<h3>Module Description:</h3>';
- $text .= '<p>The Tripal Library module is an interface for the Chado Library module which groups features (sequences) into genetic libraries.
- This module provides support for visualization of "library" pages, editing and updating.</p>';
- $text .= '<h3>Setup Instructions:</h3>';
- $text .= '<ol>';
- $text .= '<li><p><b>Set Permissions</b>: The library module supports the Drupal user permissions interface for
- controlling access to library content and functions. These permissions include viewing,
- creating, editing or administering of
- library content. The default is that only the original site administrator has these
- permissions. You can <a href="'.url('admin/user/roles').'">add roles</a> for classifying users,
- <a href="'.url('admin/user/user').'">assign users to roles</a> and
- <a href="'.url('admin/user/permissions').'">assign permissions</a> for the library content to
- those roles. For a simple setup, allow anonymous users access to view organism content and
- allow the site administrator all other permissions.</p></li>';
- $text .= '<li><p><b>Sync any Existing Libraries</b>: Near the top of the '.l('Library Configuration page','admin/tripal/tripal_library/configuration').' there is
- a Sync Libraries section which provides list of libraries currently in chado which can be sync\'d.
- Simply select the libraries you would like to create Drupal/Tripal pages for and click Sync Libraries.</p></li>';
- $text .= '</ol>';
-
-
- $text .= '<h3>Features of this Module:</h3>';
- $text .= '<ul>';
- $text .= '<li><b>Add/Edit/Delete Libraries</b>: Libraries with no associated features can be created '.l('here', 'node/add/chado-library').' but it is
- recommended to create the library using the feature loader. For example, when you load FASTA files using the Tripal loader you are
- given the option of specifying a library for all created features. Existing Libraries (regardless of the method used to create them) can be
- edited or deleted by clicking the Edit tab at the top of the Library Page.</li>';
- $text .= '<li><p><b>Integration with Drupal Views</b>: <a href="http://drupal.org/project/views">Drupal Views</a> is
- a powerful tool that allows the site administrator to create lists or basic searching forms of Chado content.
- It provides a graphical interface within Drupal to allow the site admin to directly query the Chado database
- and create custom lists without PHP programming or customization of Tripal source code. Views can also
- be created to filter content that has not yet been synced with Druapl in order to protect access to non
- published data (only works if Chado was installed using Tripal). You can see a list of available pre-existing
- Views <a href="'.url('admin/build/views/').'">here</a>, as well as create your own. </p></li>';
- $text .= '<li><b>Basic Listing</b>: This module provides a basic <a href="'.url('libraries').'">library display
- tool</a> for finding or listing libraries in Chado. It does not require indexing for Drupal searching but relies
- on Drupal Views. <a href="http://drupal.org/project/views">Drupal Views</a> must be installed.</li>';
- $text .= '</ul>';
-
- return $text;
- }
- function tripal_library_admin () {
- $form = array();
-
-
-
- $active_jobs = FALSE;
- if(tripal_get_module_active_jobs('tripal_library')){
- $active_jobs = TRUE;
- }
-
- if(!$active_jobs){
- get_tripal_library_admin_form_sync_set ($form);
- get_tripal_library_admin_form_reindex_set($form);
- get_tripal_library_admin_form_taxonomy_set($form);
- get_tripal_library_admin_form_cleanup_set($form);
- get_tripal_library_admin_form_menu_set($form);
- } else {
- $form['notice'] = array(
- '#type' => 'fieldset',
- '#title' => t('Library Management Temporarily Unavailable')
- );
- $form['notice']['message'] = array(
- '#value' => t('Currently, library management jobs are waiting or are running. . Managemment features have been hidden until these jobs complete. Please check back later once these jobs have finished. You can view the status of pending jobs in the Tripal jobs page.'),
- );
- }
- return system_settings_form($form);
- }
- function tripal_library_nodeapi(&$node, $op, $teaser, $page) {
- switch ($op) {
-
-
- case 'view':
-
- if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
- $node->content['tripal_library_index_version'] = array(
- '#value' => theme('tripal_library_search_index',$node),
- );
- } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
- $node->content['tripal_library_index_version'] = array(
- '#value' => theme('tripal_library_search_result',$node),
- );
- } else {
- switch($node->type){
- case 'chado_organism':
-
- $node->content['tripal_organism_libraries'] = array(
- '#value' => theme('tripal_organism_libraries', $node),
- );
- break;
- case 'chado_feature':
-
- $node->content['tripal_feature_libraries'] = array(
- '#value' => theme('tripal_feature_libraries', $node),
- );
- break;
- }
- }
- break;
- }
- }
- function tripal_library_theme () {
- return array(
- 'tripal_library_library_table' => array (
- 'arguments' => array('libraries'),
- ),
- 'tripal_library_search_index' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_library_search_result' => array (
- 'arguments' => array('node'),
- ),
- 'tripal_organism_libraries' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_organism_libraries',
- ),
- 'tripal_feature_libraries' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_feature_libraries',
- ),
- 'tripal_library_base' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_library_base',
- ),
- 'tripal_library_synonyms' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_library_synonyms',
- ),
- 'tripal_library_references' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_library_references',
- ),
- 'tripal_library_properties' => array (
- 'arguments' => array('node'=> null),
- 'template' => 'tripal_library_properties',
- ),
- );
- }
- function theme_tripal_library_search_index ($node) {
- if ($node->type == 'chado_organism') {
- $content = "";
-
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT * FROM {library} L ".
- "WHERE L.Organism_id = $node->organism_id";
- $libraries = array();
- $results = db_query($sql);
- while($library = db_fetch_object($results)){
-
- $sql = "SELECT * FROM {libraryprop} LP ".
- " INNER JOIN CVTerm CVT ON CVT.cvterm_id = LP.type_id ".
- "WHERE LP.library_id = $library->library_id ".
- " AND CVT.name = 'library_description'";
- $desc = db_fetch_object(db_query($sql));
- $library->description = $desc->value;
- $libraries[] = $library;
- }
- tripal_db_set_active($previous_db);
- if(count($libraries) > 0){
- foreach ($libraries as $library){
- $content .= "$library->name ";
- $content .= "$library->description";
- };
- }
-
- } else if ($node->type == 'chado_feature') {
- $content = "";
- $organism_id = $node->feature->organism_id;
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT * FROM {library} L ".
- " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
- "WHERE LF.feature_id = " . $node->feature->feature_id;
- $libraries = array();
- $results = db_query($sql);
- while($library = db_fetch_object($results)){
- $libraries[] = $library;
- }
- tripal_db_set_active($previous_db);
- if(count($libraries) > 0){
- $lib_additions = array();
- foreach ($libraries as $library){
- $content .= $library->name;
- };
- }
- }
- return $content;
- }
- function theme_tripal_library_node_libraries($node) {
- $content = "";
-
-
-
-
- if ($node->type == 'chado_organism' && $node->organism_id) {
- $box_status = variable_get("tripal_library-box-libraries","menu_off");
-
- if(strcmp($box_status,"menu_off")==0){
- return get_tripal_library_organism_libraries($node->nid);
- }
- }
-
-
-
- else if ($node->type == 'chado_feature' && $node->feature->feature_id) {
- $organism_id = $node->feature->organism_id;
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT * FROM {library} L ".
- " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
- "WHERE LF.feature_id = " . $node->feature->feature_id;
- $libraries = array();
- $results = db_query($sql);
- while($library = db_fetch_object($results)){
- $libraries[] = $library;
- }
- tripal_db_set_active($previous_db);
- if(count($libraries) > 0){
- $lib_additions = array();
- foreach ($libraries as $library){
- $sql = "SELECT nid FROM {chado_library} WHERE library_id = %d";
- $lib_nid = db_result(db_query($sql, $library->library_id));
- if ($lib_nid) {
- $lib_url = url("node/$lib_nid");
- }
- $lib_additions[$lib_url] = $library->name;
- };
- $node->lib_additions = $lib_additions;
- }
- }
- return $content;
- }
- function get_tripal_library_admin_form_menu_set(&$form){
- $form['menu'] = array(
- '#type' => 'fieldset',
- '#title' => t('Set Menu')
- );
-
- $types = array(
- 'tripal_organism' => t('Organism Page'),
- 'tripal_feature' => t('Feature Page'),
- );
- $defaults = variable_get('tripal_lib_menu_node_types','');
- $form['menu']['tripal_lib_menu_node_types'] = array(
- '#title' => 'Page Types',
- '#type' => 'checkboxes',
- '#description' => t("Libraries can be associated with other data types ".
- "such as organisms, features, analyses, etc. The library module ".
- "will add to the respective pages a box or menu item with a ".
- "list of libraries associated with the data type. This list ".
- "will appear in a box on the page by default. To place this ".
- "list as a menu item rather than a box, check the appropriate boxes ".
- "above"),
- '#options' => $types,
- '#default_value' => $defaults,
- '#weight' => 1,
- );
- }
- function get_tripal_library_admin_form_cleanup_set(&$form) {
- $form['cleanup'] = array(
- '#type' => 'fieldset',
- '#title' => t('Clean Up')
- );
- $form['cleanup']['description'] = array(
- '#type' => 'item',
- '#value' => t("With Drupal and chado residing in different databases ".
- "it is possible that nodes in Drupal and libraries in Chado become ".
- "\"orphaned\". This can occur if an library node in Drupal is ".
- "deleted but the corresponding chado library is not and/or vice ".
- "versa. Click the button below to resolve these discrepancies."),
- '#weight' => 1,
- );
- $form['cleanup']['button'] = array(
- '#type' => 'submit',
- '#value' => t('Clean up orphaned libraries'),
- '#weight' => 2,
- );
- }
- function get_tripal_library_admin_form_taxonomy_set(&$form) {
- $form['taxonify'] = array(
- '#type' => 'fieldset',
- '#title' => t('Assign Drupal Taxonomy to Library Features')
- );
-
- $sql = "SELECT * FROM {Library} ORDER BY uniquename";
- $previous_db = tripal_db_set_active('chado');
- $lib_rset = db_query($sql);
- tripal_db_set_active($previous_db);
-
- $lib_boxes = array();
- while($library = db_fetch_object($lib_rset)){
- $lib_boxes[$library->library_id] = "$library->name";
- }
- $form['taxonify']['description'] = array(
- '#type' => 'item',
- '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
- "nodes. These terms allow for advanced filtering during searching. This option allows ".
- "for setting taxonomy only for features that belong to the selected libraries below. All other features will be unaffected. To set taxonomy for all features in the site see the Feature Administration page."),
- '#weight' => 1,
- );
- $form['taxonify']['tx-libraries'] = array (
- '#title' => t('Libraries'),
- '#type' => t('checkboxes'),
- '#description' => t("Check the libraries whose features you want to reset taxonomy. Note: this list contains all libraries, even those that may not be synced."),
- '#required' => FALSE,
- '#prefix' => '<div id="lib_boxes">',
- '#suffix' => '</div>',
- '#options' => $lib_boxes,
- '#weight' => 2
- );
- $form['taxonify']['tx-button'] = array(
- '#type' => 'submit',
- '#value' => t('Set Feature Taxonomy'),
- '#weight' => 3
- );
- }
- function get_tripal_library_admin_form_reindex_set(&$form) {
-
- $form['reindex'] = array(
- '#type' => 'fieldset',
- '#title' => t('Reindex Library Features')
- );
-
- $sql = "SELECT * FROM {Library} ORDER BY uniquename";
- $previous_db = tripal_db_set_active('chado');
- $lib_rset = db_query($sql);
- tripal_db_set_active($previous_db);
-
- $lib_boxes = array();
- while($library = db_fetch_object($lib_rset)){
- $lib_boxes[$library->library_id] = "$library->name";
- }
- $form['reindex']['description'] = array(
- '#type' => 'item',
- '#value' => t("This option allows for reindexing of only those features that belong to the selected libraries below. All other features will be unaffected. To reindex all features in the site see the Feature Administration page."),
- '#weight' => 1,
- );
- $form['reindex']['re-libraries'] = array (
- '#title' => t('Libraries'),
- '#type' => t('checkboxes'),
- '#description' => t("Check the libraries whoee features you want to reindex. Note: this list contains all libraries, even those that may not be synced."),
- '#required' => FALSE,
- '#prefix' => '<div id="lib_boxes">',
- '#suffix' => '</div>',
- '#options' => $lib_boxes,
- '#weight' => 2,
- );
- $form['reindex']['re-button'] = array(
- '#type' => 'submit',
- '#value' => t('Reindex Features'),
- '#weight' => 3,
- );
- }
- function get_tripal_library_admin_form_sync_set (&$form) {
-
- $form['sync'] = array(
- '#type' => 'fieldset',
- '#title' => t('Sync Libraries')
- );
-
- $sql = "SELECT * FROM {Library} ORDER BY uniquename";
- $previous_db = tripal_db_set_active('chado');
- $lib_rset = db_query($sql);
- tripal_db_set_active($previous_db);
-
-
-
-
- $lib_boxes = array();
- $added = 0;
- while($library = db_fetch_object($lib_rset)){
-
-
- $sql = "SELECT * FROM {chado_library} WHERE library_id = %d";
- if(!db_fetch_object(db_query($sql,$library->library_id))){
- $lib_boxes[$library->library_id] = "$library->name";
- $added++;
- }
- }
-
-
- if($added > 0){
- $lib_boxes['all'] = "All Libraries";
- $form['reindex']['description'] = array(
- '#type' => 'item',
- '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
- '#weight' => 1,
- );
- $form['sync']['libraries'] = array (
- '#title' => t('Available Libraries'),
- '#type' => t('checkboxes'),
- '#description' => t("Check the libraries you want to sync. Drupal content will be created for each of the libraries listed above. Select 'All Libraries' to sync all of them."),
- '#required' => FALSE,
- '#prefix' => '<div id="lib_boxes">',
- '#suffix' => '</div>',
- '#options' => $lib_boxes,
- '#weight' => 2,
- );
- $form['sync']['button'] = array(
- '#type' => 'submit',
- '#value' => t('Sync Libraries'),
- '#weight' => 3,
- );
- }
-
- else {
- $form['sync']['value'] = array(
- '#value' => t('All libraries in Chado are currently synced with Drupal.')
- );
- }
- }
- function tripal_library_admin_validate($form, &$form_state) {
- global $user;
- $job_args = array();
-
- if ($form_state['values']['op'] == t('Sync Libraries')) {
-
-
- $libraries = $form_state['values']['libraries'];
- $do_all = FALSE;
- $to_sync = array();
- foreach ($libraries as $library_id){
- if(preg_match("/^all$/i",$library_id)){
- $do_all = TRUE;
- }
- if($library_id and preg_match("/^\d+$/i",$library_id)){
-
- $sql = "SELECT * FROM {Library} WHERE library_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $library = db_fetch_object(db_query($sql,$library_id));
- tripal_db_set_active($previous_db);
- $to_sync[$library_id] = $library->name;
- }
- }
-
- if($do_all){
- tripal_add_job('Sync all libraries','tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
- }
- else{
- foreach($to_sync as $library_id => $name){
- $job_args[0] = $library_id;
- tripal_add_job("Sync library: $name",'tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
- }
- }
- }
-
-
- if ($form_state['values']['op'] == t('Reindex Features')) {
- $libraries = $form_state['values']['re-libraries'];
- foreach ($libraries as $library_id){
- if($library_id and preg_match("/^\d+$/i",$library_id)){
-
- $sql = "SELECT * FROM {Library} WHERE library_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $library = db_fetch_object(db_query($sql,$library_id));
- tripal_db_set_active($previous_db);
- $job_args[0] = $library_id;
- tripal_add_job("Reindex features for library: $library->name",'tripal_library',
- 'tripal_library_reindex_features',$job_args,$user->uid);
- }
- }
- }
-
-
- if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
- $libraries = $form_state['values']['tx-libraries'];
- foreach ($libraries as $library_id){
- if($library_id and preg_match("/^\d+$/i",$library_id)){
-
- $sql = "SELECT * FROM {Library} WHERE library_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $library = db_fetch_object(db_query($sql,$library_id));
- tripal_db_set_active($previous_db);
- $job_args[0] = $library_id;
- tripal_add_job("Set taxonomy for features in library: $library->name",'tripal_library',
- 'tripal_library_taxonify_features',$job_args,$user->uid);
- }
- }
- }
-
-
- if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
- tripal_add_job('Cleanup orphaned libraries','tripal_library',
- 'tripal_library_cleanup',$job_args,$user->uid);
- }
- }
- function tripal_library_show_libraries (){
-
- $sql = "SELECT COUNT(library_id) FROM {chado_library}";
- $no_libs = db_result(db_query ($sql));
- if($no_libs != 0) {
- $libraries = get_chado_libraries ();
- if($no_libs != count($libraries)) {
- drupal_set_message("Synchronization needed.");
- }
- return theme('tripal_library_library_table', &$libraries);
- } else {
- return t("No library exists. Please contact administrators to ".
- "synchronize libraries.");
- }
- }
- function tripal_library_cron (){
- }
- function tripal_library_library_access($op, $node, $account){
- if ($op == 'create') {
- return user_access('create chado_library content', $account);
- }
- if ($op == 'update') {
- if (user_access('edit any chado_library content', $account) ||
- (user_access('edit own chado_library content', $account) &&
- ($account->uid == $node->uid))){
- return TRUE;
- }
- }
- if ($op == 'delete') {
- if (user_access('delete any chado_library content', $account) ||
- (user_access('delete own chado_library content', $account) &&
- ($account->uid == $node->uid))) {
- return TRUE;
- }
- }
- }
- function chado_library_validate($node){
- $lib = 0;
-
-
- if($node->library_id){
- $sql = "SELECT * FROM ".
- "{Library} WHERE ".
- "uniquename = '%s' ".
- "AND NOT library_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $lib = db_fetch_object(db_query($sql, $node->uniquename,
- $node->library_id));
- tripal_db_set_active($previous_db);
- } else {
- $sql = "SELECT * FROM ".
- "{Library} ".
- "WHERE uniquename = '%s'";
- $previous_db = tripal_db_set_active('chado');
- $lib = db_fetch_object(db_query($sql, $node->uniquename));
- tripal_db_set_active($previous_db);
- }
- if($lib){
- form_set_error('uniquename',t('The unique library name already exists. '.
- 'Please choose another'));
- }
- }
- function chado_library_insert($node){
-
- $values = array(
- 'cv_id' => array(
- 'name' => 'tripal'
- ),
- 'name' => $node->library_type
- );
- $type = tripal_core_chado_select('cvterm',array('cvterm_id'),$values);
- $values = array(
- 'name' => $node->title,
- 'uniquename' => $node->uniquename,
- 'organism_id' => $node->organism_id,
- 'type_id' => $type[0]->cvterm_id
- );
- $library = tripal_core_chado_insert('library', $values);
- if ($library) {
-
- tripal_library_insert_property($library['library_id'],'library_description',$node->library_description);
-
-
- $library_id = chado_get_id_for_node('library',$node) ;
- if(!$library_id){
-
- $sql = "INSERT INTO {chado_library} (nid, vid, library_id) ".
- "VALUES (%d, %d, %d)";
- db_query($sql,$node->nid,$node->vid,$library['library_id']);
- }
- } else {
- drupal_set_message('Unable to add library.', 'warning');
- watchdog('tripal_library',
- 'Insert feature: Unable to create library where values: %values',
- array('%values' => print_r($values, TRUE)),
- WATCHDOG_WARNING
- );
- }
- }
- function chado_library_update($node){
- if($node->revision){
-
- } else {
- $library_id = chado_get_id_for_node('library',$node) ;
-
- $values = array(
- 'cv_id' => array(
- 'name' => 'tripal'
- ),
- 'name' => $node->library_type
- );
- $type = tripal_core_chado_select('cvterm',array('cvterm_id'),$values);
-
- $match = array(
- 'library_id' => $library_id,
- );
- $values = array(
- 'name' => $node->title,
- 'uniquename' => $node->uniquename,
- 'organism_id' => $node->organism_id,
- 'type_id' => $type[0]->cvterm_id
- );
- $status = tripal_core_chado_update('library', $match,$values);
- tripal_library_update_property($library_id,'library_description',$node->library_description);
- }
- }
- function tripal_library_add_taxonomy ($node,$library_id){
-
-
-
- module_load_include('inc', 'taxonomy', 'taxonomy.admin');
-
-
- $vocab_form['values']['name'] = 'DNA Libraries';
- $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
- $vocab_form['values']['help'] = '';
- $vocab_form['values']['module'] = 'taxonomy';
- drupal_execute('taxonomy_form_vocabulary',$vocab_form);
- return;
-
-
- if($vid){
- $tree = taxonomy_get_tree($vid);
- $found = 0;
- foreach ($tree as $term) {
- if($term->name == $node->title){
- $found = 1;
- }
- }
-
- if(!$found){
- $form_state = array();
- $form_state['values']['name'] = $node->title;
- $form_state['values']['description'] = $library_id;
- drupal_execute('taxonomy_form_term',$form_state,$vid);
- }
- }
- }
- function chado_library_form ($node){
- $form = array();
- $library = $node->library;
-
-
- $uniquename = $node->uniquename;
- if(!$uniquename){
- $uniquename = $library->uniquename;
- }
- $library_type = $node->library_type;
- if(!$library_type){
- $library_type = $library->type_id->name;
- }
- $organism_id = $node->organism_id;
- if(!$organism_id){
- $organism_id = $library->organism_id->organism_id;
- }
- $library_description = $node->library_description;
- if(!$library_description){
- $libprop = tripal_library_get_property($library->library_id,'library_description');
- $library_description = $libprop->value;
- }
-
-
- $form['library_id'] = array(
- '#type' => 'value',
- '#value' => $library->library_id,
- );
- $form['title']= array(
- '#type' => 'textfield',
- '#title' => t('Library Title'),
- '#description' => t('Please enter the title for this library. '.
- 'This appears at the top of the library page.'),
- '#required' => TRUE,
- '#default_value' => $node->title,
- '#weight' => 1
- );
- $form['uniquename']= array(
- '#type' => 'textfield',
- '#title' => t('Unique Library Name'),
- '#description' => t('Please enter a unique name for this library'),
- '#required' => TRUE,
- '#default_value' => $uniquename,
- '#weight' => 2
- );
-
- $library_types = array();
- $library_types[''] = '';
- $library_types['cdna_library'] = 'cDNA Library';
- $library_types['bac_library'] = 'BAC Library';
- $library_types['fosmid_library'] = 'FOSMID Library';
- $library_types['cosmid_library'] = 'COSMID Library';
- $library_types['yac_library'] = 'YAC Library';
-
-
- $form['library_type'] = array (
- '#title' => t('Library Type'),
- '#type' => t('select'),
- '#description' => t("Choose the library type."),
- '#required' => TRUE,
- '#default_value' => $library_type,
- '#options' => $library_types,
- '#weight' => 3
- );
-
- $sql = "SELECT * FROM {Organism}";
- $previous_db = tripal_db_set_active('chado');
- $org_rset = db_query($sql);
- tripal_db_set_active($previous_db);
- $organisms = array();
- $organisms[''] = '';
- while($organism = db_fetch_object($org_rset)){
- $organisms[$organism->organism_id] =
- "$organism->genus $organism->species ($organism->common_name)";
- }
- $form['organism_id'] = array (
- '#title' => t('Organism'),
- '#type' => t('select'),
- '#description' => t("Choose the organism with which this library is ".
- "associated."),
- '#required' => TRUE,
- '#default_value' => $organism_id,
- '#options' => $organisms,
- '#weight' => 4,
- );
- $form['library_description']= array(
- '#type' => 'textarea',
- '#title' => t('Library Description'),
- '#description' => t('A brief description of the library'),
- '#required' => TRUE,
- '#default_value' => $library_description,
- '#weight' => 5
- );
- return $form;
- }
- function tripal_library_sync_libraries ($library_id = NULL, $job_id = NULL){
- global $user;
- $page_content = '';
-
- if(!$library_id){
- $sql = "SELECT * FROM {Library} L";
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql);
- tripal_db_set_active($previous_db);
- } else {
- $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql,$library_id);
- tripal_db_set_active($previous_db);
- }
-
-
- $sql = "SELECT * FROM {chado_library} ".
- "WHERE library_id = %d";
- while($library = db_fetch_object($results)){
-
-
- if(!db_fetch_object(db_query($sql,$library->library_id))){
- $new_node = new stdClass();
- $new_node->type = 'chado_library';
- $new_node->uid = $user->uid;
- $new_node->title = "$library->name";
- $new_node->library_id = $library->library_id;
- $new_node->organism_id = $library->organism_id;
- $new_node->uniquename = $library->uniquename;
- $new_node->type_id = $library->type_id;
- node_validate($new_node);
- if(!form_get_errors()){
- $node = node_submit($new_node);
- node_save($node);
- if($node->nid){
- $page_content .= "Added $library->name<br>";
- }
- }
- } else {
- $page_content .= "Skipped $library->name<br>";
- }
- }
- return $page_content;
- }
- function chado_library_load($node){
-
- $library_id = chado_get_id_for_node('library',$node);
- $values = array('library_id' => $library_id);
- $library = tripal_core_generate_chado_var('library',$values);
- $additions->library = $library;
- return $additions;
- }
- function chado_library_view ($node, $teaser = FALSE, $page = FALSE) {
-
- if (!$teaser) {
- $node = node_prepare($node, $teaser);
-
-
-
- if(!$node->genus) {
- $previous_db = tripal_db_set_active('chado');
- $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
- $data = db_fetch_object(db_query($sql, $node->organism_id));
- $node->genus = $data->genus;
- $node->species = $data->species;
- $node->common_name = $data->common_name;
- tripal_db_set_active($previous_db);
- }
- }
- return $node;
- }
- function theme_tripal_library_library_table ($libraries) {
-
- $output = "<div id=\"libraries\">";
- $output .= '<table>';
- $output .= "<tr>";
- $output .= "<th>Name</th>";
- $output .= "<th>Type</th>";
- $output .= "<th>Organism</th>";
- $output .= "<th>Description</th>";
- $output .= "</tr>";
- foreach($libraries as $library){
- $lib_url = url("node/$library->node_id");
- $output .= "<tr>";
- $output .= "<td>".
- " <a href=\"$lib_url\">$library->name</a>".
- "</td>";
- if(strcasecmp($library->type_name, 'bac_library') == 0){
- $output .= "<td>BAC</td>";
- }
- elseif(strcasecmp($library->type_name, 'cdna_library') == 0){
- $output .= "<td>cDNA</td>";
- }
- else{
- $output .= "<td>$library->type_name</td>";
- }
- $output .= "<td nowrap>".
- " $library->common_name".
- "</td>";
- $description = $library->library_description;
- $output .= "<td>$description</td>";
- $output .= "</tr>";
- }
- $output .= "</table>";
- $output .= "</div>";
- return $output;
- }
- function tripal_library_feature_set_taxonomy($library_id = NULL){
-
-
- $vocabularies = taxonomy_get_vocabularies();
- $vid = NULL;
- foreach($vocabularies as $vocab){
- if($vocab->name == 'Library'){
- $vid = $vocab->vid;
- }
- }
- if(!$vid){
- return;
- }
-
- if($library_id){
- print "Finding features for library with ID: $library_id\n";
- $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
- "FROM {library_feature} LF ".
- "INNER JOIN Library L ON LF.library_id = L.library_id ".
- "WHERE L.library_id = $library_id ".
- "ORDER BY LF.feature_id";
- $previous_db = tripal_db_set_active('chado');
- $features = db_query($sql);
- tripal_db_set_active($previous_db);
- } else {
- print "Finding features for all libraries\n";
- $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
- "FROM {library_feature} LF ".
- "INNER JOIN Library L ON LF.library_id = L.library_id ".
- "ORDER BY LF.feature_id";
- $previous_db = tripal_db_set_active('chado');
- $features = db_query($sql);
- tripal_db_set_active($previous_db);
- }
- $node_sql = "SELECT * FROM {chado_feature} CF ".
- " INNER JOIN {node} N ON CF.nid = N.nid ".
- "WHERE feature_id = %d";
-
- while($feature = db_fetch_object($features)){
- $node = db_fetch_object(db_query($node_sql,$feature->feature_id));
- $tags["$vid"] = $feature->libname;
- $terms['tags'] = $tags;
- taxonomy_node_save($node,$terms);
- print "Updated $feature->feature_id as $feature->libname\n";
- }
- }
- function tripal_library_reindex_features ($library_id = NULL, $job_id = NULL){
- $i = 0;
-
-
-
- if($library_id){
- $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
- " FROM {library_feature} LF ".
- " INNER JOIN Library L ON LF.library_id = L.library_id ".
- "WHERE L.library_id = $library_id ".
- "ORDER BY LF.feature_id";
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql);
- tripal_db_set_active($previous_db);
- }
- else {
- $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
- " FROM {library_feature} LF ".
- " INNER JOIN Library L ON LF.library_id = L.library_id ".
- "ORDER BY LF.feature_id";
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql);
- tripal_db_set_active($previous_db);
- }
-
- $count = 0;
- $ids = array();
- while($id = db_fetch_object($results)){
- $ids[$count] = $id->feature_id;
- $count++;
- }
- $interval = intval($count * 0.01);
- foreach($ids as $feature_id){
-
- if($job_id and $i % interval == 0){
- tripal_job_set_progress($job_id,intval(($i/$count)*100));
- }
- tripal_feature_sync_feature ($feature_id);
- $i++;
- }
- }
- function tripal_library_taxonify_features ($library_id = NULL, $job_id = NULL){
- $i = 0;
-
-
-
- if($library_id){
- $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
- " FROM {library_feature} LF ".
- " INNER JOIN Library L ON LF.library_id = L.library_id ".
- "WHERE L.library_id = $library_id ".
- "ORDER BY LF.feature_id";
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql);
- tripal_db_set_active($previous_db);
- }
- else {
- $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
- " FROM {library_feature} LF ".
- " INNER JOIN Library L ON LF.library_id = L.library_id ".
- "ORDER BY LF.feature_id";
- $previous_db = tripal_db_set_active('chado');
- $results = db_query($sql);
- tripal_db_set_active($previous_db);
- }
-
- $count = 0;
- $ids = array();
- while($id = db_fetch_object($results)){
- $ids[$count] = $id->feature_id;
- $count++;
- }
-
- tripal_feature_set_vocabulary();
-
- $nsql = "SELECT * FROM {chado_feature} CF ".
- " INNER JOIN {node} N ON N.nid = CF.nid ".
- "WHERE feature_id = %d";
-
- $interval = intval($count * 0.01);
- foreach($ids as $feature_id){
-
- if($job_id and $i % interval == 0){
- tripal_job_set_progress($job_id,intval(($i/$count)*100));
- }
- $node = db_fetch_object(db_query($nsql,$feature_id));
- tripal_feature_set_taxonomy($node,$feature_id);
- $i++;
- }
- }
- function chado_library_delete(&$node){
-
-
- $sql_drupal = "SELECT library_id ".
- "FROM {chado_library} ".
- "WHERE nid = %d AND vid = %d";
- $library_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
-
-
- $sql_del = "DELETE FROM {chado_library} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
- $sql_del = "DELETE FROM {node_revisions} ".
- "WHERE nid = %d ".
- "AND vid = %d";
- db_query($sql_del, $node->nid, $node->vid);
-
- $previous_db = tripal_db_set_active('chado');
- db_query("DELETE FROM {library} WHERE library_id = %d", $library_id);
- db_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
- tripal_db_set_active($previous_db);
- }
- function tripal_library_block($op = 'list', $delta = '0', $edit = array()){
- switch($op){
- case 'list':
- $blocks['libreferences']['info'] = t('Tripal Library References');
- $blocks['libreferences']['cache'] = BLOCK_NO_CACHE;
- $blocks['libbase']['info'] = t('Tripal Library Details');
- $blocks['libbase']['cache'] = BLOCK_NO_CACHE;
- $blocks['libsynonyms']['info'] = t('Tripal Library Synonyms');
- $blocks['libsynonyms']['cache'] = BLOCK_NO_CACHE;
- $blocks['libproperties']['info'] = t('Tripal Library Properties');
- $blocks['libproperties']['cache'] = BLOCK_NO_CACHE;
- $blocks['featurelibs']['info'] = t('Tripal Feature Libraries');
- $blocks['featurelibs']['cache'] = BLOCK_NO_CACHE;
- $blocks['orglibs']['info'] = t('Tripal Organism Libraries');
- $blocks['orglibs']['cache'] = BLOCK_NO_CACHE;
- return $blocks;
- case 'view':
- if(user_access('access chado_library content') and arg(0) == 'node' and is_numeric(arg(1))) {
- $nid = arg(1);
- $node = node_load($nid);
-
- $block = array();
- switch($delta){
- case 'libreferences':
- $block['subject'] = t('References');
- $block['content'] = theme('tripal_library_references',$node);
- break;
- case 'libbase':
- $block['subject'] = t('Library Details');
- $block['content'] = theme('tripal_library_base',$node);
- break;
- case 'libsynonyms':
- $block['subject'] = t('Synonyms');
- $block['content'] = theme('tripal_library_synonyms',$node);
- break;
- case 'libproperties':
- $block['subject'] = t('Properties');
- $block['content'] = theme('tripal_library_properties',$node);
- break;
- case 'featurelibs':
- $block['subject'] = t('Libraries');
- $block['content'] = theme('tripal_feature_libraries',$node);
- break;
- case 'orglibs':
- $block['subject'] = t('Libraries');
- $block['content'] = theme('tripal_organism_libraries',$node);
- break;
- default :
- }
- return $block;
- }
- }
- }
- function get_chado_libraries() {
- $sql_drupal = "SELECT COUNT (library_id) FROM {chado_library}";
- $no_libs = db_result(db_query($sql_drupal));
- if ($no_libs != 0) {
-
- $sql = "SELECT library_id, nid FROM {chado_library}";
- $result = db_query($sql);
- $previous_db = tripal_db_set_active('chado');
-
- $sql_info = "SELECT L.name, uniquename, genus, species, common_name, ".
- " CVT.name as type_name ".
- "FROM {Library} L ".
- "INNER JOIN Organism O ON L.organism_id = O.organism_id ".
- "INNER JOIN CVTerm CVT ON L.type_id = CVT.cvterm_id ".
- "WHERE library_id=%d";
-
- $sql_desc = "SELECT value ".
- "FROM {Libraryprop} LP ".
- "INNER JOIN CVTerm CVT ON LP.type_id = CVT.cvterm_id ".
- "WHERE library_id = %d AND CVT.name = 'library_description'";
- $libraries = array();
- while ($data = db_fetch_object($result)) {
- $library = db_fetch_object(db_query($sql_info, $data->library_id));
- $library->node_id = $data->nid;
- $desc = db_fetch_object(db_query($sql_desc, $data->library_id))->value;
- if ($desc) {
- $library->library_description = $desc;
- } else {
- $library->library_description = "NA";
- }
-
- $key = strtolower($library->uniquename);
- $libraries [$key] = $library;
- }
- tripal_db_set_active($previous_db);
-
- ksort($libraries, SORT_STRING);
- return $libraries;
- }
- }
- function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
-
- $dsql = "SELECT * FROM {node} WHERE type = 'chado_library' order by nid";
- $nsql = "SELECT * FROM {node} WHERE nid = %d";
- $csql = "SELECT * FROM {chado_library} where nid = %d ";
- $cosql= "SELECT * FROM {chado_library}";
- $tsql = "SELECT * FROM {library} L WHERE library_id = %d";
-
- $results = db_query($dsql);
- $count = 0;
- $nodes = array();
- while($node = db_fetch_object($results)){
- $nodes[$count] = $node;
- $count++;
- }
-
- $results = db_query($cosql);
- $cnodes = array();
- while($node = db_fetch_object($results)){
- $cnodes[$count] = $node;
- $count++;
- }
- $interval = intval($count * 0.01);
-
- foreach($nodes as $nid){
-
- if($job_id and $i % $interval == 0){
- tripal_job_set_progress($job_id,intval(($i/$count)*100));
- }
-
-
- $library = db_fetch_object(db_query($csql,$nid->nid));
- if(!$library){
- node_delete($nid->nid);
- $message = "Missing in chado_library table.... DELETING: $nid->nid\n";
- watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
- continue;
- }
- $i++;
- }
-
- foreach($cnodes as $nid){
-
- if($job_id and $i % $interval == 0){
- tripal_job_set_progress($job_id,intval(($i/$count)*100));
- }
- $node = db_fetch_object(db_query($nsql,$nid->nid));
- if(!$node){
- db_query("DELETE FROM {chado_library} WHERE nid = $nid->nid");
- $message = "chado_library missing node.... DELETING: $nid->nid\n";
- watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
- }
- $i++;
- }
- return '';
- }
|