tripal_feature.chado_node.inc 25 KB

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