tripal_feature.chado_node.inc 34 KB

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