tripal_stock.chado_node.inc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. <?php
  2. /**
  3. * @file Stock Node Functionality
  4. */
  5. /**
  6. * Implements hook_node_info(): registers a stock node type
  7. *
  8. * @return
  9. * An array describing various details of the node
  10. *
  11. * @ingroup tripal_stock
  12. */
  13. function tripal_stock_node_info() {
  14. return array(
  15. 'chado_stock' => array(
  16. 'name' => t('Stock'),
  17. 'base' => 'chado_stock',
  18. 'description' => t('A Chado Stock is a collection of material that can be sampled and have experiments performed on it.'),
  19. 'has_title' => TRUE,
  20. 'has_body' => FALSE,
  21. 'chado_node_api' => array(
  22. 'base_table' => 'stock',
  23. 'hook_prefix' => 'chado_stock',
  24. 'record_type_title' => array(
  25. 'singular' => t('Stock'),
  26. 'plural' => t('Stocks')
  27. ),
  28. 'sync_filters' => array(
  29. 'type_id' => TRUE,
  30. 'organism_id' => TRUE
  31. ),
  32. )
  33. ),
  34. );
  35. }
  36. /**
  37. * Implements hook_load(): Prepares the chado_stock node
  38. *
  39. * @param $node
  40. * The basic node containing all variables common to all nodes
  41. *
  42. * @return
  43. * A stock node containing all the variables from the basic node and all stock specific variables
  44. *
  45. * D7 @todo: Make optimizations to take advantage of $nodes
  46. *
  47. * @ingroup tripal_stock
  48. */
  49. function chado_stock_load($nodes) {
  50. $new_nodes = array();
  51. foreach ($nodes as $nid => $node) {
  52. // get the stock details from chado
  53. $stock_id = chado_get_id_for_node('stock', $node->nid);
  54. if (empty($stock_id)) {
  55. tripal_core_report_error(
  56. 'tripal_stock',
  57. TRIPAL_ERROR,
  58. 'Unable to retrieve stock_id for %title (NID = %nid).',
  59. array('%title' => $node->title, '%nid' => $node->nid)
  60. );
  61. }
  62. else {
  63. // build the variable with all the stock details
  64. $values = array('stock_id' => $stock_id);
  65. $stock = tripal_core_generate_chado_var('stock', $values);
  66. // by default, the titles are saved using the unique constraint. We will
  67. // keep it the same, but remove the duplicate name if the unique name and name
  68. // are identical
  69. $title_type = variable_get('chado_stock_title', 'unique_constraint');
  70. if($title_type == 'unique_constraint') {
  71. if (strcmp($stock->name, $stock->uniquename)==0) {
  72. $node->title = $stock->name . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  73. }
  74. // in previous version of Tripal, the stock title was simply the unique name.
  75. // so, we recreate the title just to be sure all of our stock pages are consistent
  76. else {
  77. $node->title = $stock->name . ", " . $stock->uniquename . " (" . $stock->type_id->name . ") " . $stock->organism_id->genus . " " . $stock->organism_id->species ;
  78. }
  79. }
  80. // set the title to be the stock name or uniquename as configured
  81. if($title_type == 'stock_name') {
  82. $node->title = $stock->name;
  83. }
  84. if($title_type == 'stock_unique_name') {
  85. $node->title = $stock->uniquename;
  86. }
  87. // add this to the node
  88. $node->stock = $stock;
  89. }
  90. $new_nodes[$nid] = $node;
  91. }
  92. return $new_nodes;
  93. }
  94. /**
  95. * Implements hook_form(): Creates the main Add/Edit/Delete Form for chado stocks
  96. *
  97. * Parts to be added by this form
  98. * name,
  99. * uniquename,
  100. * description,
  101. * type => select from cvterm with key cvterm_id,
  102. * organism => select from available with key organism_id
  103. * main_db_reference => accession, version, description, db_name(select from dropdown)
  104. *
  105. * @param $node
  106. * An empty node object on insert OR the current stock node object on update
  107. * @param $form_state
  108. * The current state of the form
  109. *
  110. * @return
  111. * A description of the form to be rendered by drupal_get_form()
  112. *
  113. * @ingroup tripal_stock
  114. */
  115. function chado_stock_form($node, $form_state) {
  116. // If existing stock then expand all fields needed using the chado API
  117. if (isset($node->nid)) {
  118. $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');
  119. foreach ($fields_needed as $field_name) {
  120. // Check to see if it's excluded and expand it if so
  121. if (isset($node->expandable_fields)) {
  122. if (in_array($field_name, $node->expandable_fields)) {
  123. $node = tripal_core_expand_chado_vars($node, 'field', $field_name);
  124. }
  125. }
  126. }
  127. }
  128. // Default values can come in the following ways:
  129. //
  130. // 1) as elements of the $node object. This occurs when editing an existing stock
  131. // 2) in the $form_state['values'] array which occurs on a failed validation or
  132. // ajax callbacks from non submit form elements
  133. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  134. // form elements and the form is being rebuilt
  135. //
  136. // set form field defaults
  137. $sname = '';
  138. $uniquename = '';
  139. $stock_id = 0;
  140. $type_id = 0;
  141. $organism_id = 0;
  142. $sdescription = '';
  143. $dbxref_accession = '';
  144. $dbxref_description = '';
  145. $dbxref_database = 0;
  146. // 1) if we are editing an existing node then the stock is already part of the node
  147. if (property_exists($node, 'stock')) {
  148. $sname = $node->stock->name;
  149. $uniquename = $node->stock->uniquename;
  150. $stock_id = $node->stock->stock_id;
  151. $type_id = $node->stock->type_id->cvterm_id;
  152. $organism_id = $node->stock->organism_id->organism_id;
  153. $sdescription = $node->stock->description;
  154. $dbxref_accession = $node->stock->dbxref_id->accession;
  155. $dbxref_description = $node->stock->dbxref_id->description;
  156. $dbxref_database = $node->stock->dbxref_id->db_id->db_id;
  157. }
  158. // 2) if we are re constructing the form from a failed validation or ajax callback
  159. // then use the $form_state['values'] values
  160. if (array_key_exists('values', $form_state)) {
  161. $sname = $form_state['values']['sname'];
  162. $uniquename = $form_state['values']['uniquename'];
  163. $stock_id = $form_state['values']['stock_id'];
  164. $type_id = $form_state['values']['type_id'];
  165. $organism_id = $form_state['values']['organism_id'];
  166. $sdescription = $form_state['values']['description'];
  167. $dbxref_accession = $form_state['values']['accession'];
  168. $dbxref_description = $form_state['values']['db_description'];
  169. $dbxref_database = $form_state['values']['database'];
  170. }
  171. // 3) if we are re building the form from after submission (from ajax call) then
  172. // the values are in the $form_state['input'] array
  173. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  174. $sname = $form_state['input']['sname'];
  175. $uniquename = $form_state['input']['uniquename'];
  176. $stock_id = $form_state['input']['stock_id'];
  177. $type_id = $form_state['input']['type_id'];
  178. $organism_id = $form_state['input']['organism_id'];
  179. $sdescription = $form_state['input']['description'];
  180. $dbxref_accession = $form_state['input']['accession'];
  181. $dbxref_description = $form_state['input']['db_description'];
  182. $dbxref_database = $form_state['input']['database'];
  183. }
  184. $form['names'] = array(
  185. '#type' => 'fieldset',
  186. '#title' => t('Stock Name')
  187. );
  188. $form['names']['sname'] = array(
  189. '#type' => 'textfield',
  190. '#title' => t('Name'),
  191. '#default_value' => $sname,
  192. '#required' => TRUE
  193. );
  194. $form['names']['uniquename'] = array(
  195. '#type' => 'textfield',
  196. '#title' => t('Unique Name'),
  197. '#default_value' => $uniquename,
  198. '#required' => TRUE
  199. );
  200. $form['names']['stock_id'] = array(
  201. '#type' => 'hidden',
  202. '#value' => $stock_id,
  203. );
  204. $form['details'] = array(
  205. '#type' => 'fieldset',
  206. '#title' => t('Stock Details')
  207. );
  208. $type_options = tripal_cv_get_cvterm_options( variable_get('chado_stock_types_cv', 'NULL') );
  209. $type_options[0] = 'Select a Type';
  210. $form['details']['type_id'] = array(
  211. '#type' => 'select',
  212. '#title' => t('Type of Stock'),
  213. '#options' => $type_options,
  214. '#default_value' => $type_id,
  215. '#required' => TRUE,
  216. );
  217. // get the list of organisms
  218. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  219. $org_rset = chado_query($sql);
  220. $organisms = array();
  221. $organisms[''] = '';
  222. while ($organism = $org_rset->fetchObject()) {
  223. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  224. }
  225. $form['details']['organism_id'] = array(
  226. '#type' => 'select',
  227. '#title' => t('Source Organism for stock'),
  228. '#default_value' => $organism_id,
  229. '#options' => $organisms,
  230. '#required' => TRUE
  231. );
  232. $form['details']['stock_description'] = array(
  233. '#type' => 'textarea',
  234. '#title' => t('Notes'),
  235. '#default_value' => $sdescription,
  236. '#description' => t('Briefly enter any notes on the above stock. This should not include phenotypes or genotypes.'),
  237. );
  238. $form['database_reference'] = array(
  239. '#type' => 'fieldset',
  240. '#title' => t('Stock Database Reference')
  241. );
  242. $form['database_reference']['accession'] = array(
  243. '#type' => 'textfield',
  244. '#title' => t('Accession'),
  245. '#default_value' => $dbxref_accession,
  246. );
  247. $form['database_reference']['db_description'] = array(
  248. '#type' => 'textarea',
  249. '#title' => t('Description of Database Reference'),
  250. '#default_value' => $dbxref_description,
  251. '#description' => t('Optionally enter a description about the database accession.')
  252. );
  253. $db_options = tripal_db_get_db_options();
  254. $db_options[0] = 'Select a Database';
  255. $form['database_reference']['database'] = array(
  256. '#type' => 'select',
  257. '#title' => t('Database'),
  258. '#options' => $db_options,
  259. '#default_value' => $dbxref_database
  260. );
  261. // PROPERTIES FORM
  262. //---------------------------------------------
  263. $ontology = variable_get('chado_stock_prop_types_cv', FALSE);
  264. if (!$ontology) {
  265. tripal_core_report_error(
  266. 'chado_stock',
  267. TRIPAL_ERROR,
  268. 'Stock property type controlled vocabulary is not set. please set it in the Stock configuration form.'
  269. );
  270. tripal_core_report_error(
  271. 'chado_stock',
  272. TRIPAL_ERROR,
  273. 'Unable to add properties form to the node form because stock property type controlled vocabulary is not set'
  274. );
  275. }
  276. else {
  277. // ontology name needed for properties api
  278. $cv = tripal_core_chado_select('cv',array('name'),array('cv_id' => $ontology));
  279. $ontology_name = $cv[0]->name;
  280. // get the stockprop types for the property drop-down
  281. $properties = array();
  282. $properties[] = 'Select a Property';
  283. $sql = "
  284. SELECT DISTINCT CVT.cvterm_id, CVT.name, CVT.definition
  285. FROM {cvterm} CVT
  286. INNER JOIN {cv} CV ON CVT.cv_id = CV.cv_id
  287. WHERE
  288. CV.name = :ontology AND
  289. NOT CVT.is_obsolete = 1
  290. ORDER BY CVT.name ASC
  291. ";
  292. $prop_types = chado_query($sql, array(':ontology' => $ontology_name));
  293. while ($prop = $prop_types->fetchObject()) {
  294. $properties[$prop->cvterm_id] = $prop->name;
  295. }
  296. $exclude = array();
  297. $include = array();
  298. $instructions = t('To add additional properties to the drop down. ' . l("Add terms to the ".$ontology_name." vocabulary", "admin/tripal/chado/tripal_cv/cvterm/add") . ".");
  299. tripal_core_properties_form($form, $form_state, 'stockprop', 'stock_id', $ontology_name,
  300. $properties, $stock_id, $exclude, $include, $instructions, 'Properties');
  301. }
  302. return $form;
  303. }
  304. /**
  305. * Implements hook_validate(): Validate the input from the chado_stock node form
  306. *
  307. * @param $node
  308. * The current node including fields with the form element names and submitted values
  309. * @param $form
  310. * A description of the form to be rendered by drupal_get_form()
  311. *
  312. * @ingroup tripal_stock
  313. */
  314. function chado_stock_validate(&$node, $form, &$form_state) {
  315. // remove surrounding whitespace
  316. $node->uniquename = trim($node->uniquename);
  317. $node->sname = trim($node->sname);
  318. $node->stock_description = trim($node->stock_description);
  319. $node->accession = trim($node->accession);
  320. $node->db_description = trim($node->db_description);
  321. // if this is a delete then don't validate
  322. if($node->op == 'Delete') {
  323. return;
  324. }
  325. // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  326. // need to validate during syncing so just skip it.
  327. if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
  328. return;
  329. }
  330. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  331. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  332. // if this is an update, we want to make sure that a different stock for
  333. // the organism doesn't already have this uniquename. We don't want to give
  334. // two sequences the same uniquename
  335. if (property_exists($node, 'nid')) {
  336. $sql = "
  337. SELECT *
  338. FROM {stock} S
  339. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  340. WHERE
  341. uniquename = :uname AND organism_id = :organism_id AND
  342. CVT.name = :cvtname AND NOT stock_id = :stock_id
  343. ";
  344. $result = chado_query($sql, array(':uname' => $node->uniquename,
  345. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  346. ':stock_id' => $node->stock_id))->fetchObject();
  347. if ($result) {
  348. 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."));
  349. }
  350. }
  351. // if this is an insert then we just need to make sure this name doesn't
  352. // already exist for this organism if it does then we need to throw an error
  353. else {
  354. $sql = "
  355. SELECT *
  356. FROM {Stock} S
  357. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  358. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  359. $result = chado_query($sql, array(':uname' => $node->uniquename,
  360. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  361. if ($result) {
  362. 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."));
  363. }
  364. }
  365. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  366. if ($node->type_id == 0) {
  367. form_set_error('type_id', 'Please select a type of stock.');
  368. }
  369. else {
  370. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  371. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  372. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  373. if ( $num_rows->count != 1) {
  374. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  375. }
  376. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  377. if ( $node->organism_id == 0) {
  378. form_set_error('organism_id', 'Please select a source organism for this stock');
  379. }
  380. else {
  381. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  382. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  383. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  384. if ( $num_rows->count != 1 ) {
  385. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  386. }
  387. // Check if Accession also database
  388. if ($node->accession != '') {
  389. if ($node->database == 0) {
  390. // there is an accession but no database selected
  391. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  392. }
  393. }
  394. else {
  395. if ($node->database > 0) {
  396. // there is a database selected but no accession
  397. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  398. }
  399. }
  400. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  401. if ( $node->database > 0) {
  402. $replace = array(':table' => 'db', ':column' => 'db_id');
  403. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  404. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  405. if ($num_rows->count != 1) {
  406. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  407. }
  408. }
  409. /**
  410. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  411. *
  412. * @param $node
  413. * The current node including fields with the form element names and submitted values
  414. *
  415. * @return
  416. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  417. *
  418. * @ingroup tripal_stock
  419. */
  420. function chado_stock_insert($node) {
  421. // If the chado stock exists (e.g. this is only a syncing operation)
  422. // then don't create but simply link to node
  423. if (isset($node->chado_stock_exists)) {
  424. if ($node->chado_stock_exists) {
  425. if (!empty($node->stock_id)) {
  426. db_insert('chado_stock')->fields(array(
  427. 'nid' => $node->nid,
  428. 'vid' => $node->vid,
  429. 'stock_id' => $node->stock_id
  430. ));
  431. }
  432. return $node;
  433. }
  434. }
  435. // if there is an stock_id in the $node object then this must be a sync so
  436. // we can skip adding the stock to chado as it is already there, although
  437. // we do need to proceed with the rest of the insert
  438. if (!property_exists($node, 'stock_id')) {
  439. // before we can add the stock, we must add the dbxref if one has been
  440. // provided by the user.
  441. $dbxref_status = 0;
  442. if (!empty($node->accession) ) {
  443. if (!empty($node->database) ) {
  444. $values = array(
  445. 'db_id' => $node->database,
  446. 'accession' => $node->accession,
  447. );
  448. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  449. $values['description'] = $node->db_description;
  450. $values['version'] = '1';
  451. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  452. if (!$dbxref_status) {
  453. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  454. watchdog('tripal_stock',
  455. 'Insert Stock: Unable to create dbxref where values:%values',
  456. array('%values' => print_r($values, TRUE)),
  457. WATCHDOG_WARNING
  458. );
  459. }
  460. }
  461. else {
  462. $dbxref_status = 1;
  463. }
  464. }
  465. }
  466. // create stock including the dbxref
  467. $stock = '';
  468. if ($dbxref_status) {
  469. $values = array(
  470. 'dbxref_id' => array(
  471. 'db_id' => $node->database,
  472. 'accession' => $node->accession
  473. ),
  474. 'organism_id' => $node->organism_id,
  475. 'name' => $node->sname,
  476. 'uniquename' => $node->uniquename,
  477. 'description' => $node->stock_description,
  478. 'type_id' => $node->type_id
  479. );
  480. $stock = tripal_core_chado_insert('stock', $values);
  481. }
  482. // create a stock without a dbxref
  483. else {
  484. $values = array(
  485. 'organism_id' => $node->organism_id,
  486. 'name' => $node->sname,
  487. 'uniquename' => $node->uniquename,
  488. 'description' => $node->stock_description,
  489. 'type_id' => $node->type_id
  490. );
  491. $stock = tripal_core_chado_insert('stock', $values);
  492. }
  493. if (is_array($stock)) {
  494. $stock_added = TRUE;
  495. }
  496. else {
  497. $stock_added = FALSE;
  498. }
  499. // Now add properties
  500. if ($stock_added) {
  501. $ontology = variable_get('chado_stock_prop_types_cv', FALSE);
  502. $cv = tripal_core_chado_select('cv',array('name'),array('cv_id' => $ontology));
  503. $ontology_name = $cv[0]->name;
  504. $properties = tripal_core_properties_form_retreive($node, $ontology_name);
  505. foreach ($properties as $property => $elements) {
  506. foreach ($elements as $rank => $value) {
  507. $success = tripal_core_insert_property(
  508. 'stock',
  509. $stock_id,
  510. $property,
  511. $ontology_name,
  512. $value
  513. );
  514. if (!$success) {
  515. watchdog(
  516. 'tripal_stock',
  517. 'Stock Insert: Unable to insert property %cvterm %value.',
  518. array('%cvterm' => $property, '%value' => $value),
  519. WATCHDOG_ERROR
  520. );
  521. }
  522. }
  523. }
  524. }
  525. } //end of adding stock to chado
  526. else {
  527. // stock already exists since this is a sync
  528. $stock_added = TRUE;
  529. $stock['stock_id'] = $node->stock_id;
  530. }
  531. // if the stock creation was succesful then add the URL and the entry in the
  532. // chado_stock table
  533. if ($stock_added) {
  534. // add the entry to the chado_stock table
  535. db_insert('chado_stock')->fields(array(
  536. 'nid' => (int) $node->nid,
  537. 'vid' => (int) $node->vid,
  538. 'stock_id' => (int) $stock['stock_id']
  539. ))->execute();
  540. }
  541. else {
  542. drupal_set_message(t('Error during stock creation.'), 'error');
  543. watchdog('tripal_stock',
  544. 'Insert Stock: Unable to create stock where values:%values',
  545. array('%values' => print_r($values, TRUE)),
  546. WATCHDOG_WARNING
  547. );
  548. return FALSE;
  549. }
  550. }
  551. /**
  552. * Implements hook_update(): Handles Editing/Updating of main stock info
  553. *
  554. * NOTE: Currently just writes over all old data
  555. *
  556. * @param $node
  557. * The current node including fields with the form element names and submitted values
  558. *
  559. * @return
  560. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  561. *
  562. * @ingroup tripal_stock
  563. */
  564. function chado_stock_update($node) {
  565. if ($node->revision) {
  566. // there is no way to handle revisions in Chado but leave
  567. // this here just to make not we've addressed it.
  568. }
  569. //update dbxref
  570. if ($node->database) {
  571. if ($node->accession) {
  572. $dbxref_mode = '';
  573. $stock = tripal_core_chado_select(
  574. 'stock',
  575. array('dbxref_id', 'type_id'),
  576. array('stock_id' => $node->stock_id)
  577. );
  578. if ($stock[0]->dbxref_id) {
  579. $values = array(
  580. 'db_id' => $node->database,
  581. 'accession' => $node->accession,
  582. 'description' => $node->db_description
  583. );
  584. $dbxref_status = tripal_core_chado_update(
  585. 'dbxref',
  586. array('dbxref_id' => $stock[0]->dbxref_id),
  587. $values
  588. );
  589. $dbxref_mode = 'Update';
  590. }
  591. else {
  592. if ($stock[0]->type_id) {
  593. //create the dbxref
  594. //used the type_id as a control to check we have a stock but not a dbxref
  595. $values = array(
  596. 'db_id' => $node->database,
  597. 'accession' => $node->accession,
  598. 'description' => $node->db_description,
  599. 'version' => '1',
  600. );
  601. $dbxref_status = tripal_core_chado_insert(
  602. 'dbxref',
  603. $values
  604. );
  605. $dbxref_mode = 'Create';
  606. }
  607. else {
  608. drupal_set_message(t('Unable to find stock to Update'), 'error');
  609. watchdog(
  610. 'tripal_stock',
  611. 'Stock Update: Unable to find stock to update using values: %values',
  612. array('%values', print_r($values, TRUE)),
  613. WATCHDOG_ERROR
  614. );
  615. return FALSE;
  616. }
  617. }
  618. }
  619. }
  620. if (!$dbxref_status) {
  621. watchdog(
  622. 'tripal_stock',
  623. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  624. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  625. WATCHDOG_WARNING
  626. );
  627. }
  628. //can't change stock id which is all thats stored in drupal thus only update chado
  629. $update_values = array(
  630. 'organism_id' => $node->organism_id,
  631. 'name' => $node->sname,
  632. 'uniquename' => $node->uniquename,
  633. 'description' => $node->stock_description,
  634. 'type_id' => $node->type_id,
  635. );
  636. if ($dbxref_status) {
  637. $update_values['dbxref_id'] = array(
  638. 'db_id' => $node->database,
  639. 'accession' => $node->accession
  640. );
  641. }
  642. $status = tripal_core_chado_update('stock', array('stock_id' => $node->stock_id), $update_values);
  643. if (!$status) {
  644. drupal_set_message(t('Unable to update stock'), 'error');
  645. watchdog(
  646. 'tripal_stock',
  647. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  648. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  649. WATCHDOG_ERROR
  650. );
  651. }
  652. else {
  653. // set the URL for this stock page
  654. $values = array('stock_id' => $node->stock_id);
  655. $stock = tripal_core_chado_select('stock', array('*'), $values);
  656. }
  657. // now update the properties
  658. if ($node->stock_id > 0) {
  659. $stock_id = $node->stock_id;
  660. $ontology = variable_get('chado_stock_prop_types_cv', FALSE);
  661. $cv = tripal_core_chado_select('cv',array('name'),array('cv_id' => $ontology));
  662. $ontology_name = $cv[0]->name;
  663. // now add in the properties by first removing any the stock
  664. // already has and adding the ones we have
  665. tripal_core_chado_delete('stockprop', array('stock_id' => $stock_id));
  666. $properties = tripal_core_properties_form_retreive($node, $ontology_name);
  667. foreach ($properties as $property => $elements) {
  668. foreach ($elements as $rank => $value) {
  669. $success = tripal_core_insert_property(
  670. 'stock',
  671. $stock_id,
  672. $property,
  673. $ontology_name,
  674. $value
  675. );
  676. if (!$success) {
  677. watchdog(
  678. 'tripal_stock',
  679. 'Stock Update: Unable to insert property %cvterm %value.',
  680. array('%cvterm' => $property, '%value' => $value),
  681. WATCHDOG_ERROR
  682. );
  683. }
  684. }
  685. }
  686. }
  687. }
  688. /**
  689. * Implements hook_delete(): Handles deleting of chado_stocks
  690. *
  691. * NOTE: Currently deletes data -no undo or record-keeping functionality
  692. *
  693. * @param $node
  694. * The current node including fields with the form element names and submitted values
  695. *
  696. * @return
  697. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  698. *
  699. * @ingroup tripal_stock
  700. */
  701. function chado_stock_delete($node) {
  702. // Set stock in chado: is_obsolete = TRUE
  703. chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
  704. //remove drupal node and all revisions
  705. db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
  706. }
  707. /**
  708. * Used by Tripal Chado Node API during sync'ing of nodes
  709. */
  710. function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
  711. $new_node->organism_id = $record->organism_id;
  712. $new_node->sname = $record->name;
  713. $new_node->uniquename = $record->uniquename;
  714. $new_node->type_id = $record->type_id;
  715. return $new_node;
  716. }
  717. /**
  718. * @ingroup tripal_stock
  719. */
  720. function tripal_stock_node_presave($node) {
  721. switch ($node->type) {
  722. case 'chado_stock':
  723. $values = array('organism_id' => $node->organism_id);
  724. $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
  725. $values = array('cvterm_id' => $node->type_id);
  726. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  727. $node->title = $node->sname . ', ' . $node->uniquename . ' (' . $cvterm[0]->name . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  728. break;
  729. }
  730. }
  731. /**
  732. * @ingroup tripal_stock
  733. */
  734. function tripal_stock_node_insert($node) {
  735. switch ($node->type) {
  736. case 'chado_stock':
  737. if (!$node->stock_id) {
  738. $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
  739. $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  740. $node->stock_id = $chado_stock->stock_id;
  741. }
  742. // remove any previous alias
  743. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  744. // set the URL for this stock page
  745. $url_alias = tripal_stock_get_stock_url($node);
  746. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  747. path_save($path_alias);
  748. break;
  749. }
  750. }
  751. /**
  752. * @ingroup tripal_stock
  753. */
  754. function tripal_stock_node_update($node) {
  755. switch ($node->type) {
  756. case 'chado_stock':
  757. // remove any previous alias
  758. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  759. // set the URL for this stock page
  760. $url_alias = tripal_stock_get_stock_url($node);
  761. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  762. path_save($path_alias);
  763. break;
  764. }
  765. }
  766. /**
  767. * @ingroup tripal_stock
  768. */
  769. function tripal_stock_node_view($node, $view_mode, $langcode) {
  770. switch ($node->type) {
  771. case 'chado_organism':
  772. if ($view_mode == 'full') {
  773. // Show stock if the organism/feature is not at teaser view
  774. $node->content['tripal_organism_stocks'] = array(
  775. '#value' => theme('tripal_organism_stocks', $node),
  776. );
  777. }
  778. break;
  779. case 'chado_stock':
  780. if ($view_mode == 'full') {
  781. $node->content['tripal_stock_base'] = array(
  782. '#value' => theme('tripal_stock_base', array('node' => $node)),
  783. );
  784. // Cross References
  785. $node->content['tripal_stock_references'] = array(
  786. '#value' => theme('tripal_stock_references', array('node' => $node)),
  787. );
  788. // Properties
  789. $node->content['tripal_stock_properties'] = array(
  790. '#value' => theme('tripal_stock_properties', array('node' => $node)),
  791. );
  792. // Publications
  793. $node->content['tripal_stock_publications'] = array(
  794. '#value' => theme('tripal_stock_publications', array('node' => $node)),
  795. );
  796. // Synonyms
  797. $node->content['tripal_stock_synonyms'] = array(
  798. '#value' => theme('tripal_stock_synonyms', array('node' => $node)),
  799. );
  800. // Relationships
  801. $node->content['tripal_stock_relationships'] = array(
  802. '#value' => theme('tripal_stock_relationships', array('node' => $node)),
  803. );
  804. // Stock Collections
  805. $node->content['tripal_stock_collections'] = array(
  806. '#value' => theme('tripal_stock_collections', array('node' => $node)),
  807. );
  808. // Stock Genotypes
  809. $node->content['tripal_stock_genotypes'] = array(
  810. '#value' => theme('tripal_stock_genotypes', array('node' => $node)),
  811. );
  812. }
  813. if ($view_mode == 'teaser') {
  814. $node->content['tripal_stock_teaser'] = array(
  815. '#value' => theme('tripal_stock_teaser', array('node' => $node)),
  816. );
  817. }
  818. break;
  819. }
  820. }