tripal_feature.module 93 KB

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