tripal_library.module 61 KB

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