tripal_feature.module 78 KB

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