tripal_library.module 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580
  1. <?php
  2. require('tripal_library.api.inc');
  3. /*******************************************************************************
  4. * Display help and module information
  5. * @param path which path of the site we're displaying help
  6. * @param arg array that holds the current path as would be returned from arg()
  7. * function
  8. * @return help text for the path
  9. */
  10. function tripal_library_help($path, $arg) {
  11. $output = '';
  12. switch ($path) {
  13. case "admin/help#tripal_library":
  14. $output = '<p>'.
  15. t("Displays links to nodes created on this date").
  16. '</p>';
  17. break;
  18. }
  19. return $output;
  20. }
  21. /*******************************************************************************
  22. * Provide information to drupal about the node types that we're creating
  23. * in this module
  24. */
  25. function tripal_library_node_info() {
  26. $nodes = array();
  27. $nodes['chado_library'] = array(
  28. 'name' => t('Library'),
  29. 'module' => 'chado_library',
  30. 'description' => t('A library from the chado database'),
  31. 'has_title' => FALSE,
  32. 'title_label' => t('Library'),
  33. 'has_body' => FALSE,
  34. 'body_label' => t('Library Description'),
  35. 'locked' => TRUE
  36. );
  37. return $nodes;
  38. }
  39. /*******************************************************************************
  40. * Set the permission types that the chado module uses. Essentially we
  41. * want permissionis that protect creation, editing and deleting of chado
  42. * data objects
  43. */
  44. function tripal_library_perm(){
  45. return array(
  46. 'access chado_library content',
  47. 'create chado_library content',
  48. 'delete chado_library content',
  49. 'edit chado_library content',
  50. );
  51. }
  52. /************************************************************************
  53. * Set the permission types that the module uses.
  54. */
  55. function chado_library_access($op, $node, $account) {
  56. if ($op == 'create') {
  57. return user_access('create chado_library content', $account);
  58. }
  59. if ($op == 'update') {
  60. if (user_access('edit chado_library content', $account)) {
  61. return TRUE;
  62. }
  63. }
  64. if ($op == 'delete') {
  65. if (user_access('delete chado_library content', $account)) {
  66. return TRUE;
  67. }
  68. }
  69. if ($op == 'view') {
  70. if (user_access('access chado_library content', $account)) {
  71. return TRUE;
  72. }
  73. }
  74. return FALSE;
  75. }
  76. /*******************************************************************************
  77. * Menu items are automatically added for the new node types created
  78. * by this module to the 'Create Content' Navigation menu item. This function
  79. * adds more menu items needed for this module.
  80. */
  81. function tripal_library_menu() {
  82. $items = array();
  83. // The administative settings menu
  84. $items['admin/tripal/tripal_library'] = array(
  85. 'title' => 'Libraries',
  86. 'description' => 'Basic Description of Tripal Library Module Functionality',
  87. 'page callback' => 'tripal_library_module_description_page',
  88. 'access arguments' => array('access administration pages'),
  89. 'type' => MENU_NORMAL_ITEM,
  90. );
  91. $items['admin/tripal/tripal_library/configuration'] = array(
  92. 'title' => 'Configuration',
  93. 'description' => 'Manage integration of Chado libraries including associated features.',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('tripal_library_admin'),
  96. 'access arguments' => array('access administration pages'),
  97. 'type' => MENU_NORMAL_ITEM,
  98. );
  99. // Synchronizing libraries from Chado to Drupal
  100. $items['chado_sync_libraries'] = array(
  101. 'title' => t('Sync Data'),
  102. 'page callback' => 'tripal_library_sync_libraries',
  103. 'access arguments' => array('access administration pages'),
  104. 'type' => MENU_CALLBACK
  105. );
  106. // Displaying libraries
  107. $items['libraries'] = array(
  108. 'menu_name' => ('primary-links'), //Enable the 'Library' primary link
  109. 'title' => t('DNA Libraries'),
  110. 'page callback' => 'tripal_library_show_libraries',
  111. 'access arguments' => array('access chado_library content'),
  112. 'type' => MENU_NORMAL_ITEM
  113. );
  114. return $items;
  115. }
  116. /*************************************************************************
  117. * Implements hook_views_api()
  118. * Purpose: Essentially this hook tells drupal that there is views support for
  119. * for this module which then includes tripal_db.views.inc where all the
  120. * views integration code is
  121. */
  122. function tripal_library_views_api() {
  123. return array(
  124. 'api' => 2.0,
  125. );
  126. }
  127. /*************************************************************************
  128. * Purpose: Provide Guidance to new Tripal Admin
  129. *
  130. * @return HTML Formatted text
  131. */
  132. function tripal_library_module_description_page() {
  133. $text = '';
  134. $text .= '<h3>Description:</h3>';
  135. $text .= '<p>TODO: Basic Description of this module including mention/link to the chado module</p>';
  136. $text .= '<h3>Post Installation Instructions:</h3>';
  137. $text .= '<p>TODO: Describe any post installation intructions here. You shouldalways include setting user permissions.</p>';
  138. $text .= '<h3>Features of this Module:</h3>';
  139. $text .= '<p>TODO: Discuss the Features of this module including links. Some features to consider are creating content, details pages/node content, editing/deleteing, basic listings and vies integration. See admin/tripal/tripal_stock for an example.</p>';
  140. return $text;
  141. }
  142. /*******************************************************************************
  143. * Administrative settings form
  144. */
  145. function tripal_library_admin () {
  146. $form = array();
  147. // before proceeding check to see if we have any
  148. // currently processing jobs. If so, we don't want
  149. // to give the opportunity to sync libraries
  150. $active_jobs = FALSE;
  151. if(tripal_get_module_active_jobs('tripal_library')){
  152. $active_jobs = TRUE;
  153. }
  154. // add the field set for syncing libraries
  155. if(!$active_jobs){
  156. get_tripal_library_admin_form_sync_set ($form);
  157. get_tripal_library_admin_form_reindex_set($form);
  158. get_tripal_library_admin_form_taxonomy_set($form);
  159. get_tripal_library_admin_form_cleanup_set($form);
  160. get_tripal_library_admin_form_menu_set($form);
  161. } else {
  162. $form['notice'] = array(
  163. '#type' => 'fieldset',
  164. '#title' => t('Library Management Temporarily Unavailable')
  165. );
  166. $form['notice']['message'] = array(
  167. '#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.'),
  168. );
  169. }
  170. return system_settings_form($form);
  171. }
  172. /*******************************************************************************
  173. * HOOK: Implementation of hook_nodeapi()
  174. * Display library information for associated features or organisms
  175. * This function also provides contents for indexing
  176. */
  177. function tripal_library_nodeapi(&$node, $op, $teaser, $page) {
  178. switch ($op) {
  179. // Note that this function only adds library view to an organism/feature
  180. // node.
  181. case 'view':
  182. // add the library to the organism/feature search indexing
  183. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  184. $node->content['tripal_library_index_version'] = array(
  185. '#value' => theme('tripal_library_search_index',$node),
  186. );
  187. } else if ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  188. $node->content['tripal_library_index_version'] = array(
  189. '#value' => theme('tripal_library_search_result',$node),
  190. );
  191. } else {
  192. switch($node->type){
  193. case 'chado_organism':
  194. // Show library if the organism/feature is not at teaser view
  195. $node->content['tripal_organism_library_list'] = array(
  196. '#value' => theme('tripal_organism_library_list', $node),
  197. );
  198. break;
  199. }
  200. }
  201. break;
  202. }
  203. }
  204. /************************************************************************
  205. * We need to let drupal know about our theme functions and their arguments.
  206. * We create theme functions to allow users of the module to customize the
  207. * look and feel of the output generated in this module
  208. */
  209. function tripal_library_theme () {
  210. return array(
  211. 'tripal_library_library_table' => array (
  212. 'arguments' => array('libraries'),
  213. ),
  214. 'tripal_library_search_index' => array (
  215. 'arguments' => array('node'),
  216. ),
  217. 'tripal_library_search_result' => array (
  218. 'arguments' => array('node'),
  219. ),
  220. 'tripal_organism_library_list' => array (
  221. 'arguments' => array('node'=> null),
  222. 'template' => 'tripal_organism_library_list',
  223. )
  224. );
  225. }
  226. /*******************************************************************************
  227. *
  228. */
  229. function tripal_library_preprocess_tripal_organism_library_list(&$variables){
  230. $organism = $variables['node']->organism;
  231. $variables['tripal_library']['libraries'] = tripal_library_load_organism_library_list($organism);
  232. }
  233. /************************************************************************
  234. *
  235. */
  236. function tripal_library_load_organism_library_list ($organism) {
  237. // get the libraries for the organism
  238. $sql = "SELECT L.library_id, L.organism_id, L.name, L.uniquename, ".
  239. " L.type_id, CVT.name as cvname, CVT.definition ".
  240. "FROM {library} L ".
  241. " INNER JOIN CVTerm CVT ON CVT.cvterm_id = L.type_id ".
  242. "WHERE L.Organism_id = %d";
  243. $previous_db = tripal_db_set_active('chado');
  244. $results = db_query($sql,$organism->organism_id);
  245. tripal_db_set_active($previous_db);
  246. // iterate through all of the libraries and add in the node and
  247. // library description
  248. $nsql = "SELECT nid FROM {chado_feature} ".
  249. "WHERE feature_id = %d";
  250. $dsql = "SELECT * FROM {libraryprop} LP ".
  251. " INNER JOIN CVTerm CVT ON CVT.cvterm_id = LP.type_id ".
  252. "WHERE LP.library_id = %d ".
  253. " AND CVT.name = 'library_description'";
  254. $i=0;
  255. $libraries = array();
  256. while($library = db_fetch_object($results)){
  257. // get the description
  258. $previous_db = tripal_db_set_active('chado');
  259. $desc = db_fetch_object(db_query($dsql,$library->library_id));
  260. tripal_db_set_active($previous_db);
  261. // get the node
  262. $node = db_fetch_object(db_query($nsql,$feature->feature_id));
  263. // add the description and the node to the library
  264. $library->nid = $node->nid;
  265. $library->description = $desc->value;
  266. $libraries[$i++] = $library;
  267. }
  268. return $libraries;
  269. }
  270. /************************************************************************
  271. *
  272. */
  273. function tripal_library_load_feature_library_list ($feature) {
  274. $content = "";
  275. $organism_id = $node->feature->organism_id;
  276. $previous_db = tripal_db_set_active('chado');
  277. $sql = "SELECT * FROM {library} L ".
  278. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  279. "WHERE LF.feature_id = " . $node->feature->feature_id;
  280. $libraries = array();
  281. $results = db_query($sql);
  282. while($library = db_fetch_object($results)){
  283. $libraries[] = $library;
  284. }
  285. tripal_db_set_active($previous_db);
  286. if(count($libraries) > 0){
  287. $lib_additions = array();
  288. foreach ($libraries as $library){
  289. $content .= "Library: ".$library->name;
  290. };
  291. }
  292. return $content;
  293. }
  294. /************************************************************************
  295. * This function is an extension of the chado_feature_view and
  296. * chado_organism_view by providing the markup for the library object
  297. * THAT WILL BE INDEXED.
  298. */
  299. function theme_tripal_library_search_index ($node) {
  300. if ($node->type == 'chado_organism') {
  301. $content = "";
  302. // get the libraries for the organism
  303. $previous_db = tripal_db_set_active('chado');
  304. $sql = "SELECT * FROM {library} L ".
  305. "WHERE L.Organism_id = $node->organism_id";
  306. $libraries = array();
  307. $results = db_query($sql);
  308. while($library = db_fetch_object($results)){
  309. // get the description
  310. $sql = "SELECT * FROM {libraryprop} LP ".
  311. " INNER JOIN CVTerm CVT ON CVT.cvterm_id = LP.type_id ".
  312. "WHERE LP.library_id = $library->library_id ".
  313. " AND CVT.name = 'library_description'";
  314. $desc = db_fetch_object(db_query($sql));
  315. $library->description = $desc->value;
  316. $libraries[] = $library;
  317. }
  318. tripal_db_set_active($previous_db);
  319. if(count($libraries) > 0){
  320. foreach ($libraries as $library){
  321. $content .= "$library->name ";
  322. $content .= "$library->description";
  323. };
  324. }
  325. // Provide library names to show in a feature page
  326. } else if ($node->type == 'chado_feature') {
  327. $content = "";
  328. $organism_id = $node->feature->organism_id;
  329. $previous_db = tripal_db_set_active('chado');
  330. $sql = "SELECT * FROM {library} L ".
  331. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  332. "WHERE LF.feature_id = " . $node->feature->feature_id;
  333. $libraries = array();
  334. $results = db_query($sql);
  335. while($library = db_fetch_object($results)){
  336. $libraries[] = $library;
  337. }
  338. tripal_db_set_active($previous_db);
  339. if(count($libraries) > 0){
  340. $lib_additions = array();
  341. foreach ($libraries as $library){
  342. $content .= $library->name;
  343. };
  344. }
  345. }
  346. return $content;
  347. }
  348. /*******************************************************************************
  349. * This function shows library information on an organism/feature node
  350. */
  351. function theme_tripal_library_node_libraries($node) {
  352. $content = "";
  353. // Show library information in a expandable box for a organism page.
  354. // Make sure we have $node->organism_id. In the case of creating a new
  355. // organism, the organism_id is not created until we save. This will cause
  356. // an error when users preview the creation without a $node->organism_id
  357. if ($node->type == 'chado_organism' && $node->organism_id) {
  358. $box_status = variable_get("tripal_library-box-libraries","menu_off");
  359. if(strcmp($box_status,"menu_off")==0){
  360. return get_tripal_library_organism_libraries($node->nid);
  361. }
  362. }
  363. // Provide library names to show in a feature page.
  364. // Make sure we have $node->feature->feature_id or there will be an error
  365. // when a feature is previewed at its creation
  366. else if ($node->type == 'chado_feature' && $node->feature->feature_id) {
  367. $organism_id = $node->feature->organism_id;
  368. $previous_db = tripal_db_set_active('chado');
  369. $sql = "SELECT * FROM {library} L ".
  370. " INNER JOIN Library_feature LF ON L.library_id = LF.library_id ".
  371. "WHERE LF.feature_id = " . $node->feature->feature_id;
  372. $libraries = array();
  373. $results = db_query($sql);
  374. while($library = db_fetch_object($results)){
  375. $libraries[] = $library;
  376. }
  377. tripal_db_set_active($previous_db);
  378. if(count($libraries) > 0){
  379. $lib_additions = array();
  380. foreach ($libraries as $library){
  381. $sql = "SELECT nid FROM {chado_library} WHERE library_id = %d";
  382. $lib_nid = db_result(db_query($sql, $library->library_id));
  383. if ($lib_nid) {
  384. $lib_url = url("node/$lib_nid");
  385. }
  386. $lib_additions[$lib_url] = $library->name;
  387. };
  388. $node->lib_additions = $lib_additions;
  389. }
  390. }
  391. return $content;
  392. }
  393. /************************************************************************
  394. *
  395. */
  396. function get_tripal_library_admin_form_menu_set(&$form){
  397. $form['menu'] = array(
  398. '#type' => 'fieldset',
  399. '#title' => t('Set Menu')
  400. );
  401. $types = array(
  402. 'tripal_organism' => t('Organism Page'),
  403. 'tripal_feature' => t('Feature Page'),
  404. );
  405. $defaults = variable_get('tripal_lib_menu_node_types','');
  406. $form['menu']['tripal_lib_menu_node_types'] = array(
  407. '#title' => 'Page Types',
  408. '#type' => 'checkboxes',
  409. '#description' => t("Libraries can be associated with other data types ".
  410. "such as organisms, features, analyses, etc. The library module ".
  411. "will add to the respective pages a box or menu item with a ".
  412. "list of libraries associated with the data type. This list ".
  413. "will appear in a box on the page by default. To place this ".
  414. "list as a menu item rather than a box, check the appropriate boxes ".
  415. "above"),
  416. '#options' => $types,
  417. '#default_value' => $defaults,
  418. '#weight' => 1,
  419. );
  420. }
  421. /************************************************************************
  422. *
  423. */
  424. function get_tripal_library_admin_form_cleanup_set(&$form) {
  425. $form['cleanup'] = array(
  426. '#type' => 'fieldset',
  427. '#title' => t('Clean Up')
  428. );
  429. $form['cleanup']['description'] = array(
  430. '#type' => 'item',
  431. '#value' => t("With Drupal and chado residing in different databases ".
  432. "it is possible that nodes in Drupal and libraries in Chado become ".
  433. "\"orphaned\". This can occur if an library node in Drupal is ".
  434. "deleted but the corresponding chado library is not and/or vice ".
  435. "versa. Click the button below to resolve these discrepancies."),
  436. '#weight' => 1,
  437. );
  438. $form['cleanup']['button'] = array(
  439. '#type' => 'submit',
  440. '#value' => t('Clean up orphaned libraries'),
  441. '#weight' => 2,
  442. );
  443. }
  444. /************************************************************************
  445. *
  446. */
  447. function get_tripal_library_admin_form_taxonomy_set(&$form) {
  448. $form['taxonify'] = array(
  449. '#type' => 'fieldset',
  450. '#title' => t('Assign Drupal Taxonomy to Library Features')
  451. );
  452. // get the list of libraries
  453. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  454. $previous_db = tripal_db_set_active('chado'); // use chado database
  455. $lib_rset = db_query($sql);
  456. tripal_db_set_active($previous_db); // now use drupal database
  457. // iterate through all of the libraries
  458. $lib_boxes = array();
  459. while($library = db_fetch_object($lib_rset)){
  460. $lib_boxes[$library->library_id] = "$library->name";
  461. }
  462. $form['taxonify']['description'] = array(
  463. '#type' => 'item',
  464. '#value' => t("Drupal allows for assignment of \"taxonomy\" or catagorical terms to " .
  465. "nodes. These terms allow for advanced filtering during searching. This option allows ".
  466. "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."),
  467. '#weight' => 1,
  468. );
  469. $form['taxonify']['tx-libraries'] = array (
  470. '#title' => t('Libraries'),
  471. '#type' => t('checkboxes'),
  472. '#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."),
  473. '#required' => FALSE,
  474. '#prefix' => '<div id="lib_boxes">',
  475. '#suffix' => '</div>',
  476. '#options' => $lib_boxes,
  477. '#weight' => 2
  478. );
  479. $form['taxonify']['tx-button'] = array(
  480. '#type' => 'submit',
  481. '#value' => t('Set Feature Taxonomy'),
  482. '#weight' => 3
  483. );
  484. }
  485. /************************************************************************
  486. *
  487. */
  488. function get_tripal_library_admin_form_reindex_set(&$form) {
  489. // define the fieldsets
  490. $form['reindex'] = array(
  491. '#type' => 'fieldset',
  492. '#title' => t('Reindex Library Features')
  493. );
  494. // get the list of libraries
  495. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  496. $previous_db = tripal_db_set_active('chado'); // use chado database
  497. $lib_rset = db_query($sql);
  498. tripal_db_set_active($previous_db); // now use drupal database
  499. // iterate through all of the libraries
  500. $lib_boxes = array();
  501. while($library = db_fetch_object($lib_rset)){
  502. $lib_boxes[$library->library_id] = "$library->name";
  503. }
  504. $form['reindex']['description'] = array(
  505. '#type' => 'item',
  506. '#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."),
  507. '#weight' => 1,
  508. );
  509. $form['reindex']['re-libraries'] = array (
  510. '#title' => t('Libraries'),
  511. '#type' => t('checkboxes'),
  512. '#description' => t("Check the libraries whoee features you want to reindex. Note: this list contains all libraries, even those that may not be synced."),
  513. '#required' => FALSE,
  514. '#prefix' => '<div id="lib_boxes">',
  515. '#suffix' => '</div>',
  516. '#options' => $lib_boxes,
  517. '#weight' => 2,
  518. );
  519. $form['reindex']['re-button'] = array(
  520. '#type' => 'submit',
  521. '#value' => t('Reindex Features'),
  522. '#weight' => 3,
  523. );
  524. }
  525. /************************************************************************
  526. *
  527. */
  528. function get_tripal_library_admin_form_sync_set (&$form) {
  529. // define the fieldsets
  530. $form['sync'] = array(
  531. '#type' => 'fieldset',
  532. '#title' => t('Sync Libraries')
  533. );
  534. // get the list of libraries
  535. $sql = "SELECT * FROM {Library} ORDER BY uniquename";
  536. $previous_db = tripal_db_set_active('chado'); // use chado database
  537. $lib_rset = db_query($sql);
  538. tripal_db_set_active($previous_db); // now use drupal database
  539. // if we've added any libraries to the list that can be synced
  540. // then we want to build the form components to allow the user
  541. // to select one or all of them. Otherwise, just present
  542. // a message stating that all libraries are currently synced.
  543. $lib_boxes = array();
  544. $added = 0;
  545. while($library = db_fetch_object($lib_rset)){
  546. // check to see if the library is already present as a node in drupal.
  547. // if so, then skip it.
  548. $sql = "SELECT * FROM {chado_library} WHERE library_id = %d";
  549. if(!db_fetch_object(db_query($sql,$library->library_id))){
  550. $lib_boxes[$library->library_id] = "$library->name";
  551. $added++;
  552. }
  553. }
  554. // if we have libraries we need to add to the checkbox then
  555. // build that form element
  556. if($added > 0){
  557. $lib_boxes['all'] = "All Libraries";
  558. $form['reindex']['description'] = array(
  559. '#type' => 'item',
  560. '#value' => t("This option allows for the creation of Drupal content for libraries in chado. Only the selected libraries will be synced."),
  561. '#weight' => 1,
  562. );
  563. $form['sync']['libraries'] = array (
  564. '#title' => t('Available Libraries'),
  565. '#type' => t('checkboxes'),
  566. '#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."),
  567. '#required' => FALSE,
  568. '#prefix' => '<div id="lib_boxes">',
  569. '#suffix' => '</div>',
  570. '#options' => $lib_boxes,
  571. '#weight' => 2,
  572. );
  573. $form['sync']['button'] = array(
  574. '#type' => 'submit',
  575. '#value' => t('Sync Libraries'),
  576. '#weight' => 3,
  577. );
  578. }
  579. // we don't have any libraries to select from
  580. else {
  581. $form['sync']['value'] = array(
  582. '#value' => t('All libraries in Chado are currently synced with Drupal.')
  583. );
  584. }
  585. }
  586. /************************************************************************
  587. *
  588. */
  589. function tripal_library_admin_validate($form, &$form_state) {
  590. global $user; // we need access to the user info
  591. $job_args = array();
  592. // Submit the Sync Job if selected
  593. if ($form_state['values']['op'] == t('Sync Libraries')) {
  594. // check to see if the user wants to sync chado and drupal. If
  595. // so then we need to register a job to do so with tripal
  596. $libraries = $form_state['values']['libraries'];
  597. $do_all = FALSE;
  598. $to_sync = array();
  599. foreach ($libraries as $library_id){
  600. if(preg_match("/^all$/i",$library_id)){
  601. $do_all = TRUE;
  602. }
  603. if($library_id and preg_match("/^\d+$/i",$library_id)){
  604. // get the library info
  605. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  606. $previous_db = tripal_db_set_active('chado'); // use chado database
  607. $library = db_fetch_object(db_query($sql,$library_id));
  608. tripal_db_set_active($previous_db); // now use drupal database
  609. $to_sync[$library_id] = $library->name;
  610. }
  611. }
  612. // submit the job to the tripal job manager
  613. if($do_all){
  614. tripal_add_job('Sync all libraries','tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
  615. }
  616. else{
  617. foreach($to_sync as $library_id => $name){
  618. $job_args[0] = $library_id;
  619. tripal_add_job("Sync library: $name",'tripal_library','tripal_library_sync_libraries',$job_args,$user->uid);
  620. }
  621. }
  622. }
  623. // -------------------------------------
  624. // Submit the Reindex Job if selected
  625. if ($form_state['values']['op'] == t('Reindex Features')) {
  626. $libraries = $form_state['values']['re-libraries'];
  627. foreach ($libraries as $library_id){
  628. if($library_id and preg_match("/^\d+$/i",$library_id)){
  629. // get the library info
  630. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  631. $previous_db = tripal_db_set_active('chado'); // use chado database
  632. $library = db_fetch_object(db_query($sql,$library_id));
  633. tripal_db_set_active($previous_db); // now use drupal database
  634. $job_args[0] = $library_id;
  635. tripal_add_job("Reindex features for library: $library->name",'tripal_library',
  636. 'tripal_library_reindex_features',$job_args,$user->uid);
  637. }
  638. }
  639. }
  640. // -------------------------------------
  641. // Submit the Taxonomy Job if selected
  642. if ($form_state['values']['op'] == t('Set Feature Taxonomy')) {
  643. $libraries = $form_state['values']['tx-libraries'];
  644. foreach ($libraries as $library_id){
  645. if($library_id and preg_match("/^\d+$/i",$library_id)){
  646. // get the library info
  647. $sql = "SELECT * FROM {Library} WHERE library_id = %d";
  648. $previous_db = tripal_db_set_active('chado'); // use chado database
  649. $library = db_fetch_object(db_query($sql,$library_id));
  650. tripal_db_set_active($previous_db); // now use drupal database
  651. $job_args[0] = $library_id;
  652. tripal_add_job("Set taxonomy for features in library: $library->name",'tripal_library',
  653. 'tripal_library_taxonify_features',$job_args,$user->uid);
  654. }
  655. }
  656. }
  657. // -------------------------------------
  658. // Submit the Cleanup Job if selected
  659. if ($form_state['values']['op'] == t('Clean up orphaned libraries')) {
  660. tripal_add_job('Cleanup orphaned libraries','tripal_library',
  661. 'tripal_library_cleanup',$job_args,$user->uid);
  662. }
  663. }
  664. /*******************************************************************************
  665. *
  666. */
  667. function tripal_library_show_libraries (){
  668. // Show libraries stored in Drupal's {chado_library} table
  669. $sql = "SELECT COUNT(library_id) FROM {chado_library}";
  670. $no_libs = db_result(db_query ($sql));
  671. if($no_libs != 0) {
  672. $libraries = get_chado_libraries ();
  673. if($no_libs != count($libraries)) {
  674. drupal_set_message("Synchronization needed.");
  675. }
  676. return theme('tripal_library_library_table', &$libraries);
  677. } else {
  678. return t("No library exists. Please contact administrators to ".
  679. "synchronize libraries.");
  680. }
  681. }
  682. /*******************************************************************************
  683. *
  684. */
  685. function tripal_library_cron (){
  686. }
  687. /*******************************************************************************
  688. *
  689. * CHADO_LIBRARY NODE FUNCTIONS
  690. *
  691. * The following function proves access control for users trying to
  692. * perform actions on data managed by this module
  693. */
  694. function tripal_library_library_access($op, $node, $account){
  695. if ($op == 'create') {
  696. return user_access('create chado_library content', $account);
  697. }
  698. if ($op == 'update') {
  699. if (user_access('edit any chado_library content', $account) ||
  700. (user_access('edit own chado_library content', $account) &&
  701. ($account->uid == $node->uid))){
  702. return TRUE;
  703. }
  704. }
  705. if ($op == 'delete') {
  706. if (user_access('delete any chado_library content', $account) ||
  707. (user_access('delete own chado_library content', $account) &&
  708. ($account->uid == $node->uid))) {
  709. return TRUE;
  710. }
  711. }
  712. }
  713. /*******************************************************************************
  714. * validates submission of form when adding or updating a library node
  715. */
  716. function chado_library_validate($node){
  717. $lib = 0;
  718. // check to make sure the unique name on the library is unique
  719. // before we try to insert into chado.
  720. if($node->library_id){
  721. $sql = "SELECT * FROM ".
  722. "{Library} WHERE ".
  723. "uniquename = '%s' ".
  724. "AND NOT library_id = %d";
  725. $previous_db = tripal_db_set_active('chado');
  726. $lib = db_fetch_object(db_query($sql, $node->uniquename,
  727. $node->library_id));
  728. tripal_db_set_active($previous_db);
  729. } else {
  730. $sql = "SELECT * FROM ".
  731. "{Library} ".
  732. "WHERE uniquename = '%s'";
  733. $previous_db = tripal_db_set_active('chado');
  734. $lib = db_fetch_object(db_query($sql, $node->uniquename));
  735. tripal_db_set_active($previous_db);
  736. }
  737. if($lib){
  738. form_set_error('uniquename',t('The unique library name already exists. '.
  739. 'Please choose another'));
  740. }
  741. }
  742. /*******************************************************************************
  743. * When a new chado_library node is created we also need to add information
  744. * to our chado_library table. This function is called on insert of a new node
  745. * of type 'chado_library' and inserts the necessary information.
  746. */
  747. function chado_library_insert($node){
  748. // If this library already exists then don't create it in chado
  749. // e.g. when tripal_library_sync_libraries call this function
  750. $l_sql= "SELECT library_id ".
  751. "FROM {Library} ".
  752. "WHERE organism_id = %d ".
  753. "AND uniquename = '%s' ".
  754. "AND type_id = %d";
  755. $previous_db = tripal_db_set_active('chado');
  756. $library = db_fetch_object(db_query($l_sql, $node->organism_id,
  757. $node->uniquename, $node->type_id));
  758. $libid = $node->library_id;
  759. // Create the chado library entry only if it doesn't exist
  760. if (!$library) {
  761. $sql = "INSERT INTO {library} (organism_id, name, uniquename, type_id)".
  762. "VALUES(%d,'%s','%s', ".
  763. " (SELECT cvterm_id ".
  764. " FROM {CVTerm} CVT ".
  765. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  766. " WHERE CV.name = 'tripal' and CVT.name = '%s'))";
  767. db_query($sql,$node->organism_id,$node->title,$node->uniquename,
  768. $node->library_type);
  769. // get the library id for this library
  770. $sql = "SELECT library_id ".
  771. "FROM {library} ".
  772. "WHERE uniquename = '%s'";
  773. $lib = db_fetch_object(db_query($sql, $node->uniquename));
  774. $libid = $lib->library_id;
  775. }
  776. // Now use drupal database
  777. tripal_db_set_active($previous_db);
  778. // Next add the node to the drupal table
  779. $sql = "INSERT INTO {chado_library} (nid, vid, library_id) ".
  780. "VALUES (%d, %d, %d)";
  781. db_query($sql, $node->nid, $node->vid, $libid);
  782. // Finally add the remaining properties if the library doesn't exist in Chado
  783. if (!$library) {
  784. tripal_library_add_properties($node,$libid);
  785. }
  786. }
  787. /*******************************************************************************
  788. * Update nodes
  789. */
  790. function chado_library_update($node){
  791. if($node->revision){
  792. // TODO -- decide what to do about revisions
  793. } else {
  794. // Get the library_id for this node:
  795. $sql = "SELECT library_id ".
  796. "FROM {chado_library} ".
  797. "WHERE vid = %d";
  798. $lib = db_fetch_object(db_query($sql, $node->vid));
  799. // Update the chado library table
  800. $sql = "UPDATE {library} ".
  801. " SET name = '%s', uniquename = '%s', organism_id = %d, ".
  802. " type_id = (SELECT cvterm_id ".
  803. " FROM {CVTerm} CVT ".
  804. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  805. " WHERE CV.name = 'local' and CVT.name = '%s') ".
  806. "WHERE library_id = %d ";
  807. $previous_db = tripal_db_set_active('chado'); // use chado database
  808. db_query($sql, $node->title, $node->uniquename, $node->organism_id,
  809. $node->library_type, $lib->library_id);
  810. tripal_db_set_active($previous_db); // now use drupal database
  811. tripal_library_add_properties($node,$lib->library_id);
  812. tripal_library_add_taxonomy($node,$lib->library_id);
  813. }
  814. }
  815. /*******************************************************************************
  816. * Add properties to the specified library.
  817. */
  818. function tripal_library_add_properties ($node,$library_id){
  819. // Use chado database
  820. $previous_db = tripal_db_set_active('chado');
  821. // Now remove any properties for this library and add the new properties
  822. // provided by the this posting.
  823. $sql = "DELETE FROM {libraryprop} ".
  824. "WHERE library_id = %d";
  825. db_query($sql,$library_id);
  826. // Now add each property one at a time
  827. $sql = "INSERT INTO {libraryprop} (library_id, type_id, value) ".
  828. "VALUES (%d,".
  829. " (SELECT cvterm_id ".
  830. " FROM {CVTerm} CVT ".
  831. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  832. " WHERE CV.name = 'tripal' and CVT.name = '%s'), ".
  833. " '%s')";
  834. if($node->library_description){
  835. db_query($sql,$library_id,'library_description',
  836. $node->library_description);
  837. }
  838. // now use drupal database
  839. tripal_db_set_active($previous_db);
  840. }
  841. /*******************************************************************************
  842. * Add the library as a taxonomy term for associating with library_features
  843. */
  844. function tripal_library_add_taxonomy ($node,$library_id){
  845. //include the file containing the required functions. We only have to
  846. // do this because Drupal 6 fails to do this globally for us and
  847. // the drupal_execute function below won't work
  848. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  849. /* // get the vocabulary id
  850. $vocabularies = taxonomy_get_vocabularies();
  851. $vid = NULL;
  852. foreach($vocabularies as $vocab){
  853. if($vocab->name == 'DNA Libraries'){
  854. $vid = $vocab->vid;
  855. }
  856. }
  857. if(!$vid){ */
  858. // add the vocabulary
  859. $vocab_form['values']['name'] = 'DNA Libraries';
  860. $vocab_form['values']['description'] = 'Allows for associating/searching of library features by library name';
  861. $vocab_form['values']['help'] = '';
  862. $vocab_form['values']['module'] = 'taxonomy';
  863. drupal_execute('taxonomy_form_vocabulary',$vocab_form);
  864. return;
  865. // }
  866. // make sure this term doesn't already exist. If it doesn't then add it
  867. if($vid){
  868. $tree = taxonomy_get_tree($vid);
  869. $found = 0;
  870. foreach ($tree as $term) {
  871. if($term->name == $node->title){
  872. $found = 1;
  873. }
  874. }
  875. // add the term to the vocabulary
  876. if(!$found){
  877. $form_state = array();
  878. $form_state['values']['name'] = $node->title;
  879. $form_state['values']['description'] = $library_id;
  880. drupal_execute('taxonomy_form_term',$form_state,$vid);
  881. }
  882. }
  883. }
  884. /*******************************************************************************
  885. * When editing or creating a new node of type 'chado_library' we need
  886. * a form. This function creates the form that will be used for this.
  887. */
  888. function chado_library_form ($node){
  889. $type = node_get_types('type',$node);
  890. $form = array();
  891. // keep track of the library id if we have. If we do have one then
  892. // this is an update as opposed to an insert.
  893. $form['library_id'] = array(
  894. '#type' => 'value',
  895. '#value' => $node->library_id,
  896. );
  897. $form['title']= array(
  898. '#type' => 'textfield',
  899. '#title' => t('Library Title'),
  900. '#description' => t('Please enter the title for this library. '.
  901. 'This appears at the top of the library page.'),
  902. '#required' => TRUE,
  903. '#default_value' => $node->title,
  904. '#weight' => 1
  905. );
  906. $form['uniquename']= array(
  907. '#type' => 'textfield',
  908. '#title' => t('Unique Library Name'),
  909. '#description' => t('Please enter a unique name for this library'),
  910. '#required' => TRUE,
  911. '#default_value' => $node->uniquename,
  912. '#weight' => 2
  913. );
  914. // These library types should not be hard coded, but for now the are...
  915. $library_types = array();
  916. $library_types[''] = '';
  917. $library_types['cdna_library'] = 'cDNA Library';
  918. $library_types['bac_library'] = 'BAC Library';
  919. $library_types['fosmid_library'] = 'FOSMID Library';
  920. $library_types['cosmid_library'] = 'COSMID Library';
  921. $library_types['yac_library'] = 'YAC Library';
  922. $form['library_type'] = array (
  923. '#title' => t('Library Type'),
  924. '#type' => t('select'),
  925. '#description' => t("Choose the library type."),
  926. '#required' => TRUE,
  927. '#default_value' => $node->library_type,
  928. '#options' => $library_types,
  929. '#weight' => 3
  930. );
  931. // get the list of organisms
  932. $sql = "SELECT * FROM {Organism}";
  933. $previous_db = tripal_db_set_active('chado'); // use chado database
  934. $org_rset = db_query($sql);
  935. tripal_db_set_active($previous_db); // now use drupal database
  936. $organisms = array();
  937. $organisms[''] = '';
  938. while($organism = db_fetch_object($org_rset)){
  939. $organisms[$organism->organism_id] =
  940. "$organism->genus $organism->species ($organism->common_name)";
  941. }
  942. $form['organism_id'] = array (
  943. '#title' => t('Organism'),
  944. '#type' => t('select'),
  945. '#description' => t("Choose the organism with which this library is ".
  946. "associated."),
  947. '#required' => TRUE,
  948. '#default_value' => $node->organism_id,
  949. '#options' => $organisms,
  950. '#weight' => 4,
  951. );
  952. $form['library_description']= array(
  953. '#type' => 'textarea',
  954. '#title' => t('Library Description'),
  955. '#description' => t('A brief description of the library'),
  956. '#required' => TRUE,
  957. '#default_value' => $node->library_description,
  958. '#weight' => 5
  959. );
  960. //Save the number of EST so it will show up in preview
  961. $form['sequence_num'] = array(
  962. '#type' => 'value',
  963. '#value' => $node->sequence_num,
  964. );
  965. return $form;
  966. }
  967. /************************************************************************
  968. *
  969. */
  970. function tripal_library_sync_libraries ($library_id = NULL, $job_id = NULL){
  971. global $user;
  972. $page_content = '';
  973. // get the list of libraries and create new nodes
  974. if(!$library_id){
  975. $sql = "SELECT * FROM {Library} L";
  976. $previous_db = tripal_db_set_active('chado'); // use chado database
  977. $results = db_query($sql);
  978. tripal_db_set_active($previous_db); // now use drupal database
  979. } else {
  980. $sql = "SELECT * FROM {Library} L WHERE library_id = %d";
  981. $previous_db = tripal_db_set_active('chado'); // use chado database
  982. $results = db_query($sql,$library_id);
  983. tripal_db_set_active($previous_db); // now use drupal database
  984. }
  985. // We'll use the following SQL statement for checking if the library
  986. // already exists as a drupal node.
  987. $sql = "SELECT * FROM {chado_library} ".
  988. "WHERE library_id = %d";
  989. while($library = db_fetch_object($results)){
  990. // check if this library already exists in the drupal database. if it
  991. // does then skip this library and go to the next one.
  992. if(!db_fetch_object(db_query($sql,$library->library_id))){
  993. $new_node = new stdClass();
  994. $new_node->type = 'chado_library';
  995. $new_node->uid = $user->uid;
  996. $new_node->title = "$library->name";
  997. $new_node->library_id = $library->library_id;
  998. $new_node->organism_id = $library->organism_id;
  999. $new_node->uniquename = $library->uniquename;
  1000. $new_node->type_id = $library->type_id;
  1001. node_validate($new_node);
  1002. if(!form_get_errors()){
  1003. $node = node_submit($new_node);
  1004. node_save($node);
  1005. if($node->nid){
  1006. $page_content .= "Added $library->name<br>";
  1007. }
  1008. }
  1009. } else {
  1010. $page_content .= "Skipped $library->name<br>";
  1011. }
  1012. }
  1013. return $page_content;
  1014. }
  1015. /************************************************************************
  1016. * When a node is requested by the user this function is called to allow us
  1017. * to add auxiliary data to the node object.
  1018. */
  1019. function chado_library_load($node){
  1020. $sql = 'SELECT library_id FROM {chado_library} WHERE vid = %d';
  1021. $map = db_fetch_object(db_query($sql, $node->vid));
  1022. $additions->library_id = $map->library_id;
  1023. $previous_db = tripal_db_set_active('chado'); // use chado database
  1024. // get information about this library
  1025. $sql = "SELECT L.organism_id, L.uniquename, CVT.name as type_name ".
  1026. "FROM {Library} L ".
  1027. "INNER JOIN CVTerm CVT ON CVT.cvterm_id = L.type_id ".
  1028. "WHERE library_id = %d";
  1029. $library = db_fetch_object(db_query($sql, $map->library_id));
  1030. $additions->uniquename = $library->uniquename;
  1031. $additions->library_type = $library->type_name;
  1032. $additions->organism_id = $library->organism_id;
  1033. $sql = "SELECT genus, species, common_name ".
  1034. "FROM {organism} O ".
  1035. "INNER JOIN library L ".
  1036. "ON L.organism_id = O.organism_id ".
  1037. "WHERE L.library_id = %d";
  1038. $organism = db_fetch_object(db_query($sql,$map->library_id));
  1039. $additions->genus = $organism->genus;
  1040. $additions->species = $organism->species;
  1041. $additions->common_name = $organism->common_name;
  1042. $sql = "SELECT LP.value ".
  1043. "FROM {libraryProp} LP ".
  1044. "WHERE LP.library_id = %d ";
  1045. $desc = db_fetch_object(db_query($sql, $map->library_id))->value;
  1046. if ($desc) {
  1047. $additions->library_description = $desc;
  1048. } else {
  1049. $additions->library_description = "NA";
  1050. }
  1051. // get the feature counts. This is dependent on a materialized view
  1052. // installed with the library module
  1053. if ($map->library_id) {
  1054. $sql = "SELECT num_features FROM {library_feature_count} ".
  1055. "WHERE Library_id = $map->library_id";
  1056. $additions->num_features = db_fetch_object(db_query($sql, $map->library_id))->num_features;
  1057. }
  1058. tripal_db_set_active($previous_db); // now use drupal database
  1059. return $additions;
  1060. }
  1061. /*******************************************************************************
  1062. * This function customizes the view of the chado_library node. It allows
  1063. * us to generate the markup. This function is required for node [Preview]
  1064. */
  1065. function chado_library_view ($node, $teaser = FALSE, $page = FALSE) {
  1066. // use drupal's default node view:
  1067. if (!$teaser) {
  1068. $node = node_prepare($node, $teaser);
  1069. // If Hook_view() is called by Hook_form(), we'll only have orgnism_id
  1070. // but not genus/species/common_name. We need to get those from chado
  1071. // database so they will show up in preview
  1072. if(!$node->genus) {
  1073. $previous_db = tripal_db_set_active('chado');
  1074. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  1075. $data = db_fetch_object(db_query($sql, $node->organism_id));
  1076. $node->genus = $data->genus;
  1077. $node->species = $data->species;
  1078. $node->common_name = $data->common_name;
  1079. tripal_db_set_active($previous_db);
  1080. }
  1081. }
  1082. return $node;
  1083. }
  1084. /************************************************************************
  1085. ** This function creates the html markup for the library table.
  1086. ** It can be overridden in the theme for the site by adding a php
  1087. ** method in the template.php file named
  1088. ** <theme_name>_tripal_library_library_table(&$libraries)
  1089. */
  1090. function theme_tripal_library_library_table ($libraries) {
  1091. // cycle through the libraries and build the libraries page
  1092. $output = "<div id=\"libraries\">";
  1093. $output .= '<table>';
  1094. $output .= "<tr>";
  1095. $output .= "<th>Name</th>";
  1096. $output .= "<th>Type</th>";
  1097. $output .= "<th>Organism</th>";
  1098. $output .= "<th>Description</th>";
  1099. $output .= "</tr>";
  1100. foreach($libraries as $library){
  1101. $lib_url = url("node/$library->node_id");
  1102. $output .= "<tr>";
  1103. $output .= "<td>".
  1104. " <a href=\"$lib_url\">$library->name</a>".
  1105. "</td>";
  1106. if(strcasecmp($library->type_name, 'bac_library') == 0){
  1107. $output .= "<td>BAC</td>";
  1108. }
  1109. elseif(strcasecmp($library->type_name, 'cdna_library') == 0){
  1110. $output .= "<td>cDNA</td>";
  1111. }
  1112. else{
  1113. $output .= "<td>$library->type_name</td>";
  1114. }
  1115. $output .= "<td nowrap>".
  1116. " $library->common_name".
  1117. "</td>";
  1118. $description = $library->library_description;
  1119. $output .= "<td>$description</td>";
  1120. $output .= "</tr>";
  1121. }
  1122. $output .= "</table>";
  1123. $output .= "</div>";
  1124. return $output;
  1125. }
  1126. /************************************************************************
  1127. *
  1128. */
  1129. function tripal_library_feature_set_taxonomy($library_id = NULL){
  1130. //TO DO : return usable error if vocabs don't exist
  1131. // get the list of vocabularies and find our two vocabularies of interest
  1132. $vocabularies = taxonomy_get_vocabularies();
  1133. $vid = NULL;
  1134. foreach($vocabularies as $vocab){
  1135. if($vocab->name == 'Library'){
  1136. $vid = $vocab->vid;
  1137. }
  1138. }
  1139. if(!$vid){
  1140. return;
  1141. }
  1142. // We'll use the following SQL statement for getting the node info
  1143. if($library_id){
  1144. print "Finding features for library with ID: $library_id\n";
  1145. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1146. "FROM {library_feature} LF ".
  1147. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1148. "WHERE L.library_id = $library_id ".
  1149. "ORDER BY LF.feature_id";
  1150. $previous_db = tripal_db_set_active('chado'); // use chado database
  1151. $features = db_query($sql);
  1152. tripal_db_set_active($previous_db); // now use drupal database
  1153. } else {
  1154. print "Finding features for all libraries\n";
  1155. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1156. "FROM {library_feature} LF ".
  1157. "INNER JOIN Library L ON LF.library_id = L.library_id ".
  1158. "ORDER BY LF.feature_id";
  1159. $previous_db = tripal_db_set_active('chado'); // use chado database
  1160. $features = db_query($sql);
  1161. tripal_db_set_active($previous_db); // now use drupal database
  1162. }
  1163. $node_sql = "SELECT * FROM {chado_feature} CF ".
  1164. " INNER JOIN {node} N ON CF.nid = N.nid ".
  1165. "WHERE feature_id = %d";
  1166. // iterate through the features and add the taxonomy
  1167. while($feature = db_fetch_object($features)){
  1168. $node = db_fetch_object(db_query($node_sql,$feature->feature_id));
  1169. $tags["$vid"] = $feature->libname;
  1170. $terms['tags'] = $tags;
  1171. taxonomy_node_save($node,$terms);
  1172. print "Updated $feature->feature_id as $feature->libname\n";
  1173. }
  1174. }
  1175. /*******************************************************************************
  1176. *
  1177. */
  1178. function tripal_library_reindex_features ($library_id = NULL, $job_id = NULL){
  1179. $i = 0;
  1180. // if the caller provided a library_id then get all of the features
  1181. // associated with the library. Otherwise get all sequences assoicated
  1182. // with all libraries.
  1183. if($library_id){
  1184. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1185. " FROM {library_feature} LF ".
  1186. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1187. "WHERE L.library_id = $library_id ".
  1188. "ORDER BY LF.feature_id";
  1189. $previous_db = tripal_db_set_active('chado'); // use chado database
  1190. $results = db_query($sql);
  1191. tripal_db_set_active($previous_db); // now use drupal database
  1192. }
  1193. else {
  1194. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1195. " FROM {library_feature} LF ".
  1196. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1197. "ORDER BY LF.feature_id";
  1198. $previous_db = tripal_db_set_active('chado'); // use chado database
  1199. $results = db_query($sql);
  1200. tripal_db_set_active($previous_db); // now use drupal database
  1201. }
  1202. // load into ids array
  1203. $count = 0;
  1204. $ids = array();
  1205. while($id = db_fetch_object($results)){
  1206. $ids[$count] = $id->feature_id;
  1207. $count++;
  1208. }
  1209. $interval = intval($count * 0.01);
  1210. foreach($ids as $feature_id){
  1211. // update the job status every 1% features
  1212. if($job_id and $i % interval == 0){
  1213. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1214. }
  1215. tripal_feature_sync_feature ($feature_id);
  1216. $i++;
  1217. }
  1218. }
  1219. /*******************************************************************************
  1220. *
  1221. */
  1222. function tripal_library_taxonify_features ($library_id = NULL, $job_id = NULL){
  1223. $i = 0;
  1224. // if the caller provided a library_id then get all of the features
  1225. // associated with the library. Otherwise get all sequences assoicated
  1226. // with all libraries.
  1227. if($library_id){
  1228. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1229. " FROM {library_feature} LF ".
  1230. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1231. "WHERE L.library_id = $library_id ".
  1232. "ORDER BY LF.feature_id";
  1233. $previous_db = tripal_db_set_active('chado'); // use chado database
  1234. $results = db_query($sql);
  1235. tripal_db_set_active($previous_db); // now use drupal database
  1236. }
  1237. else {
  1238. $sql = "SELECT LF.feature_id, L.library_id, L.name as libname ".
  1239. " FROM {library_feature} LF ".
  1240. " INNER JOIN Library L ON LF.library_id = L.library_id ".
  1241. "ORDER BY LF.feature_id";
  1242. $previous_db = tripal_db_set_active('chado'); // use chado database
  1243. $results = db_query($sql);
  1244. tripal_db_set_active($previous_db); // now use drupal database
  1245. }
  1246. // load into ids array
  1247. $count = 0;
  1248. $ids = array();
  1249. while($id = db_fetch_object($results)){
  1250. $ids[$count] = $id->feature_id;
  1251. $count++;
  1252. }
  1253. // make sure our vocabularies are set before proceeding
  1254. tripal_feature_set_vocabulary();
  1255. // use this SQL for getting the nodes
  1256. $nsql = "SELECT * FROM {chado_feature} CF ".
  1257. " INNER JOIN {node} N ON N.nid = CF.nid ".
  1258. "WHERE feature_id = %d";
  1259. // iterate through the features and set the taxonomy
  1260. $interval = intval($count * 0.01);
  1261. foreach($ids as $feature_id){
  1262. // update the job status every 1% features
  1263. if($job_id and $i % interval == 0){
  1264. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1265. }
  1266. $node = db_fetch_object(db_query($nsql,$feature_id));
  1267. tripal_feature_set_taxonomy($node,$feature_id);
  1268. $i++;
  1269. }
  1270. }
  1271. /*******************************************************************************
  1272. * Delete data from drupal and chado databases when a node is deleted
  1273. */
  1274. function chado_library_delete(&$node){
  1275. // Before removing, get library_id so we can remove it from chado database
  1276. // later
  1277. $sql_drupal = "SELECT library_id ".
  1278. "FROM {chado_library} ".
  1279. "WHERE nid = %d AND vid = %d";
  1280. $library_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  1281. // Remove data from {chado_library}, {node} and {node_revisions} tables of
  1282. // drupal database
  1283. $sql_del = "DELETE FROM {chado_library} ".
  1284. "WHERE nid = %d ".
  1285. "AND vid = %d";
  1286. db_query($sql_del, $node->nid, $node->vid);
  1287. $sql_del = "DELETE FROM {node} ".
  1288. "WHERE nid = %d ".
  1289. "AND vid = %d";
  1290. db_query($sql_del, $node->nid, $node->vid);
  1291. $sql_del = "DELETE FROM {node_revisions} ".
  1292. "WHERE nid = %d ".
  1293. "AND vid = %d";
  1294. db_query($sql_del, $node->nid, $node->vid);
  1295. // Remove data from library and libraryprop tables of chado database as well
  1296. $previous_db = tripal_db_set_active('chado');
  1297. db_query("DELETE FROM {library} WHERE library_id = %d", $library_id);
  1298. db_query("DELETE FROM {libraryprop} WHERE library_id = %d", $library_id);
  1299. tripal_db_set_active($previous_db);
  1300. }
  1301. /************************************************************************
  1302. * Display block with libraries
  1303. * @param op - parameter to define the phase being called for the block
  1304. * @param delta - id of the block to return (ignored when op is list)
  1305. * @param edit - when op is save, contains the submitted form data
  1306. */
  1307. function tripal_library_block($op = 'list', $delta = '0', $edit = array()){
  1308. switch($op){
  1309. case 'list':
  1310. $blocks[0]['info'] = t('Libraries');
  1311. return $blocks;
  1312. case 'view':
  1313. if(user_access('access chado_library content')){
  1314. // Show libraries stored in Drupal's {chado_library} table
  1315. $sql = "SELECT COUNT(library_id) FROM {chado_library}";
  1316. $no_libs = db_result(db_query ($sql));
  1317. if($no_libs != 0) {
  1318. $libraries = get_chado_libraries();
  1319. foreach($libraries as $library){
  1320. // get the node id for this organism
  1321. $items[] = l($library->name, 'node/' . $library->node_id);
  1322. }
  1323. } else {
  1324. $items[] = t("No library exists.");
  1325. }
  1326. $block['subject'] = t('Libraries');
  1327. //We theme our array of links as an unordered list
  1328. $block['content'] = theme('item_list', $items);
  1329. }
  1330. return $block;
  1331. }
  1332. }
  1333. /*******************************************************************************
  1334. * This function uses library_id's of all drupal library nodes as input and
  1335. * pull the library information (name, uniquename, type, genus, species,
  1336. * common_name, description) from chado database. The return type is an object
  1337. * array that stores sorted $library objects
  1338. */
  1339. function get_chado_libraries() {
  1340. $sql_drupal = "SELECT COUNT (library_id) FROM {chado_library}";
  1341. $no_libs = db_result(db_query($sql_drupal));
  1342. if ($no_libs != 0) {
  1343. // Get library_id's from drupal
  1344. $sql = "SELECT library_id, nid FROM {chado_library}";
  1345. $result = db_query($sql);
  1346. $previous_db = tripal_db_set_active('chado');
  1347. // Get library info from chado's library, organism, and cvterm tables
  1348. $sql_info = "SELECT L.name, uniquename, genus, species, common_name, ".
  1349. " CVT.name as type_name ".
  1350. "FROM {Library} L ".
  1351. "INNER JOIN Organism O ON L.organism_id = O.organism_id ".
  1352. "INNER JOIN CVTerm CVT ON L.type_id = CVT.cvterm_id ".
  1353. "WHERE library_id=%d";
  1354. // Get library description from libraryprop if there is any
  1355. $sql_desc = "SELECT value ".
  1356. "FROM {Libraryprop} LP ".
  1357. "INNER JOIN CVTerm CVT ON LP.type_id = CVT.cvterm_id ".
  1358. "WHERE library_id = %d AND CVT.name = 'library_description'";
  1359. $libraries = array();
  1360. while ($data = db_fetch_object($result)) {
  1361. $library = db_fetch_object(db_query($sql_info, $data->library_id));
  1362. $library->node_id = $data->nid;
  1363. $desc = db_fetch_object(db_query($sql_desc, $data->library_id))->value;
  1364. if ($desc) {
  1365. $library->library_description = $desc;
  1366. } else {
  1367. $library->library_description = "NA";
  1368. }
  1369. // Use uniquename as the key so we can sort by uniquename later
  1370. $key = strtolower($library->uniquename);
  1371. $libraries [$key] = $library;
  1372. }
  1373. tripal_db_set_active($previous_db);
  1374. //Sort libraries by uniquename
  1375. ksort($libraries, SORT_STRING);
  1376. return $libraries;
  1377. }
  1378. }
  1379. /************************************************************************
  1380. *
  1381. */
  1382. function tripal_library_cleanup($dummy = NULL, $job_id = NULL) {
  1383. // build the SQL statments needed to check if nodes point to valid analyses
  1384. $dsql = "SELECT * FROM {node} WHERE type = 'chado_library' order by nid";
  1385. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1386. $csql = "SELECT * FROM {chado_library} where nid = %d ";
  1387. $cosql= "SELECT * FROM {chado_library}";
  1388. $tsql = "SELECT * FROM {library} L WHERE library_id = %d";
  1389. // load into nodes array
  1390. $results = db_query($dsql);
  1391. $count = 0;
  1392. $nodes = array();
  1393. while($node = db_fetch_object($results)){
  1394. $nodes[$count] = $node;
  1395. $count++;
  1396. }
  1397. // load the chado_analyses into an array
  1398. $results = db_query($cosql);
  1399. $cnodes = array();
  1400. while($node = db_fetch_object($results)){
  1401. $cnodes[$count] = $node;
  1402. $count++;
  1403. }
  1404. $interval = intval($count * 0.01);
  1405. // iterate through all of the chado_library nodes and delete those that aren't valid
  1406. foreach($nodes as $nid){
  1407. // update the job status every 1% analyses
  1408. if($job_id and $i % $interval == 0){
  1409. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1410. }
  1411. // first check to see if the node has a corresponding entry
  1412. // in the chado_library table. If not then delete the node.
  1413. $library = db_fetch_object(db_query($csql,$nid->nid));
  1414. if(!$library){
  1415. node_delete($nid->nid);
  1416. $message = "Missing in chado_library table.... DELETING: $nid->nid\n";
  1417. watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
  1418. continue;
  1419. }
  1420. $i++;
  1421. }
  1422. // iterate through all of the chado_library nodes and delete those that aren't valid
  1423. foreach($cnodes as $nid){
  1424. // update the job status every 1% analyses
  1425. if($job_id and $i % $interval == 0){
  1426. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1427. }
  1428. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1429. if(!$node){
  1430. db_query("DELETE FROM {chado_library} WHERE nid = $nid->nid");
  1431. $message = "chado_library missing node.... DELETING: $nid->nid\n";
  1432. watchdog('tripal_library',$message,array(),WATCHDOG_WARNING);
  1433. }
  1434. $i++;
  1435. }
  1436. return '';
  1437. }