tripal_stock.chado_node.inc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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. 'title' => array(
  25. 'singular' => t('Stock'),
  26. 'plural' => t('Stocks')
  27. ),
  28. 'select_by' => 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. // ADDITIONAL DBXREFS FORM
  303. //---------------------------------------------
  304. $details = array(
  305. 'linking_table' => 'stock_dbxref',
  306. 'base_foreign_key' => 'stock_id',
  307. 'base_key_value' => $stock_id
  308. );
  309. tripal_core_additional_dbxrefs_form($form, $form_state, $details);
  310. return $form;
  311. }
  312. /**
  313. * Implements hook_validate(): Validate the input from the chado_stock node form
  314. *
  315. * @param $node
  316. * The current node including fields with the form element names and submitted values
  317. * @param $form
  318. * A description of the form to be rendered by drupal_get_form()
  319. *
  320. * @ingroup tripal_stock
  321. */
  322. function chado_stock_validate(&$node, $form, &$form_state) {
  323. // remove surrounding whitespace
  324. $node->uniquename = trim($node->uniquename);
  325. $node->sname = trim($node->sname);
  326. $node->stock_description = trim($node->stock_description);
  327. $node->accession = trim($node->accession);
  328. $node->db_description = trim($node->db_description);
  329. // if this is a delete then don't validate
  330. if($node->op == 'Delete') {
  331. return;
  332. }
  333. // we are syncing if we do not have a node ID but we do have a stock_id. We don't
  334. // need to validate during syncing so just skip it.
  335. if (is_null($node->nid) and property_exists($node, 'stock_id') and $node->stock_id != 0) {
  336. return;
  337. }
  338. $int_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  339. $string_in_chado_sql = "SELECT count(*) as count FROM {:table} WHERE :column = :value";
  340. // if this is an update, we want to make sure that a different stock for
  341. // the organism doesn't already have this uniquename. We don't want to give
  342. // two sequences the same uniquename
  343. if (property_exists($node, 'nid')) {
  344. $sql = "
  345. SELECT *
  346. FROM {stock} S
  347. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  348. WHERE
  349. uniquename = :uname AND organism_id = :organism_id AND
  350. CVT.name = :cvtname AND NOT stock_id = :stock_id
  351. ";
  352. $result = chado_query($sql, array(':uname' => $node->uniquename,
  353. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id,
  354. ':stock_id' => $node->stock_id))->fetchObject();
  355. if ($result) {
  356. 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."));
  357. }
  358. }
  359. // if this is an insert then we just need to make sure this name doesn't
  360. // already exist for this organism if it does then we need to throw an error
  361. else {
  362. $sql = "
  363. SELECT *
  364. FROM {Stock} S
  365. INNER JOIN {cvterm} CVT ON S.type_id = CVT.cvterm_id
  366. WHERE uniquename = :uname AND organism_id = :organism_id AND CVT.name = :cvtname";
  367. $result = chado_query($sql, array(':uname' => $node->uniquename,
  368. ':organism_id' => $node->organism_id, ':cvtname' => $node->type_id))->fetchObject();
  369. if ($result) {
  370. 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."));
  371. }
  372. }
  373. // Check Type of Stock is valid cvterm_id in chado ( $form['values']['details']['type_id'] )
  374. if ($node->type_id == 0) {
  375. form_set_error('type_id', 'Please select a type of stock.');
  376. }
  377. else {
  378. $replace = array(':table' => 'cvterm', ':column' => 'cvterm_id');
  379. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  380. $num_rows = chado_query($new_sql, array(':value' => $node->type_id))->fetchObject();
  381. if ( $num_rows->count != 1) {
  382. form_set_error('type_id', "The type you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  383. }
  384. // Check Source Organism is valid organism_id in chado ( $form['values']['details']['organism_id'] )
  385. if ( $node->organism_id == 0) {
  386. form_set_error('organism_id', 'Please select a source organism for this stock');
  387. }
  388. else {
  389. $replace = array(':table' => 'organism', ':column' => 'organism_id');
  390. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  391. $num_rows = chado_query($new_sql, array(':value' => $node->organism_id))->fetchObject();
  392. if ( $num_rows->count != 1 ) {
  393. form_set_error('organism_id', "The organism you selected is not valid. Please choose another one. (CODE:$num_rows)"); }
  394. }
  395. // Check if Accession also database
  396. if ($node->accession != '') {
  397. if ($node->database == 0) {
  398. // there is an accession but no database selected
  399. form_set_error('database', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  400. }
  401. }
  402. else {
  403. if ($node->database > 0) {
  404. // there is a database selected but no accession
  405. form_set_error('accession', 'You need to enter both a database and an accession for that database in order to add a database reference.');
  406. }
  407. }
  408. // Check database is valid db_id in chado ( $form['values']['database_reference']['database'] )
  409. if ( $node->database > 0) {
  410. $replace = array(':table' => 'db', ':column' => 'db_id');
  411. $new_sql = str_replace(array_keys($replace),$replace,$int_in_chado_sql);
  412. $num_rows = chado_query($new_sql, array(':value' => $node->database))->fetchObject();
  413. if ($num_rows->count != 1) {
  414. form_set_error('database', 'The database you selected is not valid. Please choose another one.'); }
  415. }
  416. }
  417. /**
  418. * Implements hook_insert(): Inserts data from chado_stock_form() into drupal and chado
  419. *
  420. * @param $node
  421. * The current node including fields with the form element names and submitted values
  422. *
  423. * @return
  424. * TRUE if the node was successfully inserted into drupal/chado; FALSE otherwise
  425. *
  426. * @ingroup tripal_stock
  427. */
  428. function chado_stock_insert($node) {
  429. // If the chado stock exists (e.g. this is only a syncing operation)
  430. // then don't create but simply link to node
  431. if (isset($node->chado_stock_exists)) {
  432. if ($node->chado_stock_exists) {
  433. if (!empty($node->stock_id)) {
  434. db_insert('chado_stock')->fields(array(
  435. 'nid' => $node->nid,
  436. 'vid' => $node->vid,
  437. 'stock_id' => $node->stock_id
  438. ));
  439. }
  440. return $node;
  441. }
  442. }
  443. // if there is an stock_id in the $node object then this must be a sync so
  444. // we can skip adding the stock to chado as it is already there, although
  445. // we do need to proceed with the rest of the insert
  446. if (!property_exists($node, 'stock_id')) {
  447. // before we can add the stock, we must add the dbxref if one has been
  448. // provided by the user.
  449. $dbxref_status = 0;
  450. if (!empty($node->accession) ) {
  451. if (!empty($node->database) ) {
  452. $values = array(
  453. 'db_id' => $node->database,
  454. 'accession' => $node->accession,
  455. );
  456. if (!tripal_core_chado_select('dbxref', array('dbxref_id'), $values)) {
  457. $values['description'] = $node->db_description;
  458. $values['version'] = '1';
  459. $dbxref_status = tripal_core_chado_insert('dbxref', $values);
  460. if (!$dbxref_status) {
  461. drupal_set_message(t('Unable to add database reference to this stock.'), 'warning');
  462. watchdog('tripal_stock',
  463. 'Insert Stock: Unable to create dbxref where values:%values',
  464. array('%values' => print_r($values, TRUE)),
  465. WATCHDOG_WARNING
  466. );
  467. }
  468. }
  469. else {
  470. $dbxref_status = 1;
  471. }
  472. }
  473. }
  474. // create stock including the dbxref
  475. $stock = '';
  476. if ($dbxref_status) {
  477. $values = array(
  478. 'dbxref_id' => array(
  479. 'db_id' => $node->database,
  480. 'accession' => $node->accession
  481. ),
  482. 'organism_id' => $node->organism_id,
  483. 'name' => $node->sname,
  484. 'uniquename' => $node->uniquename,
  485. 'description' => $node->stock_description,
  486. 'type_id' => $node->type_id
  487. );
  488. $stock = tripal_core_chado_insert('stock', $values);
  489. }
  490. // create a stock without a dbxref
  491. else {
  492. $values = array(
  493. 'organism_id' => $node->organism_id,
  494. 'name' => $node->sname,
  495. 'uniquename' => $node->uniquename,
  496. 'description' => $node->stock_description,
  497. 'type_id' => $node->type_id
  498. );
  499. $stock = tripal_core_chado_insert('stock', $values);
  500. }
  501. if (is_array($stock)) {
  502. $stock_added = TRUE;
  503. }
  504. else {
  505. $stock_added = FALSE;
  506. }
  507. // Now add properties
  508. if ($stock_added) {
  509. $ontology = variable_get('chado_stock_prop_types_cv', FALSE);
  510. $cv = tripal_core_chado_select('cv',array('name'),array('cv_id' => $ontology));
  511. $ontology_name = $cv[0]->name;
  512. $properties = tripal_core_properties_form_retreive($node, $ontology_name);
  513. foreach ($properties as $property => $elements) {
  514. foreach ($elements as $rank => $value) {
  515. $success = tripal_core_insert_property(
  516. 'stock',
  517. $stock_id,
  518. $property,
  519. $ontology_name,
  520. $value
  521. );
  522. if (!$success) {
  523. watchdog(
  524. 'tripal_stock',
  525. 'Stock Insert: Unable to insert property %cvterm %value.',
  526. array('%cvterm' => $property, '%value' => $value),
  527. WATCHDOG_ERROR
  528. );
  529. }
  530. }
  531. }
  532. }
  533. // Now add the additional references
  534. if ($stock_added) {
  535. tripal_core_additional_dbxrefs_form_update_dbxrefs(
  536. $node,
  537. 'stock_dbxref',
  538. 'stock_id',
  539. $stock_id
  540. );
  541. }
  542. } //end of adding stock to chado
  543. else {
  544. // stock already exists since this is a sync
  545. $stock_added = TRUE;
  546. $stock['stock_id'] = $node->stock_id;
  547. }
  548. // if the stock creation was succesful then add the URL and the entry in the
  549. // chado_stock table
  550. if ($stock_added) {
  551. // add the entry to the chado_stock table
  552. db_insert('chado_stock')->fields(array(
  553. 'nid' => (int) $node->nid,
  554. 'vid' => (int) $node->vid,
  555. 'stock_id' => (int) $stock['stock_id']
  556. ))->execute();
  557. }
  558. else {
  559. drupal_set_message(t('Error during stock creation.'), 'error');
  560. watchdog('tripal_stock',
  561. 'Insert Stock: Unable to create stock where values:%values',
  562. array('%values' => print_r($values, TRUE)),
  563. WATCHDOG_WARNING
  564. );
  565. return FALSE;
  566. }
  567. }
  568. /**
  569. * Implements hook_update(): Handles Editing/Updating of main stock info
  570. *
  571. * NOTE: Currently just writes over all old data
  572. *
  573. * @param $node
  574. * The current node including fields with the form element names and submitted values
  575. *
  576. * @return
  577. * TRUE if the node was successfully updated in drupal/chado; FALSE otherwise
  578. *
  579. * @ingroup tripal_stock
  580. */
  581. function chado_stock_update($node) {
  582. if ($node->revision) {
  583. // there is no way to handle revisions in Chado but leave
  584. // this here just to make not we've addressed it.
  585. }
  586. //update dbxref
  587. if ($node->database) {
  588. if ($node->accession) {
  589. $dbxref_mode = '';
  590. $stock = tripal_core_chado_select(
  591. 'stock',
  592. array('dbxref_id', 'type_id'),
  593. array('stock_id' => $node->stock_id)
  594. );
  595. if ($stock[0]->dbxref_id) {
  596. $values = array(
  597. 'db_id' => $node->database,
  598. 'accession' => $node->accession,
  599. 'description' => $node->db_description
  600. );
  601. $dbxref_status = tripal_core_chado_update(
  602. 'dbxref',
  603. array('dbxref_id' => $stock[0]->dbxref_id),
  604. $values
  605. );
  606. $dbxref_mode = 'Update';
  607. }
  608. else {
  609. if ($stock[0]->type_id) {
  610. //create the dbxref
  611. //used the type_id as a control to check we have a stock but not a dbxref
  612. $values = array(
  613. 'db_id' => $node->database,
  614. 'accession' => $node->accession,
  615. 'description' => $node->db_description,
  616. 'version' => '1',
  617. );
  618. $dbxref_status = tripal_core_chado_insert(
  619. 'dbxref',
  620. $values
  621. );
  622. $dbxref_mode = 'Create';
  623. }
  624. else {
  625. drupal_set_message(t('Unable to find stock to Update'), 'error');
  626. watchdog(
  627. 'tripal_stock',
  628. 'Stock Update: Unable to find stock to update using values: %values',
  629. array('%values', print_r($values, TRUE)),
  630. WATCHDOG_ERROR
  631. );
  632. return FALSE;
  633. }
  634. }
  635. }
  636. }
  637. if (!$dbxref_status) {
  638. watchdog(
  639. 'tripal_stock',
  640. 'Stock Update: Unable to %mode main stock dbxref with values: %values',
  641. array('%values' => print_r($values, TRUE), '%mode' => $dbxref_mode),
  642. WATCHDOG_WARNING
  643. );
  644. }
  645. //can't change stock id which is all thats stored in drupal thus only update chado
  646. $update_values = array(
  647. 'organism_id' => $node->organism_id,
  648. 'name' => $node->sname,
  649. 'uniquename' => $node->uniquename,
  650. 'description' => $node->stock_description,
  651. 'type_id' => $node->type_id,
  652. );
  653. if ($dbxref_status) {
  654. $update_values['dbxref_id'] = array(
  655. 'db_id' => $node->database,
  656. 'accession' => $node->accession
  657. );
  658. }
  659. $status = tripal_core_chado_update('stock', array('stock_id' => $node->stock_id), $update_values);
  660. if (!$status) {
  661. drupal_set_message(t('Unable to update stock'), 'error');
  662. watchdog(
  663. 'tripal_stock',
  664. 'Stock Update: Unable to update stock using match values: %mvalues and update values: %uvalues',
  665. array('%mvalues' => print_r(array('stock_id' => $node->stock_id), TRUE), '%uvalues' => print_r($update_values, TRUE)),
  666. WATCHDOG_ERROR
  667. );
  668. }
  669. else {
  670. // set the URL for this stock page
  671. $values = array('stock_id' => $node->stock_id);
  672. $stock = tripal_core_chado_select('stock', array('*'), $values);
  673. }
  674. // now update the properties
  675. if ($node->stock_id > 0) {
  676. $stock_id = $node->stock_id;
  677. $ontology = variable_get('chado_stock_prop_types_cv', FALSE);
  678. $cv = tripal_core_chado_select('cv',array('name'),array('cv_id' => $ontology));
  679. $ontology_name = $cv[0]->name;
  680. // now add in the properties by first removing any the stock
  681. // already has and adding the ones we have
  682. tripal_core_chado_delete('stockprop', array('stock_id' => $stock_id));
  683. $properties = tripal_core_properties_form_retreive($node, $ontology_name);
  684. foreach ($properties as $property => $elements) {
  685. foreach ($elements as $rank => $value) {
  686. $success = tripal_core_insert_property(
  687. 'stock',
  688. $stock_id,
  689. $property,
  690. $ontology_name,
  691. $value
  692. );
  693. if (!$success) {
  694. watchdog(
  695. 'tripal_stock',
  696. 'Stock Update: Unable to insert property %cvterm %value.',
  697. array('%cvterm' => $property, '%value' => $value),
  698. WATCHDOG_ERROR
  699. );
  700. }
  701. }
  702. }
  703. }
  704. // now update the additional dbxrefs
  705. if ($node->stock_id > 0) {
  706. $stock_id = $node->stock_id;
  707. tripal_core_additional_dbxrefs_form_update_dbxrefs(
  708. $node,
  709. 'stock_dbxref',
  710. 'stock_id',
  711. $stock_id
  712. );
  713. }
  714. }
  715. /**
  716. * Implements hook_delete(): Handles deleting of chado_stocks
  717. *
  718. * NOTE: Currently deletes data -no undo or record-keeping functionality
  719. *
  720. * @param $node
  721. * The current node including fields with the form element names and submitted values
  722. *
  723. * @return
  724. * TRUE if the node was successfully deleted from drupal/chado; FALSE otherwise
  725. *
  726. * @ingroup tripal_stock
  727. */
  728. function chado_stock_delete($node) {
  729. // Set stock in chado: is_obsolete = TRUE
  730. chado_query("DELETE FROM {stock} WHERE stock_id = :stock_id", array(':stock_id' => $node->stock->stock_id));
  731. //remove drupal node and all revisions
  732. db_query("DELETE FROM {chado_stock} WHERE nid = :nid", array(':nid' => $node->nid));
  733. }
  734. /**
  735. * Used by Tripal Chado Node API during sync'ing of nodes
  736. */
  737. function chado_stock_chado_node_sync_create_new_node($new_node, $record) {
  738. $new_node->organism_id = $record->organism_id;
  739. $new_node->sname = $record->name;
  740. $new_node->uniquename = $record->uniquename;
  741. $new_node->type_id = $record->type_id;
  742. return $new_node;
  743. }
  744. /**
  745. * @ingroup tripal_stock
  746. */
  747. function tripal_stock_node_presave($node) {
  748. switch ($node->type) {
  749. case 'chado_stock':
  750. $values = array('organism_id' => $node->organism_id);
  751. $organism = tripal_core_chado_select('organism', array('genus','species'), $values);
  752. $values = array('cvterm_id' => $node->type_id);
  753. $cvterm = tripal_core_chado_select('cvterm', array('name'), $values);
  754. $node->title = $node->sname . ', ' . $node->uniquename . ' (' . $cvterm[0]->name . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  755. break;
  756. }
  757. }
  758. /**
  759. * @ingroup tripal_stock
  760. */
  761. function tripal_stock_node_insert($node) {
  762. switch ($node->type) {
  763. case 'chado_stock':
  764. if (!$node->stock_id) {
  765. $sql = "SELECT * FROM {chado_stock} WHERE nid = :nid";
  766. $chado_stock = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  767. $node->stock_id = $chado_stock->stock_id;
  768. }
  769. // remove any previous alias
  770. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  771. // set the URL for this stock page
  772. $url_alias = tripal_stock_get_stock_url($node);
  773. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  774. path_save($path_alias);
  775. break;
  776. }
  777. }
  778. /**
  779. * @ingroup tripal_stock
  780. */
  781. function tripal_stock_node_update($node) {
  782. switch ($node->type) {
  783. case 'chado_stock':
  784. // remove any previous alias
  785. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  786. // set the URL for this stock page
  787. $url_alias = tripal_stock_get_stock_url($node);
  788. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  789. path_save($path_alias);
  790. break;
  791. }
  792. }
  793. /**
  794. * @ingroup tripal_stock
  795. */
  796. function tripal_stock_node_view($node, $view_mode, $langcode) {
  797. switch ($node->type) {
  798. case 'chado_organism':
  799. if ($view_mode == 'full') {
  800. // Show stock if the organism/feature is not at teaser view
  801. $node->content['tripal_organism_stocks'] = array(
  802. '#value' => theme('tripal_organism_stocks', $node),
  803. );
  804. }
  805. break;
  806. case 'chado_stock':
  807. if ($view_mode == 'full') {
  808. $node->content['tripal_stock_base'] = array(
  809. '#value' => theme('tripal_stock_base', array('node' => $node)),
  810. );
  811. // Cross References
  812. $node->content['tripal_stock_references'] = array(
  813. '#value' => theme('tripal_stock_references', array('node' => $node)),
  814. );
  815. // Properties
  816. $node->content['tripal_stock_properties'] = array(
  817. '#value' => theme('tripal_stock_properties', array('node' => $node)),
  818. );
  819. // Synonyms
  820. $node->content['tripal_stock_synonyms'] = array(
  821. '#value' => theme('tripal_stock_synonyms', array('node' => $node)),
  822. );
  823. // Relationships
  824. $node->content['tripal_stock_relationships'] = array(
  825. '#value' => theme('tripal_stock_relationships', array('node' => $node)),
  826. );
  827. // Stock Collections
  828. $node->content['tripal_stock_collections'] = array(
  829. '#value' => theme('tripal_stock_collections', array('node' => $node)),
  830. );
  831. // Stock Genotypes
  832. $node->content['tripal_stock_genotypes'] = array(
  833. '#value' => theme('tripal_stock_genotypes', array('node' => $node)),
  834. );
  835. }
  836. if ($view_mode == 'teaser') {
  837. $node->content['tripal_stock_teaser'] = array(
  838. '#value' => theme('tripal_stock_teaser', array('node' => $node)),
  839. );
  840. }
  841. break;
  842. }
  843. }