tripal_feature.module 93 KB

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