tripal_stock.module 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. 'adminster 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. );
  302. return $items;
  303. }
  304. /**
  305. * Implements hook_help().
  306. * Adds a help page to the module list
  307. *
  308. * @ingroup tripal_stock
  309. */
  310. function tripal_stock_help ($path, $arg) {
  311. if ($path == 'admin/help#tripal_stock') {
  312. return theme('tripal_stock_help', array());
  313. }
  314. }
  315. /**
  316. * Implements hook_block_info().
  317. *
  318. * @ingroup tripal_stock
  319. */
  320. function tripal_stock_block_info() {
  321. $blocks['base']['info'] = t('Tripal Stock Details');
  322. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  323. $blocks['properties']['info'] = t('Tripal Stock Properties');
  324. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';
  325. $blocks['references']['info'] = t('Tripal Stock References');
  326. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  327. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  328. $blocks['relationships_as_object']['cache'] = 'BLOCK_NO_CACHE';
  329. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  330. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  331. $blocks['collections']['info'] = t('Tripal Stock Collections');
  332. $blocks['collections']['cache'] = 'BLOCK_NO_CACHE';
  333. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  334. $blocks['phenotypes']['cache'] = 'BLOCK_NO_CACHE';
  335. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  336. $blocks['genotypes']['cache'] = 'BLOCK_NO_CACHE';
  337. $blocks['locations']['info'] = t('Tripal Stock Locations');
  338. $blocks['locations']['cache'] = 'BLOCK_NO_CACHE';
  339. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  340. $blocks['orgstocks']['cache'] = 'BLOCK_NO_CACHE';
  341. return $blocks;
  342. }
  343. /**
  344. * Implements hook_block_view().
  345. *
  346. * @ingroup tripal_stock
  347. */
  348. function tripal_stock_block_view($delta = '') {
  349. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  350. $nid = arg(1);
  351. $node = node_load($nid);
  352. $block = array();
  353. switch ($delta) {
  354. case 'base':
  355. $block['subject'] = t('Stock Details');
  356. $block['content'] = theme('tripal_stock_base', $node);
  357. break;
  358. case 'properties':
  359. $block['subject'] = t('Properties');
  360. $block['content'] = theme('tripal_stock_properties', $node);
  361. break;
  362. case 'references':
  363. $block['subject'] = t('References');
  364. $block['content'] = theme('tripal_stock_references', $node);
  365. break;
  366. case 'relationships':
  367. $block['subject'] = t('Relationships');
  368. $block['content'] = theme('tripal_stock_relationships', $node);
  369. break;
  370. case 'synonyms':
  371. $block['subject'] = t('Synonyms');
  372. $block['content'] = theme('tripal_stock_synonyms', $node);
  373. break;
  374. case 'collections':
  375. $block['subject'] = t('Stock Collections');
  376. $block['content'] = theme('tripal_stock_collections', $node);
  377. break;
  378. case 'phenotypes':
  379. $block['subject'] = t('Stock Phenotypes');
  380. $block['content'] = theme('tripal_stock_phenotypes', $node);
  381. break;
  382. case 'genotypes':
  383. $block['subject'] = t('Stock Genotypes');
  384. $block['content'] = theme('tripal_stock_genotypes', $node);
  385. break;
  386. case 'locations':
  387. $block['subject'] = t('Stock Locations');
  388. $block['content'] = theme('tripal_stock_locations', $node);
  389. break;
  390. case 'orgstocks':
  391. $block['subject'] = t('Organism Stocks');
  392. $block['content'] = theme('tripal_organism_stocks', $node);
  393. break;
  394. }
  395. return $block;
  396. }
  397. }
  398. /*
  399. * Uses the value provided in the $id argument to find all stocks that match
  400. * that ID by name, stockname or synonym. If it matches uniquenly to a single
  401. * stock it will redirect to that stock page, otherwise, a list of matching
  402. * stocks is shown.
  403. *
  404. * @ingroup tripal_stock
  405. */
  406. function tripal_stock_match_stocks_page($id) {
  407. // if the URL alias configuration is set such that the URL
  408. // always begins with 'stock' then we want to use the ID as it is and
  409. // forward it on. Otherwise, try to find the matching stock.
  410. $url_alias = variable_get('chado_stock_url_string', '/stock/[genus]/[species]/[type]/[uniquename]');
  411. if (!$url_alias) {
  412. $url_alias = '/stock/[genus]/[species]/[type]/[uniquename]';
  413. }
  414. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  415. if (preg_match('/^stock\//', $url_alias)) {
  416. drupal_goto($id);
  417. }
  418. $sql = "
  419. SELECT
  420. S.name, S.uniquename, S.stock_id,
  421. O.genus, O.species, O.organism_id,
  422. CVT.cvterm_id, CVT.name as type_name,
  423. CS.nid
  424. FROM {stock} S
  425. INNER JOIN {organism} O on S.organism_id = O.organism_id
  426. INNER JOIN {cvterm} CVT on CVT.cvterm_id = S.type_id
  427. INNER JOIN public.chado_stock CS on CS.stock_id = S.stock_id
  428. WHERE
  429. S.uniquename = :uname or S.name = :name
  430. ";
  431. $results = chado_query($sql, array(':uname' => $id, ':name' => $id));
  432. $num_matches = 0;
  433. // iterate through the matches and build the table for showing matches
  434. $header = array('Uniquename', 'Name', 'Type', 'Species');
  435. $rows = array();
  436. $curr_match;
  437. while ($match = $results->fetchObject()) {
  438. $curr_match = $match;
  439. $rows[] = array(
  440. $match->uniquename,
  441. "<a href=\"" . url("node/". $match->nid) ."\">" . $match->name . "</a>",
  442. $match->type_name,
  443. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  444. );
  445. $num_matches++;
  446. }
  447. // if we have more than one match then generate the table, otherwise, redirect
  448. // to the matched stock
  449. if ($num_matches == 1) {
  450. drupal_goto("node/" . $curr_match->nid);
  451. }
  452. if ($num_matches == 0) {
  453. return "<p>No stocks matched the given name '$id'</p>";
  454. }
  455. $table_attrs = array(
  456. 'class' => 'tripal-table tripal-table-horz'
  457. );
  458. $output = "<p>The following stocks match the name '$id'.</p>";
  459. $output .= theme_table($header, $rows, $table_attrs, $caption);
  460. return $output;
  461. }
  462. /**
  463. * Implementation of hook_form_alter().
  464. *
  465. * @param $form
  466. * @param $form_state
  467. * @param $form_id
  468. *
  469. * @ingroup tripal_stock
  470. */
  471. function tripal_stock_form_alter(&$form, &$form_state, $form_id) {
  472. // turn of preview button for insert/updates
  473. if ($form_id == "chado_stock_node_form") {
  474. $form['actions']['preview']['#access'] = FALSE;
  475. }
  476. }
  477. /**
  478. * Load the arguments for the organism stock counts browser
  479. *
  480. * @param $organism
  481. * The organism of interest
  482. *
  483. * @ingroup tripal_stock
  484. */
  485. function tripal_stock_load_organism_stock_counts($organism) {
  486. $args = array();
  487. $order = array();
  488. $names = array();
  489. // build the where clause for the SQL statement if we have a custom term list
  490. // we'll also keep track of the names the admin provided (if any) and the
  491. // order that the terms should appear.
  492. $is_custom = 0;
  493. $temp = rtrim(variable_get('tripal_stock_summary_report_mapping', ''));
  494. $where = '';
  495. if ($temp) {
  496. $is_custom = 1;
  497. $temp = explode("\n", $temp);
  498. $i = 0;
  499. foreach ($temp as $value) {
  500. // separate the key value pairs
  501. $temp2 = explode("=", $value);
  502. $stock_type = rtrim($temp2[0]);
  503. $order[] = $stock_type; // save the order of the these terms
  504. $where .= " OFC.stock_type = :name$i OR ";
  505. $args[":name$i"] = rtrim($temp2[0]);
  506. // if the admin specified a new name then store that otherwise use the
  507. // the default sequence ontology term name
  508. if(count($temp2) == 2) {
  509. $names[] = rtrim($temp2[1]);
  510. }
  511. else {
  512. $names[] = $stock_type;
  513. }
  514. $i++;
  515. }
  516. if ($where) {
  517. $where = drupal_substr($where, 0, -4); # remove OR from the end
  518. $where = "($where) AND";
  519. }
  520. }
  521. // get the stock counts. This is dependent on a materialized view
  522. // installed with the organism module
  523. $sql = "
  524. SELECT OFC.num_stocks,OFC.stock_type,CVT.definition
  525. FROM {organism_stock_count} OFC
  526. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  527. WHERE $where organism_id = :organism_id
  528. ORDER BY num_stocks desc
  529. ";
  530. $args[':organism_id'] = $organism->organism_id;
  531. $org_stocks = chado_query($sql, $args);
  532. // iterate through the types
  533. $types = array();
  534. while ($type = $org_stocks->fetchObject()) {
  535. $types[$type->stock_type] = $type;
  536. // if we don't have an order this means we didn't go through the loop
  537. // above to set the names, so do that now
  538. if (!$is_custom) {
  539. $names[] = $type->stock_type;
  540. $order[] = $type->stock_type;
  541. }
  542. }
  543. // now reorder the types
  544. $ordered_types = array();
  545. foreach ($order as $type) {
  546. $ordered_types[] = $types[$type];
  547. }
  548. return array(
  549. 'types' => $ordered_types,
  550. 'names' => $names
  551. );
  552. }