tripal_feature.module 63 KB

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