tripal_stock.module 35 KB

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