tripal_feature.module 81 KB

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