tripal_feature.module 95 KB

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