tripal_feature.module 79 KB

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