tripal_stock.module 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. <?php
  2. /**
  3. * @file
  4. * Implements Tripal Stock Module hooks
  5. */
  6. /**
  7. * @defgroup tripal_stock Stock Module
  8. * @{
  9. * Provides functions for managing chado stocks including creating details pages for each stock
  10. *
  11. * The Tripal Stock Module provides functionality for adding, editing, deleting and accessing chado
  12. * stocks. The stock module was designed to store information about stock collections in a
  13. * laboratory. What is called a stock could also be called a strain or an accession. There is a lot
  14. * in common between a Drosophila stock and a Saccharomyces strain and an Arabidopsis line. They all
  15. * come from some taxon, have genotypes, physical locations in the lab, some conceivable
  16. * relationship with a publication, some conceivable relationship with a sequence feature (such as a
  17. * transgene), and could be described by some ontology term. For more information about the chado
  18. * Stock Module see the GMOD Wiki Page (http://gmod.org/wiki/Chado_Stock_Module)
  19. * @}
  20. * @ingroup tripal_modules
  21. */
  22. require_once("includes/tripal_stock-administration.inc");
  23. require_once("includes/other_module_api_functions.inc");
  24. require_once("includes/tripal_stock-secondary_tables.inc");
  25. require_once("includes/tripal_stock-properties.inc");
  26. require_once("includes/tripal_stock-relationships.inc");
  27. require_once("includes/tripal_stock-db_references.inc");
  28. require_once("api/tripal_stock.api.inc");
  29. /**
  30. * Implements hook_menu(): Adds menu items for the tripal_stock
  31. *
  32. * @return
  33. * Menu definitions for the tripal_stock
  34. *
  35. * @ingroup tripal_stock
  36. */
  37. function tripal_stock_menu() {
  38. $items = array();
  39. //Administrative settings menu-----------------
  40. $items['admin/tripal/tripal_stock'] = array(
  41. 'title' => 'Stocks',
  42. 'description' => 'Basic Description of Tripal Stock Module Functionality',
  43. 'page callback' => 'tripal_stock_module_description_page',
  44. 'access arguments' => array('administer site configuration'),
  45. 'type' => MENU_NORMAL_ITEM
  46. );
  47. $items['admin/tripal/tripal_stock/configuration'] = array(
  48. 'title' => 'Configuration',
  49. 'description' => 'Settings for Chado Stocks',
  50. 'page callback' => 'drupal_get_form',
  51. 'page arguments' => array('tripal_stock_admin'),
  52. 'access arguments' => array('administer site configuration'),
  53. 'type' => MENU_NORMAL_ITEM
  54. );
  55. // Adding Secondary Properties-----------------
  56. $items['node/%cs_node/properties'] = array(
  57. 'title' => 'Add Properties & Synonyms',
  58. 'description' => 'Settings for Chado Stocks',
  59. 'page callback' => 'tripal_stock_add_ALL_property_page',
  60. 'page arguments' => array(1),
  61. 'access arguments' => array('create chado_stock content'),
  62. 'type' => MENU_CALLBACK
  63. );
  64. $items['node/%cs_node/db_references'] = array(
  65. 'title' => 'Add Database References',
  66. 'description' => 'Settings for Chado Stocks',
  67. 'page callback' => 'tripal_stock_add_ALL_dbreferences_page',
  68. 'page arguments' => array(1),
  69. 'access arguments' => array('create chado_stock content'),
  70. 'type' => MENU_CALLBACK
  71. );
  72. $items['node/%cs_node/relationships'] = array(
  73. 'title' => 'Add Relationships',
  74. 'description' => 'Settings for Chado Stocks',
  75. 'page callback' => 'tripal_stock_add_ALL_relationships_page',
  76. 'page arguments' => array(1),
  77. 'access arguments' => array('create chado_stock content'),
  78. 'type' => MENU_CALLBACK
  79. );
  80. //Edit/Deleting Secondary Properties-------------
  81. $items['node/%cs_node/edit_properties'] = array(
  82. 'title' => 'Edit Properties',
  83. 'description' => 'Settings for Chado Stocks',
  84. 'page callback' => 'tripal_stock_edit_ALL_properties_page',
  85. 'page arguments' => array(1),
  86. 'access arguments' => array('edit chado_stock content'),
  87. 'type' => MENU_LOCAL_TASK,
  88. 'weight' => 8,
  89. );
  90. $items['node/%cs_node/edit_relationships'] = array(
  91. 'title' => 'Edit Relationships',
  92. 'description' => 'Settings for Chado Stocks',
  93. 'page callback' => 'tripal_stock_edit_ALL_relationships_page',
  94. 'page arguments' => array(1),
  95. 'access arguments' => array('edit chado_stock content'),
  96. 'type' => MENU_LOCAL_TASK,
  97. 'weight' => 9,
  98. );
  99. $items['node/%cs_node/edit_db_references'] = array(
  100. 'title' => 'Edit DB References',
  101. 'description' => 'Settings for Chado Stocks',
  102. 'page callback' => 'tripal_stock_edit_ALL_dbreferences_page',
  103. 'page arguments' => array(1),
  104. 'access arguments' => array('edit chado_stock content'),
  105. 'type' => MENU_LOCAL_TASK,
  106. 'weight' => 10,
  107. );
  108. return $items;
  109. }
  110. /**
  111. * Implements Menu wildcard_load hook
  112. *
  113. * Purpose: Allows the node ID of a chado stock to be dynamically
  114. * pulled from the path. The node is loaded from this node ID
  115. * and supplied to the page as an arguement. This is an example
  116. * of dynamic argument replacement using wildcards in the path.
  117. *
  118. * @param $nid
  119. * The node ID passed in from the path
  120. *
  121. * @return
  122. * The node object with the passed in nid
  123. *
  124. * @ingroup tripal_stock
  125. */
  126. function cs_node_load($nid) {
  127. if (is_numeric($nid)) {
  128. $node = node_load($nid);
  129. if ($node->type == 'chado_stock') {
  130. return $node;
  131. }
  132. }
  133. return FALSE;
  134. }
  135. /**
  136. * Implementation of hook_perm().
  137. *
  138. * Purpose: Set the permission types that the chado stock module uses
  139. *
  140. * @return
  141. * Listing of the possible permission catagories
  142. *
  143. * @ingroup tripal_stock
  144. */
  145. function tripal_stock_perm() {
  146. return array(
  147. 'access chado_stock content',
  148. 'create chado_stock content',
  149. 'edit chado_stock content',
  150. 'delete chado_stock content'
  151. );
  152. }
  153. /**
  154. * Implements hook_access(): Maps permission catagories to actions
  155. *
  156. * @param $op
  157. * The operation current operation: one of create, update, delete
  158. * @param $node
  159. * The node object the current operation is being performed on
  160. * @param $account
  161. * A user object representing the user for whom the operation is to be performed
  162. *
  163. * @return
  164. * TRUE grants access; FALSE denies it
  165. *
  166. * @ingroup tripal_stock
  167. */
  168. function chado_stock_access($op, $node, $account) {
  169. if ($op == 'create') {
  170. if (!user_access('create chado_stock content', $account)) {
  171. return FALSE;
  172. }
  173. }
  174. if ($op == 'update') {
  175. if (!user_access('edit chado_stock content', $account)) {
  176. return FALSE;
  177. }
  178. }
  179. if ($op == 'delete') {
  180. if (!user_access('delete chado_stock content', $account)) {
  181. return FALSE;
  182. }
  183. }
  184. if ($op == 'view') {
  185. if (!user_access('access chado_stock content', $account)) {
  186. return FALSE;
  187. }
  188. }
  189. return NULL;
  190. }
  191. /**
  192. * Implements hook_views_api()
  193. *
  194. * Purpose: Essentially this hook tells drupal that there is views support for
  195. * for this module which then includes tripal_stock.views.inc where all the
  196. * views integration code is
  197. *
  198. * @return
  199. * An array with fields important for views integration
  200. *
  201. * @ingroup tripal_stock
  202. */
  203. function tripal_stock_views_api() {
  204. return array(
  205. 'api' => 2.0,
  206. );
  207. }
  208. /**
  209. * Implements hook_theme(): Register themeing functions for this module
  210. *
  211. * @return
  212. * An array of themeing functions to register
  213. *
  214. * @ingroup tripal_stock
  215. */
  216. function tripal_stock_theme() {
  217. return array(
  218. // Property edit forms--------------------------
  219. 'tripal_stock_edit_ALL_properties_form' => array(
  220. 'arguments' => array('form'),
  221. 'function' => 'theme_tripal_stock_edit_ALL_properties_form',
  222. ),
  223. 'tripal_stock_edit_ALL_db_references_form' => array(
  224. 'arguments' => array('form'),
  225. 'function' => 'theme_tripal_stock_edit_ALL_db_references_form',
  226. ),
  227. 'tripal_stock_edit_ALL_relationships_form' => array(
  228. 'arguments' => array('form'),
  229. 'function' => 'theme_tripal_stock_edit_ALL_relationships_form',
  230. ),
  231. // Block Templates------------------------------
  232. 'tripal_stock_base' => array(
  233. 'arguments' => array('node' => NULL),
  234. 'template' => 'tripal_stock_base',
  235. ),
  236. 'tripal_stock_properties' => array(
  237. 'arguments' => array('node' => NULL),
  238. 'template' => 'tripal_stock_properties',
  239. ),
  240. 'tripal_stock_references' => array(
  241. 'arguments' => array('node' => NULL),
  242. 'template' => 'tripal_stock_references',
  243. ),
  244. 'tripal_stock_relationships' => array(
  245. 'arguments' => array('node' => NULL),
  246. 'template' => 'tripal_stock_relationships',
  247. ),
  248. 'tripal_stock_synonyms' => array(
  249. 'arguments' => array('node' => NULL),
  250. 'template' => 'tripal_stock_synonyms',
  251. ),
  252. 'tripal_stock_collections' => array(
  253. 'arguments' => array('node' => NULL),
  254. 'template' => 'tripal_stock_collections',
  255. ),
  256. 'tripal_stock_collections' => array(
  257. 'arguments' => array('node' => NULL),
  258. 'template' => 'tripal_stock_collections',
  259. ),
  260. 'tripal_stock_phenotypes' => array(
  261. 'arguments' => array('node' => NULL),
  262. 'template' => 'tripal_stock_phenotypes',
  263. ),
  264. 'tripal_stock_genotypes' => array(
  265. 'arguments' => array('node' => NULL),
  266. 'template' => 'tripal_stock_genotypes',
  267. ),
  268. 'tripal_stock_locations' => array(
  269. 'arguments' => array('node' => NULL),
  270. 'template' => 'tripal_stock_locations',
  271. ),
  272. 'tripal_organism_stocks' => array(
  273. 'arguments' => array('node' => NULL),
  274. 'template' => 'tripal_organism_stocks',
  275. ),
  276. );
  277. }
  278. /**
  279. * Implements hook_node_info(): registers a stock node type
  280. *
  281. * @return
  282. * An array describing various details of the node
  283. *
  284. * @ingroup tripal_stock
  285. */
  286. function tripal_stock_node_info() {
  287. return array(
  288. 'chado_stock' => array(
  289. 'name' => t('Stock'),
  290. 'module' => 'chado_stock',
  291. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  292. 'has_title' => TRUE,
  293. 'has_body' => FALSE,
  294. ),
  295. );
  296. }
  297. /**
  298. * Implements hook_load(): Prepares the chado_stock node
  299. *
  300. * @param $node
  301. * The basic node containing all variables common to all nodes
  302. *
  303. * @return
  304. * A stock node containing all the variables from the basic node and all stock specific variables
  305. *
  306. * @ingroup tripal_stock
  307. */
  308. function chado_stock_load($node) {
  309. // get the stock details from chado
  310. $stock_id = chado_get_id_for_node('stock', $node);
  311. // build the variable with all the stock details
  312. $values = array('stock_id' => $stock_id);
  313. $stock = tripal_core_generate_chado_var('stock', $values);
  314. // add this to the node
  315. $additions = new stdClass();
  316. $additions->stock = $stock;
  317. return $additions;
  318. }
  319. /**
  320. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  321. *
  322. * Parts to be added by this form
  323. * name,
  324. * uniquename,
  325. * description,
  326. * type => select from cvterm with key cvterm_id,
  327. * organism => select from available with key organism_id
  328. * main_db_reference => accession, version, description, db_name(select from dropdown)
  329. *
  330. * @param $node
  331. * An empty node object on insert OR the current stock node object on update
  332. * @param $form_state
  333. * The current state of the form
  334. *
  335. * @return
  336. * A description of the form to be rendered by drupal_get_form()
  337. *
  338. * @ingroup tripal_stock
  339. */
  340. function chado_stock_form($node, $form_state) {
  341. // Expand all fields needed
  342. $fields_needed = array('stock.uniquename', 'stock.name', 'stock.stock_id', 'stock.type_id', 'stock.organism_id', 'stock.description', 'stock.dbxref_id', 'dbxref.accession', 'dbxref.description', 'dbxref.db_id', 'db.db_id');
  343. foreach ($fields_needed as $field_name) {
  344. // Check to see if it's excluded and expand it if so
  345. if ($node->expandable_fields) {
  346. if (in_array($field_name, $node->expandable_fields)) {
  347. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  348. }
  349. }
  350. }
  351. // This defines the path for the next step in a simulated multipart form
  352. // NOTE: The %node gets replaced with the nid in insert
  353. $form['next_step_path'] = array(
  354. '#type' => 'hidden',
  355. '#value' => 'node/%node/properties'
  356. );
  357. // If you don't want a multipart form set this to false
  358. // Will then do default redirect (to new node) on submit
  359. $form['simulate_multipart'] = array(
  360. '#type' => 'textfield',
  361. '#attributes' => array('style' => "display:none"),
  362. '#default_value' => TRUE
  363. );
  364. if (!isset($node->stock->uniquename)) {
  365. $form['progress'] = array(
  366. '#type' => 'item',
  367. '#value' => tripal_stock_add_chado_properties_progress('main')
  368. );
  369. }
  370. $form['names'] = array(
  371. '#type' => 'fieldset',
  372. '#title' => t('Stock Name')
  373. );
  374. $form['names']['title'] = array(
  375. '#type' => 'textfield',
  376. '#title' => t('Name'),
  377. '#default_value' => $node->stock->name,
  378. '#required' => TRUE
  379. );
  380. $form['names']['uniquename'] = array(
  381. '#type' => 'textfield',
  382. '#title' => t('Unique Name'),
  383. '#default_value' => $node->stock->uniquename,
  384. '#required' => TRUE
  385. );
  386. $form['names']['stock_id'] = array(
  387. '#type' => 'hidden',
  388. '#value' => $node->stock->stock_id
  389. );
  390. $form['details'] = array(
  391. '#type' => 'fieldset',
  392. '#title' => t('Stock Details')
  393. );
  394. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
  395. $type_options[0] = 'Select a Type';
  396. if ($node->nid == '') {
  397. $type_default = 0; }
  398. else { $type_default = $node->stock->type_id->cvterm_id; }
  399. $form['details']['type_id'] = array(
  400. '#type' => 'select',
  401. '#title' => t('Type of Stock'),
  402. '#options' => $type_options,
  403. '#default_value' => $type_default,
  404. '#required' => TRUE,
  405. );
  406. $stock_oganism_options = tripal_organism_get_organism_options();
  407. $stock_oganism_options[0] = 'Select An Organism';
  408. if ($node->nid == '') {
  409. $organism_default = 0; }
  410. else { $organism_default = $node->stock->organism_id->organism_id; }
  411. $form['details']['organism_id'] = array(
  412. '#type' => 'select',
  413. '#title' => t('Source Organism for stock'),
  414. '#default_value' => $organism_default,
  415. '#options' => $stock_oganism_options,
  416. '#required' => TRUE
  417. );
  418. $form['details']['stock_description'] = array(
  419. '#type' => 'textarea',
  420. '#title' => t('Notes'),
  421. '#default_value' => $node->stock->description,
  422. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  423. );
  424. $form['database_reference'] = array(
  425. '#type' => 'fieldset',
  426. '#title' => t('Stock Database Reference')
  427. );
  428. $form['database_reference']['accession'] = array(
  429. '#type' => 'textfield',
  430. '#title' => t('Accession'),
  431. '#default_value' => $node->stock->dbxref_id->accession
  432. );
  433. $form['database_reference']['db_description'] = array(
  434. '#type' => 'textarea',
  435. '#title' => t('Description of Database Reference'),
  436. '#default_value' => $node->stock->dbxref_id->description,
  437. '#description' => t('Optionally enter a description about the database accession.')
  438. );
  439. $db_options = tripal_db_get_db_options();
  440. $db_options[0] = 'Select a Database';
  441. if ($node->nid == '') {
  442. $db_default = 0; }
  443. else { $db_default = $node->stock->dbxref_id->db_id->db_id; }
  444. $form['database_reference']['database'] = array(
  445. '#type' => 'select',
  446. '#title' => t('Database'),
  447. '#options' => $db_options,
  448. '#default_value' => $db_default
  449. );
  450. return $form;
  451. }
  452. /**
  453. * Implements hook_validate(): Validate the input from the chado_stock node form
  454. *
  455. * @param $node
  456. * The current node including fields with the form element names and submitted values
  457. * @param $form
  458. * A description of the form to be rendered by drupal_get_form()
  459. *
  460. * @ingroup tripal_stock
  461. */
  462. function chado_stock_validate($node, &$form) {
  463. $int_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s=%d";
  464. $string_in_chado_sql = "SELECT count(*) as count FROM %s WHERE %s='%s'";
  465. // Validate Uniquename only if add
  466. if (empty($node->stock_id)) {
  467. $chado_row = db_fetch_object(chado_query("SELECT * FROM {stock} WHERE uniquename='" . $node->uniquename . "'"));
  468. if (!empty($chado_row->stock_id)) {
  469. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=" . $chado_row->stock_id));
  470. if (!empty($drupal_row->nid)) {
  471. $link = l('node/' . $drupal_row->nid, $node->uniquename);
  472. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  473. }
  474. else {
  475. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  476. }
  477. }
  478. }
  479. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  480. if ( $node->type_id == 0) {
  481. form_set_error('type_id', 'Please select a type of stock.');
  482. }
  483. else {
  484. $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  485. if ( $num_rows->count != 1) {
  486. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  487. }
  488. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  489. if ( $node->organism_id == 0) {
  490. form_set_error('organism_id', 'Please select a source organism for this stock');
  491. }
  492. else {
  493. $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  494. if ( $num_rows->count != 1 ) {
  495. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  496. }
  497. // Check if Accession also database
  498. if ($node->accession != '') {
  499. if ($node->database == 0) {
  500. // there is an accession but no database selected
  501. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  502. }
  503. }
  504. else {
  505. if ($node->database > 0) {
  506. // there is a database selected but no accession
  507. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  508. }
  509. }
  510. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  511. if ( $node->database > 0) {
  512. $num_rows = db_fetch_object(chado_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  513. if ($num_rows->count != 1) {
  514. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  515. }
  516. }
  517. /**
  518. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  519. *
  520. * @param $node
  521. * The current node including fields with the form element names and submitted values
  522. *
  523. * @return
  524. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  525. *
  526. * @ingroup tripal_stock
  527. */
  528. function chado_stock_insert($node) {
  529. //If the chado stock exists
  530. // then don't create but simply link to node
  531. if ($node->chado_stock_exists) {
  532. if (!empty($node->stock_id)) {
  533. db_query(
  534. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  535. ."VALUES (%d, %d, %d)",
  536. $node->nid,
  537. $node->vid,
  538. $node->stock_id
  539. );
  540. }
  541. return $node;
  542. }
  543. // create dbxref
  544. if ( !empty($node->accession) ) {
  545. if ( !empty($node->database) ) {
  546. $values = array(
  547. 'db_id' => $node->database,
  548. 'accession' => $node->accession,
  549. );
  550. if (!tripal_core_chado_select('dbxref', array(dbxref_id), $values)) {
  551. $values['description'] = $node->db_description;
  552. $values['version'] = '1';
  553. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  554. if (!$dbxref_status) {
  555. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  556. watchdog('tripal_stock',
  557. 'Insert Stock: Unable to create dbxref where values:%values',
  558. array('%values' => print_r($values, TRUE)),
  559. WATCHDOG_WARNING
  560. );
  561. }
  562. }
  563. else { $dbxref_status = 1; }
  564. }
  565. }
  566. // create stock
  567. if ($dbxref_status) {
  568. $values = array(
  569. 'dbxref_id' => array(
  570. 'db_id' => $node->database,
  571. 'accession' => $node->accession
  572. ),
  573. 'organism_id' => $node->organism_id,
  574. 'name' => $node->title,
  575. 'uniquename' => $node->uniquename,
  576. 'description' => $node->stock_description,
  577. 'type_id' => $node->type_id
  578. );
  579. $stock_status = tripal_core_chado_insert('stock', $values);
  580. }
  581. else {
  582. $values = array(
  583. 'organism_id' => $node->organism_id,
  584. 'name' => $node->title,
  585. 'uniquename' => $node->uniquename,
  586. 'description' => $node->stock_description,
  587. 'type_id' => $node->type_id
  588. );
  589. $stock_status = tripal_core_chado_insert('stock', $values);
  590. }
  591. // create drupal chado_stock entry
  592. if ($stock_status) {
  593. $values = array(
  594. 'organism_id' => $node->organism_id,
  595. 'uniquename' => $node->uniquename,
  596. 'type_id' => $node->type_id
  597. );
  598. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  599. if (!empty($chado_stock[0]->stock_id)) {
  600. db_query(
  601. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  602. ."VALUES (%d, %d, %d)",
  603. $node->nid,
  604. $node->vid,
  605. $chado_stock[0]->stock_id
  606. );
  607. //Move on to next stage of Stock Creation based on next_stage_path field
  608. if ($node->simulate_multipart) {
  609. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  610. $_REQUEST['destination'] = $next_stage_path;
  611. }
  612. }
  613. else {
  614. drupal_set_message(t('Error during stock creation.'), 'error');
  615. watchdog('tripal_stock',
  616. 'Insert Stock: Unable to find newly created stock where values:%values',
  617. array('%values' => print_r($values, TRUE)),
  618. WATCHDOG_ERROR
  619. );
  620. return FALSE;
  621. }
  622. }
  623. else {
  624. drupal_set_message(t('Error during stock creation.'), 'error');
  625. watchdog('tripal_stock',
  626. 'Insert Stock: Unable to create stock where values:%values',
  627. array('%values' => print_r($values, TRUE)),
  628. WATCHDOG_WARNING
  629. );
  630. return FALSE;
  631. }
  632. }
  633. /**
  634. * Implements hook_update(): Handles Editing/Updating of main stock info
  635. *
  636. * NOTE: Currently just writes over all old data
  637. *
  638. * @param $node
  639. * The current node including fields with the form element names and submitted values
  640. *
  641. * @return
  642. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  643. *
  644. * @ingroup tripal_stock
  645. */
  646. function chado_stock_update($node) {
  647. if ($node->revision) {
  648. // there is no way to handle revisions in Chado but leave
  649. // this here just to make not we've addressed it.
  650. }
  651. // if ($node->revision) {
  652. // chado_stock_insert($node);
  653. // }
  654. // else {
  655. //update dbxref
  656. if ($node->database) {
  657. if ($node->accession) {
  658. $dbxref_mode = '';
  659. $stock = tripal_core_chado_select(
  660. 'stock',
  661. array('dbxref_id', 'type_id'),
  662. array('stock_id' => $node->stock_id)
  663. );
  664. if ($stock[0]->dbxref_id) {
  665. $values = array(
  666. 'db_id' => $node->database,
  667. 'accession' => $node->accession,
  668. 'description' => $node->db_description
  669. );
  670. $dbxref_status = tripal_core_chado_update(
  671. 'dbxref',
  672. array('dbxref_id' => $stock[0]->dbxref_id),
  673. $values
  674. );
  675. $dbxref_mode = 'Update';
  676. }
  677. else {
  678. if ($stock[0]->type_id) {
  679. //create the dbxref
  680. //used the type_id as a control to check we have a stock but not a dbxref
  681. $values = array(
  682. 'db_id' => $node->database,
  683. 'accession' => $node->accession,
  684. 'description' => $node->db_description,
  685. 'version' => '1',
  686. );
  687. $dbxref_status = tripal_core_chado_insert(
  688. 'dbxref',
  689. $values
  690. );
  691. $dbxref_mode = 'Create';
  692. }
  693. else {
  694. drupal_set_message(t('Unable to find stock to Update'), 'error');
  695. watchdog(
  696. 'tripal_stock',
  697. 'Stock Update: Unable to find stock to update using values: %values',
  698. array('%values', print_r($values, TRUE)),
  699. WATCHDOG_ERROR
  700. );
  701. return FALSE;
  702. }
  703. }
  704. }
  705. }
  706. if (!$dbxref_status) {
  707. watchdog(
  708. 'tripal_stock',
  709. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  710. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  711. WATCHDOG_WARNING
  712. );
  713. }
  714. //can't change stock id which is all thats stored in drupal thus only update chado
  715. $update_values = array(
  716. 'organism_id' => $node->organism_id,
  717. 'name' => $node->title,
  718. 'uniquename' => $node->uniquename,
  719. 'description' => $node->stock_description,
  720. 'type_id' => $node->type_id,
  721. );
  722. if ($dbxref_status) {
  723. $update_values['dbxref_id'] = array(
  724. 'db_id' => $node->database,
  725. 'accession' => $node->accession
  726. );
  727. }
  728. $status = tripal_core_chado_update(
  729. 'stock',
  730. array('stock_id' => $node->stock_id),
  731. $update_values
  732. );
  733. if (!$status) {
  734. drupal_set_message(t('Unable to update stock'), 'error');
  735. watchdog(
  736. 'tripal_stock',
  737. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  738. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  739. WATCHDOG_ERROR
  740. );
  741. }
  742. }
  743. /**
  744. * Implements hook_delete(): Handles deleting of chado_stocks
  745. *
  746. * NOTE: Currently deletes data -no undo or record-keeping functionality
  747. *
  748. * @param $node
  749. * The current node including fields with the form element names and submitted values
  750. *
  751. * @return
  752. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  753. *
  754. * @ingroup tripal_stock
  755. */
  756. function chado_stock_delete($node) {
  757. // Set stock in chado: is_obsolete = TRUE
  758. chado_query(
  759. "DELETE FROM {stock} WHERE stock_id=%d",
  760. $node->stock->stock_id
  761. );
  762. //remove drupal node and all revisions
  763. db_query(
  764. "DELETE FROM {chado_stock} WHERE nid=%d",
  765. $node->nid
  766. );
  767. }
  768. /**
  769. * Purpose: Implement Blocks relating to stock content
  770. *
  771. * @param $op
  772. * What kind of information to retrieve about the block or blocks.
  773. * Possible values include list, configure, save, view.
  774. * @param $delta
  775. * Which block to return (not applicable if $op is 'list').
  776. * @param $edit
  777. * If $op is 'save', the submitted form data from the configuration form.
  778. *
  779. * @return
  780. * One of the following depending on $op: An array of block descriptions (list), the configuration
  781. * form (configure), nothing (save), an array defining subject and content for the block indexed
  782. * by $delta (view)
  783. *
  784. * @ingroup tripal_stock
  785. */
  786. function tripal_stock_block($op = 'list', $delta = 0, $edit=array()) {
  787. switch ($op) {
  788. case 'list':
  789. $blocks['base']['info'] = t('Tripal Stock Details');
  790. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  791. $blocks['properties']['info'] = t('Tripal Stock Properties');
  792. $blocks['properties']['cache'] = BLOCK_NO_CACHE;
  793. $blocks['references']['info'] = t('Tripal Stock References');
  794. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  795. $blocks['relationships_as_object']['info'] = t('Tripal Stock Relationships');
  796. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  797. $blocks['synonyms']['info'] = t('Tripal Stock Synonyms');
  798. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  799. $blocks['collections']['info'] = t('Tripal Stock Collections');
  800. $blocks['collections']['cache'] = BLOCK_NO_CACHE;
  801. $blocks['phenotypes']['info'] = t('Tripal Stock Phenotypes');
  802. $blocks['phenotypes']['cache'] = BLOCK_NO_CACHE;
  803. $blocks['genotypes']['info'] = t('Tripal Stock Genotypes');
  804. $blocks['genotypes']['cache'] = BLOCK_NO_CACHE;
  805. $blocks['locations']['info'] = t('Tripal Stock Locations');
  806. $blocks['locations']['cache'] = BLOCK_NO_CACHE;
  807. $blocks['orgstocks']['info'] = t('Tripal Organism Stocks');
  808. $blocks['orgstocks']['cache'] = BLOCK_NO_CACHE;
  809. return $blocks;
  810. case 'view':
  811. if (user_access('access chado_stock content') and arg(0) == 'node' and is_numeric(arg(1))) {
  812. $nid = arg(1);
  813. $node = node_load($nid);
  814. $block = array();
  815. switch ($delta) {
  816. case 'base':
  817. $block['subject'] = t('Stock Details');
  818. $block['content'] = theme('tripal_stock_base', $node);
  819. break;
  820. case 'properties':
  821. $block['subject'] = t('Properties');
  822. $block['content'] = theme('tripal_stock_properties', $node);
  823. break;
  824. case 'references':
  825. $block['subject'] = t('References');
  826. $block['content'] = theme('tripal_stock_references', $node);
  827. break;
  828. case 'relationships':
  829. $block['subject'] = t('Relationships');
  830. $block['content'] = theme('tripal_stock_relationships', $node);
  831. break;
  832. case 'synonyms':
  833. $block['subject'] = t('Synonyms');
  834. $block['content'] = theme('tripal_stock_synonyms', $node);
  835. break;
  836. case 'collections':
  837. $block['subject'] = t('Stock Collections');
  838. $block['content'] = theme('tripal_stock_collections', $node);
  839. break;
  840. case 'phenotypes':
  841. $block['subject'] = t('Stock Phenotypes');
  842. $block['content'] = theme('tripal_stock_phenotypes', $node);
  843. break;
  844. case 'genotypes':
  845. $block['subject'] = t('Stock Genotypes');
  846. $block['content'] = theme('tripal_stock_genotypes', $node);
  847. break;
  848. case 'locations':
  849. $block['subject'] = t('Stock Locations');
  850. $block['content'] = theme('tripal_stock_locations', $node);
  851. break;
  852. case 'orgstocks':
  853. $block['subject'] = t('Organism Stocks');
  854. $block['content'] = theme('tripal_organism_stocks', $node);
  855. break;
  856. }
  857. return $block;
  858. }
  859. }
  860. }
  861. /**
  862. *
  863. *
  864. * @ingroup tripal_stock
  865. */
  866. function tripal_stock_preprocess_tripal_stock_relationships(&$variables) {
  867. // we want to provide a new variable that contains the matched stocks.
  868. $stock = $variables['node']->stock;
  869. // normally we would use tripal_core_expand_chado_vars to expand our
  870. // organism object and add in the relationships, however whan a large
  871. // number of relationships are present this significantly slows the
  872. // query, therefore we will manually perform the query
  873. $sql = "
  874. SELECT S.name, S.uniquename, S.stock_id, CS.nid, CVT.name as rel_type, CVTs.name as obj_type
  875. FROM stock_relationship SR
  876. INNER JOIN stock S on SR.object_id = S.stock_id
  877. INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id
  878. INNER JOIN cvterm CVTs on S.type_id = CVTs.cvterm_id
  879. LEFT JOIN chado_stock CS on S.stock_id = CS.stock_id
  880. WHERE SR.subject_id = %d
  881. ";
  882. $as_subject = chado_query($sql, $stock->stock_id);
  883. $sql = "
  884. SELECT S.name, S.uniquename, S.stock_id, CS.nid, CVT.name as rel_type, CVTs.name as sub_type
  885. FROM stock_relationship SR
  886. INNER JOIN stock S on SR.subject_id = S.stock_id
  887. INNER JOIN cvterm CVT on SR.type_id = CVT.cvterm_id
  888. INNER JOIN cvterm CVTs on S.type_id = CVTs.cvterm_id
  889. LEFT JOIN chado_stock CS on S.stock_id = CS.stock_id
  890. WHERE SR.object_id = %d
  891. ";
  892. $as_object = chado_query($sql, $stock->stock_id);
  893. // combine both object and subject relationshisp into a single array
  894. $relationships = array();
  895. $relationships['object'] = array();
  896. $relationships['subject'] = array();
  897. // iterate through the object relationships
  898. while ($relationship = db_fetch_object($as_object)) {
  899. // get the relationship and child types
  900. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  901. $sub_type = t(preg_replace('/_/', " ", $relationship->sub_type));
  902. if (!array_key_exists($rel_type, $relationships['object'])) {
  903. $relationships['object'][$rel_type] = array();
  904. }
  905. if (!array_key_exists($sub_type, $relationships['object'][$rel_type])) {
  906. $relationships['object'][$rel_type][$sub_type] = array();
  907. }
  908. $relationships['object'][$rel_type][$sub_type][] = $relationship;
  909. }
  910. // now add in the subject relationships
  911. while ($relationship = db_fetch_object($as_subject)) {
  912. // get the relationship and child types
  913. $rel_type = t(preg_replace('/_/', " ", $relationship->rel_type));
  914. $obj_type = t(preg_replace('/_/', " ", $relationship->obj_type));
  915. if (!array_key_exists($rel_type, $relationships['subject'])) {
  916. $relationships['subject'][$rel_type] = array();
  917. }
  918. if (!array_key_exists($obj_type, $relationships['subject'][$rel_type])) {
  919. $relationships['subject'][$rel_type][$obj_type] = array();
  920. }
  921. $relationships['subject'][$rel_type][$obj_type][] = $relationship;
  922. }
  923. $stock->all_relationships = $relationships;
  924. }
  925. /**
  926. * Implementation of hook_nodeapi().
  927. * Display stock information for associated organisms
  928. * This function also provides contents for indexing
  929. *
  930. * @ingroup tripal_stock
  931. */
  932. function tripal_stock_nodeapi(&$node, $op, $teaser, $page) {
  933. switch ($op) {
  934. // Note that this function only adds stock view to an organism/feature
  935. // node.
  936. case 'view':
  937. // add the stock to the organism/feature search indexing
  938. if ($node->build_mode == NODE_BUILD_SEARCH_INDEX) {
  939. }
  940. elseif ($node->build_mode == NODE_BUILD_SEARCH_RESULT) {
  941. }
  942. else {
  943. switch ($node->type) {
  944. case 'chado_organism':
  945. // Show stock if the organism/feature is not at teaser view
  946. $node->content['tripal_organism_stocks'] = array(
  947. '#value' => theme('tripal_organism_stocks', $node),
  948. );
  949. break;
  950. }
  951. }
  952. break;
  953. }
  954. }
  955. /**
  956. *
  957. *
  958. * @ingroup tripal_stock
  959. */
  960. function tripal_stock_preprocess_tripal_organism_stocks(&$variables) {
  961. $organism = $variables['node']->organism;
  962. if (!$organism) {
  963. return;
  964. }
  965. // get the stocks for this organism
  966. $sql = "SELECT S.name, S.stock_id, S.uniquename, CVT.name as cvname ".
  967. "FROM {stock} S ".
  968. " INNER JOIN {cvterm} CVT on S.type_id = CVT.cvterm_id ".
  969. "WHERE organism_id = %s".
  970. "ORDER BY stock_id ASC";
  971. // the counting SQL
  972. $csql = "SELECT count(*) ".
  973. "FROM {stock} S".
  974. " INNER JOIN {cvterm} CVT on S.type_id = CVT.cvterm_id ".
  975. "WHERE organism_id = %s ".
  976. "GROUP BY organism_id ";
  977. $previous_db = tripal_db_set_active('chado'); // use chado database
  978. $org_stocks = pager_query($sql, 25, 0, $csql, $organism->organism_id);
  979. tripal_db_set_active($previous_db); // now use drupal database
  980. // get the pager and reformat so it shows the right block
  981. $pager = theme('pager');
  982. // prepare the query that will lookup node ids
  983. $sql = "SELECT nid FROM {chado_stock} ".
  984. "WHERE stock_id = %d";
  985. $i=0;
  986. $stocks = array();
  987. while ($stock = db_fetch_object($org_stocks)) {
  988. $node = db_fetch_object(db_query($sql, $stock->stock_id));
  989. $stock->nid = $node->nid;
  990. $stocks[$i++] = $stock;
  991. }
  992. $organism->stock_browser = array('stocks' => $stocks, 'pager' => $pager, 'enabled' => TRUE );
  993. }
  994. /**
  995. *
  996. *
  997. * @ingroup tripal_stock
  998. */
  999. function tripal_stock_preprocess_tripal_stock_base(&$variables) {
  1000. $stock = $variables['node']->stock;
  1001. $num_seq = db_result(chado_query("SELECT count(*) FROM feature_stock WHERE stock_id = %d", $stock->stock_id));
  1002. $stock->num_seq = $num_seq;
  1003. }