tripal_stock.module 40 KB

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