tripal_feature.chado_node.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. <?php
  2. /**
  3. * @file
  4. * Implementation of hooks to create a feature content type
  5. */
  6. /**
  7. * Implements hook_node_info().
  8. *
  9. * Provide information to drupal about the node types that we're creating
  10. * in this module
  11. *
  12. * @ingroup tripal_feature
  13. */
  14. function tripal_feature_node_info() {
  15. $nodes = array();
  16. $nodes['chado_feature'] = array(
  17. 'name' => t('Feature'),
  18. 'base' => 'chado_feature',
  19. 'description' => t('A feature from the chado database'),
  20. 'has_title' => TRUE,
  21. 'locked' => TRUE,
  22. 'chado_node_api' => array(
  23. 'base_table' => 'feature',
  24. 'hook_prefix' => 'chado_feature',
  25. 'record_type_title' => array(
  26. 'singular' => t('Feature'),
  27. 'plural' => t('Features')
  28. ),
  29. 'sync_filters' => array(
  30. 'type_id' => TRUE,
  31. 'organism_id' => TRUE
  32. ),
  33. )
  34. );
  35. return $nodes;
  36. }
  37. /**
  38. * Implementation of hook_form().
  39. *
  40. * @ingroup tripal_feature
  41. */
  42. function chado_feature_form($node, &$form_state) {
  43. $form = array();
  44. // Default values can come in the following ways:
  45. //
  46. // 1) as elements of the $node object. This occurs when editing an existing feature
  47. // 2) in the $form_state['values'] array which occurs on a failed validation or
  48. // ajax callbacks from non submit form elements
  49. // 3) in the $form_state['input'[ array which occurs on ajax callbacks from submit
  50. // form elements and the form is being rebuilt
  51. //
  52. // set form field defaults
  53. $feature = null;
  54. $feature_id = null;
  55. $uniquename = '';
  56. $fname = '';
  57. $feature_type = '';
  58. $organism_id = '';
  59. $residues = '';
  60. $is_obsolete = '';
  61. $analyses = '';
  62. $references = '';
  63. $synonyms = '';
  64. // if we are editing an existing node then the feature is already part of the node
  65. if (property_exists($node, 'feature')) {
  66. $feature = $node->feature;
  67. $feature = chado_expand_var($feature, 'field', 'feature.residues');
  68. $feature_id = $feature->feature_id;
  69. $uniquename = $feature->uniquename;
  70. $fname = $feature->name;
  71. $feature_type = $feature->type_id->name;
  72. $organism_id = $feature->organism_id->organism_id;
  73. $residues = $feature->residues;
  74. $is_obsolete = $feature->is_obsolete;
  75. // get the synonyms from a previous post
  76. $synonyms = '';
  77. if(property_exists($node, 'synonyms')) {
  78. $synonyms = $node->synonyms;
  79. }
  80. // get synonyms from the database if we don't already have them
  81. if (!$synonyms) {
  82. $options = array('return_array' => 1);
  83. $feature = chado_expand_var($feature, 'table', 'feature_synonym', $options);
  84. $feature_synonyms = (isset($feature->feature_synonym)) ? $feature->feature_synonym : array();
  85. foreach ($feature_synonyms as $index => $synonym) {
  86. $synonyms .= $synonym->synonym_id->name . "\n";
  87. }
  88. }
  89. // keep track of the feature id
  90. $form['feature_id'] = array(
  91. '#type' => 'value',
  92. '#value' => $feature_id,
  93. );
  94. }
  95. // if we are re constructing the form from a failed validation or ajax callback
  96. // then use the $form_state['values'] values
  97. if (array_key_exists('values', $form_state) and isset($form_state['values']['uniquename'])) {
  98. $uniquename = $form_state['values']['uniquename'];
  99. $fname = $form_state['values']['fname'];
  100. $feature_type = $form_state['values']['feature_type'];
  101. $organism_id = $form_state['values']['organism_id'];
  102. $residues = $form_state['values']['residues'];
  103. $is_obsolete = $form_state['values']['is_obsolete'];
  104. $synonyms = $form_state['values']['synonyms'];
  105. }
  106. // if we are re building the form from after submission (from ajax call) then
  107. // the values are in the $form_state['input'] array
  108. if (array_key_exists('input', $form_state) and !empty($form_state['input'])) {
  109. $uniquename = $form_state['input']['uniquename'];
  110. $fname = $form_state['input']['fname'];
  111. $feature_type = $form_state['input']['feature_type'];
  112. $organism_id = $form_state['input']['organism_id'];
  113. $residues = $form_state['input']['residues'];
  114. $is_obsolete = array_key_exists('is_obsolete', $form_state['input']) ? $form_state['input']['is_obsolete'] : FALSE;
  115. $synonyms = $form_state['input']['synonyms'];
  116. }
  117. $form['fname']= array(
  118. '#type' => 'textfield',
  119. '#title' => t('Feature Name'),
  120. '#required' => TRUE,
  121. '#default_value' => $fname,
  122. '#description' => t('Enter the name used by humans to refer to this feature.'),
  123. '#maxlength' => 255
  124. );
  125. $form['uniquename']= array(
  126. '#type' => 'textfield',
  127. '#title' => t('Unique Feature Name'),
  128. '#required' => TRUE,
  129. '#default_value' => $uniquename,
  130. '#description' => t('Enter a unique name for this feature. This name must be unique for the organism and feature type.'),
  131. '#maxlength' => 255
  132. );
  133. // get the sequence ontology CV ID
  134. $values = array('name' => 'sequence');
  135. $cv = chado_select_record('cv', array('cv_id'), $values);
  136. $cv_id = $cv[0]->cv_id;
  137. $form['feature_type'] = array(
  138. '#title' => t('Feature Type'),
  139. '#type' => 'textfield',
  140. '#description' => t("Choose the feature type."),
  141. '#required' => TRUE,
  142. '#default_value' => $feature_type,
  143. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  144. );
  145. // get the list of organisms
  146. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  147. $org_rset = chado_query($sql);
  148. $organisms = array();
  149. $organisms[''] = '';
  150. while ($organism = $org_rset->fetchObject()) {
  151. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  152. }
  153. $form['organism_id'] = array(
  154. '#title' => t('Organism'),
  155. '#type' => t('select'),
  156. '#description' => t("Choose the organism with which this feature is associated"),
  157. '#required' => TRUE,
  158. '#default_value' => $organism_id,
  159. '#options' => $organisms,
  160. );
  161. // Get synonyms
  162. $syn_text = '';
  163. if ($synonyms) {
  164. if (is_array($synonyms)) {
  165. foreach ($synonyms as $synonym) {
  166. $syn_text .= "$synonym->name\n";
  167. }
  168. }
  169. else {
  170. $syn_text = $synonyms;
  171. }
  172. }
  173. $form['synonyms']= array(
  174. '#type' => 'textarea',
  175. '#title' => t('Synonyms'),
  176. '#required' => FALSE,
  177. '#default_value' => $syn_text,
  178. '#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.'),
  179. );
  180. $form['residues']= array(
  181. '#type' => 'textarea',
  182. '#title' => t('Residues'),
  183. '#required' => FALSE,
  184. '#default_value' => $residues,
  185. '#description' => t('Enter the nucelotide sequences for this feature'),
  186. );
  187. $checked = '';
  188. if ($is_obsolete == 't') {
  189. $checked = '1';
  190. }
  191. $form['is_obsolete']= array(
  192. '#type' => 'checkbox',
  193. '#title' => t('Is Obsolete'),
  194. '#required' => FALSE,
  195. '#default_value' => $checked,
  196. '#description' => t('Check this box if this sequence should be retired'),
  197. );
  198. // PROPERTIES FORM
  199. //---------------------------------------------
  200. $details = array(
  201. 'property_table' => 'featureprop', // the name of the prop table
  202. 'chado_id' => $feature_id, // the value of feature_id for this record
  203. 'cv_name' => 'feature_property' // the cv.name of the cv governing featureprop.type_id
  204. );
  205. // Adds the form elements to your current form
  206. chado_add_node_form_properties($form, $form_state, $details);
  207. // ADDITIONAL DBXREFS FORM
  208. //---------------------------------------------
  209. $details = array(
  210. 'linking_table' => 'feature_dbxref', // the name of the _dbxref table
  211. 'base_foreign_key' => 'feature_id', // the name of the key in your base chado table
  212. 'base_key_value' => $feature_id // the value of feature_id for this record
  213. );
  214. // Adds the form elements to your current form
  215. chado_add_node_form_dbxrefs($form, $form_state, $details);
  216. return $form;
  217. }
  218. /**
  219. * Implementation of hook_validate().
  220. *
  221. * This validation is being used for three activities:
  222. * CASE A: Update a node that exists in both drupal and chado
  223. * CASE B: Synchronizing a node from chado to drupal
  224. * CASE C: Inserting a new node that exists in niether drupal nor chado
  225. *
  226. * @ingroup tripal_feature
  227. */
  228. function chado_feature_validate($node, $form, &$form_state) {
  229. // if this is a delete then don't validate
  230. if($node->op == 'Delete') {
  231. return;
  232. }
  233. // we are syncing if we do not have a node ID but we do have a feature_id. We don't
  234. // need to validate during syncing so just skip it.
  235. if (is_null($node->nid) and property_exists($node, 'feature_id') and $node->feature_id != 0) {
  236. return;
  237. }
  238. // remove surrounding white-space on submitted values
  239. $node->uniquename = trim($node->uniquename);
  240. $node->fname = trim($node->fname);
  241. $node->feature_type = trim($node->feature_type);
  242. $node->residues = trim($node->residues);
  243. // Validating for an update
  244. if (property_exists($node, 'nid')) {
  245. // make sure the feature type is a real sequence ontology term
  246. $type = tripal_cv_get_cvterm_by_name($node->feature_type, NULL, 'sequence');
  247. if (!$type) {
  248. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  249. }
  250. // if this is an update, we want to make sure that a different feature for
  251. // the organism doesn't already have this uniquename. We don't want to give
  252. // two sequences the same uniquename
  253. if (property_exists($node, 'feature_id') and $node->feature_id != 0) {
  254. $sql = "
  255. SELECT *
  256. FROM {feature} F
  257. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  258. WHERE
  259. F.uniquename = :uname AND
  260. F.organism_id = :organism_id AND
  261. CVT.name = :cvtname AND
  262. NOT f.feature_id = :feature_id
  263. ";
  264. $args = array(':uname' => $node->uniquename, ':organism_id' => $node->organism_id,
  265. ':cvtname' => $node->feature_type, ':feature_id' => $node->feature_id);
  266. $result = chado_query($sql, $args)->fetchObject();
  267. if ($result) {
  268. 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."));
  269. }
  270. }
  271. }
  272. // Validating for an insert
  273. else {
  274. // make sure the feature type is a real sequence ontology term
  275. $type = tripal_cv_get_cvterm_by_name($node->feature_type, NULL, 'sequence');
  276. if (!$type) {
  277. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  278. }
  279. // if this is an insert then we just need to make sure this name doesn't
  280. // already exist for this organism if it does then we need to throw an error
  281. $sql = "
  282. SELECT *
  283. FROM {feature} F
  284. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  285. WHERE
  286. F.uniquename = :name AND
  287. F.organism_id = :organism_id AND
  288. CVT.name = :cvtname
  289. ";
  290. $args = array(':name' => $node->uniquename, ':organism_id' => $node->organism_id, ':cvtname' => $node->feature_type);
  291. $result = chado_query($sql, $args)->fetchObject();
  292. if ($result) {
  293. 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."));
  294. }
  295. }
  296. }
  297. /**
  298. * Implement hook_node_access().
  299. *
  300. * This hook allows node modules to limit access to the node types they define.
  301. *
  302. * @param $node
  303. * The node on which the operation is to be performed, or, if it does not yet exist, the
  304. * type of node to be created
  305. *
  306. * @param $op
  307. * The operation to be performed
  308. *
  309. * @param $account
  310. * A user object representing the user for whom the operation is to be performed
  311. *
  312. * @return
  313. * If the permission for the specified operation is not set then return FALSE. If the
  314. * permission is set then return NULL as this allows other modules to disable
  315. * access. The only exception is when the $op == 'create'. We will always
  316. * return TRUE if the permission is set.
  317. *
  318. * @ingroup tripal_feature
  319. */
  320. function chado_feature_node_access($node, $op, $account) {
  321. $node_type = $node;
  322. if (is_object($node)) {
  323. $node_type = $node->type;
  324. }
  325. if($node_type == 'chado_feature') {
  326. if ($op == 'create') {
  327. if (!user_access('create chado_feature content', $account)) {
  328. return NODE_ACCESS_DENY;
  329. }
  330. return NODE_ACCESS_ALLOW;
  331. }
  332. if ($op == 'update') {
  333. if (!user_access('edit chado_feature content', $account)) {
  334. return NODE_ACCESS_DENY;
  335. }
  336. }
  337. if ($op == 'delete') {
  338. if (!user_access('delete chado_feature content', $account)) {
  339. return NODE_ACCESS_DENY;
  340. }
  341. }
  342. if ($op == 'view') {
  343. if (!user_access('access chado_feature content', $account)) {
  344. return NODE_ACCESS_DENY;
  345. }
  346. }
  347. }
  348. return NODE_ACCESS_IGNORE;
  349. }
  350. /**
  351. * Implements hook_insert().
  352. *
  353. * When a new chado_feature node is created we also need to add information
  354. * to our chado_feature table. This function is called on insert of a new node
  355. * of type 'chado_feature' and inserts the necessary information.
  356. *
  357. * @ingroup tripal_feature
  358. */
  359. function chado_feature_insert($node) {
  360. $node->uniquename = trim($node->uniquename);
  361. $node->fname = trim($node->fname);
  362. $node->feature_type = trim($node->feature_type);
  363. $node->residues = trim($node->residues);
  364. // remove spaces, newlines from residues
  365. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  366. $obsolete = 'FALSE';
  367. if ($node->is_obsolete) {
  368. $obsolete = 'TRUE';
  369. }
  370. // get the feature type id
  371. $values = array(
  372. 'cv_id' => array(
  373. 'name' => 'sequence'
  374. ),
  375. 'name' => $node->feature_type
  376. );
  377. $type = chado_select_record('cvterm', array('cvterm_id'), $values);
  378. $feature_id = '';
  379. // if there is an feature_id in the $node object then this must be a sync so
  380. // we can skip adding the feature as it is already there, although
  381. // we do need to proceed with the rest of the insert
  382. if (!property_exists($node, 'feature_id')) {
  383. $values = array(
  384. 'organism_id' => $node->organism_id,
  385. 'name' => $node->fname,
  386. 'uniquename' => $node->uniquename,
  387. 'residues' => $residues,
  388. 'seqlen' => drupal_strlen($residues),
  389. 'is_obsolete' => $obsolete,
  390. 'type_id' => $type[0]->cvterm_id,
  391. 'md5checksum' => md5($residues)
  392. );
  393. $feature = chado_insert_record('feature', $values);
  394. if (!$feature) {
  395. drupal_set_message(t('Unable to add feature.'), 'warning');
  396. tripal_report_error('tripal_feature', TRIPAL_WARNING, 'Insert feature: Unable to create feature where values: %values',
  397. array('%values' => print_r($values, TRUE)));
  398. return;
  399. }
  400. $feature_id = $feature['feature_id'];
  401. // add the genbank accession and synonyms
  402. chado_feature_add_synonyms($node->synonyms, $feature_id);
  403. // * Properties Form *
  404. $details = array(
  405. 'property_table' => 'featureprop', // the name of the prop table
  406. 'base_table' => 'feature', // the name of your chado base table
  407. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  408. 'foreignkey_value' => $feature_id // the value of the feature_id key
  409. );
  410. chado_update_node_form_properties($node, $details);
  411. // * Additional DBxrefs Form *
  412. $details = array(
  413. 'linking_table' => 'feature_dbxref', // the name of your _dbxref table
  414. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  415. 'foreignkey_value' => $feature_id // the value of the feature_id key
  416. );
  417. chado_update_node_form_dbxrefs($node, $details);
  418. }
  419. else {
  420. $feature_id = $node->feature_id;
  421. }
  422. // Make sure the entry for this feature doesn't already exist in the
  423. // chado_feature table if it doesn't exist then we want to add it.
  424. $check_org_id = chado_get_id_from_nid('feature', $node->nid);
  425. if (!$check_org_id) {
  426. $record = new stdClass();
  427. $record->nid = $node->nid;
  428. $record->vid = $node->vid;
  429. $record->feature_id = $feature_id;
  430. drupal_write_record('chado_feature', $record);
  431. }
  432. }
  433. /**
  434. * Implements hook_update().
  435. *
  436. * @ingroup tripal_feature
  437. */
  438. function chado_feature_update($node) {
  439. $node->uniquename = trim($node->uniquename);
  440. $node->fname = trim($node->fname);
  441. $node->feature_type = trim($node->feature_type);
  442. $node->residues = trim($node->residues);
  443. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  444. $obsolete = 'FALSE';
  445. if ($node->is_obsolete) {
  446. $obsolete = 'TRUE';
  447. }
  448. // get the feature type id
  449. $values = array(
  450. 'cv_id' => array(
  451. 'name' => 'sequence'
  452. ),
  453. 'name' => $node->feature_type
  454. );
  455. $type = chado_select_record('cvterm', array('cvterm_id'), $values);
  456. $feature_id = chado_get_id_from_nid('feature', $node->nid) ;
  457. if (sizeof($type) > 0) {
  458. $match = array(
  459. 'feature_id' => $feature_id,
  460. );
  461. $values = array(
  462. 'organism_id' => $node->organism_id,
  463. 'name' => $node->fname,
  464. 'uniquename' => $node->uniquename,
  465. 'residues' => $residues,
  466. 'seqlen' => drupal_strlen($residues),
  467. 'is_obsolete' => $obsolete,
  468. 'type_id' => $type[0]->cvterm_id,
  469. 'md5checksum' => md5($residues)
  470. );
  471. $options = array('return_record' => TRUE);
  472. $status = chado_update_record('feature', $match, $values, $options);
  473. // add the genbank synonyms
  474. chado_feature_add_synonyms($node->synonyms, $feature_id);
  475. // * Properties Form *
  476. $details = array(
  477. 'property_table' => 'featureprop', // the name of the prop table
  478. 'base_table' => 'feature', // the name of your chado base table
  479. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  480. 'foreignkey_value' => $feature_id // the value of the feature_id key
  481. );
  482. chado_update_node_form_properties($node, $details);
  483. // * Additional DBxrefs Form *
  484. $details = array(
  485. 'linking_table' => 'feature_dbxref', // the name of your _dbxref table
  486. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  487. 'foreignkey_value' => $feature_id // the value of the feature_id key
  488. );
  489. chado_update_node_form_dbxrefs($node, $details);
  490. }
  491. else {
  492. drupal_set_message(t('Unable to update feature.'), 'warning');
  493. tripal_report_error('tripal_feature', TRIPAL_WARNING,
  494. 'Update feature: Unable to update feature where values: %values',
  495. array('%values' => print_r($values, TRUE))
  496. );
  497. }
  498. }
  499. /**
  500. * Implements hook_delete().
  501. *
  502. * @ingroup tripal_feature
  503. */
  504. function chado_feature_delete($node) {
  505. $feature_id = chado_get_id_from_nid('feature', $node->nid);
  506. // if we don't have a library id for this node then this isn't a node of
  507. // type chado_library or the entry in the chado_library table was lost.
  508. if (!$feature_id) {
  509. return;
  510. }
  511. // remove the drupal content
  512. $sql_del = "DELETE FROM {chado_feature} WHERE nid = :nid AND vid = :vid";
  513. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  514. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  515. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  516. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  517. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  518. // Remove data from feature tables of chado database. This will
  519. // cause a cascade delete and remove all data in referencing tables
  520. // for this feature
  521. chado_query("DELETE FROM {feature} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
  522. drupal_set_message(t("The feature and all associated data were removed from") .
  523. "chado");
  524. }
  525. /**
  526. * Add synonyms to a feature
  527. *
  528. * @param $synonyms
  529. * A string containing synonyms separated by a return character
  530. * @param $feature_id
  531. * The feature to attach the synonyms to
  532. *
  533. * @ingroup tripal_feature
  534. */
  535. function chado_feature_add_synonyms($synonyms, $feature_id) {
  536. // separate synomys by carriage returns
  537. $synonyms = preg_replace("/[\n\r]+/", " ", $synonyms);
  538. // split the synonyms into an array based on a space as the delimieter
  539. $syn_array = array();
  540. $syn_array = explode(" ", $synonyms);
  541. // remove any old synonyms
  542. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = :feature_id";
  543. if (!chado_query($feature_syn_dsql, array(':feature_id' => $feature_id))) {
  544. tripal_report_error('tripal_feature', TRIPAL_ERROR, "Could not remove synonyms from feature. ", array());
  545. return;
  546. }
  547. // return if we don't have any synonmys to add
  548. if (!$synonyms) {
  549. return;
  550. }
  551. // iterate through each synonym and add it to the database
  552. foreach ($syn_array as $syn) {
  553. // skip this item if it's empty
  554. if (!$syn) {
  555. break;
  556. }
  557. // check to see if we have this accession number already in the database
  558. // if so then don't add it again. it messes up drupal if the insert fails.
  559. // It is possible for the accession number to be present and not the feature
  560. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  561. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  562. if (!$synonym) {
  563. $synonym_isql = "
  564. INSERT INTO {synonym} (name, synonym_sgml, type_id)
  565. VALUES (:name, :synonym_sgml,
  566. (SELECT cvterm_id
  567. FROM {cvterm} CVT
  568. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  569. WHERE CV.name = 'feature_property' and CVT.name = 'synonym')
  570. )
  571. ";
  572. if (!chado_query($synonym_isql, array(':name' => $syn, ':synonym_sgml' => $syn))) {
  573. tripal_report_error('tripal_feature', "Could not add synonym. ", array(), TRIPAL_WARNING);
  574. return;
  575. }
  576. // now get the synonym we just added
  577. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  578. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  579. }
  580. // now add in our new sysnonym
  581. $feature_syn_isql = "
  582. INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id)
  583. VALUES (:synonym_id, :feature_id, :pub_id)";
  584. $args = array(':synonym_id' => $synonym->synonym_id, ':feature_id' => $feature_id, ':pub_id'=> 1);
  585. if (!chado_query($feature_syn_isql, $args)) {
  586. tripal_report_error('tripal_feature', "Could not associate synonym with feature. ", array(), TRIPAL_WARNING);
  587. return;
  588. }
  589. }
  590. }
  591. /**
  592. * Implements hook_load().
  593. *
  594. * When a node is requested by the user this function is called to allow us
  595. * to add auxiliary data to the node object.
  596. *
  597. * @ingroup tripal_feature
  598. */
  599. function chado_feature_load($nodes) {
  600. foreach ($nodes as $nid => $node) {
  601. // find the feature and add in the details
  602. $feature_id = chado_get_id_from_nid('feature', $nid);
  603. // build the feature variable
  604. $values = array('feature_id' => $feature_id);
  605. $feature = chado_generate_var('feature', $values);
  606. $nodes[$nid]->feature = $feature;
  607. // by default, the titles are saved using the unique constraint. We will
  608. // keep it the same, but remove the duplicate name if the unique name and name
  609. // are identical. This doesn't change the title saved in the database, just what is shown
  610. // to the user on the page
  611. $title_type = variable_get('chado_feature_title', 'unique_constraint');
  612. if ($title_type == 'unique_constraint') {
  613. if (strcmp($feature->name, $feature->uniquename)==0) {
  614. $node->title = $feature->name . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  615. }
  616. // in previous version of Tripal, the feature title was simply the unique name.
  617. // so, we recreate the title just to be sure all of our feature pages are consistent
  618. else {
  619. $node->title = $feature->name . ", " . $feature->uniquename . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  620. }
  621. }
  622. // set the title to be the feature name or uniquename as configured
  623. if ($title_type == 'feature_name') {
  624. $node->title = $feature->name;
  625. }
  626. if ($title_type == 'feature_unique_name') {
  627. $node->title = $feature->uniquename;
  628. }
  629. }
  630. }
  631. /**
  632. * Implements hook_node_presave().
  633. * Acts on all content types.
  634. *
  635. * @ingroup tripal_feature
  636. */
  637. function tripal_feature_node_presave($node) {
  638. // set the title to ensure it is always unique
  639. switch ($node->type) {
  640. case 'chado_feature':
  641. // for a form submission the fields are part of the node object
  642. // but for a sync the fields are in an object of the node
  643. $name = '';
  644. $uname = '';
  645. $type = '';
  646. $organism_id = null;
  647. if(property_exists($node, 'uniquename')) {
  648. $organism_id = $node->organism_id;
  649. $name = $node->name;
  650. $uname = $node->uniquename;
  651. $type = $node->feature_type;
  652. }
  653. else if (property_exists($node, 'feature')) {
  654. $organism_id = $node->feature->organism_id;
  655. $name = $node->feature->name;
  656. $uname = $node->feature->uniquename;
  657. $type_id = $node->feature->type_id;
  658. $values = array('cvterm_id' => $type_id);
  659. $ftype = chado_select_record('cv', array('name'), $values);
  660. $type = $ftype[0]->name;
  661. }
  662. $values = array('organism_id' => $organism_id);
  663. $organism = chado_select_record('organism', array('genus', 'species'), $values);
  664. $node->title = "$name, $uname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  665. if ($name == $uname) {
  666. $node->title = "$name ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  667. }
  668. break;
  669. }
  670. }
  671. /**
  672. * Implements hook_node_insert().
  673. * Acts on all content types.
  674. *
  675. * @ingroup tripal_feature
  676. */
  677. function tripal_feature_node_insert($node) {
  678. // set the URL path after inserting. We do it here because we do not
  679. // know the feature_id in the presave
  680. switch ($node->type) {
  681. case 'chado_feature':
  682. // remove any previous alias
  683. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  684. // set the URL for this feature page
  685. $url_alias = tripal_feature_get_feature_url($node);
  686. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  687. path_save($path_alias);
  688. break;
  689. }
  690. }
  691. /**
  692. * Implements hook_node_update().
  693. * Acts on all content types.
  694. *
  695. * @ingroup tripal_feature
  696. */
  697. function tripal_feature_node_update($node) {
  698. // add items to other nodes, build index and search results
  699. switch ($node->type) {
  700. case 'chado_feature':
  701. // remove any previous alias
  702. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  703. // set the URL for this feature page
  704. $url_alias = tripal_feature_get_feature_url($node);
  705. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  706. path_save($path_alias);
  707. break;
  708. }
  709. }
  710. /**
  711. * Return the url alias for a feature
  712. *
  713. * @param $node
  714. * A node object containing at least the feature_id and nid
  715. * @param $url_alias
  716. * Optional. This should be the URL alias syntax string that contains
  717. * placeholders such as [id], [genus], [species], [name], [uniquename],
  718. * and [type]. These placeholders will be substituted for actual values.
  719. * If this parameter is not provided then the value of the
  720. * chado_feature_url_string Drupal variable will be used.
  721. *
  722. * @ingroup tripal_feature
  723. */
  724. function tripal_feature_get_feature_url($node, $url_alias = NULL) {
  725. // get the starting URL alias
  726. if(!$url_alias) {
  727. $url_alias = variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]');
  728. if (!$url_alias) {
  729. $url_alias = '/feature/[genus]/[species]/[type]/[uniquename]';
  730. }
  731. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  732. }
  733. // get the feature
  734. $values = array('feature_id' => $node->feature_id);
  735. $feature = chado_select_record('feature', array('*'), $values);
  736. if (!$feature) {
  737. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find feature when setting URL alias for feature: %id", array('%id' => $node->feature_id));
  738. return FALSE;
  739. }
  740. $feature = (object) $feature[0];
  741. // get the organism
  742. $values = array('organism_id' => $feature->organism_id);
  743. $organism = chado_select_record('organism', array('*'), $values);
  744. if (!$organism) {
  745. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find organism when setting URL alias for feature: %id", array('%id' => $node->feature_id));
  746. return FALSE;
  747. }
  748. $genus = preg_replace('/\s/', '_', strtolower($organism[0]->genus));
  749. $species = preg_replace('/\s/', '_', strtolower($organism[0]->species));
  750. // get the type
  751. $values = array('cvterm_id' => $feature->type_id);
  752. $cvterm = chado_select_record('cvterm', array('name'), $values);
  753. if (!$cvterm) {
  754. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot find type when setting URL alias for feature: %id", array('%id' => $node->feature_id));
  755. return FALSE;
  756. }
  757. $type = preg_replace('/\s/', '_', $cvterm[0]->name);
  758. // now substitute in the values
  759. $url_alias = preg_replace('/\[id\]/', $feature->feature_id, $url_alias);
  760. $url_alias = preg_replace('/\[genus\]/', $genus, $url_alias);
  761. $url_alias = preg_replace('/\[species\]/', $species, $url_alias);
  762. $url_alias = preg_replace('/\[type\]/', $type, $url_alias);
  763. $url_alias = preg_replace('/\[name\]/', $feature->name, $url_alias);
  764. $url_alias = preg_replace('/\[uniquename\]/', $feature->uniquename, $url_alias);
  765. // the dst field of the url_alias table is only 128 characters long.
  766. // if this is the case then simply return the node URL, we can't set this one
  767. if (strlen($url_alias) > 128) {
  768. tripal_report_error('trp-seturl', TRIPAL_ERROR, "Cannot set alias longer than 128 characters: %alias.", array('%alias' => $url_alias));
  769. return "node/" . $node->nid;
  770. }
  771. return $url_alias;
  772. }
  773. /**
  774. * Implements hook_node_view().
  775. * Acts on all content types.
  776. *
  777. * @ingroup tripal_feature
  778. */
  779. function tripal_feature_node_view($node, $view_mode, $langcode) {
  780. switch ($node->type) {
  781. case 'chado_feature':
  782. // Show feature browser and counts
  783. if ($view_mode == 'full') {
  784. $node->content['tripal_feature_alignments'] = array(
  785. '#markup' => theme('tripal_feature_alignments', array('node' => $node)),
  786. '#tripal_toc_id' => 'alignments',
  787. '#tripal_toc_title' => 'Alignments',
  788. );
  789. $node->content['tripal_feature_analyses'] = array(
  790. '#markup' => theme('tripal_feature_analyses', array('node' => $node)),
  791. '#tripal_toc_id' => 'analyses',
  792. '#tripal_toc_title' => 'Analyses',
  793. );
  794. $node->content['tripal_feature_base'] = array(
  795. '#markup' => theme('tripal_feature_base', array('node' => $node)),
  796. '#tripal_toc_id' => 'base',
  797. '#tripal_toc_title' => 'Overview',
  798. '#weight' => -100,
  799. );
  800. $node->content['tripal_feature_properties'] = array(
  801. '#markup' => theme('tripal_feature_properties', array('node' => $node)),
  802. '#tripal_toc_id' => 'properties',
  803. '#tripal_toc_title' => 'Properties',
  804. );
  805. $node->content['tripal_feature_publications'] = array(
  806. '#markup' => theme('tripal_feature_publications', array('node' => $node)),
  807. '#tripal_toc_id' => 'publications',
  808. '#tripal_toc_title' => 'Publications',
  809. );
  810. $node->content['tripal_feature_references'] = array(
  811. '#markup' => theme('tripal_feature_references', array('node' => $node)),
  812. '#tripal_toc_id' => 'references',
  813. '#tripal_toc_title' => 'Cross References',
  814. );
  815. $node->content['tripal_feature_relationships'] = array(
  816. '#markup' => theme('tripal_feature_relationships', array('node' => $node)),
  817. '#tripal_toc_id' => 'relationships',
  818. '#tripal_toc_title' => 'Relationships',
  819. );
  820. $node->content['tripal_feature_seqence'] = array(
  821. '#markup' => theme('tripal_feature_sequence', array('node' => $node)),
  822. '#tripal_toc_id' => 'sequence',
  823. '#tripal_toc_title' => 'Sequence',
  824. );
  825. $node->content['tripal_feature_synonyms'] = array(
  826. '#markup' => theme('tripal_feature_synonyms', array('node' => $node)),
  827. '#tripal_toc_id' => 'synonyms',
  828. '#tripal_toc_title' => 'Synonyms',
  829. );
  830. $node->content['tripal_feature_terms'] = array(
  831. '#markup' => theme('tripal_feature_terms', array('node' => $node)),
  832. '#tripal_toc_id' => 'terms',
  833. '#tripal_toc_title' => 'Annotated Terms',
  834. );
  835. }
  836. if ($view_mode == 'teaser') {
  837. $node->content['tripal_feature_teaser'] = array(
  838. '#markup' => theme('tripal_feature_teaser', array('node' => $node)),
  839. );
  840. }
  841. break;
  842. case 'chado_organism':
  843. // Show feature browser and counts
  844. if ($view_mode == 'full') {
  845. $node->content['tripal_organism_feature_counts'] = array(
  846. '#markup' => theme('tripal_organism_feature_counts', array('node' => $node)),
  847. '#tripal_toc_id' => 'feature_counts',
  848. '#tripal_toc_title' => 'Data Type Summary',
  849. );
  850. $node->content['tripal_organism_feature_browser'] = array(
  851. '#markup' => theme('tripal_organism_feature_browser', array('node' => $node)),
  852. '#tripal_toc_id' => 'feature_browser',
  853. '#tripal_toc_title' => 'Feature Browser',
  854. );
  855. }
  856. break;
  857. // TODO: handle these node types. Should we also have a feature browser?
  858. case 'chado_library':
  859. break;
  860. case 'chado_stock':
  861. break;
  862. case 'chado_analysis':
  863. break;
  864. }
  865. }