tripal_feature.module 81 KB

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