tripal_library.module 62 KB

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