tripal_stock.module 14 KB

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