tripal_stock.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /**
  3. * @file
  4. * Basic functionality for stocks
  5. */
  6. require_once 'api/tripal_stock.api.inc';
  7. require_once 'api/tripal_stock.DEPRECATED.inc';
  8. require_once 'theme/tripal_stock.theme.inc';
  9. require_once 'includes/tripal_stock.admin.inc';
  10. require_once 'includes/tripal_stock.chado_node.inc';
  11. /**
  12. * @defgroup tripal_stock Stock Module
  13. * @ingroup tripal_modules
  14. * @{
  15. * Integrates the Chado Stock module with Drupal Nodes & Views
  16. *
  17. * The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado
  18. * stocks. The stock module was designed to store information about stock collections in a
  19. * laboratory. What is called a stock could also be called a strain or an accession. There is a lot
  20. * in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all
  21. * come from some taxon, have genotypes, physical locations in the lab, some conceivable
  22. * relationship with a publication, some conceivable relationship with a sequence feature (such as a
  23. * transgene), and could be described by some ontology term. For more information about the chado
  24. * Stock Module see the GMOD Wiki Page (http://gmod.org/wiki/Chado_Stock_Module)
  25. * @}
  26. */
  27. /**
  28. * Implements hook_menu().
  29. * Adds menu items for the tripal_stock
  30. *
  31. * @return
  32. * Menu definitions for the tripal_stock
  33. *
  34. * @ingroup tripal_stock
  35. */
  36. function tripal_stock_menu() {
  37. $items = array();
  38. // the menu link for addressing any stock (by name, uniquename, synonym)
  39. $items['stock/%'] = array(
  40. 'page callback' => 'tripal_stock_match_stocks_page',
  41. 'page arguments' => array(1),
  42. 'access arguments' => array('access chado_stock content'),
  43. 'type' => MENU_LOCAL_TASK,
  44. );
  45. //Administrative settings menu-----------------
  46. $items['admin/tripal/chado/tripal_stock'] = array(
  47. 'title' => 'Stocks',
  48. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  49. 'page callback' => 'tripal_stock_admin_stock_view',
  50. 'access arguments' => array('administer tripal stock'),
  51. 'type' => MENU_NORMAL_ITEM
  52. );
  53. $items['admin/tripal/chado/tripal_stock/configuration'] = array(
  54. 'title' => 'Settings',
  55. 'description' => 'Settings for Chado Stocks',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('tripal_stock_admin'),
  58. 'access arguments' => array('administer tripal stock'),
  59. 'type' => MENU_LOCAL_TASK,
  60. 'weight' => 5
  61. );
  62. $items['admin/tripal/chado/tripal_stock/sync'] = array(
  63. 'title' => ' Sync',
  64. 'description' => 'Sync stocks from Chado with Drupal',
  65. 'page callback' => 'drupal_get_form',
  66. //'page arguments' => array('tripal_stock_sync_form'),
  67. 'page arguments' => array('chado_node_sync_form', 'tripal_stock', 'chado_stock'),
  68. 'access arguments' => array('administer tripal stock'),
  69. 'type' => MENU_LOCAL_TASK,
  70. 'weight' => 0
  71. );
  72. $items['admin/tripal/chado/tripal_stock/help'] = array(
  73. 'title' => 'Help',
  74. 'description' => 'Basic Description of Tripal Stock Module Functionality',
  75. 'page callback' => 'theme',
  76. 'page arguments' => array('tripal_stock_help'),
  77. 'access arguments' => array('administer tripal stock'),
  78. 'type' => MENU_LOCAL_TASK,
  79. 'weight' => 10
  80. );
  81. return $items;
  82. }
  83. /**
  84. * Implements hook_search_biological_data_views().
  85. *
  86. * Adds the described views to the "Search Data" Page created by Tripal Views
  87. */
  88. function tripal_stock_search_biological_data_views() {
  89. return array(
  90. 'tripal_stock_user_stocks' => array(
  91. 'machine_name' => 'tripal_stock_user_stocks',
  92. 'human_name' => 'Stocks',
  93. 'description' => 'A stock is the physical entities of an organism, either living or preserved.',
  94. 'link' => 'chado/stock'
  95. ),
  96. );
  97. }
  98. /**
  99. * Implements Menu wildcard_load hook
  100. *
  101. * Allows the node ID of a chado stock to be dynamically
  102. * pulled from the path. The node is loaded from this node ID
  103. * and supplied to the page as an arguement. This is an example
  104. * of dynamic argument replacement using wildcards in the path.
  105. *
  106. * @param $nid
  107. * The node ID passed in from the path
  108. *
  109. * @return
  110. * The node object with the passed in nid
  111. *
  112. * @ingroup tripal_stock
  113. */
  114. function cs_node_load($nid) {
  115. if (is_numeric($nid)) {
  116. $node = node_load($nid);
  117. if ($node->type == 'chado_stock') {
  118. return $node;
  119. }
  120. }
  121. return FALSE;
  122. }
  123. /**
  124. * Implements hook_permission().
  125. * Set the permission types that the chado stock module uses
  126. *
  127. * @return
  128. * Listing of the possible permission catagories
  129. *
  130. * @ingroup tripal_stock
  131. */
  132. function tripal_stock_permission() {
  133. return array(
  134. 'access chado_stock content' => array(
  135. 'title' => t('View Stocks'),
  136. 'description' => t('Allow users to view stock pages.'),
  137. ),
  138. 'create chado_stock content' => array(
  139. 'title' => t('Create Stocks'),
  140. 'description' => t('Allow users to create new stock pages.'),
  141. ),
  142. 'delete chado_stock content' => array(
  143. 'title' => t('Delete Stocks'),
  144. 'description' => t('Allow users to delete stock pages.'),
  145. ),
  146. 'edit chado_stock content' => array(
  147. 'title' => t('Edit Stocks'),
  148. 'description' => t('Allow users to edit stock pages.'),
  149. ),
  150. 'administer tripal stock' => array(
  151. 'title' => t('Administer Stocks'),
  152. 'description' => t('Allow users to administer all stocks.'),
  153. ),
  154. );
  155. }
  156. /**
  157. * Implement hook_node_access().
  158. *
  159. * This hook allows node modules to limit access to the node types they define.
  160. *
  161. * @param $node
  162. * The node on which the operation is to be performed, or, if it does not yet exist, the
  163. * type of node to be created
  164. *
  165. * @param $op
  166. * The operation to be performed
  167. *
  168. * @param $account
  169. * A user object representing the user for whom the operation is to be performed
  170. *
  171. * @return
  172. * If the permission for the specified operation is not set then return FALSE. If the
  173. * permission is set then return NULL as this allows other modules to disable
  174. * access. The only exception is when the $op == 'create'. We will always
  175. * return TRUE if the permission is set.
  176. *
  177. * @ingroup tripal_stock
  178. */
  179. function chado_stock_node_access($node, $op, $account) {
  180. if ($op == 'create') {
  181. if (!user_access('create chado_stock content', $account)) {
  182. return FALSE;
  183. }
  184. return TRUE;
  185. }
  186. if ($op == 'update') {
  187. if (!user_access('edit chado_stock content', $account)) {
  188. return FALSE;
  189. }
  190. }
  191. if ($op == 'delete') {
  192. if (!user_access('delete chado_stock content', $account)) {
  193. return FALSE;
  194. }
  195. }
  196. if ($op == 'view') {
  197. if (!user_access('access chado_stock content', $account)) {
  198. return FALSE;
  199. }
  200. }
  201. return NULL;
  202. }
  203. /**
  204. * Implements hook_views_api().
  205. *
  206. * Purpose: Essentially this hook tells drupal that there is views support for
  207. * for this module which then includes tripal_stock.views.inc where all the
  208. * views integration code is
  209. *
  210. * @return
  211. * An array with fields important for views integration
  212. *
  213. * @ingroup tripal_stock
  214. */
  215. function tripal_stock_views_api() {
  216. return array(
  217. 'api' => 3.0,
  218. );
  219. }
  220. /**
  221. * Implements hook_theme().
  222. * Register themeing functions for this module
  223. *
  224. * @return
  225. * An array of themeing functions to register
  226. *
  227. * @ingroup tripal_stock
  228. */
  229. function tripal_stock_theme($existing, $type, $theme, $path) {
  230. $core_path = drupal_get_path('module', 'tripal_core');
  231. $items = array(
  232. // tripal_stock templates
  233. 'node__chado_stock' => array(
  234. 'template' => 'node--chado-generic',
  235. 'render element' => 'node',
  236. 'base hook' => 'node',
  237. 'path' => "$core_path/theme/templates",
  238. ),
  239. 'tripal_stock_base' => array(
  240. 'variables' => array('node' => NULL),
  241. 'template' => 'tripal_stock_base',
  242. 'path' => "$path/theme/templates",
  243. ),
  244. 'tripal_stock_properties' => array(
  245. 'variables' => array('node' => NULL),
  246. 'template' => 'tripal_stock_properties',
  247. 'path' => "$path/theme/templates",
  248. ),
  249. 'tripal_stock_publications' => array(
  250. 'variables' => array('node' => NULL),
  251. 'template' => 'tripal_stock_publications',
  252. 'path' => "$path/theme/templates",
  253. ),
  254. 'tripal_stock_references' => array(
  255. 'variables' => array('node' => NULL),
  256. 'template' => 'tripal_stock_references',
  257. 'path' => "$path/theme/templates",
  258. ),
  259. 'tripal_stock_relationships' => array(
  260. 'variables' => array('node' => NULL),
  261. 'template' => 'tripal_stock_relationships',
  262. 'path' => "$path/theme/templates",
  263. ),
  264. 'tripal_stock_synonyms' => array(
  265. 'arguments' => array('node' => NULL),
  266. 'template' => 'tripal_stock_synonyms',
  267. 'path' => "$path/theme/templates",
  268. ),
  269. 'tripal_stock_collections' => array(
  270. 'variables' => array('node' => NULL),
  271. 'template' => 'tripal_stock_collections',
  272. 'path' => "$path/theme/templates",
  273. ),
  274. 'tripal_stock_collections' => array(
  275. 'variables' => array('node' => NULL),
  276. 'template' => 'tripal_stock_collections',
  277. 'path' => "$path/theme/templates",
  278. ),
  279. 'tripal_stock_phenotypes' => array(
  280. 'variables' => array('node' => NULL),
  281. 'template' => 'tripal_stock_phenotypes',
  282. 'path' => "$path/theme/templates",
  283. ),
  284. 'tripal_stock_locations' => array(
  285. 'variables' => array('node' => NULL),
  286. 'template' => 'tripal_stock_locations',
  287. 'path' => "$path/theme/templates",
  288. ),
  289. // tripal_organism templates
  290. 'tripal_organism_stocks' => array(
  291. 'variables' => array('node' => NULL),
  292. 'template' => 'tripal_organism_stocks',
  293. 'path' => "$path/theme/templates",
  294. ),
  295. // help template
  296. 'tripal_stock_help' => array(
  297. 'template' => 'tripal_stock_help',
  298. 'variables' => array(NULL),
  299. 'path' => "$path/theme/templates",
  300. ),
  301. // themed teaser
  302. 'tripal_stock_teaser' => array(
  303. 'variables' => array('node' => NULL),
  304. 'template' => 'tripal_stock_teaser',
  305. 'path' => "$path/theme/templates",
  306. ),
  307. );
  308. return $items;
  309. }
  310. /**
  311. * Implements hook_help().
  312. * Adds a help page to the module list
  313. *
  314. * @ingroup tripal_stock
  315. */
  316. function tripal_stock_help ($path, $arg) {
  317. if ($path == 'admin/help#tripal_stock') {
  318. return theme('tripal_stock_help', array());
  319. }
  320. }
  321. /**
  322. * Implements hook_block_info().
  323. *
  324. * @ingroup tripal_stock
  325. */
  326. function tripal_stock_block_info() {
  327. $blocks['base']['info'] = t('Tripal Stock Details');
  328. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  329. $blocks['properties']['info'] = t('Tripal Stock Properties');
  330. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';
  331. $blocks['references']['info'] = t('Tripal Stock References');
  332. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  333. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  334. $blocks['relationships_as_object']['cache'] = 'BLOCK_NO_CACHE';
  335. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  336. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  337. $blocks['collections']['info'] = t('Tripal Stock Collections');
  338. $blocks['collections']['cache'] = 'BLOCK_NO_CACHE';
  339. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  340. $blocks['phenotypes']['cache'] = 'BLOCK_NO_CACHE';
  341. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  342. $blocks['genotypes']['cache'] = 'BLOCK_NO_CACHE';
  343. $blocks['locations']['info'] = t('Tripal Stock Locations');
  344. $blocks['locations']['cache'] = 'BLOCK_NO_CACHE';
  345. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  346. $blocks['orgstocks']['cache'] = 'BLOCK_NO_CACHE';
  347. return $blocks;
  348. }
  349. /**
  350. * Implements hook_block_view().
  351. *
  352. * @ingroup tripal_stock
  353. */
  354. function tripal_stock_block_view($delta = '') {
  355. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  356. $nid = arg(1);
  357. $node = node_load($nid);
  358. $block = array();
  359. switch ($delta) {
  360. case 'base':
  361. $block['subject'] = t('Stock Details');
  362. $block['content'] = theme('tripal_stock_base', $node);
  363. break;
  364. case 'properties':
  365. $block['subject'] = t('Properties');
  366. $block['content'] = theme('tripal_stock_properties', $node);
  367. break;
  368. case 'references':
  369. $block['subject'] = t('References');
  370. $block['content'] = theme('tripal_stock_references', $node);
  371. break;
  372. case 'relationships':
  373. $block['subject'] = t('Relationships');
  374. $block['content'] = theme('tripal_stock_relationships', $node);
  375. break;
  376. case 'synonyms':
  377. $block['subject'] = t('Synonyms');
  378. $block['content'] = theme('tripal_stock_synonyms', $node);
  379. break;
  380. case 'collections':
  381. $block['subject'] = t('Stock Collections');
  382. $block['content'] = theme('tripal_stock_collections', $node);
  383. break;
  384. case 'phenotypes':
  385. $block['subject'] = t('Stock Phenotypes');
  386. $block['content'] = theme('tripal_stock_phenotypes', $node);
  387. break;
  388. case 'genotypes':
  389. $block['subject'] = t('Stock Genotypes');
  390. $block['content'] = theme('tripal_stock_genotypes', $node);
  391. break;
  392. case 'locations':
  393. $block['subject'] = t('Stock Locations');
  394. $block['content'] = theme('tripal_stock_locations', $node);
  395. break;
  396. case 'orgstocks':
  397. $block['subject'] = t('Organism Stocks');
  398. $block['content'] = theme('tripal_organism_stocks', $node);
  399. break;
  400. }
  401. return $block;
  402. }
  403. }
  404. /*
  405. * Uses the value provided in the $id argument to find all stocks that match
  406. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  407. * stock it will redirect to that stock page, otherwise, a list of matching
  408. * stocks is shown.
  409. *
  410. * @ingroup tripal_stock
  411. */
  412. function tripal_stock_match_stocks_page($id) {
  413. // if the URL alias configuration is set such that the URL
  414. // always begins with 'stock' then we want to use the ID as it is and
  415. // forward it on. Otherwise, try to find the matching stock.
  416. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  417. if (!$url_alias) {
  418. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  419. }
  420. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  421. if (preg_match('/^stock\//', $url_alias)) {
  422. drupal_goto($id);
  423. }
  424. $sql = "
  425. SELECT
  426. S.name, S.uniquename, S.stock_id,
  427. O.genus, O.species, O.organism_id,
  428. CVT.cvterm_id, CVT.name as type_name,
  429. CS.nid
  430. FROM {stock} S
  431. INNER JOIN {organism} O on S.organism_id = O.organism_id
  432. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  433. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  434. WHERE
  435. S.uniquename = :uname or S.name = :name
  436. ";
  437. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  438. $num_matches = 0;
  439. // iterate through the matches and build the table for showing matches
  440. $header = array('Uniquename', 'Name', 'Type', 'Species');
  441. $rows = array();
  442. $curr_match;
  443. while ($match = $results->fetchObject()) {
  444. $curr_match = $match;
  445. $rows[] = array(
  446. $match->uniquename,
  447. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  448. $match->type_name,
  449. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  450. );
  451. $num_matches++;
  452. }
  453. // if we have more than one match then generate the table, otherwise, redirect
  454. // to the matched stock
  455. if ($num_matches == 1) {
  456. drupal_goto("node/" . $curr_match->nid);
  457. }
  458. if ($num_matches == 0) {
  459. return "<p>No stocks matched the given name '$id'</p>";
  460. }
  461. $table_attrs = array(
  462. 'class' => 'tripal-table tripal-table-horz'
  463. );
  464. $output = "<p>The following stocks match the name '$id'.</p>";
  465. $output .= theme_table($header, $rows, $table_attrs, $caption);
  466. return $output;
  467. }
  468. /**
  469. * Implementation of hook_form_alter().
  470. *
  471. * @param $form
  472. * @param $form_state
  473. * @param $form_id
  474. *
  475. * @ingroup tripal_stock
  476. */
  477. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  478. // turn of preview button for insert/updates
  479. if ($form_id == "chado_stock_node_form") {
  480. $form['actions']['preview']['#access'] = FALSE;
  481. //remove the body field
  482. unset($form['body']);
  483. }
  484. }
  485. /**
  486. * Load the arguments for the organism stock counts browser
  487. *
  488. * @param $organism
  489. * The organism of interest
  490. *
  491. * @ingroup tripal_stock
  492. */
  493. function tripal_stock_load_organism_stock_counts($organism) {
  494. $args = array();
  495. $order = array();
  496. $names = array();
  497. // build the where clause for the SQL statement if we have a custom term list
  498. // we'll also keep track of the names the admin provided (if any) and the
  499. // order that the terms should appear.
  500. $is_custom = 0;
  501. $temp = rtrim(variable_get('tripal_stock_summary_report_mapping', ''));
  502. $where = '';
  503. if ($temp) {
  504. $is_custom = 1;
  505. $temp = explode("\n", $temp);
  506. $i = 0;
  507. foreach ($temp as $value) {
  508. // separate the key value pairs
  509. $temp2 = explode("=", $value);
  510. $stock_type = rtrim($temp2[0]);
  511. $order[] = $stock_type; // save the order of the these terms
  512. $where .= " OFC.stock_type = :name$i OR ";
  513. $args[":name$i"] = rtrim($temp2[0]);
  514. // if the admin specified a new name then store that otherwise use the
  515. // the default sequence ontology term name
  516. if(count($temp2) == 2) {
  517. $names[] = rtrim($temp2[1]);
  518. }
  519. else {
  520. $names[] = $stock_type;
  521. }
  522. $i++;
  523. }
  524. if ($where) {
  525. $where = drupal_substr($where, 0, -4); # remove OR from the end
  526. $where = "($where) AND";
  527. }
  528. }
  529. // get the stock counts. This is dependent on a materialized view
  530. // installed with the organism module
  531. $sql = "
  532. SELECT OFC.num_stocks,OFC.stock_type,CVT.definition
  533. FROM {organism_stock_count} OFC
  534. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  535. WHERE $where organism_id = :organism_id
  536. ORDER BY num_stocks desc
  537. ";
  538. $args[':organism_id'] = $organism->organism_id;
  539. $org_stocks = chado_query($sql, $args);
  540. // iterate through the types
  541. $types = array();
  542. while ($type = $org_stocks->fetchObject()) {
  543. $types[$type->stock_type] = $type;
  544. // if we don't have an order this means we didn't go through the loop
  545. // above to set the names, so do that now
  546. if (!$is_custom) {
  547. $names[] = $type->stock_type;
  548. $order[] = $type->stock_type;
  549. }
  550. }
  551. // now reorder the types
  552. $ordered_types = array();
  553. foreach ($order as $type) {
  554. $ordered_types[] = $types[$type];
  555. }
  556. return array(
  557. 'types' => $ordered_types,
  558. 'names' => $names
  559. );
  560. }