tripal_feature.module 89 KB

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