tripal_feature.module 88 KB

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