tripal_feature.module 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. * @defgroup tripal_feature Feature Module
  8. * @ingroup tripal_modules
  9. * @{
  10. * Provides functions for managing chado features including creating details pages for each feature
  11. * @}
  12. */
  13. require_once "api/tripal_feature.api.inc";
  14. require_once "api/tripal_feature.schema.api.inc";
  15. require_once "theme/tripal_feature.theme.inc";
  16. require_once "includes/tripal_feature.admin.inc";
  17. require_once "includes/tripal_feature.sync_features.inc";
  18. require_once "includes/tripal_feature.fasta_loader.inc";
  19. require_once "includes/tripal_feature.gff_loader.inc";
  20. require_once "includes/tripal_feature.seq_extract.inc";
  21. require_once "includes/tripal_feature.delete.inc";
  22. require_once "includes/tripal_feature.form.inc";
  23. /**
  24. *
  25. * @ingroup tripal_feature
  26. */
  27. function tripal_feature_init() {
  28. drupal_add_css(drupal_get_path('module', 'tripal_feature') . '/theme/css/tripal_feature.css');
  29. drupal_add_js(drupal_get_path('module', 'tripal_feature') . '/theme/js/tripal_feature.js');
  30. }
  31. /**
  32. * Implements hook_views_api()
  33. *
  34. * Purpose: Essentially this hook tells drupal that there is views support for
  35. * for this module which then includes tripal_db.views.inc where all the
  36. * views integration code is
  37. *
  38. * @ingroup tripal_feature
  39. */
  40. function tripal_feature_views_api() {
  41. return array(
  42. 'api' => 2.0,
  43. );
  44. }
  45. /**
  46. * Display help and module information
  47. *
  48. * @param
  49. * path which path of the site we're displaying help
  50. * @param
  51. * arg array that holds the current path as would be returned from arg() function
  52. *
  53. * @return
  54. * help text for the path
  55. *
  56. * @ingroup tripal_feature
  57. */
  58. function tripal_feature_help($path, $arg) {
  59. $output = '';
  60. switch ($path) {
  61. case "admin/help#tripal_feature":
  62. $output='<p>' . t("Displays links to nodes created on this date") . '</p>';
  63. break;
  64. }
  65. return $output;
  66. }
  67. /**
  68. * Provide information to drupal about the node types that we're creating
  69. * in this module
  70. *
  71. * @ingroup tripal_feature
  72. */
  73. function tripal_feature_node_info() {
  74. $nodes = array();
  75. $nodes['chado_feature'] = array(
  76. 'name' => t('Feature'),
  77. 'base' => 'chado_feature',
  78. 'description' => t('A feature from the chado database'),
  79. 'has_title' => FALSE,
  80. 'title_label' => t('Feature'),
  81. 'has_body' => FALSE,
  82. 'body_label' => t('Feature Description'),
  83. 'locked' => TRUE
  84. );
  85. return $nodes;
  86. }
  87. /**
  88. * Set the permission types that the chado module uses. Essentially we
  89. * want permissionis that protect creation, editing and deleting of chado
  90. * data objects
  91. *
  92. * @ingroup tripal_feature
  93. */
  94. function tripal_feature_permissions() {
  95. return array(
  96. 'access chado_feature content' => array(
  97. 'title' => t('View Features'),
  98. 'description' => t('Allow users to view feature pages.'),
  99. ),
  100. 'create chado_feature content' => array(
  101. 'title' => t('Create Features'),
  102. 'description' => t('Allow users to create new feature pages.'),
  103. ),
  104. 'delete chado_feature content' => array(
  105. 'title' => t('Delete Features'),
  106. 'description' => t('Allow users to delete feature pages.'),
  107. ),
  108. 'edit chado_feature content' => array(
  109. 'title' => t('Edit Features'),
  110. 'description' => t('Allow users to edit feature pages.'),
  111. ),
  112. 'adminster tripal feature' => array(
  113. 'title' => t('Administer Features'),
  114. 'description' => t('Allow users to administer all features.'),
  115. ),
  116. );
  117. }
  118. /**
  119. * Implement hook_access().
  120. *
  121. * This hook allows node modules to limit access to the node types they define.
  122. *
  123. * @param $node
  124. * The node on which the operation is to be performed, or, if it does not yet exist, the
  125. * type of node to be created
  126. *
  127. * @param $op
  128. * The operation to be performed
  129. *
  130. * @param $account
  131. * A user object representing the user for whom the operation is to be performed
  132. *
  133. * @return
  134. * If the permission for the specified operation is not set then return FALSE. If the
  135. * permission is set then return NULL as this allows other modules to disable
  136. * access. The only exception is when the $op == 'create'. We will always
  137. * return TRUE if the permission is set.
  138. *
  139. * @ingroup tripal_feature
  140. */
  141. function chado_feature_node_access($node, $op, $account) {
  142. if ($op == 'create') {
  143. if (!user_access('create chado_feature content', $account)) {
  144. return FALSE;
  145. }
  146. return TRUE;
  147. }
  148. if ($op == 'update') {
  149. if (!user_access('edit chado_feature content', $account)) {
  150. return FALSE;
  151. }
  152. }
  153. if ($op == 'delete') {
  154. if (!user_access('delete chado_feature content', $account)) {
  155. return FALSE;
  156. }
  157. }
  158. if ($op == 'view') {
  159. if (!user_access('access chado_feature content', $account)) {
  160. return FALSE;
  161. }
  162. }
  163. return NULL;
  164. }
  165. /**
  166. * Menu items are automatically added for the new node types created
  167. * by this module to the 'Create Content' Navigation menu item. This function
  168. * adds more menu items needed for this module.
  169. *
  170. * @ingroup tripal_feature
  171. */
  172. function tripal_feature_menu() {
  173. $items = array();
  174. // the administative settings menu
  175. $items['find/sequences'] = array(
  176. 'title' => 'Sequence Retrieval',
  177. 'description' => 'Download a file of sequences',
  178. 'page callback' => 'tripal_feature_seq_extract_page',
  179. 'access arguments' => array('access chado_feature content'),
  180. 'type' => MENU_CALLBACK,
  181. );
  182. $items['find/sequences/ajax'] = array(
  183. 'title' => 'Sequence Retrieval',
  184. 'page callback' => 'tripal_feature_seq_extract_form_ahah_update',
  185. 'access arguments' => array('access chado_feature content'),
  186. 'type' => MENU_CALLBACK,
  187. );
  188. // the administative settings menu
  189. $items['admin/tripal/chado/tripal_feature'] = array(
  190. 'title' => 'Features',
  191. 'description' => 'A biological sequence or a section of a biological sequence, or a collection of such sections.',
  192. 'page callback' => 'tripal_feature_admin_feature_view',
  193. 'access arguments' => array('administer tripal features'),
  194. 'type' => MENU_NORMAL_ITEM,
  195. );
  196. $items['admin/tripal/chado/tripal_feature/sync'] = array(
  197. 'title' => 'Sync',
  198. 'description' => 'Sync features from Chado with Drupal',
  199. 'page callback' => 'drupal_get_form',
  200. 'page arguments' => array('tripal_feature_sync_form'),
  201. 'access arguments' => array('administer tripal features'),
  202. 'type' => MENU_LOCAL_TASK,
  203. 'weight' => 0
  204. );
  205. $items['admin/tripal/chado/tripal_feature/delete'] = array(
  206. 'title' => ' Delete',
  207. 'description' => 'Delete multiple features from Chado',
  208. 'page callback' => 'drupal_get_form',
  209. 'page arguments' => array('tripal_feature_delete_form'),
  210. 'access arguments' => array('administer tripal features'),
  211. 'type' => MENU_LOCAL_TASK,
  212. 'weight' => 2
  213. );
  214. $items['admin/tripal/chado/tripal_feature/configuration'] = array(
  215. 'title' => 'Settings',
  216. 'description' => 'Configure the Tripal Feature module.',
  217. 'page callback' => 'drupal_get_form',
  218. 'page arguments' => array('tripal_feature_admin'),
  219. 'access arguments' => array('administer tripal features'),
  220. 'type' => MENU_LOCAL_TASK,
  221. 'weight' => 5
  222. );
  223. $items['admin/tripal/chado/tripal_feature/help'] = array(
  224. 'title' => 'Help',
  225. 'description' => 'Help with the Tripal Feature module.',
  226. 'page callback' => 'theme',
  227. 'page arguments' => array('tripal_feature_help'),
  228. 'access arguments' => array('administer tripal features'),
  229. 'type' => MENU_LOCAL_TASK,
  230. 'weight' => 10
  231. );
  232. /** Loaders */
  233. $items['admin/tripal/loaders/fasta_loader'] = array(
  234. 'title' => 'Multi-FASTA file Loader',
  235. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  236. 'page callback' => 'drupal_get_form',
  237. 'page arguments' => array('tripal_feature_fasta_load_form'),
  238. 'access arguments' => array('administer tripal features'),
  239. 'type' => MENU_NORMAL_ITEM,
  240. );
  241. $items['admin/tripal/loaders/gff3_load'] = array(
  242. 'title' => 'GFF3 file Loader',
  243. 'description' => 'Import a GFF3 file into Chado',
  244. 'page callback' => 'drupal_get_form',
  245. 'page arguments' => array('tripal_feature_gff3_load_form'),
  246. 'access arguments' => array('administer tripal features'),
  247. 'type' => MENU_NORMAL_ITEM,
  248. );
  249. // the menu link for addressing any feature (by name, uniquename, synonym)
  250. $items['feature/%'] = array(
  251. 'page callback' => 'tripal_feature_match_features_page',
  252. 'page arguments' => array(1),
  253. 'access arguments' => array('access chado_feature content'),
  254. 'type' => MENU_LOCAL_TASK,
  255. );
  256. // Enable admin view
  257. $items['admin/tripal/chado/tripal_feature/views/features/enable'] = array(
  258. 'title' => 'Enable feature Administrative View',
  259. 'page callback' => 'tripal_views_admin_enable_view',
  260. 'page arguments' => array('tripal_feature_admin_features', 'admin/tripal/chado/tripal_feature'),
  261. 'access arguments' => array('administer tripal feature'),
  262. 'type' => MENU_CALLBACK,
  263. );
  264. return $items;
  265. }
  266. /**
  267. * We need to let drupal know about our theme functions and their arguments.
  268. * We create theme functions to allow users of the module to customize the
  269. * look and feel of the output generated in this module
  270. *
  271. * @ingroup tripal_feature
  272. */
  273. function tripal_feature_theme($existing, $type, $theme, $path) {
  274. $core_path = drupal_get_path('module', 'tripal_core');
  275. $items = array(
  276. 'node__chado_feature' => array(
  277. 'template' => 'node--chado-generic',
  278. 'render element' => 'node',
  279. 'base hook' => 'node',
  280. 'path' => "$core_path/theme",
  281. ),
  282. 'tripal_feature_alignments' => array(
  283. 'variables' => array('node' => NULL),
  284. 'template' => 'tripal_feature_alignments',
  285. 'path' => "$path/theme/tripal_feature",
  286. ),
  287. 'tripal_feature_analyses' => array(
  288. 'variables' => array('node' => NULL),
  289. 'template' => 'tripal_feature_analyses',
  290. 'path' => "$path/theme/tripal_feature",
  291. ),
  292. 'tripal_feature_base' => array(
  293. 'variables' => array('node' => NULL),
  294. 'template' => 'tripal_feature_base',
  295. 'path' => "$path/theme/tripal_feature",
  296. ),
  297. 'tripal_feature_featurepos' => array(
  298. 'arguments' => array('node' => NULL),
  299. 'template' => 'tripal_feature_featurepos',
  300. 'path' => "$path/theme/tripal_feature",
  301. ),
  302. 'tripal_feature_sequence' => array(
  303. 'variables' => array('node' => NULL),
  304. 'template' => 'tripal_feature_sequence',
  305. 'path' => "$path/theme/tripal_feature",
  306. ),
  307. 'tripal_feature_proteins' => array(
  308. 'variables' => array('node' => NULL),
  309. 'template' => 'tripal_feature_proteins',
  310. 'path' => "$path/theme/tripal_feature",
  311. ),
  312. 'tripal_feature_publications' => array(
  313. 'variables' => array('node' => NULL),
  314. 'template' => 'tripal_feature_publications',
  315. 'path' => "$path/theme/tripal_feature",
  316. ),
  317. 'tripal_feature_synonyms' => array(
  318. 'variables' => array('node' => NULL),
  319. 'template' => 'tripal_feature_synonyms',
  320. 'path' => "$path/theme/tripal_feature",
  321. ),
  322. 'tripal_feature_phenotypes' => array(
  323. 'variables' => array('node' => NULL),
  324. 'template' => 'tripal_feature_phenotypes',
  325. 'path' => "$path/theme/tripal_feature",
  326. ),
  327. 'tripal_feature_references' => array(
  328. 'variables' => array('node' => NULL),
  329. 'template' => 'tripal_feature_references',
  330. 'path' => "$path/theme/tripal_feature",
  331. ),
  332. 'tripal_feature_properties' => array(
  333. 'variables' => array('node' => NULL),
  334. 'template' => 'tripal_feature_properties',
  335. 'path' => "$path/theme/tripal_feature",
  336. ),
  337. 'tripal_feature_terms' => array(
  338. 'variables' => array('node' => NULL),
  339. 'template' => 'tripal_feature_terms',
  340. 'path' => "$path/theme/tripal_feature",
  341. ),
  342. 'tripal_feature_relationships' => array(
  343. 'variables' => array('node' => NULL),
  344. 'template' => 'tripal_feature_relationships',
  345. 'path' => "$path/theme/tripal_feature",
  346. ),
  347. 'tripal_feature_help' => array(
  348. 'template' => 'tripal_feature_help',
  349. 'variables' => array(NULL),
  350. 'path' => drupal_get_path('module', 'tripal_feature') . '/theme'
  351. ),
  352. // template for the organism page
  353. 'tripal_organism_feature_browser' => array(
  354. 'variables' => array('node' => NULL),
  355. 'template' => 'tripal_organism_feature_browser',
  356. 'path' => "$path/theme/tripal_organism",
  357. ),
  358. 'tripal_organism_feature_counts' => array(
  359. 'variables' => array('node' => NULL),
  360. 'template' => 'tripal_organism_feature_counts',
  361. 'path' => "$path/theme/tripal_organism",
  362. ),
  363. // themed forms
  364. 'tripal_feature_seq_extract_form' => array(
  365. 'arguments' => array('form'),
  366. ),
  367. // themed teaser
  368. 'tripal_feature_teaser' => array(
  369. 'variables' => array('node' => NULL),
  370. 'template' => 'tripal_feature_teaser',
  371. 'path' => "$path/theme/tripal_feature",
  372. ),
  373. );
  374. return $items;
  375. }
  376. /**
  377. *
  378. *
  379. * @ingroup tripal_feature
  380. */
  381. function tripal_feature_block_info() {
  382. $blocks['references']['info'] = t('Tripal Feature References');
  383. $blocks['references']['cache'] = 'BLOCK_NO_CACHE';
  384. $blocks['base']['info'] = t('Tripal Feature Details');
  385. $blocks['base']['cache'] = 'BLOCK_NO_CACHE';
  386. $blocks['sequence']['info'] = t('Tripal Feature Sequence');
  387. $blocks['sequence']['cache'] = 'BLOCK_NO_CACHE';
  388. $blocks['featureloc_sequences']['info'] = t('Tripal Feature Annotated Sequence');
  389. $blocks['featureloc_sequences']['cache'] = 'BLOCK_NO_CACHE';
  390. $blocks['synonyms']['info'] = t('Tripal Feature Synonyms');
  391. $blocks['synonyms']['cache'] = 'BLOCK_NO_CACHE';
  392. $blocks['properties']['info'] = t('Tripal Feature Properties');
  393. $blocks['properties']['cache'] = 'BLOCK_NO_CACHE';;
  394. $blocks['terms']['info'] = t('Tripal Annotated Terms');
  395. $blocks['terms']['cache'] = 'BLOCK_NO_CACHE';;
  396. $blocks['alignments']['info'] = t('Tripal Feature Alignments');
  397. $blocks['alignments']['cache'] = 'BLOCK_NO_CACHE';
  398. $blocks['relationships']['info'] = t('Tripal Feature Relationships');
  399. $blocks['relationships']['cache'] = 'BLOCK_NO_CACHE';
  400. $blocks['org_feature_counts']['info'] = t('Tripal Organism Feature Counts');
  401. $blocks['org_feature_counts']['cache'] = 'BLOCK_NO_CACHE';
  402. $blocks['org_feature_browser']['info'] = t('Tripal Organism Feature Browser');
  403. $blocks['org_feature_browser']['cache'] = 'BLOCK_NO_CACHE';
  404. return $blocks;
  405. }
  406. /**
  407. *
  408. *
  409. * @ingroup tripal_feature
  410. */
  411. function tripal_feature_block_view($delta = '') {
  412. if (user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  413. $nid = arg(1);
  414. $node = node_load($nid);
  415. $block = array();
  416. switch ($delta) {
  417. case 'references':
  418. $block['subject'] = t('References');
  419. $block['content'] = theme('tripal_feature_references', $node);
  420. break;
  421. case 'base':
  422. $block['subject'] = t('Feature Details');
  423. $block['content'] = theme('tripal_feature_base', $node);
  424. break;
  425. case 'synonyms':
  426. $block['subject'] = t('Synonyms');
  427. $block['content'] = theme('tripal_feature_synonyms', $node);
  428. break;
  429. case 'properties':
  430. $block['subject'] = t('Properties');
  431. $block['content'] = theme('tripal_feature_properties', $node);
  432. break;
  433. case 'terms':
  434. $block['subject'] = t('Annotated Terms');
  435. $block['content'] = theme('tripal_feature_terms', $node);
  436. break;
  437. case 'sequence':
  438. $block['subject'] = t('Sequence');
  439. $block['content'] = theme('tripal_feature_sequence', $node);
  440. break;
  441. case 'featureloc_sequences':
  442. $block['subject'] = t('Formatted Sequences');
  443. $block['content'] = theme('tripal_feature_featureloc_sequences', $node);
  444. break;
  445. case 'alignments':
  446. $block['subject'] = t('Alignments');
  447. $block['content'] = theme('tripal_feature_alignments', $node);
  448. break;
  449. case 'relationships':
  450. $block['subject'] = t('Relationships');
  451. $block['content'] = theme('tripal_feature_relationships', $node);
  452. break;
  453. case 'org_feature_counts':
  454. $block['subject'] = t('Feature Type Summary');
  455. $block['content'] = theme('tripal_organism_feature_counts', array('node' => $node));
  456. break;
  457. case 'org_feature_browser':
  458. $block['subject'] = t('Feature Browser');
  459. $block['content'] = theme('tripal_organism_feature_browser', array('node' => $node));
  460. break;
  461. case 'library_feature_browser':
  462. $block['subject'] = t('Library Feature Browser');
  463. $block['content'] = theme('tripal_library_feature_browser', $node);
  464. break;
  465. case 'analysis_feature_browser':
  466. $block['subject'] = t('Analysis Feature Browser');
  467. $block['content'] = theme('tripal_analysis_feature_browser', $node);
  468. break;
  469. default :
  470. }
  471. return $block;
  472. }
  473. }
  474. /**
  475. * When a new chado_feature node is created we also need to add information
  476. * to our chado_feature table. This function is called on insert of a new node
  477. * of type 'chado_feature' and inserts the necessary information.
  478. *
  479. * @ingroup tripal_feature
  480. */
  481. function chado_feature_insert($node) {
  482. $node->uniquename = trim($node->uniquename);
  483. $node->fname = trim($node->fname);
  484. $node->feature_type = trim($node->feature_type);
  485. $node->residues = trim($node->residues);
  486. // remove spaces, newlines from residues
  487. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  488. $obsolete = 'FALSE';
  489. if ($node->is_obsolete) {
  490. $obsolete = 'TRUE';
  491. }
  492. $feature_id = '';
  493. // if there is an feature_id in the $node object then this must be a sync so
  494. // we can skip adding the feature as it is already there, although
  495. // we do need to proceed with the rest of the insert
  496. if (!property_exists($node, 'feature_id')) {
  497. $values = array(
  498. 'organism_id' => $node->organism_id,
  499. 'name' => $node->fname,
  500. 'uniquename' => $node->uniquename,
  501. 'residues' => $residues,
  502. 'seqlen' => drupal_strlen($residues),
  503. 'is_obsolete' => $obsolete,
  504. 'type_id' => $type[0]->cvterm_id,
  505. 'md5checksum' => md5($residues)
  506. );
  507. $feature = tripal_core_chado_select('feature', array('*'), $values);
  508. if (!$feature) {
  509. drupal_set_message(t('Unable to add feature.'), 'warning');
  510. watchdog('tripal_feature', 'Insert feature: Unable to create feature where values: %values',
  511. array('%values' => print_r($values, TRUE)), WATCHDOG_WARNING);
  512. return;
  513. }
  514. $feature_id = $feature->feature_id;
  515. // add the genbank accession and synonyms
  516. chado_feature_add_synonyms($node->synonyms, $node->feature_id);
  517. }
  518. else {
  519. $feature_id = $node->feature_id;
  520. }
  521. // Make sure the entry for this feature doesn't already exist in the
  522. // chado_feature table if it doesn't exist then we want to add it.
  523. $check_org_id = chado_get_id_for_node('feature', $node->nid);
  524. if (!$check_org_id) {
  525. $record = new stdClass();
  526. $record->nid = $node->nid;
  527. $record->vid = $node->vid;
  528. $record->feature_id = $feature_id;
  529. drupal_write_record('chado_feature', $record);
  530. }
  531. }
  532. /**
  533. *
  534. *
  535. * @ingroup tripal_feature
  536. */
  537. function chado_feature_update($node) {
  538. $node->uniquename = trim($node->uniquename);
  539. $node->fname = trim($node->fname);
  540. $node->feature_type = trim($node->feature_type);
  541. $node->residues = trim($node->residues);
  542. if ($node->revision) {
  543. // there is no way to handle revisions in Chado but leave
  544. // this here just to make not we've addressed it.
  545. }
  546. $residues = preg_replace("/[\n\r\s]/", "", $node->residues);
  547. $obsolete = 'FALSE';
  548. if ($node->is_obsolete) {
  549. $obsolete = 'TRUE';
  550. }
  551. // get the feature type id
  552. $values = array(
  553. 'cv_id' => array(
  554. 'name' => 'sequence'
  555. ),
  556. 'name' => $node->feature_type
  557. );
  558. $type = tripal_core_chado_select('cvterm', array('cvterm_id'), $values);
  559. $feature_id = chado_get_id_for_node('feature', $node->nid) ;
  560. if (sizeof($type) > 0) {
  561. $match = array(
  562. 'feature_id' => $feature_id,
  563. );
  564. $values = array(
  565. 'organism_id' => $node->organism_id,
  566. 'name' => $node->fname,
  567. 'uniquename' => $node->uniquename,
  568. 'residues' => $residues,
  569. 'seqlen' => drupal_strlen($residues),
  570. 'is_obsolete' => $obsolete,
  571. 'type_id' => $type[0]->cvterm_id,
  572. 'md5checksum' => md5($residues)
  573. );
  574. $options = array('return_record' => TRUE);
  575. $status = tripal_core_chado_update('feature', $match, $values, $options);
  576. // add the genbank synonyms
  577. chado_feature_add_synonyms($node->synonyms, $feature_id);
  578. }
  579. else {
  580. drupal_set_message(t('Unable to update feature.'), 'warning');
  581. watchdog('tripal_feature',
  582. 'Update feature: Unable to update feature where values: %values',
  583. array('%values' => print_r($values, TRUE)),
  584. WATCHDOG_WARNING
  585. );
  586. }
  587. }
  588. /**
  589. *
  590. *
  591. * @ingroup tripal_feature
  592. */
  593. function chado_feature_delete($node) {
  594. $feature_id = chado_get_id_for_node('feature', $node->nid);
  595. // if we don't have a library id for this node then this isn't a node of
  596. // type chado_library or the entry in the chado_library table was lost.
  597. if (!$feature_id) {
  598. return;
  599. }
  600. // remove the drupal content
  601. $sql_del = "DELETE FROM {chado_feature} WHERE nid = :nid AND vid = :vid";
  602. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  603. $sql_del = "DELETE FROM {node} WHERE nid = :nid AND vid = :vid";
  604. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  605. $sql_del = "DELETE FROM {node_revision} WHERE nid = :nid AND vid = :vid";
  606. db_query($sql_del, array(':nid' => $node->nid, ':vid' => $node->vid));
  607. // Remove data from feature tables of chado database. This will
  608. // cause a cascade delete and remove all data in referencing tables
  609. // for this feature
  610. chado_query("DELETE FROM {feature} WHERE feature_id = :feature_id", array(':feature_id' => $feature_id));
  611. drupal_set_message(t("The feature and all associated data were removed from") .
  612. "chado");
  613. }
  614. /**
  615. *
  616. *
  617. * @ingroup tripal_feature
  618. */
  619. function chado_feature_add_synonyms($synonyms, $feature_id) {
  620. // separate synomys by carriage returns
  621. $synonyms = preg_replace("/[\n\r]+/", " ", $synonyms);
  622. // split the synonyms into an array based on a space as the delimieter
  623. $syn_array = array();
  624. $syn_array = explode(" ", $synonyms);
  625. // remove any old synonyms
  626. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = :feature_id";
  627. if (!chado_query($feature_syn_dsql, array(':feature_id' => $feature_id))) {
  628. watchdog('tripal_feature', "Could not remove synonyms from feature. ", array(), WATCHDOG_ERROR);
  629. return;
  630. }
  631. // return if we don't have any synonmys to add
  632. if (!$synonyms) {
  633. return;
  634. }
  635. // iterate through each synonym and add it to the database
  636. foreach ($syn_array as $syn) {
  637. // skip this item if it's empty
  638. if (!$syn) {
  639. break;
  640. }
  641. // check to see if we have this accession number already in the database
  642. // if so then don't add it again. it messes up drupal if the insert fails.
  643. // It is possible for the accession number to be present and not the feature
  644. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  645. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  646. if (!$synonym) {
  647. $synonym_isql = "
  648. INSERT INTO {synonym} (name, synonym_sgml, type_id)
  649. VALUES (:name, :synonym_sgml,
  650. (SELECT cvterm_id
  651. FROM {cvterm} CVT
  652. INNER JOIN {cv} ON CVT.cv_id = CV.cv_id
  653. WHERE CV.name = 'feature_property' and CVT.name = 'synonym')
  654. )
  655. ";
  656. if (!chado_query($synonym_isql, array(':name' => $syn, ':synonym_sgml' => $syn))) {
  657. watchdog('tripal_feature', "Could not add synonym. ", array(), WATCHDOG_WARNING);
  658. return;
  659. }
  660. // now get the synonym we just added
  661. $synonym_sql = "SELECT synonym_id FROM {synonym} WHERE name = :name";
  662. $synonym = chado_query($synonym_sql, array(':name' => $syn))->fetchObject();
  663. }
  664. // now add in our new sysnonym
  665. $feature_syn_isql = "
  666. INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id)
  667. VALUES (:synonym_id, :feature_id, :pub_id)";
  668. $args = array(':synonym_id' => $synonym->synonym_id, ':feature_id' => $feature_id, ':pub_id'=> 1);
  669. if (!chado_query($feature_syn_isql, $args)) {
  670. watchdog('tripal_feature', "Could not associate synonym with feature. ", array(), WATCHDOG_WARNING);
  671. return;
  672. }
  673. }
  674. }
  675. /**
  676. * When a node is requested by the user this function is called to allow us
  677. * to add auxiliary data to the node object.
  678. *
  679. * @ingroup tripal_feature
  680. */
  681. function chado_feature_load($nodes) {
  682. foreach ($nodes as $nid => $node) {
  683. // find the feature and add in the details
  684. $feature_id = chado_get_id_for_node('feature', $nid);
  685. // build the feature variable
  686. $values = array('feature_id' => $feature_id);
  687. $feature = tripal_core_generate_chado_var('feature', $values);
  688. $nodes[$nid]->feature = $feature;
  689. // by default, the titles are saved using the unique constraint. We will
  690. // keep it the same, but remove the duplicate name if the unique name and name
  691. // are identical. This doesn't change the title saved in the database, just what is shown
  692. // to the user on the page
  693. $title_type = variable_get('chado_feature_title', 'unique_constraint');
  694. if ($title_type == 'unique_constraint') {
  695. if (strcmp($feature->name, $feature->uniquename)==0) {
  696. $node->title = $feature->name . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  697. }
  698. // in previous version of Tripal, the feature title was simply the unique name.
  699. // so, we recreate the title just to be sure all of our feature pages are consistent
  700. else {
  701. $node->title = $feature->name . ", " . $feature->uniquename . " (" . $feature->type_id->name . ") " . $feature->organism_id->genus . " " . $feature->organism_id->species ;
  702. }
  703. }
  704. // set the title to be the feature name or uniquename as configured
  705. if ($title_type == 'feature_name') {
  706. $node->title = $feature->name;
  707. }
  708. if ($title_type == 'feature_unique_name') {
  709. $node->title = $feature->uniquename;
  710. }
  711. }
  712. }
  713. /**
  714. *
  715. *
  716. * @ingroup tripal_feature
  717. */
  718. function tripal_feature_load_featurelocs($feature_id, $side = 'as_parent', $aggregate = 1) {
  719. $sql = "
  720. SELECT
  721. F.name, F.feature_id, F.uniquename,
  722. FS.name as src_name, FS.feature_id as src_feature_id, FS.uniquename as src_uniquename,
  723. CVT.name as cvname, CVT.cvterm_id,
  724. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  725. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand, FL.phase
  726. FROM {featureloc} FL
  727. INNER JOIN {feature} F ON FL.feature_id = F.feature_id
  728. INNER JOIN {feature} FS ON FS.feature_id = FL.srcfeature_id
  729. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  730. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  731. ";
  732. if (strcmp($side, 'as_parent')==0) {
  733. $sql .= "WHERE FL.srcfeature_id = :feature_id ";
  734. }
  735. if (strcmp($side, 'as_child')==0) {
  736. $sql .= "WHERE FL.feature_id = :feature_id ";
  737. }
  738. $flresults = chado_query($sql, array(':feature_id' => $feature_id));
  739. // copy the results into an array
  740. $i=0;
  741. $featurelocs = array();
  742. while ($loc = $flresults->fetchObject()) {
  743. // if a drupal node exists for this feature then add the nid to the
  744. // results object
  745. $loc->fnid = chado_get_node_id('feature', $loc->feature_id);
  746. $loc->snid = chado_get_node_id('feature', $loc->src_feature_id);
  747. // add the result to the array
  748. $featurelocs[$i++] = $loc;
  749. }
  750. // Add the relationship feature locs if aggregate is turned on
  751. if ($aggregate and strcmp($side, 'as_parent')==0) {
  752. // get the relationships for this feature without substituting any children
  753. // for the parent. We want all relationships
  754. $relationships = tripal_feature_get_aggregate_relationships($feature_id, 0);
  755. foreach ($relationships as $rindex => $rel) {
  756. // get the featurelocs for each of the relationship features
  757. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  758. foreach ($rel_featurelocs as $findex => $rfloc) {
  759. $featurelocs[$i++] = $rfloc;
  760. }
  761. }
  762. }
  763. usort($featurelocs, 'tripal_feature_sort_locations');
  764. return $featurelocs;
  765. }
  766. /**
  767. * used to sort the feature locs by start position
  768. *
  769. * @ingroup tripal_feature
  770. */
  771. function tripal_feature_sort_locations($a, $b) {
  772. return strnatcmp($a->fmin, $b->fmin);
  773. }
  774. /**
  775. *
  776. *
  777. * @ingroup tripal_feature
  778. */
  779. function tripal_feature_load_relationships($feature_id, $side = 'as_subject') {
  780. // get the relationships for this feature. The query below is used for both
  781. // querying the object and subject relationships
  782. $sql = "
  783. SELECT
  784. FS.name as subject_name, FS.uniquename as subject_uniquename,
  785. CVTS.name as subject_type, CVTS.cvterm_id as subject_type_id,
  786. FR.subject_id, FR.type_id as relationship_type_id, FR.object_id, FR.rank,
  787. CVT.name as rel_type,
  788. FO.name as object_name, FO.uniquename as object_uniquename,
  789. CVTO.name as object_type, CVTO.cvterm_id as object_type_id
  790. FROM {feature_relationship} FR
  791. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  792. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  793. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  794. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  795. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  796. ";
  797. if (strcmp($side, 'as_object')==0) {
  798. $sql .= " WHERE FR.object_id = :feature_id";
  799. }
  800. if (strcmp($side, 'as_subject')==0) {
  801. $sql .= " WHERE FR.subject_id = :feature_id";
  802. }
  803. $sql .= " ORDER BY FR.rank";
  804. // get the relationships
  805. $results = chado_query($sql, array(':feature_id' => $feature_id));
  806. // iterate through the relationships, put these in an array and add
  807. // in the Drupal node id if one exists
  808. $i=0;
  809. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = :feature_id";
  810. $relationships = array();
  811. while ($rel = $results->fetchObject()) {
  812. $node = db_query($nodesql, array(':feature_id' => $rel->subject_id))->fetchObject();
  813. if ($node) {
  814. $rel->subject_nid = $node->nid;
  815. }
  816. $node = db_query($nodesql, array(':feature_id' => $rel->object_id))->fetchObject();
  817. if ($node) {
  818. $rel->object_nid = $node->nid;
  819. }
  820. $relationships[$i++] = $rel;
  821. }
  822. return $relationships;
  823. }
  824. /**
  825. *
  826. *
  827. * @ingroup tripal_feature
  828. */
  829. function tripal_feature_get_aggregate_relationships($feature_id, $substitute=1,
  830. $levels=0, $base_type_id=NULL, $depth=0) {
  831. // we only want to recurse to as many levels deep as indicated by the
  832. // $levels variable, but only if this variable is > 0. If 0 then we
  833. // recurse until we reach the end of the relationships tree.
  834. if ($levels > 0 and $levels == $depth) {
  835. return NULL;
  836. }
  837. // first get the relationships for this feature
  838. return tripal_feature_load_relationships($feature_id, 'as_object');
  839. }
  840. /**
  841. *
  842. *
  843. * @ingroup tripal_feature
  844. */
  845. function tripal_feature_load_featureloc_sequences($feature_id, $featurelocs) {
  846. // if we don't have any featurelocs then no point in continuing
  847. if (!$featurelocs) {
  848. return array();
  849. }
  850. // get the list of relationships (including any aggregators) and iterate
  851. // through each one to find information needed to color-code the reference sequence
  852. $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  853. if (!$relationships) {
  854. return array();
  855. }
  856. // iterate through each of the realtionships features and get their
  857. // locations
  858. foreach ($relationships as $rindex => $rel) {
  859. // get the featurelocs for each of the relationship features
  860. $rel_featurelocs = tripal_feature_load_featurelocs($rel->subject_id, 'as_child', 0);
  861. foreach ($rel_featurelocs as $rfindex => $rel_featureloc) {
  862. // keep track of this unique source feature
  863. $src = $rel_featureloc->src_feature_id . "-" . $rel_featureloc->src_cvterm_id;
  864. // copy over the results to the relationship object. Since there can
  865. // be more than one feature location for each relationship feature we
  866. // use the '$src' variable to keep track of these.
  867. $rel->featurelocs = new stdClass();
  868. $rel->featurelocs->$src = new stdClass();
  869. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  870. $rel->featurelocs->$src->src_cvterm_id = $rel_featureloc->src_cvterm_id;
  871. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  872. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  873. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  874. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  875. // keep track of the individual parts for each relationship
  876. $start = $rel->featurelocs->$src->fmin;
  877. $end = $rel->featurelocs->$src->fmax;
  878. $type = $rel->subject_type;
  879. $rel_locs[$src]['parts'][$start][$type]['start'] = $start;
  880. $rel_locs[$src]['parts'][$start][$type]['end'] = $end;
  881. $rel_locs[$src]['parts'][$start][$type]['type'] = $type;
  882. }
  883. }
  884. // the featurelocs array provided to the function contains the locations
  885. // where this feature is found. We want to get the sequence for each
  886. // location and then annotate it with the parts found from the relationships
  887. // locations determiend above.
  888. $floc_sequences = array();
  889. foreach ($featurelocs as $featureloc) {
  890. // build the src name so we can keep track of the different parts for each feature
  891. $src = $featureloc->srcfeature_id->feature_id . "-" . $featureloc->srcfeature_id->type_id->cvterm_id;
  892. // orient the parts to the beginning of the feature sequence
  893. if (!empty($rel_locs[$src]['parts'])) {
  894. $parts = $rel_locs[$src]['parts'];
  895. $rparts = array(); // we will fill this up if we're on the reverse strand
  896. foreach ($parts as $start => $types) {
  897. foreach ($types as $type_name => $type) {
  898. if ($featureloc->strand >= 0) {
  899. // this is on the forward strand. We need to convert the start on the src feature to the
  900. // start on this feature's sequence
  901. $parts[$start][$type_name]['start'] = $parts[$start][$type_name]['start'] - $featureloc->fmin;
  902. $parts[$start][$type_name]['end'] = $parts[$start][$type_name]['end'] - $featureloc->fmin;
  903. $parts[$start][$type_name]['type'] = $type_name;
  904. }
  905. else {
  906. // this is on the reverse strand. We need to swap the start and stop and calculate from the
  907. // begining of the reverse sequence
  908. $size = ($featureloc->fmax - $featureloc->fmin);
  909. $start_orig = $parts[$start][$type_name]['start'];
  910. $end_orig = $parts[$start][$type_name]['end'];
  911. $new_start = $size - ($end_orig - $featureloc->fmin);
  912. $new_end = $size - ($start_orig - $featureloc->fmin);
  913. $rparts[$new_start][$type_name]['start'] = $new_start;
  914. $rparts[$new_start][$type_name]['end'] = $new_end;
  915. $rparts[$new_start][$type_name]['type'] = $type_name;
  916. }
  917. }
  918. }
  919. // now sort the parts
  920. // if we're on the reverse strand we need to resort
  921. if ($featureloc->strand >= 0) {
  922. usort($parts, 'tripal_feature_sort_rel_parts_by_start');
  923. }
  924. else {
  925. usort($rparts, 'tripal_feature_sort_rel_parts_by_start');
  926. $parts = $rparts;
  927. }
  928. $floc_sequences[$src]['src'] = $src;
  929. $floc_sequences[$src]['type'] = $featureloc->feature_id->type_id->name;
  930. $args = array(':feature_id' => $featureloc->srcfeature_id->feature_id);
  931. $start = $featureloc->fmin + 1;
  932. $size = $featureloc->fmax - $featureloc->fmin;
  933. // TODO: fix the hard coded $start and $size
  934. // the $start and $size variables are hard-coded in the SQL statement
  935. // because the db_query function places quotes around all placeholders
  936. // (e.g. :start & :size) and screws up the substring function
  937. $sql = "
  938. SELECT substring(residues from $start for $size) as residues
  939. FROM {feature}
  940. WHERE feature_id = :feature_id
  941. ";
  942. $sequence = chado_query($sql, $args)->fetchObject();
  943. $residues = $sequence->residues;
  944. if ($featureloc->strand < 0) {
  945. $residues = tripal_feature_reverse_complement($residues);
  946. }
  947. $strand = '.';
  948. if ($featureloc->strand == 1) {
  949. $strand = '+';
  950. }
  951. elseif ($featureloc->strand == -1) {
  952. $strand = '-';
  953. }
  954. $defline = $featureloc->feature_id->name . " " . $featureloc->srcfeature_id->name . ":" . ($featureloc->fmin + 1) . ".." . $featureloc->fmax . " " . $strand;
  955. $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence($residues, $parts, $defline);
  956. }
  957. }
  958. return $floc_sequences;
  959. }
  960. /**
  961. *
  962. *
  963. * @ingroup tripal_feature
  964. */
  965. function tripal_feature_get_matched_alignments($feature) {
  966. // This function is for features that align through an intermediate such
  967. // as 'EST_match' or 'match'. This occurs in the case where two sequences
  968. // align but where one does not align perfectly. Some ESTs may be in a contig
  969. // but not all of the EST. Portions may overhang and not be included in the
  970. // consensus if quality is bad.
  971. // For example:
  972. //
  973. // Feature 1: Contig --------------------
  974. // Feature 2: EST_match -------
  975. // Feature 3: EST ---------
  976. //
  977. // The feature provided to the function will always be the feature 1. The
  978. // featureloc columns prefixed with 'right' (e.g. right_fmin) belong to the
  979. // alignment of feature 3 with feature 2
  980. //
  981. // Features may align to more than one feature and are not matches. We do
  982. // not want to include these, so we have to filter on the SO terms:
  983. // match, or %_match
  984. //
  985. $sql = "
  986. SELECT
  987. FL1.featureloc_id as left_featureloc_id,
  988. FL1.srcfeature_id as left_srcfeature_id,
  989. FL1.feature_id as left_feature_id,
  990. FL1.fmin as left_fmin,
  991. FL1.is_fmin_partial as left_is_fmin_partial,
  992. FL1.fmax as left_fmax,
  993. FL1.is_fmax_partial as left_is_fmax_partial,
  994. FL1.strand as left_strand,
  995. FL1.phase as left_phase,
  996. FL1.locgroup as left_locgroup,
  997. FL1.rank as left_rank,
  998. FL2.featureloc_id as right_featureloc_id,
  999. FL2.srcfeature_id as right_srcfeature_id,
  1000. FL2.feature_id as right_feature_id,
  1001. FL2.fmin as right_fmin,
  1002. FL2.is_fmin_partial as right_is_fmin_partial,
  1003. FL2.fmax as right_fmax,
  1004. FL2.is_fmax_partial as right_is_fmax_partial,
  1005. FL2.strand as right_strand,
  1006. FL2.phase as right_phase,
  1007. FL2.locgroup as right_locgroup,
  1008. FL2.rank as right_rank
  1009. FROM {feature} F1
  1010. INNER JOIN {featureloc} FL1 on FL1.srcfeature_id = F1.feature_id
  1011. INNER JOIN {feature} F2 on FL1.feature_id = F2.feature_id
  1012. INNER JOIN {featureloc} FL2 on FL2.feature_id = F2.feature_id
  1013. INNER JOIN {cvterm} CVT2 on F2.type_id = CVT2.cvterm_id
  1014. WHERE
  1015. F1.feature_id = :feature_id AND
  1016. (CVT2.name = 'match' or CVT2.name like '%_match')
  1017. ORDER BY FL1.fmin
  1018. ";
  1019. $results = chado_query($sql, array(':feature_id' => $feature->feature_id));
  1020. // iterate through the results and add them to our featurelocs array
  1021. $featurelocs = array();
  1022. while ($fl = $results->fetchObject()) {
  1023. // ignore featurelocs where the left and right srcfeature is the same
  1024. if (strcmp($fl->left_srcfeature_id, $fl->right_srcfeature_id) == 0) {
  1025. continue;
  1026. }
  1027. $featurelocs[] = $fl ;
  1028. }
  1029. return $featurelocs;
  1030. }
  1031. /**
  1032. *
  1033. *
  1034. * @ingroup tripal_feature
  1035. */
  1036. function tripal_feature_load_organism_feature_counts($organism) {
  1037. // don't show the browser if the settings in the admin page is turned off
  1038. // instead return the array indicating the status of the browser
  1039. $show_counts = variable_get('tripal_feature_summary_setting', 'show_feature_summary');
  1040. if (strcmp($show_counts, 'show_feature_summary')!=0) {
  1041. return array('enabled' => FALSE );
  1042. }
  1043. $args = array();
  1044. $names = array();
  1045. $order = array();
  1046. // build the where clause for the SQL statement if we have a custom term list
  1047. // we'll also keep track of the names the admin provided (if any) and the
  1048. // order that the terms should appear.
  1049. $is_custom = 0;
  1050. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  1051. $where = '';
  1052. if ($temp) {
  1053. $is_custom = 1;
  1054. $temp = explode("\n", $temp);
  1055. $i = 0;
  1056. foreach ($temp as $key => $value) {
  1057. // separate the key value pairs
  1058. $temp2 = explode("=", $value);
  1059. $feature_type = rtrim($temp2[0]);
  1060. $args[] = $feature_type;
  1061. $order[] = $feature_type;
  1062. // if a new name is provided then use that otherwise just
  1063. // use the feature type
  1064. if (count($temp2) == 2) {
  1065. $names[":name$i"] = rtrim($temp2[1]);
  1066. }
  1067. else {
  1068. $names[":name$i"] = $feature_type;
  1069. }
  1070. $where .= " OFC.feature_type = :name$i OR ";
  1071. $i++;
  1072. }
  1073. if ($where) {
  1074. $where = drupal_substr($where, 0, -5); # remove OR from the end
  1075. $where = "($where) AND";
  1076. }
  1077. }
  1078. // get the feature counts. This is dependent on a materialized view
  1079. // installed with the organism module
  1080. $sql = "
  1081. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  1082. FROM {organism_feature_count} OFC
  1083. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  1084. WHERE $where organism_id = :organism_id
  1085. ORDER BY num_features desc
  1086. ";
  1087. $args[':organism_id'] = $organism->organism_id;
  1088. $org_features = chado_query($sql, $args);
  1089. // iterate through the types
  1090. $types = array();
  1091. while ($type = $org_features->fetchObject()) {
  1092. $types[$type->feature_type] = $type;
  1093. // if we don't have an order this means we didn't go through the loop
  1094. // above to set the names, so do that now
  1095. if (!$is_custom) {
  1096. $names[] = $type->feature_type;
  1097. $order[] = $type->feature_type;
  1098. }
  1099. }
  1100. # now reorder the types
  1101. $ordered_types = array();
  1102. foreach ($order as $type) {
  1103. $ordered_types[] = $types[$type];
  1104. }
  1105. return array( 'types' => $ordered_types, 'names' => $names, 'enabled' => TRUE );
  1106. }
  1107. /**
  1108. * used to sort the list of relationship parts by start position
  1109. *
  1110. * @ingroup tripal_feature
  1111. */
  1112. function tripal_feature_sort_rel_parts_by_start($a, $b) {
  1113. foreach ($a as $type_name => $details) {
  1114. $astart = $a[$type_name]['start'];
  1115. break;
  1116. }
  1117. foreach ($b as $type_name => $details) {
  1118. $bstart = $b[$type_name]['start'];
  1119. break;
  1120. }
  1121. return strnatcmp($astart, $bstart);
  1122. }
  1123. /**
  1124. * used to sort the list of relationship parts by start position
  1125. *
  1126. * @ingroup tripal_feature
  1127. */
  1128. function tripal_feature_sort_rel_parts_by_end($a, $b) {
  1129. $val = strnatcmp($b['end'], $a['end']);
  1130. if ($val == 0) {
  1131. return strcmp($a['type'], $b['type']);
  1132. }
  1133. return $val;
  1134. }
  1135. /**
  1136. *
  1137. *
  1138. * @ingroup tripal_feature
  1139. */
  1140. function tripal_feature_color_sequence($sequence, $parts, $defline) {
  1141. $types = array();
  1142. // first get the list of types so we can create a color legend
  1143. foreach ($parts as $index => $t) {
  1144. foreach ($t as $type_name => $details) {
  1145. $types[$type_name] = 1;
  1146. }
  1147. }
  1148. $newseq = "<div id=\"tripal_feature-featureloc_sequence-legend\">Legend: ";
  1149. foreach ($types as $type_name => $present) {
  1150. $newseq .= "<span id=\"tripal_feature-legend-$type_name\" class=\"tripal_feature-legend-item tripal_feature-featureloc_sequence-$type_name\" script=\"\">$type_name</span>";
  1151. }
  1152. $newseq .= "</div>Hold the cursor over a type above to highlight its positions in the sequence below. The colors in the sequence below merge when types overlap.";
  1153. // set the background color of the rows based on the type
  1154. $pos = 0;
  1155. $newseq .= "<pre id=\"tripal_feature-featureloc_sequence\">";
  1156. $newseq .= ">$defline\n";
  1157. // iterate through the parts. They should be in order.
  1158. $ends = array();
  1159. $seqcount = 0;
  1160. foreach ($parts as $index => $types) {
  1161. // get the start for this part. All types in this part start at the
  1162. // same position so we only need the first record
  1163. foreach ($types as $type => $child) {
  1164. $start = $child['start'];
  1165. break;
  1166. }
  1167. // add in the sequence up to the start of this part
  1168. for ($i = $pos; $i < $start; $i++) {
  1169. $newseq .= $sequence{$pos};
  1170. $seqcount++;
  1171. if ($seqcount % 50 == 0) {
  1172. $newseq .= "\n";
  1173. }
  1174. if (array_key_exists($pos, $ends)) {
  1175. foreach ($ends[$pos] as $end) {
  1176. $newseq .= "</span>";
  1177. }
  1178. }
  1179. $pos++;
  1180. }
  1181. // we want to sort the parts by their end. We want the span tag to
  1182. // to be added in the order the parts end.
  1183. usort($types, 'tripal_feature_sort_rel_parts_by_end');
  1184. // now add the child span for all types that start at this position
  1185. foreach ($types as $type) {
  1186. $class = "tripal_feature-featureloc_sequence-" . $type['type'];
  1187. $newseq .= "<span class=\"$class\">";
  1188. // add the end position
  1189. $end = $type['end'];
  1190. $ends[$end][] = $end;
  1191. }
  1192. }
  1193. // add in rest of the sequence
  1194. for ($i = $pos; $i < strlen($sequence); $i++) {
  1195. $newseq .= $sequence{$pos};
  1196. $seqcount++;
  1197. if ($seqcount % 50 == 0) {
  1198. $newseq .= "\n";
  1199. }
  1200. if (array_key_exists($pos, $ends)) {
  1201. foreach ($ends[$pos] as $end) {
  1202. $newseq .= "</span>";
  1203. }
  1204. }
  1205. $pos++;
  1206. }
  1207. $newseq .= "</pre>";
  1208. return $newseq;
  1209. }
  1210. /**
  1211. *
  1212. * @ingroup tripal_feature
  1213. */
  1214. function tripal_feature_node_presave($node) {
  1215. // set the title to ensure it is always unique
  1216. switch ($node->type) {
  1217. case 'chado_feature':
  1218. $values = array('organism_id' => $node->organism_id);
  1219. $organism = tripal_core_chado_select('organism', array('genus', 'species'), $values);
  1220. $node->title = $node->fname . ', ' . $node->uniquename . ' (' . $node->feature_type . ') ' . $organism[0]->genus . ' ' . $organism[0]->species;
  1221. break;
  1222. }
  1223. }
  1224. /**
  1225. *
  1226. * @ingroup tripal_feature
  1227. */
  1228. function tripal_feature_node_insert($node) {
  1229. // set the URL path after inserting. We do it here because we do not
  1230. // know the feature_id in the presave
  1231. switch ($node->type) {
  1232. case 'chado_feature':
  1233. if (!$node->feature_id) {
  1234. $sql = "SELECT * FROM {chado_feature} WHERE nid = :nid";
  1235. $chado_feature = db_query($sql, array(':nid' => $node->nid))->fetchObject();
  1236. $node->feature_id = $chado_feature->feature_id;
  1237. }
  1238. // remove any previous alias
  1239. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1240. // set the URL for this feature page
  1241. $url_alias = tripal_feature_get_feature_url($node);
  1242. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1243. path_save($path_alias);
  1244. break;
  1245. }
  1246. }
  1247. /**
  1248. *
  1249. * @ingroup tripal_feature
  1250. */
  1251. function tripal_feature_node_view($node, $view_mode, $langcode) {
  1252. switch ($node->type) {
  1253. case 'chado_feature':
  1254. // Show feature browser and counts
  1255. if ($view_mode == 'full') {
  1256. $node->content['tripal_feature_alignments'] = array(
  1257. '#value' => theme('tripal_feature_alignments', array('node' => $node)),
  1258. );
  1259. $node->content['tripal_feature_analyses'] = array(
  1260. '#value' => theme('tripal_feature_analyses', array('node' => $node)),
  1261. );
  1262. $node->content['tripal_feature_base'] = array(
  1263. '#value' => theme('tripal_feature_base', array('node' => $node)),
  1264. );
  1265. $node->content['tripal_feature_phenotypes'] = array(
  1266. '#value' => theme('tripal_feature_phenotypes', array('node' => $node)),
  1267. );
  1268. $node->content['tripal_feature_properties'] = array(
  1269. '#value' => theme('tripal_feature_properties', array('node' => $node)),
  1270. );
  1271. $node->content['tripal_feature_publications'] = array(
  1272. '#value' => theme('tripal_feature_properties', array('node' => $node)),
  1273. );
  1274. $node->content['tripal_feature_references'] = array(
  1275. '#value' => theme('tripal_feature_references', array('node' => $node)),
  1276. );
  1277. $node->content['tripal_feature_relationships'] = array(
  1278. '#value' => theme('tripal_feature_relationships', array('node' => $node)),
  1279. );
  1280. $node->content['tripal_feature_seqence'] = array(
  1281. '#value' => theme('tripal_feature_sequence', array('node' => $node)),
  1282. );
  1283. $node->content['tripal_feature_synonyms'] = array(
  1284. '#value' => theme('tripal_feature_synonyms', array('node' => $node)),
  1285. );
  1286. $node->content['tripal_feature_terms'] = array(
  1287. '#value' => theme('tripal_feature_terms', array('node' => $node)),
  1288. );
  1289. }
  1290. if ($view_mode == 'teaser') {
  1291. $node->content['tripal_feature_teaser'] = array(
  1292. '#value' => theme('tripal_feature_teaser', array('node' => $node)),
  1293. );
  1294. }
  1295. break;
  1296. case 'chado_organism':
  1297. // Show feature browser and counts
  1298. if ($view_mode == 'full') {
  1299. $node->content['tripal_organism_feature_counts'] = array(
  1300. '#value' => theme('tripal_organism_feature_counts', array('node' => $node)),
  1301. );
  1302. $node->content['tripal_organism_feature_browser'] = array(
  1303. '#value' => theme('tripal_organism_feature_browser', array('node' => $node)),
  1304. );
  1305. }
  1306. break;
  1307. // TODO: handle these node types. Should we also have a feature browser?
  1308. case 'chado_library':
  1309. break;
  1310. case 'chado_stock':
  1311. break;
  1312. case 'chado_analysis':
  1313. break;
  1314. }
  1315. }
  1316. /**
  1317. *
  1318. * @ingroup tripal_feature
  1319. */
  1320. function tripal_feature_node_update($node) {
  1321. // add items to other nodes, build index and search results
  1322. switch ($node->type) {
  1323. case 'chado_feature':
  1324. // remove any previous alias
  1325. db_query("DELETE FROM {url_alias} WHERE source = :source", array(':source' => "node/$node->nid"));
  1326. // set the URL for this feature page
  1327. $url_alias = tripal_feature_get_feature_url($node);
  1328. $path_alias = array("source" => "node/$node->nid", "alias" => $url_alias);
  1329. path_save($path_alias);
  1330. break;
  1331. }
  1332. }
  1333. /**
  1334. *
  1335. *
  1336. * @ingroup tripal_feature
  1337. */
  1338. function tripal_feature_cv_chart($chart_id) {
  1339. // we only want the chart to show feature types setup by the admin
  1340. $temp = rtrim(variable_get('tripal_feature_summary_report_mapping', ''));
  1341. $where = '';
  1342. if ($temp) {
  1343. $temp = explode("\n", $temp);
  1344. foreach ($temp as $key => $value) {
  1345. $temp2 = explode("=", $value);
  1346. $feature_type = rtrim($temp2[0]);
  1347. $where .= "CNT.feature_type = '$feature_type' OR \n";
  1348. }
  1349. if ($where) {
  1350. $where = drupal_substr($where, 0, -5); # remove OR from the end
  1351. $where = "($where) AND";
  1352. }
  1353. }
  1354. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/", "$1", $chart_id);
  1355. // The CV module will create the JSON array necessary for buillding a
  1356. // pie chart using jgChart and Google Charts. We have to pass to it
  1357. // a table that contains count information, tell it which column
  1358. // contains the cvterm_id and provide a filter for getting the
  1359. // results we want from the table.
  1360. $options = array(
  1361. count_mview => 'organism_feature_count',
  1362. cvterm_id_column => 'cvterm_id',
  1363. count_column => 'num_features',
  1364. size => '550x200',
  1365. filter => "$where CNT.organism_id = $organism_id",
  1366. );
  1367. return $options;
  1368. }
  1369. /**
  1370. *
  1371. *
  1372. * @ingroup tripal_feature
  1373. */
  1374. function tripal_feature_cv_tree($tree_id) {
  1375. // The CV module will create the JSON array necessary for buillding a
  1376. // pie chart using jgChart and Google Charts. We have to pass to it
  1377. // a table that contains count information, tell it which column
  1378. // contains the cvterm_id and provide a filter for getting the
  1379. // results we want from the table.
  1380. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/", "$1", $tree_id);
  1381. $options = array(
  1382. cv_id => tripal_cv_get_cv_id('sequence'),
  1383. count_mview => 'organism_feature_count',
  1384. cvterm_id_column => 'cvterm_id',
  1385. count_column => 'num_features',
  1386. filter => "CNT.organism_id = $organism_id",
  1387. label => 'Features',
  1388. );
  1389. return $options;
  1390. }
  1391. /**
  1392. *
  1393. *
  1394. * @ingroup tripal_feature
  1395. */
  1396. function tripal_feature_del_vocabulary() {
  1397. //include the file containing the required functions for adding taxonomy vocabs
  1398. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1399. // get the vocabularies
  1400. $vocabularies = taxonomy_get_vocabularies();
  1401. // These taxonomic terms are hard coded because we
  1402. // know we have these relationships in the chado tables
  1403. // through foreign key relationships. The tripal
  1404. // modules that correspond to these chado "modules" don't
  1405. // need to be installed for the taxonomy to work.
  1406. foreach ($vocabularies as $vocab) {
  1407. if ($vocab->name == 'Feature Type') {
  1408. taxonomy_vocabulary_delete($vocab->vid);
  1409. }
  1410. if ($vocab->name == 'Organism') {
  1411. taxonomy_vocabulary_delete($vocab->vid);
  1412. }
  1413. if ($vocab->name == 'Library') {
  1414. taxonomy_vocabulary_delete($vocab->vid);
  1415. }
  1416. if ($vocab->name == 'Analysis') {
  1417. taxonomy_vocabulary_delete($vocab->vid);
  1418. }
  1419. }
  1420. }
  1421. /**
  1422. *
  1423. *
  1424. * @ingroup tripal_feature
  1425. */
  1426. function tripal_feature_job_describe_args($callback, $args) {
  1427. $new_args = array();
  1428. if ($callback == 'tripal_feature_load_fasta') {
  1429. $new_args['FASTA file'] = $args[0];
  1430. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  1431. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1432. $new_args['Sequence Type'] = $args[2];
  1433. $new_args['Name Match Type'] = $args[14];
  1434. $new_args['Name RE'] = $args[4];
  1435. $new_args['Unique Name RE'] = $args[5];
  1436. // add in the relationship arguments
  1437. $new_args['Relationship Type'] = $args[8];
  1438. $new_args['Relationship Parent RE'] = $args[9];
  1439. $new_args['Relationship Parent Type'] = $args[10];
  1440. // add in the database reference arguments
  1441. if ($args[7]) {
  1442. $db = tripal_core_chado_select('db', array('name'), array('db_id' => $args[7]));
  1443. }
  1444. $new_args['Database Reference'] = $db[0]->name;
  1445. $new_args['Accession RE'] = $args[6];
  1446. $new_args['Method'] = $args[11];
  1447. // add in the analysis
  1448. if ($args[13]) {
  1449. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[13]));
  1450. }
  1451. $new_args['Analysis'] = $analysis[0]->name;
  1452. }
  1453. if ($callback == 'tripal_feature_delete_features') {
  1454. if ($args[0]) {
  1455. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  1456. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1457. }
  1458. else {
  1459. $new_args['Organism'] = '';
  1460. }
  1461. if ($args[1]) {
  1462. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[1]));
  1463. $new_args['Analysis'] = $analysis[0]->name;
  1464. }
  1465. else {
  1466. $new_args['Analysis'] = '';
  1467. }
  1468. $new_args['Sequence Type'] = $args[2];
  1469. $new_args['Is Unique Name'] = $args[3];
  1470. $new_args['Features Names'] = $args[4];
  1471. }
  1472. elseif ($callback == 'tripal_feature_load_gff3') {
  1473. $new_args['GFF File'] = $args[0];
  1474. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[1]));
  1475. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1476. $analysis = tripal_core_chado_select('analysis', array('name'), array('analysis_id' => $args[2]));
  1477. $new_args['Analysis'] = $analysis[0]->name;
  1478. $new_args['Use a Transaction'] = ($args[7] == 1) ? "Yes" : "No";
  1479. $new_args['Import only new features'] = ($args[3] == 1) ? "Yes" : "No";
  1480. $new_args['Import all and update'] = ($args[4] == 1) ? "Yes" : "No";
  1481. $new_args['Import all and replace'] = ($args[5] == 1) ? "Yes" : "No";
  1482. $new_args['Delete features'] = ($args[6] == 1) ? "Yes" : "No";
  1483. if ($args[8]) {
  1484. $target_organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[8]));
  1485. $new_args['Target organism'] = $target_organism[0]->genus . " " . $target_organism[0]->species;
  1486. }
  1487. else {
  1488. $new_args['Target organism'] = '';
  1489. }
  1490. $new_args['Target type'] = $args[9];
  1491. $new_args['Create target'] = ($args[10] == 1) ? "Yes" : "No";
  1492. $new_args['Starting line'] = $args[11];
  1493. $new_args['Landmark Type'] = $args[12];
  1494. $new_args['Alternate ID attribute'] = $args[13];
  1495. $new_args['Create Organism'] = ($args[14] == 1) ? "Yes" : "No";
  1496. }
  1497. if ($callback == 'tripal_feature_sync_features') {
  1498. if ($args[0]) {
  1499. $organism = tripal_core_chado_select('organism', array('genus', 'species'), array('organism_id' => $args[0]));
  1500. $new_args['Organism'] = $organism[0]->genus . " " . $organism[0]->species;
  1501. }
  1502. else {
  1503. $new_args['Organism'] = '';
  1504. }
  1505. $new_args['Feature Types'] = $args[1];
  1506. }
  1507. return $new_args;
  1508. }
  1509. /**
  1510. * Implements hook_coder_ignore().
  1511. * Defines the path to the file (tripal_core.coder_ignores.txt) where ignore rules for coder are stored
  1512. */
  1513. function tripal_feature_coder_ignore() {
  1514. return array(
  1515. 'path' => drupal_get_path('module', 'tripal_feature'),
  1516. 'line prefix' => drupal_get_path('module', 'tripal_feature'),
  1517. );
  1518. }
  1519. /*
  1520. * Uses the value provided in the $id argument to find all features that match
  1521. * that ID by name, featurename or synonym. If it matches uniquenly to a single
  1522. * feature it will redirect to that feature page, otherwise, a list of matching
  1523. * features is shown.
  1524. */
  1525. function tripal_feature_match_features_page($id) {
  1526. // if the URL alias configuration is set such that the URL
  1527. // always begins with 'feature' then we want to use the ID as it is and
  1528. // forward it on. Otherwise, try to find the matching feature.
  1529. $url_alias = variable_get('chado_feature_url_string', '/feature/[genus]/[species]/[type]/[uniquename]');
  1530. if (!$url_alias) {
  1531. $url_alias = '/feature/[genus]/[species]/[type]/[uniquename]';
  1532. }
  1533. $url_alias = preg_replace('/^\//', '', $url_alias); // remove any preceeding forward slash
  1534. if (preg_match('/^feature\//', $url_alias)) {
  1535. drupal_goto($id);
  1536. }
  1537. $sql = "
  1538. SELECT
  1539. F.name, F.uniquename, F.feature_id,
  1540. O.genus, O.species, O.organism_id,
  1541. CVT.cvterm_id, CVT.name as type_name,
  1542. CF.nid,
  1543. array_agg(S.name) as synonyms
  1544. FROM {feature} F
  1545. INNER JOIN {organism} O on F.organism_id = O.organism_id
  1546. INNER JOIN {cvterm} CVT on CVT.cvterm_id = F.type_id
  1547. LEFT JOIN {feature_synonym} FS on FS.feature_id = F.feature_id
  1548. LEFT JOIN {synonym} S on S.synonym_id = FS.synonym_id
  1549. INNER JOIN public.chado_feature CF on CF.feature_id = F.feature_id
  1550. WHERE
  1551. F.uniquename = :uname or
  1552. F.name = :fname' or
  1553. S.name = :sname
  1554. GROUP BY F.name, F.uniquename, F.feature_id, O.genus, O.species,
  1555. O.organism_id, CVT.cvterm_id, CVT.name, CF.nid
  1556. ";
  1557. $results = chado_query($sql, array(':uname' => $id, ':fname' => $id, ':sname' => $id));
  1558. $num_matches = 0;
  1559. // iterate through the matches and build the table for showing matches
  1560. $header = array('Uniquename', 'Name', 'Type', 'Species', 'Synonyms');
  1561. $rows = array();
  1562. $curr_match;
  1563. while ($match = $results->fetchObject()) {
  1564. $curr_match = $match;
  1565. $synonyms = $match->synonyms;
  1566. $synonyms = preg_replace('/[\"\{\}]/', '', $synonyms);
  1567. $rows[] = array(
  1568. $match->uniquename,
  1569. "<a href=\"" . url("node/" . $match->nid) . "\">" . $match->name . "</a>",
  1570. $match->type_name,
  1571. '<i>' . $match->genus . ' ' . $match->species . '</i>',
  1572. $synonyms,
  1573. );
  1574. $num_matches++;
  1575. }
  1576. // if we have more than one match then generate the table, otherwise, redirect
  1577. // to the matched feature
  1578. if ($num_matches == 1) {
  1579. drupal_goto("node/" . $curr_match->nid);
  1580. }
  1581. if ($num_matches == 0) {
  1582. return "<p>No features matched the given name '$id'</p>";
  1583. }
  1584. $table_attrs = array(
  1585. 'class' => 'tripal-table tripal-table-horz'
  1586. );
  1587. $output = "<p>The following features match the name '$id'.</p>";
  1588. $output .= theme_table($header, $rows, $table_attrs, $caption);
  1589. return $output;
  1590. }
  1591. /**
  1592. * Implementation of hook_form_alter()
  1593. *
  1594. * @param $form
  1595. * @param $form_state
  1596. * @param $form_id
  1597. */
  1598. function tripal_feature_form_alter(&$form, &$form_state, $form_id) {
  1599. if ($form_id == "tripal_feature_seq_extract_form") {
  1600. // updating the form through the ahah callback sets the action of
  1601. // the form to the ahah callback URL. We need to set it back
  1602. // to the normal form URL
  1603. $form['#action'] = url("find/sequences");
  1604. }
  1605. // turn off preview button for insert/updates
  1606. if ($form_id == "chado_feature_node_form") {
  1607. $form['actions']['preview']['#access'] = FALSE;
  1608. }
  1609. }