tripal_feature.module 94 KB

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