tripal_feature.chado_node.inc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. /**
  3. * Implementation of hook_form
  4. *
  5. * @param $node
  6. * @param $param
  7. *
  8. * @return
  9. * Drupal form array
  10. *
  11. * @ingroup tripal_feature
  12. */
  13. function chado_feature_form($node, &$form_state) {
  14. $form = array();
  15. // Default values can come in the following ways:
  16. //
  17. // 1) as elements of the $node object. This occurs when editing an existing feature
  18. // 2) in the $form_state['values'] array which occurs on a failed validation or
  19. // ajax callbacks from non submit form elements
  20. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  21. // form elements and the form is being rebuilt
  22. //
  23. // set form field defaults
  24. $feature = null;
  25. $feature_id = null;
  26. $uniquename = '';
  27. $fname = '';
  28. $feature_type = '';
  29. $organism_id = '';
  30. $residues = '';
  31. $is_obsolete = '';
  32. $analyses = '';
  33. $references = '';
  34. $synonyms = '';
  35. // if we are editing an existing node then the feature is already part of the node
  36. if (property_exists($node, 'feature')) {
  37. $feature = $node->feature;
  38. $feature = tripal_core_expand_chado_vars($feature, 'field', 'feature.residues');
  39. $feature_id = $feature->feature_id;
  40. $uniquename = $feature->uniquename;
  41. $fname = $feature->name;
  42. $feature_type = $feature->type_id->name;
  43. $organism_id = $feature->organism_id->organism_id;
  44. $residues = $feature->residues;
  45. $is_obsolete = $feature->is_obsolete;
  46. // get the synonyms from a previous post
  47. $synonyms = '';
  48. if(property_exists($node, 'synonyms')) {
  49. $synonyms = $node->synonyms;
  50. }
  51. // get synonyms from the database if we don't already have them
  52. if (!$synonyms) {
  53. $options = array('return_array' => 1);
  54. $feature = tripal_core_expand_chado_vars($feature, 'table', 'feature_synonym', $options);
  55. $feature_synonyms = $feature->feature_synonym;
  56. foreach ($feature_synonyms as $index => $synonym) {
  57. $synonyms .= $synonym->synonym_id->name . "\n";
  58. }
  59. }
  60. // keep track of the feature id
  61. $form['feature_id'] = array(
  62. '#type' => 'value',
  63. '#value' => $feature_id,
  64. );
  65. }
  66. // if we are re constructing the form from a failed validation or ajax callback
  67. // then use the $form_state['values'] values
  68. if (array_key_exists('values', $form_state)) {
  69. $uniquename = $form_state['values']['uniquename'];
  70. $fname = $form_state['values']['fname'];
  71. $feature_type = $form_state['values']['feature_type'];
  72. $organism_id = $form_state['values']['organism_id'];
  73. $residues = $form_state['values']['residues'];
  74. $is_obsolete = $form_state['values']['is_obsolete'];
  75. $synonyms = $form_state['values']['synonyms'];
  76. }
  77. // if we are re building the form from after submission (from ajax call) then
  78. // the values are in the $form_state['input'] array
  79. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  80. $uniquename = $form_state['input']['uniquename'];
  81. $fname = $form_state['input']['fname'];
  82. $feature_type = $form_state['input']['feature_type'];
  83. $organism_id = $form_state['input']['organism_id'];
  84. $residues = $form_state['input']['residues'];
  85. $is_obsolete = array_key_exists('is_obsolete', $form_state['input']) ? $form_state['input']['is_obsolete'] : FALSE;
  86. $synonyms = $form_state['input']['synonyms'];
  87. }
  88. $form['fname']= array(
  89. '#type' => 'textfield',
  90. '#title' => t('Feature Name'),
  91. '#required' => TRUE,
  92. '#default_value' => $fname,
  93. '#description' => t('Enter the name used by humans to refer to this feature.'),
  94. '#maxlength' => 255
  95. );
  96. $form['uniquename']= array(
  97. '#type' => 'textfield',
  98. '#title' => t('Unique Feature Name'),
  99. '#required' => TRUE,
  100. '#default_value' => $uniquename,
  101. '#description' => t('Enter a unique name for this feature. This name must be unique for the organism and feature type.'),
  102. '#maxlength' => 255
  103. );
  104. // get the sequence ontology CV ID
  105. $values = array('name' => 'sequence');
  106. $cv = tripal_core_chado_select('cv', array('cv_id'), $values);
  107. $cv_id = $cv[0]->cv_id;
  108. $form['feature_type'] = array(
  109. '#title' => t('Feature Type'),
  110. '#type' => 'textfield',
  111. '#description' => t("Choose the feature type."),
  112. '#required' => TRUE,
  113. '#default_value' => $feature_type,
  114. '#autocomplete_path' => "admin/tripal/tripal_cv/cvterm/auto_name/$cv_id",
  115. );
  116. // get the list of organisms
  117. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  118. $org_rset = chado_query($sql);
  119. $organisms = array();
  120. $organisms[''] = '';
  121. while ($organism = $org_rset->fetchObject()) {
  122. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  123. }
  124. $form['organism_id'] = array(
  125. '#title' => t('Organism'),
  126. '#type' => t('select'),
  127. '#description' => t("Choose the organism with which this feature is associated"),
  128. '#required' => TRUE,
  129. '#default_value' => $organism_id,
  130. '#options' => $organisms,
  131. );
  132. // Get synonyms
  133. $syn_text = '';
  134. if ($synonyms) {
  135. if (is_array($synonyms)) {
  136. foreach ($synonyms as $synonym) {
  137. $syn_text .= "$synonym->name\n";
  138. }
  139. }
  140. else {
  141. $syn_text = $synonyms;
  142. }
  143. }
  144. $form['synonyms']= array(
  145. '#type' => 'textarea',
  146. '#title' => t('Synonyms'),
  147. '#required' => FALSE,
  148. '#default_value' => $syn_text,
  149. '#description' => t('Enter alternate names (synonmys) for this feature to help in searching and identification. You may enter as many alternate names as needed each on different lines.'),
  150. );
  151. $form['residues']= array(
  152. '#type' => 'textarea',
  153. '#title' => t('Residues'),
  154. '#required' => FALSE,
  155. '#default_value' => $residues,
  156. '#description' => t('Enter the nucelotide sequences for this feature'),
  157. );
  158. $checked = '';
  159. if ($is_obsolete == 't') {
  160. $checked = '1';
  161. }
  162. $form['is_obsolete']= array(
  163. '#type' => 'checkbox',
  164. '#title' => t('Is Obsolete'),
  165. '#required' => FALSE,
  166. '#default_value' => $checked,
  167. '#description' => t('Check this box if this sequence should be retired'),
  168. );
  169. return $form;
  170. }
  171. /**
  172. * Implementation of hook_validate
  173. *
  174. * This validation is being used for three activities:
  175. * CASE A: Update a node that exists in both drupal and chado
  176. * CASE B: Synchronizing a node from chado to drupal
  177. * CASE C: Inserting a new node that exists in niether drupal nor chado
  178. *
  179. * @param $node
  180. *
  181. *
  182. * @ingroup tripal_feature
  183. */
  184. function chado_feature_validate($node, $form, &$form_state) {
  185. // remove surrounding white-space on submitted values
  186. $node->uniquename = trim($node->uniquename);
  187. $node->fname = trim($node->fname);
  188. $node->feature_type = trim($node->feature_type);
  189. $node->residues = trim($node->residues);
  190. // if this is a delete then don't validate
  191. if($node->op == 'Delete') {
  192. return;
  193. }
  194. // we are syncing if we do not have a node ID but we do have a feature_id. We don't
  195. // need to validate during syncing so just skip it.
  196. if (is_null($node->nid) and property_exists($node, 'feature_id') and $node->feature_id != 0) {
  197. return;
  198. }
  199. // Validating for an update
  200. if (property_exists($node, 'nid')) {
  201. // make sure the feature type is a real sequence ontology term
  202. $type = tripal_cv_get_cvterm_by_name($node->feature_type, NULL, 'sequence');
  203. if (!$type) {
  204. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  205. }
  206. // if this is an update, we want to make sure that a different feature for
  207. // the organism doesn't already have this uniquename. We don't want to give
  208. // two sequences the same uniquename
  209. if (property_exists($node, 'feature_id') and $node->feature_id != 0) {
  210. $sql = "
  211. SELECT *
  212. FROM {feature} F
  213. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  214. WHERE
  215. F.uniquename = :uname AND
  216. F.organism_id = :organism_id AND
  217. CVT.name = :cvtname AND
  218. NOT f.feature_id = :feature_id
  219. ";
  220. $args = array(':uname' => $node->uniquename, ':organism_id' => $node->organism_id,
  221. ':cvtname' => $node->feature_type, ':feature_id' => $node->feature_id);
  222. $result = chado_query($sql, $args)->fetchObject();
  223. if ($result) {
  224. form_set_error('uniquename', t("Feature update cannot proceed. The feature name '$node->uniquename' is not unique for this organism. Please provide a unique name for this feature."));
  225. }
  226. }
  227. }
  228. // Validating for an insert
  229. else {
  230. // make sure the feature type is a real sequence ontology term
  231. $type = tripal_cv_get_cvterm_by_name($node->feature_type, NULL, 'sequence');
  232. if (!$type) {
  233. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  234. }
  235. // if this is an insert then we just need to make sure this name doesn't
  236. // already exist for this organism if it does then we need to throw an error
  237. $sql = "
  238. SELECT *
  239. FROM {feature} F
  240. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  241. WHERE
  242. F.uniquename = :name AND
  243. F.organism_id = :organism_id AND
  244. CVT.name = :cvtname
  245. ";
  246. $args = array(':name' => $node->uniquename, ':organism_id' => $node->organism_id, ':cvtname' => $node->feature_type);
  247. $result = chado_query($sql, $args)->fetchObject();
  248. if ($result) {
  249. form_set_error('uniquename', t("Feature insert cannot proceed. The feature name '$node->uniquename' already exists for this organism. Please provide a unique name for this feature."));
  250. }
  251. }
  252. }
  253. /**
  254. * Implement hook_access().
  255. *
  256. * This hook allows node modules to limit access to the node types they define.
  257. *
  258. * @param $node
  259. * The node on which the operation is to be performed, or, if it does not yet exist, the
  260. * type of node to be created
  261. *
  262. * @param $op
  263. * The operation to be performed
  264. *
  265. * @param $account
  266. * A user object representing the user for whom the operation is to be performed
  267. *
  268. * @return
  269. * If the permission for the specified operation is not set then return FALSE. If the
  270. * permission is set then return NULL as this allows other modules to disable
  271. * access. The only exception is when the $op == 'create'. We will always
  272. * return TRUE if the permission is set.
  273. *
  274. * @ingroup tripal_feature
  275. */
  276. function chado_feature_node_access($node, $op, $account) {
  277. if ($op == 'create') {
  278. if (!user_access('create chado_feature content', $account)) {
  279. return FALSE;
  280. }
  281. return TRUE;
  282. }
  283. if ($op == 'update') {
  284. if (!user_access('edit chado_feature content', $account)) {
  285. return FALSE;
  286. }
  287. }
  288. if ($op == 'delete') {
  289. if (!user_access('delete chado_feature content', $account)) {
  290. return FALSE;
  291. }
  292. }
  293. if ($op == 'view') {
  294. if (!user_access('access chado_feature content', $account)) {
  295. return FALSE;
  296. }
  297. }
  298. return NULL;
  299. }
  300. /**
  301. * When a new chado_feature node is created we also need to add information
  302. * to our chado_feature table. This function is called on insert of a new node
  303. * of type 'chado_feature' and inserts the necessary information.
  304. *
  305. * @ingroup tripal_feature
  306. */
  307. function chado_feature_insert($node) {
  308. $node->uniquename = trim($node->uniquename);
  309. $node->fname = trim($node->fname);
  310. $node->feature_type = trim($node->feature_type);
  311. $node->residues = trim($node->residues);
  312. // remove spaces, newlines from residues
  313. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  314. $obsolete = 'FALSE';
  315. if ($node->is_obsolete) {
  316. $obsolete = 'TRUE';
  317. }
  318. $feature_id = '';
  319. // if there is an feature_id in the $node object then this must be a sync so
  320. // we can skip adding the feature as it is already there, although
  321. // we do need to proceed with the rest of the insert
  322. if (!property_exists($node, 'feature_id')) {
  323. $values = array(
  324. 'organism_id' => $node->organism_id,
  325. 'name' => $node->fname,
  326. 'uniquename' => $node->uniquename,
  327. 'residues' => $residues,
  328. 'seqlen' => drupal_strlen($residues),
  329. 'is_obsolete' => $obsolete,
  330. 'type_id' => $type[0]->cvterm_id,
  331. 'md5checksum' => md5($residues)
  332. );
  333. $feature = tripal_core_chado_insert('feature', $values);
  334. if (!$feature) {
  335. drupal_set_message(t('Unable to add feature.'), 'warning');
  336. watchdog('tripal_feature', 'Insert feature: Unable to create feature where values: %values',
  337. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  338. return;
  339. }
  340. $feature_id = $feature->feature_id;
  341. // add the genbank accession and synonyms
  342. chado_feature_add_synonyms($node->synonyms, $node->feature_id);
  343. }
  344. else {
  345. $feature_id = $node->feature_id;
  346. }
  347. // Make sure the entry for this feature doesn't already exist in the
  348. // chado_feature table if it doesn't exist then we want to add it.
  349. $check_org_id = chado_get_id_for_node('feature', $node->nid);
  350. if (!$check_org_id) {
  351. $record = new stdClass();
  352. $record->nid = $node->nid;
  353. $record->vid = $node->vid;
  354. $record->feature_id = $feature_id;
  355. drupal_write_record('chado_feature', $record);
  356. }
  357. }
  358. /**
  359. *
  360. *
  361. * @ingroup tripal_feature
  362. */
  363. function chado_feature_update($node) {
  364. $node->uniquename = trim($node->uniquename);
  365. $node->fname = trim($node->fname);
  366. $node->feature_type = trim($node->feature_type);
  367. $node->residues = trim($node->residues);
  368. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  369. $obsolete = 'FALSE';
  370. if ($node->is_obsolete) {
  371. $obsolete = 'TRUE';
  372. }
  373. // get the feature type id
  374. $values = array(
  375. 'cv_id' => array(
  376. 'name' => 'sequence'
  377. ),
  378. 'name' => $node->feature_type
  379. );
  380. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  381. $feature_id = chado_get_id_for_node('feature', $node->nid) ;
  382. if (sizeof($type) > 0) {
  383. $match = array(
  384. 'feature_id' => $feature_id,
  385. );
  386. $values = array(
  387. 'organism_id' => $node->organism_id,
  388. 'name' => $node->fname,
  389. 'uniquename' => $node->uniquename,
  390. 'residues' => $residues,
  391. 'seqlen' => drupal_strlen($residues),
  392. 'is_obsolete' => $obsolete,
  393. 'type_id' => $type[0]->cvterm_id,
  394. 'md5checksum' => md5($residues)
  395. );
  396. $options = array('return_record' => TRUE);
  397. $status = tripal_core_chado_update('feature', $match, $values, $options);
  398. // add the genbank synonyms
  399. chado_feature_add_synonyms($node->synonyms, $feature_id);
  400. }
  401. else {
  402. drupal_set_message(t('Unable to update feature.'), 'warning');
  403. watchdog('tripal_feature',
  404. 'Update feature: Unable to update feature where values: %values',
  405. array('%values' => print_r($values, TRUE)),
  406. WATCHDOG_WARNING
  407. );
  408. }
  409. }
  410. /**
  411. *
  412. *
  413. * @ingroup tripal_feature
  414. */
  415. function chado_feature_delete($node) {
  416. $feature_id = chado_get_id_for_node('feature', $node->nid);
  417. // if we don't have a library id for this node then this isn't a node of
  418. // type chado_library or the entry in the chado_library table was lost.
  419. if (!$feature_id) {
  420. return;
  421. }
  422. // remove the drupal content
  423. $sql_del = "DELETE FROM {chado_feature} WHERE nid = :nid AND vid = :vid";
  424. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  425. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  426. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  427. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  428. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  429. // Remove data from feature tables of chado database. This will
  430. // cause a cascade delete and remove all data in referencing tables
  431. // for this feature
  432. chado_query("DELETE FROM {feature} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
  433. drupal_set_message(t("The feature and all associated data were removed from") .
  434. "chado");
  435. }
  436. /**
  437. *
  438. *
  439. * @ingroup tripal_feature
  440. */
  441. function chado_feature_add_synonyms($synonyms, $feature_id) {
  442. // separate synomys by carriage returns
  443. $synonyms = preg_replace("/[\n\r]+/", " ", $synonyms);
  444. // split the synonyms into an array based on a space as the delimieter
  445. $syn_array = array();
  446. $syn_array = explode(" ", $synonyms);
  447. // remove any old synonyms
  448. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = :feature_id";
  449. if (!chado_query($feature_syn_dsql, array(':feature_id' => $feature_id))) {
  450. watchdog('tripal_feature', "Could not remove synonyms from feature. ", array(), WATCHDOG_ERROR);
  451. return;
  452. }
  453. // return if we don't have any synonmys to add
  454. if (!$synonyms) {
  455. return;
  456. }
  457. // iterate through each synonym and add it to the database
  458. foreach ($syn_array as $syn) {
  459. // skip this item if it's empty
  460. if (!$syn) {
  461. break;
  462. }
  463. // check to see if we have this accession number already in the database
  464. // if so then don't add it again. it messes up drupal if the insert fails.
  465. // It is possible for the accession number to be present and not the feature
  466. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  467. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  468. if (!$synonym) {
  469. $synonym_isql = "
  470. INSERT INTO {synonym} (name, synonym_sgml, type_id)
  471. VALUES (:name, :synonym_sgml,
  472. (SELECT cvterm_id
  473. FROM {cvterm} CVT
  474. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  475. WHERE CV.name = 'feature_property' and CVT.name = 'synonym')
  476. )
  477. ";
  478. if (!chado_query($synonym_isql, array(':name' => $syn, ':synonym_sgml' => $syn))) {
  479. watchdog('tripal_feature', "Could not add synonym. ", array(), WATCHDOG_WARNING);
  480. return;
  481. }
  482. // now get the synonym we just added
  483. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  484. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  485. }
  486. // now add in our new sysnonym
  487. $feature_syn_isql = "
  488. INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id)
  489. VALUES (:synonym_id, :feature_id, :pub_id)";
  490. $args = array(':synonym_id' => $synonym->synonym_id, ':feature_id' => $feature_id, ':pub_id'=> 1);
  491. if (!chado_query($feature_syn_isql, $args)) {
  492. watchdog('tripal_feature', "Could not associate synonym with feature. ", array(), WATCHDOG_WARNING);
  493. return;
  494. }
  495. }
  496. }
  497. /**
  498. * When a node is requested by the user this function is called to allow us
  499. * to add auxiliary data to the node object.
  500. *
  501. * @ingroup tripal_feature
  502. */
  503. function chado_feature_load($nodes) {
  504. foreach ($nodes as $nid => $node) {
  505. // find the feature and add in the details
  506. $feature_id = chado_get_id_for_node('feature', $nid);
  507. // build the feature variable
  508. $values = array('feature_id' => $feature_id);
  509. $feature = tripal_core_generate_chado_var('feature', $values);
  510. $nodes[$nid]->feature = $feature;
  511. // by default, the titles are saved using the unique constraint. We will
  512. // keep it the same, but remove the duplicate name if the unique name and name
  513. // are identical. This doesn't change the title saved in the database, just what is shown
  514. // to the user on the page
  515. $title_type = variable_get('chado_feature_title', 'unique_constraint');
  516. if ($title_type == 'unique_constraint') {
  517. if (strcmp($feature->name, $feature->uniquename)==0) {
  518. $node->title = $feature->name . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  519. }
  520. // in previous version of Tripal, the feature title was simply the unique name.
  521. // so, we recreate the title just to be sure all of our feature pages are consistent
  522. else {
  523. $node->title = $feature->name . ", " . $feature->uniquename . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  524. }
  525. }
  526. // set the title to be the feature name or uniquename as configured
  527. if ($title_type == 'feature_name') {
  528. $node->title = $feature->name;
  529. }
  530. if ($title_type == 'feature_unique_name') {
  531. $node->title = $feature->uniquename;
  532. }
  533. }
  534. }
  535. /**
  536. * Provide information to drupal about the node types that we're creating
  537. * in this module
  538. *
  539. * @ingroup tripal_feature
  540. */
  541. function tripal_feature_node_info() {
  542. $nodes = array();
  543. $nodes['chado_feature'] = array(
  544. 'name' => t('Feature'),
  545. 'base' => 'chado_feature',
  546. 'description' => t('A feature from the chado database'),
  547. 'has_title' => FALSE,
  548. 'title_label' => t('Feature'),
  549. 'has_body' => FALSE,
  550. 'body_label' => t('Feature Description'),
  551. 'locked' => TRUE
  552. );
  553. return $nodes;
  554. }
  555. /**
  556. *
  557. * @ingroup tripal_feature
  558. */
  559. function tripal_feature_node_presave($node) {
  560. // set the title to ensure it is always unique
  561. switch ($node->type) {
  562. case 'chado_feature':
  563. $values = array('organism_id' => $node->organism_id);
  564. $organism = tripal_core_chado_select('organism', array('genus', 'species'), $values);
  565. $node->title = $node->fname . ', ' . $node->uniquename . ' (' . $node->feature_type . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  566. break;
  567. }
  568. }
  569. /**
  570. *
  571. * @ingroup tripal_feature
  572. */
  573. function tripal_feature_node_insert($node) {
  574. // set the URL path after inserting. We do it here because we do not
  575. // know the feature_id in the presave
  576. switch ($node->type) {
  577. case 'chado_feature':
  578. if (!$node->feature_id) {
  579. $sql = "SELECT * FROM {chado_feature} WHERE nid = :nid";
  580. $chado_feature = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  581. $node->feature_id = $chado_feature->feature_id;
  582. }
  583. // remove any previous alias
  584. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  585. // set the URL for this feature page
  586. $url_alias = tripal_feature_get_feature_url($node);
  587. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  588. path_save($path_alias);
  589. break;
  590. }
  591. }
  592. /**
  593. *
  594. * @ingroup tripal_feature
  595. */
  596. function tripal_feature_node_view($node, $view_mode, $langcode) {
  597. switch ($node->type) {
  598. case 'chado_feature':
  599. // Show feature browser and counts
  600. if ($view_mode == 'full') {
  601. $node->content['tripal_feature_alignments'] = array(
  602. '#value' => theme('tripal_feature_alignments', array('node' => $node)),
  603. );
  604. $node->content['tripal_feature_analyses'] = array(
  605. '#value' => theme('tripal_feature_analyses', array('node' => $node)),
  606. );
  607. $node->content['tripal_feature_base'] = array(
  608. '#value' => theme('tripal_feature_base', array('node' => $node)),
  609. );
  610. $node->content['tripal_feature_featurepos'] = array(
  611. '#value' => theme('tripal_feature_featurepos', array('node' => $node)),
  612. );
  613. $node->content['tripal_feature_properties'] = array(
  614. '#value' => theme('tripal_feature_properties', array('node' => $node)),
  615. );
  616. $node->content['tripal_feature_publications'] = array(
  617. '#value' => theme('tripal_feature_publications', array('node' => $node)),
  618. );
  619. $node->content['tripal_feature_references'] = array(
  620. '#value' => theme('tripal_feature_references', array('node' => $node)),
  621. );
  622. $node->content['tripal_feature_relationships'] = array(
  623. '#value' => theme('tripal_feature_relationships', array('node' => $node)),
  624. );
  625. $node->content['tripal_feature_seqence'] = array(
  626. '#value' => theme('tripal_feature_sequence', array('node' => $node)),
  627. );
  628. $node->content['tripal_feature_synonyms'] = array(
  629. '#value' => theme('tripal_feature_synonyms', array('node' => $node)),
  630. );
  631. $node->content['tripal_feature_terms'] = array(
  632. '#value' => theme('tripal_feature_terms', array('node' => $node)),
  633. );
  634. }
  635. if ($view_mode == 'teaser') {
  636. $node->content['tripal_feature_teaser'] = array(
  637. '#value' => theme('tripal_feature_teaser', array('node' => $node)),
  638. );
  639. }
  640. break;
  641. case 'chado_organism':
  642. // Show feature browser and counts
  643. if ($view_mode == 'full') {
  644. $node->content['tripal_organism_feature_counts'] = array(
  645. '#value' => theme('tripal_organism_feature_counts', array('node' => $node)),
  646. );
  647. $node->content['tripal_organism_feature_browser'] = array(
  648. '#value' => theme('tripal_organism_feature_browser', array('node' => $node)),
  649. );
  650. }
  651. break;
  652. // TODO: handle these node types. Should we also have a feature browser?
  653. case 'chado_library':
  654. break;
  655. case 'chado_stock':
  656. break;
  657. case 'chado_analysis':
  658. break;
  659. }
  660. }
  661. /**
  662. *
  663. * @ingroup tripal_feature
  664. */
  665. function tripal_feature_node_update($node) {
  666. // add items to other nodes, build index and search results
  667. switch ($node->type) {
  668. case 'chado_feature':
  669. // remove any previous alias
  670. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  671. // set the URL for this feature page
  672. $url_alias = tripal_feature_get_feature_url($node);
  673. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  674. path_save($path_alias);
  675. break;
  676. }
  677. }