tripal_feature.module 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. <?php
  2. //
  3. // Copyright 2009 Clemson University
  4. //
  5. require_once "syncFeatures.php";
  6. require_once "indexFeatures.php";
  7. /*************************************************************************
  8. *
  9. */
  10. function tripal_feature_init(){
  11. // add the jGCharts JS and CSS
  12. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_feature.js');
  13. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jgcharts/jgcharts.js');
  14. }
  15. /*************************************************************************
  16. * Implements hook_views_api()
  17. * Purpose: Essentially this hook tells drupal that there is views support for
  18. * for this module which then includes tripal_db.views.inc where all the
  19. * views integration code is
  20. */
  21. function tripal_feature_views_api() {
  22. return array(
  23. 'api' => 2.0,
  24. );
  25. }
  26. /************************************************************************
  27. *
  28. */
  29. function tripal_feature_admin () {
  30. // before proceeding check to see if we have any
  31. // currently processing jobs. If so, we don't want
  32. // to give the opportunity to sync libraries
  33. $active_jobs = FALSE;
  34. if(tripal_get_module_active_jobs('tripal_feature')){
  35. $active_jobs = TRUE;
  36. }
  37. if(!$active_jobs){
  38. $form['chado_feature_accession_prefix'] = array (
  39. '#title' => t('Accession Prefix'),
  40. '#type' => t('textfield'),
  41. '#description' => t("Accession numbers for features consist of the ".
  42. "chado feature_id and a site specific prefix. Set the prefix that ".
  43. "will be incorporated in front of each feature_id to form a unique ".
  44. "accession number for this site."),
  45. '#required' => TRUE,
  46. '#default_value' => variable_get('chado_feature_accession_prefix','ID'),
  47. );
  48. $form['chado_feature_types'] = array(
  49. '#title' => t('Feature Types'),
  50. '#type' => 'textarea',
  51. '#description' => t('Enter the names of the sequence types that the ".
  52. "site will support with independent pages. Pages for these data ".
  53. "types will be built automatically for features that exist in the ".
  54. "chado database. The names listed here should be spearated by ".
  55. "spaces or entered separately on new lines. The names must match ".
  56. "exactly (spelling and case) with terms in the sequence ontology'),
  57. '#required' => TRUE,
  58. '#default_value' => variable_get('chado_feature_types','EST contig'),
  59. );
  60. $form['browser'] = array(
  61. '#type' => 'fieldset',
  62. '#title' => t('Feature Browser')
  63. );
  64. $allowedoptions1 = array (
  65. 'show_feature_browser' => "Show the feature browser on the organism page. The browser loads when page loads. This may be slow for large sites.",
  66. 'hide_feature_browser' => "Hide the feature browser on the organism page. Disables the feature browser completely.",
  67. );
  68. // $allowedoptions ['allow_feature_browser'] = "Allow loading of the feature browsing through AJAX. For large sites the initial page load will be quick with the feature browser loading afterwards.";
  69. $form['browser']['browse_features'] = array(
  70. '#title' => 'Feature Browser on Organism Page',
  71. '#description' => 'A feature browser can be added to an organism page to allow users to quickly '.
  72. 'access a feature. This will most likely not be the ideal mechanism for accessing feature '.
  73. 'information, especially for large sites, but it will alow users exploring the site (such '.
  74. 'as students) to better understand the data types available on the site.',
  75. '#type' => 'radios',
  76. '#options' => $allowedoptions1,
  77. '#default_value'=>variable_get('tripal_feature_browse_setting', 'show_feature_browser'),
  78. );
  79. $form['browser']['set_browse_button'] = array(
  80. '#type' => 'submit',
  81. '#value' => t('Set Browser'),
  82. '#weight' => 2,
  83. );
  84. $form['summary'] = array(
  85. '#type' => 'fieldset',
  86. '#title' => t('Feature Summary')
  87. );
  88. $allowedoptions2 ['show_feature_summary'] = "Show the feature summary on the organism page. The summary loads when page loads.";
  89. $allowedoptions2 ['hide_feature_summary'] = "Hide the feature summary on the organism page. Disables the feature summary.";
  90. $form['summary']['feature_summary'] = array(
  91. '#title' => 'Feature Summary on Organism Page',
  92. '#description' => 'A feature summary can be added to an organism page to allow users to see the '.
  93. 'type and quantity of features available for the organism.',
  94. '#type' => 'radios',
  95. '#options' => $allowedoptions2,
  96. '#default_value'=>variable_get('tripal_feature_summary_setting', 'show_feature_summary'),
  97. );
  98. $form['summary']['set_summary_button'] = array(
  99. '#type' => 'submit',
  100. '#value' => t('Set Summary'),
  101. '#weight' => 2,
  102. );
  103. get_tripal_feature_admin_form_sync_set($form);
  104. get_tripal_feature_admin_form_taxonomy_set($form);
  105. get_tripal_feature_admin_form_reindex_set($form);
  106. get_tripal_feature_admin_form_cleanup_set($form);
  107. } else {
  108. $form['notice'] = array(
  109. '#type' => 'fieldset',
  110. '#title' => t('Feature Management Temporarily Unavailable')
  111. );
  112. $form['notice']['message'] = array(
  113. '#value' => t('Currently, feature management jobs are waiting or ".
  114. "are running. Managemment features have been hidden until these ".
  115. "jobs complete. Please check back later once these jobs have ".
  116. "finished. You can view the status of pending jobs in the Tripal ".
  117. "jobs page.'),
  118. );
  119. }
  120. return system_settings_form($form);
  121. }
  122. /************************************************************************
  123. *
  124. */
  125. function tripal_feature_admin_validate($form, &$form_state) {
  126. global $user; // we need access to the user info
  127. $job_args = array();
  128. // if the user wants to sync up the chado features then
  129. // add the job to the management queue
  130. if ($form_state['values']['op'] == t('Sync all Features')) {
  131. tripal_add_job('Sync all features','tripal_feature',
  132. 'tripal_feature_sync_features',$job_args,$user->uid);
  133. }
  134. if ($form_state['values']['op'] == t('Set/Reset Taxonomy for all feature nodes')) {
  135. tripal_add_job('Set all feature taxonomy','tripal_feature',
  136. 'tripal_features_set_taxonomy',$job_args,$user->uid);
  137. }
  138. if ($form_state['values']['op'] == t('Reindex all feature nodes')) {
  139. tripal_add_job('Reindex all features','tripal_feature',
  140. 'tripal_features_reindex',$job_args,$user->uid);
  141. }
  142. if ($form_state['values']['op'] == t('Clean up orphaned features')) {
  143. tripal_add_job('Cleanup orphaned features','tripal_feature',
  144. 'tripal_features_cleanup',$job_args,$user->uid);
  145. }
  146. if ($form_state['values']['op'] == t('Set Browser')) {
  147. variable_set('tripal_feature_browse_setting',$form_state['values']['browse_features']);
  148. }
  149. if ($form_state['values']['op'] == t('Set Summary')) {
  150. variable_set('tripal_feature_summary_setting',$form_state['values']['feature_summary']);
  151. }
  152. }
  153. /************************************************************************
  154. *
  155. */
  156. function get_tripal_feature_admin_form_cleanup_set(&$form) {
  157. $form['cleanup'] = array(
  158. '#type' => 'fieldset',
  159. '#title' => t('Clean Up')
  160. );
  161. $form['cleanup']['description'] = array(
  162. '#type' => 'item',
  163. '#value' => t("With Drupal and chado residing in different databases ".
  164. "it is possible that nodes in Drupal and features in Chado become ".
  165. "\"orphaned\". This can occur if a feature node in Drupal is ".
  166. "deleted but the corresponding chado feature is not and/or vice ".
  167. "versa. The Cleanup function will also remove nodes for features ".
  168. "that are not in the list of allowed feature types as specified ".
  169. "above. This is helpful when a feature type needs to be ".
  170. "removed but was previously present as Drupal nodes. ".
  171. "Click the button below to resolve these discrepancies."),
  172. '#weight' => 1,
  173. );
  174. $form['cleanup']['button'] = array(
  175. '#type' => 'submit',
  176. '#value' => t('Clean up orphaned features'),
  177. '#weight' => 2,
  178. );
  179. }
  180. /************************************************************************
  181. *
  182. */
  183. function get_tripal_feature_admin_form_reindex_set(&$form) {
  184. $form['reindex'] = array(
  185. '#type' => 'fieldset',
  186. '#title' => t('Reindex')
  187. );
  188. $form['reindex']['description'] = array(
  189. '#type' => 'item',
  190. '#value' => t("Reindexing of nodes is important when content for nodes ".
  191. "is updated external to drupal, such as external uploads to chado. ".
  192. "Features need to be reindexed to ensure that updates to features ".
  193. "are searchable. Depending on the number of features this may take ".
  194. "quite a while. Click the button below to begin reindexing of ".
  195. "features."),
  196. '#weight' => 1,
  197. );
  198. $form['reindex']['button'] = array(
  199. '#type' => 'submit',
  200. '#value' => t('Reindex all feature nodes'),
  201. '#weight' => 2,
  202. );
  203. }
  204. /************************************************************************
  205. *
  206. */
  207. function get_tripal_feature_admin_form_taxonomy_set (&$form) {
  208. $form['taxonomy'] = array(
  209. '#type' => 'fieldset',
  210. '#title' => t('Set Taxonomy')
  211. );
  212. $form['taxonomy']['description'] = array(
  213. '#type' => 'item',
  214. '#value' => t("Drupal allows for assignment of \"taxonomy\" or ".
  215. "catagorical terms to nodes. These terms allow for advanced ".
  216. "filtering during searching."),
  217. '#weight' => 1,
  218. );
  219. $tax_options = array (
  220. 'organism' => t('Organism name'),
  221. 'feature_type' => t('Feature Type (e.g. EST, mRNA, etc.)'),
  222. 'analysis' => t('Analysis Name'),
  223. 'library' => t('Library Name'),
  224. );
  225. $form['taxonomy']['tax_classes'] = array (
  226. '#title' => t('Available Taxonomic Classes'),
  227. '#type' => t('checkboxes'),
  228. '#description' => t("Please select the class of terms to assign to ".
  229. "chado features"),
  230. '#required' => FALSE,
  231. '#prefix' => '<div id="taxclass_boxes">',
  232. '#suffix' => '</div>',
  233. '#options' => $tax_options,
  234. '#weight' => 2,
  235. '#default_value' => variable_get('tax_classes',''),
  236. );
  237. $form['taxonomy']['button'] = array(
  238. '#type' => 'submit',
  239. '#value' => t('Set/Reset Taxonomy for all feature nodes'),
  240. '#weight' => 3,
  241. );
  242. }
  243. /************************************************************************
  244. *
  245. */
  246. function get_tripal_feature_admin_form_sync_set (&$form) {
  247. // get the list of organisms which will be synced.
  248. $feature_sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' and organism_id = %d";
  249. $previous_db = tripal_db_set_active('chado');
  250. $feature = db_fetch_object(db_query($feature_sql,$node->title,$node->organism_id));
  251. tripal_db_set_active($previous_db);
  252. // define the fieldsets
  253. $form['sync'] = array(
  254. '#type' => 'fieldset',
  255. '#title' => t('Sync Features')
  256. );
  257. $form['sync']['description'] = array(
  258. '#type' => 'item',
  259. '#value' => t("Click the 'Sync all Features' button to create Drupal ".
  260. "content for features in chado. Only features of the types listed ".
  261. "above in the Feature Types box will be synced. Depending on the ".
  262. "number of features in the chado database this may take a long ".
  263. "time to complete. "),
  264. '#weight' => 1,
  265. );
  266. $orgs = tripal_organism_get_synced();
  267. $org_list = '';
  268. foreach($orgs as $org){
  269. $org_list .= "$org->genus $org->species, ";
  270. }
  271. $form['sync']['description2'] = array(
  272. '#type' => 'item',
  273. '#value' => "Only features for the following organisms will be synced: ".
  274. " $org_list",
  275. '#weight' => 1,
  276. );
  277. $form['sync']['button'] = array(
  278. '#type' => 'submit',
  279. '#value' => t('Sync all Features'),
  280. '#weight' => 3,
  281. );
  282. }
  283. /************************************************************************
  284. * Display help and module information
  285. * @param path which path of the site we're displaying help
  286. * @param arg array that holds the current path as would be returned from arg() function
  287. * @return help text for the path
  288. */
  289. function tripal_feature_help($path, $arg) {
  290. $output = '';
  291. switch ($path) {
  292. case "admin/help#tripal_feature":
  293. $output='<p>'.t("Displays links to nodes created on this date").'</p>';
  294. break;
  295. }
  296. return $output;
  297. }
  298. /************************************************************************
  299. * Provide information to drupal about the node types that we're creating
  300. * in this module
  301. */
  302. function tripal_feature_node_info() {
  303. $nodes = array();
  304. $nodes['chado_feature'] = array(
  305. 'name' => t('Feature'),
  306. 'module' => 'chado_feature',
  307. 'description' => t('A feature from the chado database'),
  308. 'has_title' => FALSE,
  309. 'title_label' => t('Feature'),
  310. 'has_body' => FALSE,
  311. 'body_label' => t('Feature Description'),
  312. 'locked' => TRUE
  313. );
  314. return $nodes;
  315. }
  316. /************************************************************************
  317. * Set the permission types that the chado module uses. Essentially we
  318. * want permissionis that protect creation, editing and deleting of chado
  319. * data objects
  320. */
  321. function tripal_feature_perm(){
  322. return array(
  323. 'access chado_feature content',
  324. 'create chado_feature content',
  325. 'delete chado_feature content',
  326. 'edit chado_feature content',
  327. );
  328. }
  329. /************************************************************************
  330. * Set the permission types that the module uses.
  331. */
  332. function chado_feature_access($op, $node, $account) {
  333. if ($op == 'create') {
  334. return user_access('create chado_feature content', $account);
  335. }
  336. if ($op == 'update') {
  337. if (user_access('edit chado_feature content', $account)) {
  338. return TRUE;
  339. }
  340. }
  341. if ($op == 'delete') {
  342. if (user_access('delete chado_feature content', $account)) {
  343. return TRUE;
  344. }
  345. }
  346. if ($op == 'view') {
  347. if (user_access('access chado_feature content', $account)) {
  348. return TRUE;
  349. }
  350. }
  351. return FALSE;
  352. }
  353. /************************************************************************
  354. * Menu items are automatically added for the new node types created
  355. * by this module to the 'Create Content' Navigation menu item. This function
  356. * adds more menu items needed for this module.
  357. */
  358. function tripal_feature_menu() {
  359. $items = array();
  360. // the administative settings menu
  361. $items['admin/tripal/tripal_feature'] = array(
  362. 'title' => 'Features',
  363. 'description' => 'Settings for Chado Features',
  364. 'page callback' => 'drupal_get_form',
  365. 'page arguments' => array('tripal_feature_admin'),
  366. 'access arguments' => array('administer site configuration'),
  367. 'type' => MENU_NORMAL_ITEM,
  368. );
  369. $items['admin/settings/tripal/tripal_feature/load'] = array(
  370. 'title' => 'Bulk Load',
  371. 'description' => 'Upload Data into Chado & Drupal',
  372. 'page callback' => 'tripal_feature_bulkload',
  373. 'access arguments' => array('administer site configuration'),
  374. 'type' => MENU_NORMAL_ITEM,
  375. );
  376. return $items;
  377. }
  378. /************************************************************************
  379. * When a new chado_feature node is created we also need to add information
  380. * to our chado_feature table. This function is called on insert of a new node
  381. * of type 'chado_feature' and inserts the necessary information.
  382. */
  383. function chado_feature_insert($node){
  384. // remove spaces, newlines from residues
  385. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  386. // If this feature already exists then don't recreate it in chado
  387. // TODO: the unique index in chado for this also includes the type_id. If the site
  388. // ever needs to have the same feature name for different types then this will break.
  389. $feature_sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' and organism_id = %d";
  390. $previous_db = tripal_db_set_active('chado');
  391. $feature = db_fetch_object(db_query($feature_sql,$node->title,$node->organism_id));
  392. tripal_db_set_active($previous_db);
  393. // if the feature doesn't exist then let's create it in chado.
  394. if(!$feature){
  395. $sql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,".
  396. " is_obsolete, type_id)".
  397. " VALUES(%d,'%s','%s','%s',%d, %s, ".
  398. " (SELECT cvterm_id ".
  399. " FROM {CVTerm} CVT ".
  400. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  401. " WHERE CV.name = 'sequence' and CVT.name = '%s'))";
  402. $obsolete = 'FALSE';
  403. if($node->is_obsolete){
  404. $obsolete = 'TRUE';
  405. }
  406. // use chado database
  407. $previous_db = tripal_db_set_active('chado');
  408. db_query($sql,$node->organism_id,$node->title,$node->title,
  409. $residues,strlen($residues),$obsolete,$node->feature_type);
  410. // now that we've added the feature, get the feature id for this feature
  411. $feature = db_fetch_object(db_query($feature_sql,$node->title,$node->organism_id));
  412. // now use drupal database
  413. tripal_db_set_active($previous_db);
  414. }
  415. // add the genbank accession and synonyms
  416. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  417. // make sure the entry for this feature doesn't already exist in the chado_feature table
  418. // if it doesn't exist then we want to add it.
  419. $node_check_sql = "SELECT * FROM {chado_feature} ".
  420. "WHERE feature_id = '%s'";
  421. $node_check = db_fetch_object(db_query($node_check_sql,$feature->feature_id));
  422. if(!$node_check){
  423. // next add the item to the drupal table
  424. $sql = "INSERT INTO {chado_feature} (nid, vid, feature_id, sync_date) ".
  425. "VALUES (%d, %d, %d, " . time() . ")";
  426. db_query($sql,$node->nid,$node->vid,$feature->feature_id);
  427. }
  428. }
  429. /************************************************************************
  430. */
  431. function chado_feature_delete($node){
  432. // get feature_id so we can remove it from chado database
  433. $sql_drupal = "SELECT feature_id ".
  434. "FROM {chado_feature} ".
  435. "WHERE nid = %d AND vid = %d";
  436. $feature_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  437. // remove the drupal content
  438. $sql_del = "DELETE FROM {chado_feature} ".
  439. "WHERE nid = %d ".
  440. "AND vid = %d";
  441. db_query($sql_del, $node->nid, $node->vid);
  442. $sql_del = "DELETE FROM {node} ".
  443. "WHERE nid = %d ".
  444. "AND vid = %d";
  445. db_query($sql_del, $node->nid, $node->vid);
  446. $sql_del = "DELETE FROM {node_revisions} ".
  447. "WHERE nid = %d ".
  448. "AND vid = %d";
  449. db_query($sql_del, $node->nid, $node->vid);
  450. // Remove data from feature tables of chado database. This will
  451. // cause a cascade delete and remove all data in referencing tables
  452. // for this feature
  453. $previous_db = tripal_db_set_active('chado');
  454. db_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
  455. tripal_db_set_active($previous_db);
  456. drupal_set_message("The feature and all associated data were removed from ".
  457. "chado");
  458. }
  459. /************************************************************************
  460. */
  461. function chado_feature_update($node){
  462. if($node->revision){
  463. // TODO -- decide what to do about revisions
  464. } else {
  465. // get the feature for this node:
  466. $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
  467. $feature = db_fetch_object(db_query($sql, $node->vid));
  468. // remove spaces, newlines from residues
  469. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  470. $sql = "UPDATE {feature} ".
  471. " SET residues = '%s', ".
  472. " name = '%s', ".
  473. " uniquename = '%s', ".
  474. " seqlen = %d, ".
  475. " organism_id = %d, ".
  476. " is_obsolete = %s, ".
  477. " type_id = (SELECT cvterm_id ".
  478. " FROM {CVTerm} CVT ".
  479. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  480. " WHERE CV.name = 'sequence' and CVT.name = '%s') ".
  481. "WHERE feature_id = %d ";
  482. $obsolete = 'FALSE';
  483. if($node->is_obsolete){
  484. $obsolete = 'TRUE';
  485. }
  486. $previous_db = tripal_db_set_active('chado'); // use chado database
  487. db_query($sql,$residues,$node->title,$node->title,
  488. strlen($residues),$node->organism_id,$obsolete,$node->feature_type,
  489. $feature->feature_id);
  490. tripal_db_set_active($previous_db); // now use drupal database
  491. // add the genbank accession & synonyms
  492. // chado_feature_add_gbaccession($node->gbaccession,$feature->feature_id);
  493. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  494. }
  495. }
  496. /************************************************************************
  497. *
  498. */
  499. function chado_feature_add_synonyms($synonyms,$feature_id){
  500. // make sure we only have a single space between each synonym
  501. $synonyms = preg_replace("/[\s\n\r]+/"," ",$synonyms);
  502. // split the synonyms into an array based on a space as the delimieter
  503. $syn_array = array();
  504. $syn_array = explode(" ",$synonyms);
  505. // use the chado database
  506. $previous_db = tripal_db_set_active('chado');
  507. // remove any old synonyms
  508. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = %d";
  509. if(!db_query($feature_syn_dsql,$feature_id)){
  510. $error .= "Could not remove synonyms from feature. ";
  511. }
  512. // return if we don't have any synonmys to add
  513. if(!$synonyms){
  514. tripal_db_set_active($previous_db);
  515. return;
  516. }
  517. // iterate through each synonym and add it to the database
  518. foreach($syn_array as $syn){
  519. // skip this item if it's empty
  520. if(!$syn){ break; }
  521. // check to see if we have this accession number already in the database
  522. // if so then don't add it again. it messes up drupal if the insert fails.
  523. // It is possible for the accession number to be present and not the feature
  524. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  525. "WHERE name = '%s'";
  526. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  527. if(!$synonym){
  528. $synonym_isql = "INSERT INTO {synonym} (name,synonym_sgml,type_id) ".
  529. "VALUES ('%s','%s', ".
  530. " (SELECT cvterm_id ".
  531. " FROM {CVTerm} CVT ".
  532. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  533. " WHERE CV.name = 'feature_property' and CVT.name = 'synonym'))";
  534. if(!db_query($synonym_isql,$syn,$syn)){
  535. $error .= "Could not add synonym. ";
  536. }
  537. // now get the synonym we just added
  538. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  539. "WHERE name = '%s'";
  540. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  541. }
  542. // now add in our new sysnonym
  543. $feature_syn_isql = "INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id) ".
  544. "VALUES (%d,%d,1)";
  545. if(!db_query($feature_syn_isql,$synonym->synonym_id,$feature_id)){
  546. $error .= "Could not add synonyms to feature. ";
  547. }
  548. }
  549. // return to the drupal database
  550. tripal_db_set_active($previous_db);
  551. return $error;
  552. }
  553. /************************************************************************
  554. *
  555. */
  556. function chado_feature_add_gbaccession($accession,$feature_id){
  557. // use chado database
  558. $previous_db = tripal_db_set_active('chado');
  559. // remove any old accession from genbank dbEST
  560. $fdbxref_dsql = "DELETE FROM {feature_dbxref} ".
  561. "WHERE feature_id = %d and dbxref_id IN ".
  562. " (SELECT DBX.dbxref_id FROM {dbxref} DBX ".
  563. " INNER JOIN DB ON DB.db_id = DBX.db_id ".
  564. " INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id ".
  565. " WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = %d)";
  566. if(!db_query($fdbxref_dsql,$feature_id,$feature_id)){
  567. $error .= "Could not remove accession from feature. ";
  568. }
  569. // if we don't have an accession number to add then just return
  570. if(!$accession){
  571. tripal_db_set_active($previous_db);
  572. return;
  573. }
  574. // get the db_id
  575. $db_sql = "SELECT db_id FROM {DB} ".
  576. "WHERE name = 'DB:Genbank_est'";
  577. $db = db_fetch_object(db_query($db_sql));
  578. // check to see if we have this accession number already in the database
  579. // if so then don't add it again. it messes up drupal if the insert fails.
  580. // It is possible for the accession number to be present and not the feature
  581. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  582. "WHERE db_id = %d and accession = '%s'";
  583. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  584. if(!$dbxref){
  585. // add the accession number
  586. $dbxref_isql = "INSERT INTO {dbxref} (db_id,accession) ".
  587. " VALUES (%d, '%s') ";
  588. if(!db_query($dbxref_isql,$db->db_id,$accession)){
  589. $error .= 'Could not add accession as a database reference ';
  590. }
  591. // get the dbxref_id for the just added accession number
  592. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  593. "WHERE db_id = %d and accession = '%s'";
  594. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  595. }
  596. // associate the accession number with the feature
  597. $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) ".
  598. " VALUES (%d, %d) ";
  599. if(!db_query($feature_dbxref_isql,$feature_id,$dbxref->dbxref_id)){
  600. $error .= 'Could not add feature database reference. ';
  601. }
  602. tripal_db_set_active($previous_db);
  603. return $error;
  604. }
  605. /************************************************************************
  606. *
  607. */
  608. function chado_feature_form ($node,$param){
  609. $type = node_get_types('type', $node);
  610. $form = array();
  611. $feature = $node->feature;
  612. $synonyms = $node->synonyms;
  613. $analyses = $node->analyses;
  614. $references = $node->references;
  615. // We need to pass above variables for preview to show
  616. $form['feature'] = array(
  617. '#type' => 'value',
  618. '#value' => $feature
  619. );
  620. // This field is read when previewing a node
  621. $form['synonyms'] = array(
  622. '#type' => 'value',
  623. '#value' => $synonyms
  624. );
  625. // This field is read when previewing a node
  626. $form['analyses'] = array(
  627. '#type' => 'value',
  628. '#value' => $analyses
  629. );
  630. // This field is read when previewing a node
  631. $form['references'] = array(
  632. '#type' => 'value',
  633. '#value' => $references
  634. );
  635. // keep track of the feature id if we have one. If we do have one then
  636. // this would indicate an update as opposed to an insert.
  637. $form['feature_id'] = array(
  638. '#type' => 'value',
  639. '#value' => $feature->feature_id,
  640. );
  641. $form['title']= array(
  642. '#type' => 'textfield',
  643. '#title' => t('Unique Feature Name'),
  644. '#required' => TRUE,
  645. '#default_value' => $feature->featurename,
  646. '#description' => t('Enter a unique name for this feature'),
  647. '#weight' => 1,
  648. '#maxlength' => 255
  649. );
  650. // get the list of supported feature types
  651. $ftypes = array();
  652. $ftypes[''] = '';
  653. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  654. foreach($supported_ftypes as $ftype){
  655. $ftypes["$ftype"] = $ftype;
  656. }
  657. $form['feature_type'] = array (
  658. '#title' => t('Feature Type'),
  659. '#type' => t('select'),
  660. '#description' => t("Choose the feature type."),
  661. '#required' => TRUE,
  662. '#default_value' => $feature->cvname,
  663. '#options' => $ftypes,
  664. '#weight' => 2
  665. );
  666. // get the list of organisms
  667. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  668. $previous_db = tripal_db_set_active('chado'); // use chado database
  669. $org_rset = db_query($sql);
  670. tripal_db_set_active($previous_db); // now use drupal database
  671. //
  672. $organisms = array();
  673. $organisms[''] = '';
  674. while($organism = db_fetch_object($org_rset)){
  675. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  676. }
  677. $form['organism_id'] = array (
  678. '#title' => t('Organism'),
  679. '#type' => t('select'),
  680. '#description' => t("Choose the organism with which this feature is associated "),
  681. '#required' => TRUE,
  682. '#default_value' => $feature->organism_id,
  683. '#options' => $organisms,
  684. '#weight' => 3,
  685. );
  686. // Get synonyms
  687. if ($synonyms) {
  688. if (is_array($synonyms)) {
  689. foreach ($synonyms as $synonym){
  690. $syn_text .= "$synonym->name\n";
  691. }
  692. } else {
  693. $syn_text = $synonyms;
  694. }
  695. }
  696. $form['synonyms']= array(
  697. '#type' => 'textarea',
  698. '#title' => t('Synonyms'),
  699. '#required' => FALSE,
  700. '#default_value' => $syn_text,
  701. '#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.'),
  702. '#weight' => 5,
  703. );
  704. $form['residues']= array(
  705. '#type' => 'textarea',
  706. '#title' => t('Residues'),
  707. '#required' => FALSE,
  708. '#default_value' => $feature->residues,
  709. '#description' => t('Enter the nucelotide sequences for this feature'),
  710. '#weight' => 6
  711. );
  712. $checked = '';
  713. if($feature->is_obsolete == 't'){
  714. $checked = '1';
  715. }
  716. $form['is_obsolete']= array(
  717. '#type' => 'checkbox',
  718. '#title' => t('Is Obsolete'),
  719. '#required' => FALSE,
  720. '#default_value' => $checked,
  721. '#description' => t('Check this box if this sequence should be retired and no longer included in further analysis.'),
  722. '#weight' => 8
  723. );
  724. return $form;
  725. }
  726. /************************************************************************
  727. *
  728. */
  729. function chado_feature_validate($node){
  730. $result = 0;
  731. // if this is an update, we want to make sure that a different feature for
  732. // the organism doesn't already have this uniquename. We don't want to give
  733. // two sequences the same uniquename
  734. if($node->feature_id){
  735. $sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' ".
  736. " AND organism_id = %d AND NOT feature_id = %d";
  737. $previous_db = tripal_db_set_active('chado');
  738. $result = db_fetch_object(db_query($sql, $node->title,$node->organism_id,$node->feature_id));
  739. tripal_db_set_active($previous_db);
  740. if($result){
  741. form_set_error('title',t("Feature update cannot proceed. The feature name '$node->title' is not unique for this organism. Please provide a unique name for this feature. "));
  742. }
  743. }
  744. // if this is an insert then we just need to make sure this name doesn't
  745. // already exist for this organism if it does then we need to throw an error
  746. else {
  747. $sql = "SELECT * FROM {Feature} WHERE uniquename = '%s' AND organism_id = %d";
  748. $previous_db = tripal_db_set_active('chado');
  749. $result = db_fetch_object(db_query($sql, $node->title,$node->organism_id));
  750. tripal_db_set_active($previous_db);
  751. if($result){
  752. form_set_error('title',t("Feature insert cannot proceed. The feature name '$node->title' already exists for this organism. Please provide a unique name for this feature. "));
  753. }
  754. }
  755. // we want to remove all characters except IUPAC nucleotide characters from the
  756. // the residues. however, residues are not required so if blank then we'll skip
  757. // this step
  758. if($node->residues){
  759. $residues = preg_replace("/[^\w]/",'',$node->residues);
  760. if(!preg_match("/^[ACTGURYMKSWBDHVN]+$/i",$residues)){
  761. form_set_error('residues',t("The residues in feature $node->title 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 ."'"));
  762. }
  763. }
  764. // we don't allow a genbank accession number for a contig
  765. if($node->feature_type == 'contig' and $node->gbaccession){
  766. form_set_error('gbaccession',t("Contigs cannot have a genbank accession number. Please change the feature type or remove the accession number"));
  767. }
  768. }
  769. /************************************************************************
  770. * When a node is requested by the user this function is called to allow us
  771. * to add auxiliary data to the node object.
  772. */
  773. function chado_feature_load($node){
  774. // get the feature_id for this node:
  775. $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
  776. $map = db_fetch_object(db_query($sql, $node->vid));
  777. $previous_db = tripal_db_set_active('chado'); // use chado database
  778. // get information about this organism and add it to the items in this node
  779. $sql = "SELECT F.feature_id, F.name as featurename, F.uniquename, ".
  780. "F.residues, F.seqlen, O.genus, O.species, O.common_name, ".
  781. " CVT.name as cvname, O.organism_id, F.type_id, F.is_obsolete ".
  782. "FROM {Feature} F ".
  783. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  784. " INNER JOIN CVterm CVT ON F.type_id = CVT.cvterm_id ".
  785. "WHERE F.feature_id = %d";
  786. $feature = db_fetch_object(db_query($sql,$map->feature_id));
  787. $additions->feature = $feature;
  788. $additions->seqlen = $feature->seqlen;
  789. // get the feature synonyms
  790. $sql = "SELECT S.name ".
  791. "FROM {Feature_Synonym} FS ".
  792. " INNER JOIN Synonym S ".
  793. " ON FS.synonym_id = S.Synonym_id ".
  794. "WHERE FS.feature_id = %d";
  795. $results = db_query($sql,$map->feature_id);
  796. $synonyms = array();
  797. $i=0;
  798. while($synonym = db_fetch_object($results)){
  799. $synonyms[$i++] = $synonym;
  800. }
  801. $additions->synonyms = $synonyms;
  802. // get feature references in external databases
  803. $sql = "SELECT F.uniquename,F.Feature_id,DBX.accession,DB.description as dbdesc, ".
  804. " DB.db_id, DB.name as db_name, DB.urlprefix ".
  805. "FROM {Feature} F ".
  806. " INNER JOIN Feature_dbxref FDBX on F.feature_id = FDBX.feature_id ".
  807. " INNER JOIN Dbxref DBX on DBX.dbxref_id = FDBX.dbxref_id ".
  808. " INNER JOIN DB on DB.db_id = DBX.db_id ".
  809. "WHERE F.feature_id = %d";
  810. $results = db_query($sql,$map->feature_id);
  811. $references = array();
  812. $i=0;
  813. while($accession = db_fetch_object($results)){
  814. $references[$i++] = $accession;
  815. // we want to specifically pull out the genbank id
  816. if(preg_match("/Genbank_est/",$accession->db_name)){
  817. $additions->gbaccession = $accession;
  818. }
  819. }
  820. $additions->references = $references;
  821. tripal_db_set_active($previous_db); // now use drupal database
  822. // get organism node nid
  823. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = %d";
  824. $org_nid = db_result(db_query($sql, $additions->feature->organism_id));
  825. $additions->org_nid = $org_nid;
  826. return $additions;
  827. }
  828. /************************************************************************
  829. * This function customizes the view of the chado_feature node. It allows
  830. * us to generate the markup.
  831. */
  832. function chado_feature_view ($node, $teaser = FALSE, $page = FALSE) {
  833. if (!$teaser) {
  834. // use drupal's default node view:
  835. $node = node_prepare($node, $teaser);
  836. // if we're building the node for searching then
  837. // we want to handle this within the module and
  838. // not allow theme customization. We don't want to
  839. // index all items (such as DNA sequence).
  840. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  841. $node->content['index_version'] = array(
  842. '#value' => theme('tripal_feature_search_index',$node),
  843. '#weight' => 1,
  844. );
  845. }
  846. else if($node->build_mode == NODE_BUILD_SEARCH_RESULT){
  847. $node->content['index_version'] = array(
  848. '#value' => theme('tripal_feature_search_results',$node),
  849. '#weight' => 1,
  850. );
  851. }
  852. else {
  853. // do nothing here, let the theme derived template handle display
  854. }
  855. }
  856. return $node;
  857. }
  858. /*******************************************************************************
  859. * Display feature information for associated organisms. This function also
  860. * provides contents for indexing
  861. */
  862. function tripal_feature_nodeapi(&$node, $op, $teaser, $page) {
  863. switch ($op) {
  864. // Note that this function only adds feature view to an organism node.
  865. // The view of a feature node is controled by the theme *.tpl file
  866. case 'view':
  867. // Set the node types for showing feature information
  868. $types_to_show = array('chado_organism', 'chado_library');
  869. // Abort if this node is not one of the types we should show.
  870. if (!in_array($node->type, $types_to_show, TRUE)) {
  871. break;
  872. }
  873. // Add feature to the content item if it's not a teaser
  874. if (!$teaser) {
  875. // Show feature browser
  876. $node->content['tripal_feature_browser'] = array(
  877. '#value' => theme('tripal_feature_browser', $node),
  878. '#weight' => 5
  879. );
  880. $node->content['tripal_feature_org_counts'] = array(
  881. '#value' => theme('tripal_feature_counts', $node),
  882. '#weight' => 4
  883. );
  884. }
  885. }
  886. }
  887. /************************************************************************
  888. * We need to let drupal know about our theme functions and their arguments.
  889. * We create theme functions to allow users of the module to customize the
  890. * look and feel of the output generated in this module
  891. */
  892. function tripal_feature_theme () {
  893. return array(
  894. 'tripal_feature_search_index' => array (
  895. 'arguments' => array('node'),
  896. ),
  897. 'tripal_feature_search_results' => array (
  898. 'arguments' => array('node'),
  899. ),
  900. 'tripal_feature_browser' => array (
  901. 'arguments' => array('node'),
  902. ),
  903. 'tripal_feature_counts' => array (
  904. 'arguments' => array('node'),
  905. )
  906. );
  907. }
  908. /*******************************************************************************
  909. * create a list of features for the organism and pie chart
  910. */
  911. function theme_tripal_feature_counts($node){
  912. // don't show the summary if the settings in the admin page is turned off
  913. $show_browser = variable_get('tripal_feature_summary_setting',array('show_feature_summary'));
  914. if(strcmp($show_browser[0],'show_feature_summary')!=0){
  915. return;
  916. }
  917. // get the feature counts. This is dependent on a materialized view
  918. // installed with the organism module
  919. $content = '';
  920. if ($node->organism_id && $node->type == 'chado_organism') {
  921. $sql = "SELECT * FROM {organism_feature_count} ".
  922. "WHERE organism_id = %d AND NOT feature_type = 'EST_match' ".
  923. "ORDER BY num_features desc";
  924. $features = array();
  925. $previous_db = tripal_db_set_active('chado'); // use chado database
  926. $results = db_query($sql,$node->organism_id);
  927. tripal_db_set_active($previous_db); // now use drupal database
  928. $feature = db_fetch_object($results); // retrieve the first result
  929. if ($feature) {
  930. $content .= "<div class=\"tripal_feature_summary-info-box\"><br>
  931. <div class=\"tripal_expandableBox\">".
  932. "<h3>Feature Summary</h3>".
  933. "</div>";
  934. $content .= "<div class=\"tripal_expandableBoxContent\">";
  935. $content .= "<table class=\"tripal_table_horz\">";
  936. $content .= " <tr>";
  937. $content .= " <th class=\"dbfieldname\">Type</th>";
  938. $content .= " <th class=\"dbfieldname\">Number</th>";
  939. $content .= " </tr>";
  940. do {
  941. $content .= "<tr>";
  942. $content .= " <td>$feature->feature_type</td>";
  943. $content .= " <td>". number_format($feature->num_features) . "</td>";
  944. $content .= "</tr>";
  945. } while($feature = db_fetch_object($results));
  946. $content .= "</table>";
  947. $content .= "
  948. <img class=\"tripal_cv_chart\" id=\"tripal_feature_cv_chart_$node->organism_id\" src=\"\" border=\"0\">
  949. ";
  950. $content .= "</div></div>";
  951. }
  952. }
  953. return $content;
  954. }
  955. /************************************************************************
  956. *
  957. */
  958. function tripal_feature_cv_chart($chart_id){
  959. // The CV module will create the JSON array necessary for buillding a
  960. // pie chart using jgChart and Google Charts. We have to pass to it
  961. // a table that contains count information, tell it which column
  962. // contains the cvterm_id and provide a filter for getting the
  963. // results we want from the table.
  964. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/","$1",$chart_id);
  965. $options = array(
  966. count_mview => 'organism_feature_count',
  967. cvterm_id_column => 'cvterm_id',
  968. count_column => 'num_features',
  969. size => '650x200',
  970. filter => "CNT.organism_id = $organism_id AND NOT feature_type = 'EST_match' ",
  971. );
  972. return $options;
  973. }
  974. /************************************************************************
  975. *
  976. */
  977. function tripal_feature_cv_tree($tree_id){
  978. // The CV module will create the JSON array necessary for buillding a
  979. // pie chart using jgChart and Google Charts. We have to pass to it
  980. // a table that contains count information, tell it which column
  981. // contains the cvterm_id and provide a filter for getting the
  982. // results we want from the table.
  983. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/","$1",$tree_id);
  984. $options = array(
  985. cv_id => tripal_cv_get_cv_id('sequence'),
  986. count_mview => 'organism_feature_count',
  987. cvterm_id_column => 'cvterm_id',
  988. count_column => 'num_features',
  989. filter => "CNT.organism_id = $organism_id",
  990. label => 'Features',
  991. );
  992. return $options;
  993. }
  994. /*******************************************************************************
  995. * create a simple paged feature browser
  996. */
  997. function theme_tripal_feature_browser($node){
  998. // don't show the browser if the settings in the admin page is turned off
  999. $show_browser = variable_get('tripal_feature_browse_setting',array('show_feature_browser'));
  1000. if(strcmp($show_browser[0],'show_feature_browser')!=0){
  1001. return;
  1002. }
  1003. if ($node->organism_id && $node->type == 'chado_organism') {
  1004. # get the list of available sequence ontology terms for which
  1005. # we will build drupal pages from features in chado. If a feature
  1006. # is not one of the specified typse we won't build a node for it.
  1007. $allowed_types = variable_get('chado_feature_types','EST contig');
  1008. $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
  1009. $so_terms = split(' ',$allowed_types);
  1010. $where_cvt = "";
  1011. foreach ($so_terms as $term){
  1012. $where_cvt .= "CVT.name = '$term' OR ";
  1013. }
  1014. $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
  1015. // get the features for this organism
  1016. $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
  1017. "FROM {feature} F ".
  1018. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1019. "WHERE organism_id = $node->organism_id and ($where_cvt) ".
  1020. "ORDER BY feature_id ASC";
  1021. // the counting SQL
  1022. $csql = "SELECT count(*) ".
  1023. "FROM {feature} F".
  1024. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1025. "WHERE organism_id = $node->organism_id and ($where_cvt) ".
  1026. "GROUP BY organism_id ";
  1027. $previous_db = tripal_db_set_active('chado'); // use chado database
  1028. $features = pager_query($sql,10,0,$csql);
  1029. tripal_db_set_active($previous_db); // now use drupal database
  1030. $content = "<br><div id=\"tripal_feature_box\" class=\"feature-info-box\">";
  1031. $content .= "<div class=\"tripal_expandableBox\">".
  1032. "<h3>Browse Features</h3>".
  1033. "</div>";
  1034. $content .= "<div class=\"tripal_expandableBoxContent\">";
  1035. $content .= "Below are the features associated with this organism.\n";
  1036. $content .= "<table class=\"tripal_table_horz\">";
  1037. $content .= " <tr>";
  1038. $content .= " <th>Feature Name</th>";
  1039. $content .= " <th>Type</th>";
  1040. $content .= " </tr>";
  1041. // prepare the query that will lookup node ids
  1042. $sql = "SELECT nid FROM {chado_feature} ".
  1043. "WHERE feature_id = %d";
  1044. while($feature = db_fetch_object($features)){
  1045. $node = db_fetch_object(db_query($sql,$feature->feature_id));
  1046. if($node){
  1047. $name= "<a href=\"" . url("node/$node->nid") . "\">$feature->name</a>";
  1048. } else {
  1049. $name= "$feature->name";
  1050. }
  1051. $content .= " <tr>";
  1052. $content .= " <td>$name</td>";
  1053. $content .= " <td>$feature->cvname</td>";
  1054. $content .= " </tr>";
  1055. }
  1056. $content .= "</table>";
  1057. $content .= theme('pager');
  1058. $content .= "</div></div>";
  1059. return $content;
  1060. }
  1061. }
  1062. /************************************************************************
  1063. * This function is an extension of the chado_feature_view by providing
  1064. * the markup for the feature object THAT WILL BE INDEXED.
  1065. */
  1066. function theme_tripal_feature_search_index ($node) {
  1067. $feature = $node->feature;
  1068. $content = '';
  1069. // get the accession prefix
  1070. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1071. $content .= "<h1>$feature->uniquename</h1>. ";
  1072. $content .= "<strong>$aprefix$feature->feature_id.</strong> ";
  1073. $content .= "$feature->cvname ";
  1074. $content .= "$feature->common_name ";
  1075. // add the synonyms of this feature to the text for searching
  1076. $synonyms = $node->synonyms;
  1077. if(count($synonyms) > 0){
  1078. foreach ($synonyms as $result){
  1079. $content .= "$result->name ";
  1080. }
  1081. }
  1082. return $content;
  1083. }
  1084. /************************************************************************
  1085. * This function is an extension of the chado_feature_view by providing
  1086. * the markup for the feature object THAT WILL BE INDEXED.
  1087. */
  1088. function theme_tripal_feature_search_results ($node) {
  1089. $feature = $node->feature;
  1090. $content = '';
  1091. // get the accession prefix
  1092. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1093. $content .= "Feature Name: <h1>$feature->uniquename</h1>. ";
  1094. $content .= "<strong>Accession: $aprefix$feature->feature_id.</strong>";
  1095. $content .= "Type: $feature->cvname. ";
  1096. $content .= "Organism: $feature->common_name. ";
  1097. // add the synonyms of this feature to the text for searching
  1098. $synonyms = $node->synonyms;
  1099. if(count($synonyms) > 0){
  1100. $content .= "Synonyms: ";
  1101. foreach ($synonyms as $result){
  1102. $content .= "$result->name, ";
  1103. }
  1104. }
  1105. return $content;
  1106. }
  1107. /************************************************************************
  1108. *
  1109. */
  1110. function tripal_feature_set_vocabulary (){
  1111. //include the file containing the required functions for adding taxonomy vocabs
  1112. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1113. // get the vocabularies so that we make sure we don't recreate
  1114. // the vocabs that already exist
  1115. $vocabularies = taxonomy_get_vocabularies();
  1116. $ft_vid = NULL;
  1117. $op_vid = NULL;
  1118. $lb_vid = NULL;
  1119. $an_vid = NULL;
  1120. // These taxonomic terms are hard coded because we
  1121. // konw we have these relationships in the chado tables
  1122. // through foreign key relationships. The tripal
  1123. // modules that correspond to these chado "modules" don't
  1124. // need to be installed for the taxonomy to work.
  1125. foreach($vocabularies as $vocab){
  1126. if($vocab->name == 'Feature Type'){
  1127. $ft_vid = $vocab->vid;
  1128. }
  1129. if($vocab->name == 'Organism'){
  1130. $op_vid = $vocab->vid;
  1131. }
  1132. if($vocab->name == 'Library'){
  1133. $lb_vid = $vocab->vid;
  1134. }
  1135. if($vocab->name == 'Analysis'){
  1136. $an_vid = $vocab->vid;
  1137. }
  1138. }
  1139. if(!$ft_vid){
  1140. $form_state = array();
  1141. $values = array(
  1142. 'name' => t('Feature Type'),
  1143. 'nodes' => array('chado_feature' => 'chado_feature'),
  1144. 'description' => t('The feature type (or SO cvterm for this feature).'),
  1145. 'help' => t('Select the term that matches the feature '),
  1146. 'tags' => 0,
  1147. 'hierarchy' => 1,
  1148. 'relations' => 1,
  1149. 'multiple' => 0,
  1150. 'required' => 0,
  1151. 'weight' => 1,
  1152. );
  1153. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1154. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1155. }
  1156. if(!$op_vid){
  1157. $form_state = array();
  1158. $values = array(
  1159. 'name' => t('Organism'),
  1160. 'nodes' => array('chado_feature' => 'chado_feature'),
  1161. 'description' => t('The organism to which this feature belongs.'),
  1162. 'help' => t('Select the term that matches the feature '),
  1163. 'tags' => 0,
  1164. 'hierarchy' => 1,
  1165. 'relations' => 1,
  1166. 'multiple' => 0,
  1167. 'required' => 0,
  1168. 'weight' => 2,
  1169. );
  1170. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1171. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1172. }
  1173. if(!$lb_vid){
  1174. $form_state = array();
  1175. $values = array(
  1176. 'name' => t('Library'),
  1177. 'nodes' => array('chado_feature' => 'chado_feature'),
  1178. 'description' => t('Chado features associated with a library are assigned the term associated with the library'),
  1179. 'help' => t('Select the term that matches the feature '),
  1180. 'tags' => 0,
  1181. 'hierarchy' => 1,
  1182. 'relations' => 1,
  1183. 'multiple' => 0,
  1184. 'required' => 0,
  1185. 'weight' => 3,
  1186. );
  1187. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  1188. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1189. }
  1190. if(!$an_vid){
  1191. $form_state = array();
  1192. $values = array(
  1193. 'name' => t('Analysis'),
  1194. 'nodes' => array('chado_feature' => 'chado_feature'),
  1195. 'description' => t('Any analysis to which this feature belongs.'),
  1196. 'help' => t('Select the term that matches the feature '),
  1197. 'tags' => 0,
  1198. 'hierarchy' => 1,
  1199. 'relations' => 1,
  1200. 'multiple' => 1,
  1201. 'required' => 0,
  1202. 'weight' => 4,
  1203. );
  1204. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1205. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1206. }
  1207. }
  1208. /************************************************************************
  1209. *
  1210. */
  1211. function tripal_feature_del_vocabulary(){
  1212. //include the file containing the required functions for adding taxonomy vocabs
  1213. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1214. // get the vocabularies
  1215. $vocabularies = taxonomy_get_vocabularies();
  1216. // These taxonomic terms are hard coded because we
  1217. // know we have these relationships in the chado tables
  1218. // through foreign key relationships. The tripal
  1219. // modules that correspond to these chado "modules" don't
  1220. // need to be installed for the taxonomy to work.
  1221. foreach($vocabularies as $vocab){
  1222. if($vocab->name == 'Feature Type'){
  1223. taxonomy_del_vocabulary($vocab->vid);
  1224. }
  1225. if($vocab->name == 'Organism'){
  1226. taxonomy_del_vocabulary($vocab->vid);
  1227. }
  1228. if($vocab->name == 'Library'){
  1229. taxonomy_del_vocabulary($vocab->vid);
  1230. }
  1231. if($vocab->name == 'Analysis'){
  1232. taxonomy_del_vocabulary($vocab->vid);
  1233. }
  1234. }
  1235. }
  1236. /************************************************************************
  1237. *
  1238. */
  1239. function tripal_features_set_taxonomy($max_sync = 0,$job_id = NULL){
  1240. // make sure our vocabularies are cleaned and reset before proceeding
  1241. tripal_feature_del_vocabulary();
  1242. tripal_feature_set_vocabulary();
  1243. // iterate through all drupal feature nodes and set the taxonomy
  1244. $results = db_query("SELECT * FROM {chado_feature}");
  1245. $nsql = "SELECT * FROM {node} ".
  1246. "WHERE nid = %d";
  1247. $i = 0;
  1248. // load into ids array
  1249. $count = 0;
  1250. $chado_features = array();
  1251. while($chado_feature = db_fetch_object($results)){
  1252. $chado_features[$count] = $chado_feature;
  1253. $count++;
  1254. }
  1255. // Iterate through features that need to be synced
  1256. $interval = intval($count * 0.01);
  1257. foreach($chado_features as $chado_feature){
  1258. // update the job status every 1% features
  1259. if($job_id and $i % $interval == 0){
  1260. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1261. }
  1262. $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
  1263. tripal_feature_set_taxonomy($node,$chado_feature->feature_id);
  1264. $i++;
  1265. }
  1266. }
  1267. /************************************************************************
  1268. *
  1269. */
  1270. function tripal_feature_set_taxonomy ($node,$feature_id){
  1271. // iterate through the taxonomy classes that have been
  1272. // selected by the admin user and make sure we only set those
  1273. $tax_classes = variable_get('tax_classes', '');
  1274. $do_ft = 0;
  1275. $do_op = 0;
  1276. $do_lb = 0;
  1277. $do_an = 0;
  1278. foreach($tax_classes as $class){
  1279. if(strcmp($class ,'organism')==0){
  1280. $do_op = 1;
  1281. }
  1282. if(strcmp($class,'feature_type')==0){
  1283. $do_ft = 1;
  1284. }
  1285. if(strcmp($class,'library')==0){
  1286. $do_lb = 1;
  1287. }
  1288. if(strcmp($class,'analysis')==0){
  1289. $do_an = 1;
  1290. }
  1291. }
  1292. // get the list of vocabularies and find our two vocabularies of interest
  1293. $vocabularies = taxonomy_get_vocabularies();
  1294. $ft_vid = NULL;
  1295. $op_vid = NULL;
  1296. $lb_vid = NULL;
  1297. $an_vid = NULL;
  1298. foreach($vocabularies as $vocab){
  1299. if($vocab->name == 'Feature Type'){
  1300. $ft_vid = $vocab->vid;
  1301. }
  1302. if($vocab->name == 'Organism'){
  1303. $op_vid = $vocab->vid;
  1304. }
  1305. if($vocab->name == 'Library'){
  1306. $lb_vid = $vocab->vid;
  1307. }
  1308. if($vocab->name == 'Analysis'){
  1309. $an_vid = $vocab->vid;
  1310. }
  1311. }
  1312. // get the cvterm and the organism for this feature
  1313. $sql = "SELECT CVT.name AS cvname, O.genus, O.species ".
  1314. "FROM {CVTerm} CVT ".
  1315. " INNER JOIN Feature F on F.type_id = CVT.cvterm_id ".
  1316. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  1317. "WHERE F.feature_id = $feature_id";
  1318. $previous_db = tripal_db_set_active('chado'); // use chado database
  1319. $feature = db_fetch_object(db_query($sql));
  1320. tripal_db_set_active($previous_db); // now use drupal database
  1321. // Set the feature type for this feature
  1322. if($do_ft && $ft_vid){
  1323. $tags["$ft_vid"] = "$feature->cvname";
  1324. }
  1325. // Set the organism for this feature type
  1326. if($do_op && $op_vid){
  1327. $tags["$op_vid"] = "$feature->genus $feature->species";
  1328. }
  1329. // get the library that this feature may belong to and add it as taxonomy
  1330. if($do_lb && $lb_vid){
  1331. $sql = "SELECT L.name ".
  1332. "FROM {Library} L ".
  1333. " INNER JOIN Library_feature LF ON LF.library_id = L.library_id ".
  1334. "WHERE LF.feature_id = %d ";
  1335. $previous_db = tripal_db_set_active('chado'); // use chado database
  1336. $library = db_fetch_object(db_query($sql,$feature_id));
  1337. tripal_db_set_active($previous_db); // now use drupal database
  1338. $tags["$lb_vid"] = "$library->name";
  1339. }
  1340. // now add the taxonomy to the node
  1341. $terms['tags'] = $tags;
  1342. taxonomy_node_save($node,$terms);
  1343. // print "Setting $node->title: " . implode(", ",$tags) . "\n";
  1344. // get the analysis that this feature may belong to and add it as taxonomy
  1345. // We'll add each one individually since there may be more than one analysis
  1346. if($do_an && $an_vid){
  1347. $sql = "SELECT A.name ".
  1348. "FROM {Analysis} A ".
  1349. " INNER JOIN Analysisfeature AF ON AF.analysis_id = A.analysis_id ".
  1350. "WHERE AF.feature_id = $feature_id ";
  1351. $results = db_query($sql);
  1352. $previous_db = tripal_db_set_active('chado'); // use chado database
  1353. $analysis_terms = array();
  1354. while($analysis=db_fetch_object($results)){
  1355. $tags2["$an_vid"] = "$analysis->name";
  1356. $terms['tags'] = $tags2;
  1357. taxonomy_node_save($node,$terms);
  1358. }
  1359. tripal_db_set_active($previous_db); // now use drupal database
  1360. }
  1361. }
  1362. /************************************************************************
  1363. *
  1364. */
  1365. function tripal_features_cleanup($dummy = NULL, $job_id = NULL) {
  1366. // build the SQL statments needed to check if nodes point to valid features
  1367. $dsql = "SELECT * FROM {node} WHERE type = 'chado_feature' order by nid";
  1368. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1369. $csql = "SELECT * FROM {chado_feature} where nid = %d ";
  1370. $cfsql= "SELECT * FROM {chado_feature}";
  1371. $tsql = "SELECT * FROM {feature} F ".
  1372. " INNER JOIN CVTerm CVT ON F.type_id = CVT.cvterm_id ".
  1373. "WHERE feature_id = %d AND (";
  1374. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  1375. foreach($supported_ftypes as $ftype){
  1376. $tsql .= " CVT.name = '$ftype' OR ";
  1377. }
  1378. $tsql .= " 0=1) "; // add a 0=1 just as a filler so we don't have to remove a trailing 'OR'
  1379. // load into nodes array
  1380. $results = db_query($dsql);
  1381. $count = 0;
  1382. $nodes = array();
  1383. while($node = db_fetch_object($results)){
  1384. $nodes[$count] = $node;
  1385. $count++;
  1386. }
  1387. // load the chado_features into an array
  1388. $results = db_query($cfsql);
  1389. $cnodes = array();
  1390. while($node = db_fetch_object($results)){
  1391. $cnodes[$count] = $node;
  1392. $count++;
  1393. }
  1394. $interval = intval($count * 0.01);
  1395. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1396. foreach($nodes as $nid){
  1397. // update the job status every 1% features
  1398. if($job_id and $i % $interval == 0){
  1399. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1400. }
  1401. // first check to see if the node has a corresponding entry
  1402. // in the chado_feature table. If not then delete the node.
  1403. $feature = db_fetch_object(db_query($csql,$nid->nid));
  1404. if(!$feature){
  1405. node_delete($nid->nid);
  1406. $message = "Missing in chado_feature table.... DELETING: $nid->nid\n";
  1407. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1408. continue;
  1409. }
  1410. // second check to see if the node is for a feature of an allowed type.
  1411. // if not, then delete the node. This check will also take care of the
  1412. // case when a node exists and an entry in the chado_feature table exists
  1413. // but no feature with a matching feature_id exists
  1414. $previous_db = tripal_db_set_active('chado'); // use chado database
  1415. $ftype = db_fetch_object(db_query($tsql,$feature->feature_id));
  1416. tripal_db_set_active($previous_db); // now use drupal database
  1417. if(!$ftype){
  1418. node_delete($nid->nid);
  1419. db_query("DELETE FROM {chado_feature} WHERE feature_id = $feature->feature_id");
  1420. $message = "Node of the wrong feature type.... DELETING: $nid->nid\n";
  1421. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1422. }
  1423. $i++;
  1424. }
  1425. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1426. foreach($cnodes as $nid){
  1427. // update the job status every 1% features
  1428. if($job_id and $i % $interval == 0){
  1429. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1430. }
  1431. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1432. if(!$node){
  1433. db_query("DELETE FROM {chado_feature} WHERE nid = $nid->nid");
  1434. $message = "chado_feature missing node.... DELETING: $nid->nid\n";
  1435. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1436. }
  1437. $i++;
  1438. }
  1439. return '';
  1440. }
  1441. /************************************************************************
  1442. *
  1443. */
  1444. function tripal_feature_bulkload(){
  1445. return drupal_get_form('tripal_feature_load_fasta_form');
  1446. }
  1447. /************************************************************************
  1448. *
  1449. */
  1450. function tripal_feature_load_fasta_form (&$form_state = NULL){
  1451. // get the list of organisms
  1452. $sql = "SELECT * FROM {Organism} ORDER BY genus,species";
  1453. $previous_db = tripal_db_set_active('chado'); // use chado database
  1454. $org_rset = db_query($sql);
  1455. tripal_db_set_active($previous_db); // now use drupal database
  1456. $organisms = array();
  1457. $organisms[''] = '';
  1458. while($organism = db_fetch_object($org_rset)){
  1459. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  1460. }
  1461. // get the list of supported feature types
  1462. $ftypes = array();
  1463. $ftypes[''] = '';
  1464. $supported_ftypes = split("[ \n]",variable_get('chado_feature_feature_types','EST contig'));
  1465. foreach($supported_ftypes as $ftype){
  1466. $ftypes["$ftype"] = $ftype;
  1467. }
  1468. // get the list of libraries
  1469. // TODO !!!! Use Ajax to filter this automatically based on the organism
  1470. // selected by the user. This will prevent mistakes from user input.
  1471. $sql = "SELECT * FROM {Library} L ".
  1472. " INNER JOIN Organism O ON L.organism_id = O.organism_id ".
  1473. "ORDER BY L.name";
  1474. $previous_db = tripal_db_set_active('chado'); // use chado database
  1475. $lib_rset = db_query($sql);
  1476. tripal_db_set_active($previous_db); // now use drupal database
  1477. $libraries = array();
  1478. $libraries[''] = '';
  1479. while($library = db_fetch_object($lib_rset)){
  1480. $libraries[$library->library_id] = "$library->name ($library->genus $library->species)";
  1481. }
  1482. $form['#attributes']['enctype'] = 'multipart/form-data';
  1483. $form['organism'] = array (
  1484. '#title' => t('Organism'),
  1485. '#type' => t('select'),
  1486. '#description' => t("Choose the organism with which these sequences are associated "),
  1487. '#required' => TRUE,
  1488. '#default_vaule' => '',
  1489. '#options' => $organisms,
  1490. '#weight' => 1,
  1491. );
  1492. $form['library'] = array (
  1493. '#title' => t('Library'),
  1494. '#type' => t('select'),
  1495. '#description' => t("Choose the library with from which these sequences are derived. Leave blank if not applicable."),
  1496. '#required' => FALSE,
  1497. '#default_vaule' => '',
  1498. '#options' => $libraries,
  1499. '#weight' => 2,
  1500. );
  1501. $form['ftype'] = array (
  1502. '#title' => t('Feature Type'),
  1503. '#type' => t('select'),
  1504. '#description' => t("Choose the category of sequences you are uploading. All sequences in the FASTA file will be imported as this type"),
  1505. '#required' => TRUE,
  1506. '#default_vaule' => '',
  1507. '#options' => $ftypes,
  1508. '#weight' => 3,
  1509. );
  1510. $form['fasta_file'] = array(
  1511. '#type' => t('file'),
  1512. '#title' => t('Fasta File'),
  1513. '#description' => t('Upload a FASTA file of sequences. The definition line should contain only the feature name. All other annotations should be removed. The file must not be larger than ' . file_upload_max_size() . ' bytes'),
  1514. '#weight' => 4,
  1515. );
  1516. $form['upload'] = array(
  1517. '#type' => 'submit',
  1518. '#value' => t('Upload File'),
  1519. '#weight' => 2,
  1520. '#executes_submit_callback' => TRUE,
  1521. '#weight' => 5,
  1522. );
  1523. return $form;
  1524. }
  1525. /************************************************************************
  1526. *
  1527. */
  1528. function tripal_feature_load_fasta_form_validate($form, &$form_state){
  1529. // TODO !!! check that the fasta file is valid
  1530. global $user;
  1531. // we need a path within the drupal installation to temporarily use as the destination
  1532. // after we upload, we'll move the file to analysis directory
  1533. $upload_url = file_directory_path() . "/chado_feature_bulk_upload/$user->uid";
  1534. // create the download directory. We do it this way rather than the
  1535. // file_check_directory because we don't want a drupal message presented
  1536. // the user when the directory is created.
  1537. if (!is_dir($upload_url)) {
  1538. mkdir($upload_url,0775,TRUE);
  1539. }
  1540. // upload the file and copy it to the proper location
  1541. $validators = array(); // we don't have any validators
  1542. if($file = file_save_upload('fasta_file',$validators,$upload_url)){
  1543. drupal_set_message("File $file->name uploaded succesfully");
  1544. } else {
  1545. form_set_error('fasta_file',t('Upload Failed'));
  1546. }
  1547. }
  1548. /************************************************************************
  1549. *
  1550. */
  1551. function tripal_feature_load_fasta_form_submit($form, &$form_state){
  1552. global $user;
  1553. // add a job to be executed
  1554. tripal_add_job ($job_name,$type,$callback,$uid);
  1555. }
  1556. /************************************************************************
  1557. *
  1558. */
  1559. function tripal_feature_return_fasta($feature,$desc){
  1560. $fasta = ">" . variable_get('chado_feature_accession_prefix','ID') . "$feature->feature_id|$feature->name";
  1561. $fasta .= " $desc\n";
  1562. $fasta .= wordwrap($feature->residues, 50, "\n", true);
  1563. $fasta .= "\n\n";
  1564. return $fasta;
  1565. }