tripal_feature.module 76 KB

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