tripal_feature.chado_node.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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_legacy_feature
  13. */
  14. function tripal_feature_node_info() {
  15. $nodes = array();
  16. $nodes['chado_feature'] = array(
  17. 'name' => t('Feature (Tripal v2 legacy)'),
  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_legacy_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. //$type_options = tripal_get_cvterm_default_select_options('feature', 'type_id', 'feature types');
  134. //$type_options[0] = 'Select a Type';
  135. $type_cv = tripal_get_default_cv('feature', 'type_id');
  136. $cv_id = $type_cv->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' => "aadmin/tripal/storage/chado/auto_name/cvterm/$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. $prop_cv = tripal_get_default_cv('featureprop', 'type_id');
  201. $cv_id = $prop_cv ? $prop_cv->cv_id : NULL;
  202. $details = array(
  203. 'property_table' => 'featureprop', // the name of the prop table
  204. 'chado_id' => $feature_id, // the value of feature_id for this record
  205. 'cv_id' => $cv_id // the cv.cv_id of the cv governing featureprop.type_id
  206. );
  207. chado_add_node_form_properties($form, $form_state, $details);
  208. // ADDITIONAL DBXREFS FORM
  209. //---------------------------------------------
  210. $details = array(
  211. 'linking_table' => 'feature_dbxref', // the name of the _dbxref table
  212. 'base_foreign_key' => 'feature_id', // the name of the key in your base chado table
  213. 'base_key_value' => $feature_id // the value of feature_id for this record
  214. );
  215. chado_add_node_form_dbxrefs($form, $form_state, $details);
  216. // RELATIONSHIPS FORM
  217. //---------------------------------------------
  218. $relationship_cv = tripal_get_default_cv('feature_relationship', 'type_id');
  219. $cv_id = $relationship_cv ? $relationship_cv->cv_id : NULL;
  220. $details = array(
  221. 'relationship_table' => 'feature_relationship',
  222. 'base_table' => 'feature',
  223. 'base_foreign_key' => 'feature_id',
  224. 'base_key_value' => $feature_id,
  225. 'nodetype' => 'feature',
  226. 'cv_id' => $cv_id
  227. );
  228. chado_add_node_form_relationships($form, $form_state, $details);
  229. return $form;
  230. }
  231. /**
  232. * Implementation of hook_validate().
  233. *
  234. * This validation is being used for three activities:
  235. * CASE A: Update a node that exists in both drupal and chado
  236. * CASE B: Synchronizing a node from chado to drupal
  237. * CASE C: Inserting a new node that exists in niether drupal nor chado
  238. *
  239. * @ingroup tripal_legacy_feature
  240. */
  241. function chado_feature_validate($node, $form, &$form_state) {
  242. // We only want to validate when the node is saved.
  243. // Since this validate can be called on AJAX and Deletion of the node
  244. // we need to make this check to ensure queries are not executed
  245. // without the proper values.
  246. if(property_exists($node, "op") and $node->op != 'Save') {
  247. return;
  248. }
  249. // we are syncing if we do not have a node ID but we do have a feature_id. We don't
  250. // need to validate during syncing so just skip it.
  251. if (!property_exists($node, 'nid') and property_exists($node, 'feature_id') and $node->feature_id != 0) {
  252. return;
  253. }
  254. // remove surrounding white-space on submitted values
  255. $node->uniquename = property_exists($node, 'uniquename') ? trim($node->uniquename) : '';
  256. $node->fname = property_exists($node, 'fname') ? trim($node->fname) : '';
  257. $node->feature_type = property_exists($node, 'feature_type') ? trim($node->feature_type) : '';
  258. $node->residues = property_exists($node, 'residues') ? trim($node->residues) : '';
  259. // Validating for an update
  260. if (property_exists($node, 'nid')) {
  261. // make sure the feature type is a real sequence ontology term
  262. $type = tripal_get_cvterm(array(
  263. 'name' => $node->feature_type,
  264. 'cv_id' => array('name' =>'sequence')
  265. ));
  266. if (!$type) {
  267. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  268. }
  269. // if this is an update, we want to make sure that a different feature for
  270. // the organism doesn't already have this uniquename. We don't want to give
  271. // two sequences the same uniquename
  272. if (property_exists($node, 'feature_id') and $node->feature_id != 0) {
  273. $sql = "
  274. SELECT *
  275. FROM {feature} F
  276. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  277. WHERE
  278. F.uniquename = :uname AND
  279. F.organism_id = :organism_id AND
  280. CVT.name = :cvtname AND
  281. NOT f.feature_id = :feature_id
  282. ";
  283. $args = array(':uname' => $node->uniquename, ':organism_id' => $node->organism_id,
  284. ':cvtname' => $node->feature_type, ':feature_id' => $node->feature_id);
  285. $result = chado_query($sql, $args)->fetchObject();
  286. if ($result) {
  287. 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."));
  288. }
  289. }
  290. }
  291. // Validating for an insert
  292. else {
  293. // make sure the feature type is a real sequence ontology term
  294. $type = tripal_get_cvterm(array(
  295. 'name' => $node->feature_type,
  296. 'cv_id' => array('name' => 'sequence')
  297. ));
  298. if (!$type) {
  299. form_set_error('feature_type', t("The feature type is not a valid name from the Sequence Ontology."));
  300. }
  301. // if this is an insert then we just need to make sure this name doesn't
  302. // already exist for this organism if it does then we need to throw an error
  303. $sql = "
  304. SELECT *
  305. FROM {feature} F
  306. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  307. WHERE
  308. F.uniquename = :name AND
  309. F.organism_id = :organism_id AND
  310. CVT.name = :cvtname
  311. ";
  312. $args = array(':name' => $node->uniquename, ':organism_id' => $node->organism_id, ':cvtname' => $node->feature_type);
  313. $result = chado_query($sql, $args)->fetchObject();
  314. if ($result) {
  315. 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."));
  316. }
  317. }
  318. }
  319. /**
  320. * Implement hook_node_access().
  321. *
  322. * This hook allows node modules to limit access to the node types they define.
  323. *
  324. * @param $node
  325. * The node on which the operation is to be performed, or, if it does not yet exist, the
  326. * type of node to be created
  327. *
  328. * @param $op
  329. * The operation to be performed
  330. *
  331. * @param $account
  332. * A user object representing the user for whom the operation is to be performed
  333. *
  334. * @return
  335. * If the permission for the specified operation is not set then return FALSE. If the
  336. * permission is set then return NULL as this allows other modules to disable
  337. * access. The only exception is when the $op == 'create'. We will always
  338. * return TRUE if the permission is set.
  339. *
  340. * @ingroup tripal_legacy_feature
  341. */
  342. function tripal_feature_node_access($node, $op, $account) {
  343. $node_type = $node;
  344. if (is_object($node)) {
  345. $node_type = $node->type;
  346. }
  347. if($node_type == 'chado_feature') {
  348. if ($op == 'create') {
  349. if (!user_access('create chado_feature content', $account)) {
  350. return NODE_ACCESS_DENY;
  351. }
  352. return NODE_ACCESS_ALLOW;
  353. }
  354. if ($op == 'update') {
  355. if (!user_access('edit chado_feature content', $account)) {
  356. return NODE_ACCESS_DENY;
  357. }
  358. }
  359. if ($op == 'delete') {
  360. if (!user_access('delete chado_feature content', $account)) {
  361. return NODE_ACCESS_DENY;
  362. }
  363. }
  364. if ($op == 'view') {
  365. if (!user_access('access chado_feature content', $account)) {
  366. return NODE_ACCESS_DENY;
  367. }
  368. }
  369. }
  370. return NODE_ACCESS_IGNORE;
  371. }
  372. /**
  373. * Implements hook_insert().
  374. *
  375. * When a new chado_feature node is created we also need to add information
  376. * to our chado_feature table. This function is called on insert of a new node
  377. * of type 'chado_feature' and inserts the necessary information.
  378. *
  379. * @ingroup tripal_legacy_feature
  380. */
  381. function chado_feature_insert($node) {
  382. $feature_id = '';
  383. // if there is a feature_id in the $node object then this must be a sync so
  384. // we can skip adding the feature as it is already there, although
  385. // we do need to proceed with insertion into the chado/drupal linking table.
  386. if (!property_exists($node, 'feature_id')) {
  387. $node->uniquename = trim($node->uniquename);
  388. $node->fname = trim($node->fname);
  389. $node->feature_type = trim($node->feature_type);
  390. $node->residues = trim($node->residues);
  391. // remove spaces, newlines from residues
  392. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  393. $obsolete = 'FALSE';
  394. if ($node->is_obsolete) {
  395. $obsolete = 'TRUE';
  396. }
  397. // get the feature type id
  398. $values = array(
  399. 'cv_id' => array(
  400. 'name' => 'sequence'
  401. ),
  402. 'name' => $node->feature_type
  403. );
  404. $type = chado_select_record('cvterm', array('cvterm_id'), $values);
  405. $values = array(
  406. 'organism_id' => $node->organism_id,
  407. 'name' => $node->fname,
  408. 'uniquename' => $node->uniquename,
  409. 'residues' => $residues,
  410. 'seqlen' => drupal_strlen($residues),
  411. 'is_obsolete' => $obsolete,
  412. 'type_id' => $type[0]->cvterm_id,
  413. 'md5checksum' => md5($residues)
  414. );
  415. $feature = chado_insert_record('feature', $values);
  416. if (!$feature) {
  417. drupal_set_message(t('Unable to add feature.'), 'warning');
  418. tripal_report_error('tripal_feature', TRIPAL_WARNING, 'Insert feature: Unable to create feature where values: %values',
  419. array('%values' => print_r($values, TRUE)));
  420. return;
  421. }
  422. $feature_id = $feature['feature_id'];
  423. // add the genbank accession and synonyms
  424. chado_feature_add_synonyms($node->synonyms, $feature_id);
  425. // * Properties Form *
  426. $details = array(
  427. 'property_table' => 'featureprop', // the name of the prop table
  428. 'base_table' => 'feature', // the name of your chado base table
  429. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  430. 'foreignkey_value' => $feature_id // the value of the feature_id key
  431. );
  432. chado_update_node_form_properties($node, $details);
  433. // * Additional DBxrefs Form *
  434. $details = array(
  435. 'linking_table' => 'feature_dbxref', // the name of your _dbxref table
  436. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  437. 'foreignkey_value' => $feature_id // the value of the feature_id key
  438. );
  439. chado_update_node_form_dbxrefs($node, $details);
  440. // * Relationships Form *
  441. $details = array(
  442. 'relationship_table' => 'feature_relationship',
  443. 'foreignkey_value' => $feature_id
  444. );
  445. chado_update_node_form_relationships($node, $details);
  446. }
  447. else {
  448. $feature_id = $node->feature_id;
  449. }
  450. // Make sure the entry for this feature doesn't already exist in the
  451. // chado_feature table if it doesn't exist then we want to add it.
  452. $check_org_id = chado_get_id_from_nid('feature', $node->nid);
  453. if (!$check_org_id) {
  454. $record = new stdClass();
  455. $record->nid = $node->nid;
  456. $record->vid = $node->vid;
  457. $record->feature_id = $feature_id;
  458. drupal_write_record('chado_feature', $record);
  459. }
  460. }
  461. /**
  462. * Implements hook_update().
  463. *
  464. * @ingroup tripal_legacy_feature
  465. */
  466. function chado_feature_update($node) {
  467. $node->uniquename = trim($node->uniquename);
  468. $node->fname = trim($node->fname);
  469. $node->feature_type = trim($node->feature_type);
  470. $node->residues = trim($node->residues);
  471. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  472. $obsolete = 'FALSE';
  473. if ($node->is_obsolete) {
  474. $obsolete = 'TRUE';
  475. }
  476. // get the feature type id
  477. $values = array(
  478. 'cv_id' => array(
  479. 'name' => 'sequence'
  480. ),
  481. 'name' => $node->feature_type
  482. );
  483. $type = chado_select_record('cvterm', array('cvterm_id'), $values);
  484. $feature_id = chado_get_id_from_nid('feature', $node->nid) ;
  485. if (sizeof($type) > 0) {
  486. $match = array(
  487. 'feature_id' => $feature_id,
  488. );
  489. $values = array(
  490. 'organism_id' => $node->organism_id,
  491. 'name' => $node->fname,
  492. 'uniquename' => $node->uniquename,
  493. 'residues' => $residues,
  494. 'seqlen' => drupal_strlen($residues),
  495. 'is_obsolete' => $obsolete,
  496. 'type_id' => $type[0]->cvterm_id,
  497. 'md5checksum' => md5($residues)
  498. );
  499. $options = array('return_record' => TRUE);
  500. $status = chado_update_record('feature', $match, $values, $options);
  501. // add the genbank synonyms
  502. chado_feature_add_synonyms($node->synonyms, $feature_id);
  503. // * Properties Form *
  504. $details = array(
  505. 'property_table' => 'featureprop', // the name of the prop table
  506. 'base_table' => 'feature', // the name of your chado base table
  507. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  508. 'foreignkey_value' => $feature_id // the value of the feature_id key
  509. );
  510. chado_update_node_form_properties($node, $details);
  511. // * Additional DBxrefs Form *
  512. $details = array(
  513. 'linking_table' => 'feature_dbxref', // the name of your _dbxref table
  514. 'foreignkey_name' => 'feature_id', // the name of the key in your base table
  515. 'foreignkey_value' => $feature_id // the value of the feature_id key
  516. );
  517. chado_update_node_form_dbxrefs($node, $details);
  518. // * Relationships Form *
  519. $details = array(
  520. 'relationship_table' => 'feature_relationship',
  521. 'foreignkey_value' => $feature_id
  522. );
  523. chado_update_node_form_relationships($node, $details);
  524. }
  525. else {
  526. drupal_set_message(t('Unable to update feature.'), 'warning');
  527. tripal_report_error('tripal_feature', TRIPAL_WARNING,
  528. 'Update feature: Unable to update feature where values: %values',
  529. array('%values' => print_r($values, TRUE))
  530. );
  531. }
  532. }
  533. /**
  534. * Implements hook_delete().
  535. *
  536. * @ingroup tripal_legacy_feature
  537. */
  538. function chado_feature_delete($node) {
  539. $feature_id = chado_get_id_from_nid('feature', $node->nid);
  540. // If we don't have a feature id for this node then this isn't a node of
  541. // type chado_library or the entry in the chado_library table was lost.
  542. if (!$feature_id) {
  543. return;
  544. }
  545. // Remove the drupal content.
  546. $sql_del = "DELETE FROM {chado_feature} WHERE nid = :nid AND vid = :vid";
  547. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  548. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  549. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  550. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  551. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  552. // Remove data from feature tables of chado database. This will
  553. // cause a cascade delete and remove all data in referencing tables
  554. // for this feature. However, we need t specifically delete from the
  555. // featureloc table because the box() PLSQL function calls another
  556. // function that does not reference the 'chado' schema and causes an error
  557. // the chado_query function can handle this problem so we specificall delete
  558. // from that table to prevent the error. The same problem exists for the
  559. // frange.featuregroup table
  560. $previous_db = chado_set_active('chado') ;
  561. db_query("DELETE FROM frange.featuregroup WHERE subject_id = :feature_id", array(':feature_id' => $feature_id));
  562. db_query("DELETE FROM frange.featuregroup WHERE object_id = :feature_id", array(':feature_id' => $feature_id));
  563. db_query("DELETE FROM frange.featuregroup WHERE group_id = :feature_id", array(':feature_id' => $feature_id));
  564. db_query("DELETE FROM frange.featuregroup WHERE srcfeature_id = :feature_id", array(':feature_id' => $feature_id));
  565. chado_set_active($previous_db);
  566. chado_query("DELETE FROM {featureloc} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
  567. chado_query("DELETE FROM {featureloc} WHERE srcfeature_id = :feature_id", array(':feature_id' => $feature_id));
  568. chado_query("DELETE FROM {feature} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
  569. drupal_set_message(t("The feature and all associated data were removed"));
  570. }
  571. /**
  572. * Add synonyms to a feature
  573. *
  574. * @param $synonyms
  575. * A string containing synonyms separated by a return character
  576. * @param $feature_id
  577. * The feature to attach the synonyms to
  578. *
  579. * @ingroup tripal_legacy_feature
  580. */
  581. function chado_feature_add_synonyms($synonyms, $feature_id) {
  582. // separate synomys by carriage returns
  583. $synonyms = preg_replace("/[\n\r]+/", " ", $synonyms);
  584. // split the synonyms into an array based on a space as the delimieter
  585. $syn_array = array();
  586. $syn_array = explode(" ", $synonyms);
  587. // remove any old synonyms
  588. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = :feature_id";
  589. if (!chado_query($feature_syn_dsql, array(':feature_id' => $feature_id))) {
  590. tripal_report_error('tripal_feature', TRIPAL_ERROR, "Could not remove synonyms from feature. ", array());
  591. return;
  592. }
  593. // return if we don't have any synonmys to add
  594. if (!$synonyms) {
  595. return;
  596. }
  597. // iterate through each synonym and add it to the database
  598. foreach ($syn_array as $syn) {
  599. // skip this item if it's empty
  600. if (!$syn) {
  601. break;
  602. }
  603. // check to see if we have this accession number already in the database
  604. // if so then don't add it again. it messes up drupal if the insert fails.
  605. // It is possible for the accession number to be present and not the feature
  606. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  607. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  608. if (!$synonym) {
  609. $synonym_isql = "
  610. INSERT INTO {synonym} (name, synonym_sgml, type_id)
  611. VALUES (:name, :synonym_sgml,
  612. (SELECT cvterm_id
  613. FROM {cvterm} CVT
  614. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  615. WHERE CV.name = 'feature_property' and CVT.name = 'synonym')
  616. )
  617. ";
  618. if (!chado_query($synonym_isql, array(':name' => $syn, ':synonym_sgml' => $syn))) {
  619. tripal_report_error('tripal_feature', "Could not add synonym. ", array(), TRIPAL_WARNING);
  620. return;
  621. }
  622. // now get the synonym we just added
  623. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  624. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  625. }
  626. // now add in our new sysnonym
  627. $feature_syn_isql = "
  628. INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id)
  629. VALUES (:synonym_id, :feature_id, :pub_id)";
  630. $args = array(':synonym_id' => $synonym->synonym_id, ':feature_id' => $feature_id, ':pub_id'=> 1);
  631. if (!chado_query($feature_syn_isql, $args)) {
  632. tripal_report_error('tripal_feature', "Could not associate synonym with feature. ", array(), TRIPAL_WARNING);
  633. return;
  634. }
  635. }
  636. }
  637. /**
  638. * Implements hook_load().
  639. *
  640. * When a node is requested by the user this function is called to allow us
  641. * to add auxiliary data to the node object.
  642. *
  643. * @ingroup tripal_legacy_feature
  644. */
  645. function chado_feature_load($nodes) {
  646. foreach ($nodes as $nid => $node) {
  647. // find the feature and add in the details
  648. $feature_id = chado_get_id_from_nid('feature', $nid);
  649. // if the nid does not have a matching record then skip this node.
  650. // this can happen with orphaned nodes.
  651. if (!$feature_id) {
  652. continue;
  653. }
  654. // build the feature variable
  655. $values = array('feature_id' => $feature_id);
  656. $feature = chado_generate_var('feature', $values);
  657. $nodes[$nid]->feature = $feature;
  658. // Now get the title
  659. $node->title = chado_get_node_title($node);
  660. }
  661. }
  662. /**
  663. * Implements hook_node_presave().
  664. * Acts on all content types.
  665. *
  666. * @ingroup tripal_legacy_feature
  667. */
  668. function tripal_feature_node_presave($node) {
  669. // set the title to ensure it is always unique
  670. switch ($node->type) {
  671. // This step is for setting the title for the Drupal node. This title
  672. // is permanent and thus is created to be unique. Title changes provided
  673. // by tokens are generated on the fly dynamically, but the node title
  674. // seen in the content listing needs to be set here. Do not call
  675. // the chado_get_node_title() function here to set the title as the node
  676. // object isn't properly filled out and the function will fail.
  677. case 'chado_feature':
  678. // for a form submission the fields are part of the node object
  679. // but for a sync the fields are in an object of the node
  680. $name = '';
  681. $uname = '';
  682. $type = '';
  683. $organism_id = null;
  684. if(property_exists($node, 'uniquename')) {
  685. $organism_id = $node->organism_id;
  686. $name = $node->name;
  687. $uname = $node->uniquename;
  688. $type = $node->feature_type;
  689. }
  690. else if (property_exists($node, 'feature')) {
  691. $organism_id = $node->feature->organism_id;
  692. $name = $node->feature->name;
  693. $uname = $node->feature->uniquename;
  694. $type = $node->feature->cvtname;
  695. }
  696. $values = array('organism_id' => $organism_id);
  697. $organism = chado_select_record('organism', array('genus', 'species'), $values);
  698. $node->title = "$name, $uname ($type) " . $organism[0]->genus . ' ' . $organism[0]->species;
  699. break;
  700. }
  701. }
  702. /**
  703. * Implements hook_node_insert().
  704. * Acts on all content types.
  705. *
  706. * @ingroup tripal_legacy_feature
  707. */
  708. function tripal_feature_node_insert($node) {
  709. // set the URL path after inserting. We do it here because we do not
  710. // know the feature_id in the presave
  711. switch ($node->type) {
  712. case 'chado_feature':
  713. // We still don't have a fully loaded node object in this hook. Therefore,
  714. // we need to simulate one so that the right values are available for
  715. // the URL to be determined.
  716. $feature_id = chado_get_id_from_nid('feature', $node->nid);
  717. $node->feature = chado_generate_var('feature', array('feature_id' => $feature_id));
  718. // Now use the API to set the path.
  719. chado_set_node_url($node);
  720. // Now get the title.
  721. $node->title = chado_get_node_title($node);
  722. break;
  723. }
  724. }
  725. /**
  726. * Implements hook_node_update().
  727. * Acts on all content types.
  728. *
  729. * @ingroup tripal_legacy_feature
  730. */
  731. function tripal_feature_node_update($node) {
  732. // add items to other nodes, build index and search results
  733. switch ($node->type) {
  734. case 'chado_feature':
  735. // Now use the API to set the path.
  736. chado_set_node_url($node);
  737. // Now get the title
  738. $node->title = chado_get_node_title($node);
  739. break;
  740. }
  741. }
  742. /**
  743. * Implements hook_node_view().
  744. * Acts on all content types.
  745. *
  746. * @ingroup tripal_legacy_feature
  747. */
  748. function tripal_feature_node_view($node, $view_mode, $langcode) {
  749. switch ($node->type) {
  750. case 'chado_feature':
  751. // Show feature browser and counts
  752. if ($view_mode == 'full') {
  753. $node->content['tripal_feature_alignments'] = array(
  754. '#theme' => 'tripal_feature_alignments',
  755. '#node' => $node,
  756. '#tripal_toc_id' => 'alignments',
  757. '#tripal_toc_title' => 'Alignments',
  758. );
  759. $node->content['tripal_feature_analyses'] = array(
  760. '#theme' => 'tripal_feature_analyses',
  761. '#node' => $node,
  762. '#tripal_toc_id' => 'analyses',
  763. '#tripal_toc_title' => 'Analyses',
  764. );
  765. $node->content['tripal_feature_base'] = array(
  766. '#theme' => 'tripal_feature_base',
  767. '#node' => $node,
  768. '#tripal_toc_id' => 'base',
  769. '#tripal_toc_title' => 'Overview',
  770. '#weight' => -100,
  771. );
  772. $node->content['tripal_feature_properties'] = array(
  773. '#theme' => 'tripal_feature_properties',
  774. '#node' => $node,
  775. '#tripal_toc_id' => 'properties',
  776. '#tripal_toc_title' => 'Properties',
  777. );
  778. $node->content['tripal_feature_publications'] = array(
  779. '#theme' => 'tripal_feature_publications',
  780. '#node' => $node,
  781. '#tripal_toc_id' => 'publications',
  782. '#tripal_toc_title' => 'Publications',
  783. );
  784. $node->content['tripal_feature_references'] = array(
  785. '#theme' => 'tripal_feature_references',
  786. '#node' => $node,
  787. '#tripal_toc_id' => 'references',
  788. '#tripal_toc_title' => 'Cross References',
  789. );
  790. $node->content['tripal_feature_relationships'] = array(
  791. '#theme' => 'tripal_feature_relationships',
  792. '#node' => $node,
  793. '#tripal_toc_id' => 'relationships',
  794. '#tripal_toc_title' => 'Relationships',
  795. );
  796. $node->content['tripal_feature_seqence'] = array(
  797. '#theme' => 'tripal_feature_sequence',
  798. '#node' => $node,
  799. '#tripal_toc_id' => 'sequences',
  800. '#tripal_toc_title' => 'Sequences',
  801. );
  802. $node->content['tripal_feature_synonyms'] = array(
  803. '#theme' => 'tripal_feature_synonyms',
  804. '#node' => $node,
  805. '#tripal_toc_id' => 'synonyms',
  806. '#tripal_toc_title' => 'Synonyms',
  807. );
  808. $node->content['tripal_feature_terms'] = array(
  809. '#theme' => 'tripal_feature_terms',
  810. '#node' => $node,
  811. '#tripal_toc_id' => 'terms',
  812. '#tripal_toc_title' => 'Annotated Terms',
  813. );
  814. }
  815. if ($view_mode == 'teaser') {
  816. $node->content['tripal_feature_teaser'] = array(
  817. '#theme' => 'tripal_feature_teaser',
  818. '#node' => $node,
  819. );
  820. }
  821. break;
  822. case 'chado_organism':
  823. // Show feature browser and counts
  824. if ($view_mode == 'full') {
  825. $node->content['tripal_organism_feature_counts'] = array(
  826. '#theme' => 'tripal_organism_feature_counts',
  827. '#node' => $node,
  828. '#tripal_toc_id' => 'feature_counts',
  829. '#tripal_toc_title' => 'Feature Summary',
  830. );
  831. $node->content['tripal_organism_feature_browser'] = array(
  832. '#theme' => 'tripal_organism_feature_browser',
  833. '#node' => $node,
  834. '#tripal_toc_id' => 'feature_browser',
  835. '#tripal_toc_title' => 'Feature Browser',
  836. );
  837. }
  838. break;
  839. // TODO: handle these node types. Should we also have a feature browser?
  840. case 'chado_library':
  841. break;
  842. case 'chado_stock':
  843. break;
  844. case 'chado_analysis':
  845. break;
  846. }
  847. }
  848. /**
  849. * Implements [content_type]_chado_node_default_title_format().
  850. *
  851. * Defines a default title format for the Chado Node API to set the titles on
  852. * Chado Feature nodes based on chado fields.
  853. */
  854. function chado_feature_chado_node_default_title_format() {
  855. return '[feature.name], [feature.uniquename] ([feature.type_id>cvterm.name]) [feature.organism_id>organism.genus] [feature.organism_id>organism.species]';
  856. }
  857. /**
  858. * Implements hook_chado_node_default_url_format().
  859. *
  860. * Designates a default URL format for feature nodes.
  861. */
  862. function chado_feature_chado_node_default_url_format() {
  863. return '/feature/[feature.organism_id>organism.genus]/[feature.organism_id>organism.species]/[feature.type_id>cvterm.name]/[feature.uniquename]';
  864. }