tripal_stock.module 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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("tripal_stock-administration.inc");
  23. require("tripal_stock.api.inc");
  24. require("other_module_api_functions.inc");
  25. require("tripal_stock-secondary_tables.inc");
  26. require("tripal_stock-properties.inc");
  27. require("tripal_stock-relationships.inc");
  28. require("tripal_stock-db_references.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. $previous_db = tripal_db_set_active('chado');
  468. $chado_row = db_fetch_object(db_query("SELECT * FROM {stock} WHERE uniquename='" . $node->uniquename . "'"));
  469. tripal_db_set_active($previous_db);
  470. if (!empty($chado_row->stock_id)) {
  471. $drupal_row = db_fetch_object(db_query("SELECT * FROM {chado_stock} WHERE stock_id=" . $chado_row->stock_id));
  472. if (!empty($drupal_row->nid)) {
  473. $link = l('node/' . $drupal_row->nid, $node->uniquename);
  474. form_set_error('uniquename', "There is already a stock with that uniquename $link. Please enter another uniquename.");
  475. }
  476. else {
  477. form_set_error('uniquename', "There is already a stock with that uniquename (although it's not sync'd with drupal). Please enter another uniquename.");
  478. }
  479. }
  480. }
  481. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  482. if ( $node->type_id == 0) {
  483. form_set_error('type_id', 'Please select a type of stock.');
  484. }
  485. else {
  486. $previous_db = tripal_db_set_active('chado');
  487. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'cvterm', 'cvterm_id', $node->type_id));
  488. tripal_db_set_active($previous_db);
  489. if ( $num_rows->count != 1) {
  490. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  491. }
  492. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  493. if ( $node->organism_id == 0) {
  494. form_set_error('organism_id', 'Please select a source organism for this stock');
  495. }
  496. else {
  497. $previous_db = tripal_db_set_active('chado');
  498. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'organism', 'organism_id', $node->organism_id));
  499. tripal_db_set_active($previous_db);
  500. if ( $num_rows->count != 1 ) {
  501. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  502. }
  503. // Check if Accession also database
  504. if ($node->accession != '') {
  505. if ($node->database == 0) {
  506. // there is an accession but no database selected
  507. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  508. }
  509. }
  510. else {
  511. if ($node->database > 0) {
  512. // there is a database selected but no accession
  513. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  514. }
  515. }
  516. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  517. if ( $node->database > 0) {
  518. $previous_db = tripal_db_set_active('chado');
  519. $num_rows = db_fetch_object(db_query($int_in_chado_sql, 'db', 'db_id', $node->database));
  520. tripal_db_set_active($previous_db);
  521. if ($num_rows->count != 1) {
  522. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  523. }
  524. }
  525. /**
  526. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  527. *
  528. * @param $node
  529. * The current node including fields with the form element names and submitted values
  530. *
  531. * @return
  532. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  533. *
  534. * @ingroup tripal_stock
  535. */
  536. function chado_stock_insert($node) {
  537. //If the chado stock exists
  538. // then don't create but simply link to node
  539. if ($node->chado_stock_exists) {
  540. if (!empty($node->stock_id)) {
  541. db_query(
  542. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  543. ."VALUES (%d, %d, %d)",
  544. $node->nid,
  545. $node->vid,
  546. $node->stock_id
  547. );
  548. }
  549. return $node;
  550. }
  551. // create dbxref
  552. if ( !empty($node->accession) ) {
  553. if ( !empty($node->database) ) {
  554. $values = array(
  555. 'db_id' => $node->database,
  556. 'accession' => $node->accession,
  557. );
  558. if (!tripal_core_chado_select('dbxref', array(dbxref_id), $values)) {
  559. $values['description'] = $node->db_description;
  560. $values['version'] = '1';
  561. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  562. if (!$dbxref_status) {
  563. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  564. watchdog('tripal_stock',
  565. 'Insert Stock: Unable to create dbxref where values:%values',
  566. array('%values' => print_r($values, TRUE)),
  567. WATCHDOG_WARNING
  568. );
  569. }
  570. }
  571. else { $dbxref_status = 1; }
  572. }
  573. }
  574. // create stock
  575. if ($dbxref_status) {
  576. $values = array(
  577. 'dbxref_id' => array(
  578. 'db_id' => $node->database,
  579. 'accession' => $node->accession
  580. ),
  581. 'organism_id' => $node->organism_id,
  582. 'name' => $node->title,
  583. 'uniquename' => $node->uniquename,
  584. 'description' => $node->stock_description,
  585. 'type_id' => $node->type_id
  586. );
  587. $stock_status = tripal_core_chado_insert('stock', $values);
  588. }
  589. else {
  590. $values = array(
  591. 'organism_id' => $node->organism_id,
  592. 'name' => $node->title,
  593. 'uniquename' => $node->uniquename,
  594. 'description' => $node->stock_description,
  595. 'type_id' => $node->type_id
  596. );
  597. $stock_status = tripal_core_chado_insert('stock', $values);
  598. }
  599. // create drupal chado_stock entry
  600. if ($stock_status) {
  601. $values = array(
  602. 'organism_id' => $node->organism_id,
  603. 'uniquename' => $node->uniquename,
  604. 'type_id' => $node->type_id
  605. );
  606. $chado_stock = tripal_core_chado_select('stock', array('stock_id'), $values);
  607. if (!empty($chado_stock[0]->stock_id)) {
  608. db_query(
  609. "INSERT INTO {chado_stock} (nid, vid, stock_id) "
  610. ."VALUES (%d, %d, %d)",
  611. $node->nid,
  612. $node->vid,
  613. $chado_stock[0]->stock_id
  614. );
  615. //Move on to next stage of Stock Creation based on next_stage_path field
  616. if ($node->simulate_multipart) {
  617. $next_stage_path = preg_replace('/%node/', $node->nid, $node->next_step_path);
  618. $_REQUEST['destination'] = $next_stage_path;
  619. }
  620. }
  621. else {
  622. drupal_set_message(t('Error during stock creation.'), 'error');
  623. watchdog('tripal_stock',
  624. 'Insert Stock: Unable to find newly created stock where values:%values',
  625. array('%values' => print_r($values, TRUE)),
  626. WATCHDOG_ERROR
  627. );
  628. return FALSE;
  629. }
  630. }
  631. else {
  632. drupal_set_message(t('Error during stock creation.'), 'error');
  633. watchdog('tripal_stock',
  634. 'Insert Stock: Unable to create stock where values:%values',
  635. array('%values' => print_r($values, TRUE)),
  636. WATCHDOG_WARNING
  637. );
  638. return FALSE;
  639. }
  640. }
  641. /**
  642. * Implements hook_update(): Handles Editing/Updating of main stock info
  643. *
  644. * NOTE: Currently just writes over all old data
  645. *
  646. * @param $node
  647. * The current node including fields with the form element names and submitted values
  648. *
  649. * @return
  650. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  651. *
  652. * @ingroup tripal_stock
  653. */
  654. function chado_stock_update($node) {
  655. if ($node->revision) {
  656. chado_stock_insert($node);
  657. }
  658. else {
  659. //update dbxref
  660. if ($node->database) {
  661. if ($node->accession) {
  662. $dbxref_mode = '';
  663. $stock = tripal_core_chado_select(
  664. 'stock',
  665. array('dbxref_id', 'type_id'),
  666. array('stock_id' => $node->stock_id)
  667. );
  668. if ($stock[0]->dbxref_id) {
  669. $values = array(
  670. 'db_id' => $node->database,
  671. 'accession' => $node->accession,
  672. 'description' => $node->db_description
  673. );
  674. $dbxref_status = tripal_core_chado_update(
  675. 'dbxref',
  676. array('dbxref_id' => $stock[0]->dbxref_id),
  677. $values
  678. );
  679. $dbxref_mode = 'Update';
  680. }
  681. else {
  682. if ($stock[0]->type_id) {
  683. //create the dbxref
  684. //used the type_id as a control to check we have a stock but not a dbxref
  685. $values = array(
  686. 'db_id' => $node->database,
  687. 'accession' => $node->accession,
  688. 'description' => $node->db_description,
  689. 'version' => '1',
  690. );
  691. $dbxref_status = tripal_core_chado_insert(
  692. 'dbxref',
  693. $values
  694. );
  695. $dbxref_mode = 'Create';
  696. }
  697. else {
  698. drupal_set_message(t('Unable to find stock to Update'), 'error');
  699. watchdog(
  700. 'tripal_stock',
  701. 'Stock Update: Unable to find stock to update using values: %values',
  702. array('%values', print_r($values, TRUE)),
  703. WATCHDOG_ERROR
  704. );
  705. return FALSE;
  706. }
  707. }
  708. }
  709. }
  710. if (!$dbxref_status) {
  711. watchdog(
  712. 'tripal_stock',
  713. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  714. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  715. WATCHDOG_WARNING
  716. );
  717. }
  718. //can't change stock id which is all thats stored in drupal thus only update chado
  719. $update_values = array(
  720. 'organism_id' => $node->organism_id,
  721. 'name' => $node->title,
  722. 'uniquename' => $node->uniquename,
  723. 'description' => $node->stock_description,
  724. 'type_id' => $node->type_id,
  725. );
  726. if ($dbxref_status) {
  727. $update_values['dbxref_id'] = array(
  728. 'db_id' => $node->database,
  729. 'accession' => $node->accession
  730. );
  731. }
  732. $status = tripal_core_chado_update(
  733. 'stock',
  734. array('stock_id' => $node->stock_id),
  735. $update_values
  736. );
  737. if (!$status) {
  738. drupal_set_message(t('Unable to update stock'), 'error');
  739. watchdog(
  740. 'tripal_stock',
  741. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  742. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  743. WATCHDOG_ERROR
  744. );
  745. }
  746. }
  747. }
  748. /**
  749. * Implements hook_delete(): Handles deleting of chado_stocks
  750. *
  751. * NOTE: Currently deletes data -no undo or record-keeping functionality
  752. *
  753. * @param $node
  754. * The current node including fields with the form element names and submitted values
  755. *
  756. * @return
  757. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  758. *
  759. * @ingroup tripal_stock
  760. */
  761. function chado_stock_delete($node) {
  762. // Set stock in chado: is_obsolete = TRUE
  763. $previous_db = tripal_db_set_active('chado');
  764. db_query(
  765. "DELETE FROM {stock} WHERE stock_id=%d",
  766. $node->stock->stock_id
  767. );
  768. tripal_db_set_active($previous_db);
  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. }