tripal_feature.module 81 KB

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