tripal_feature.module 92 KB

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