tripal_feature.module 95 KB

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