tripal_feature.module 96 KB

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