tripal_feature.module 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074
  1. <?php
  2. require_once "tripal_feature.admin.inc";
  3. require_once "syncFeatures.php";
  4. require_once "indexFeatures.php";
  5. require_once "fasta_loader.php";
  6. require_once "gff_loader.php";
  7. require_once "tripal_feature.api.inc";
  8. require_once "tripal_feature-secondary_tables.inc";
  9. require_once "tripal_feature-properties.inc";
  10. require_once "tripal_feature-relationships.inc";
  11. require_once "tripal_feature-db_references.inc";
  12. /*************************************************************************
  13. *
  14. */
  15. function tripal_feature_init(){
  16. // add the jGCharts JS and CSS
  17. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/tripal_feature.js');
  18. drupal_add_js (drupal_get_path('theme', 'tripal').'/js/jgcharts/jgcharts.js');
  19. drupal_add_css(drupal_get_path('theme', 'tripal').
  20. '/css/tripal_feature.css');
  21. }
  22. /*************************************************************************
  23. * Implements hook_views_api()
  24. * Purpose: Essentially this hook tells drupal that there is views support for
  25. * for this module which then includes tripal_db.views.inc where all the
  26. * views integration code is
  27. */
  28. function tripal_feature_views_api() {
  29. return array(
  30. 'api' => 2.0,
  31. );
  32. }
  33. /************************************************************************
  34. * Display help and module information
  35. * @param path which path of the site we're displaying help
  36. * @param arg array that holds the current path as would be returned from arg() function
  37. * @return help text for the path
  38. */
  39. function tripal_feature_help($path, $arg) {
  40. $output = '';
  41. switch ($path) {
  42. case "admin/help#tripal_feature":
  43. $output='<p>'.t("Displays links to nodes created on this date").'</p>';
  44. break;
  45. }
  46. return $output;
  47. }
  48. /************************************************************************
  49. * Provide information to drupal about the node types that we're creating
  50. * in this module
  51. */
  52. function tripal_feature_node_info() {
  53. $nodes = array();
  54. $nodes['chado_feature'] = array(
  55. 'name' => t('Feature'),
  56. 'module' => 'chado_feature',
  57. 'description' => t('A feature from the chado database'),
  58. 'has_title' => FALSE,
  59. 'title_label' => t('Feature'),
  60. 'has_body' => FALSE,
  61. 'body_label' => t('Feature Description'),
  62. 'locked' => TRUE
  63. );
  64. return $nodes;
  65. }
  66. /************************************************************************
  67. * Set the permission types that the chado module uses. Essentially we
  68. * want permissionis that protect creation, editing and deleting of chado
  69. * data objects
  70. */
  71. function tripal_feature_perm(){
  72. return array(
  73. 'access chado_feature content',
  74. 'create chado_feature content',
  75. 'delete chado_feature content',
  76. 'edit chado_feature content',
  77. 'manage chado_feature aggregator',
  78. );
  79. }
  80. /************************************************************************
  81. * Set the permission types that the module uses.
  82. */
  83. function chado_feature_access($op, $node, $account) {
  84. if ($op == 'create') {
  85. return user_access('create chado_feature content', $account);
  86. }
  87. if ($op == 'update') {
  88. if (user_access('edit chado_feature content', $account)) {
  89. return TRUE;
  90. }
  91. }
  92. if ($op == 'delete') {
  93. if (user_access('delete chado_feature content', $account)) {
  94. return TRUE;
  95. }
  96. }
  97. if ($op == 'view') {
  98. if (user_access('access chado_feature content', $account)) {
  99. return TRUE;
  100. }
  101. }
  102. return FALSE;
  103. }
  104. /************************************************************************
  105. * Menu items are automatically added for the new node types created
  106. * by this module to the 'Create Content' Navigation menu item. This function
  107. * adds more menu items needed for this module.
  108. */
  109. function tripal_feature_menu() {
  110. $items = array();
  111. // the administative settings menu
  112. $items['admin/tripal/tripal_feature'] = array(
  113. 'title' => 'Features',
  114. 'description' => 'Basic Description of Tripal Organism Module Functionality',
  115. 'page callback' => 'tripal_feature_module_description_page',
  116. 'access arguments' => array('administer site configuration'),
  117. 'type' => MENU_NORMAL_ITEM,
  118. );
  119. $items['admin/tripal/tripal_feature/configuration'] = array(
  120. 'title' => 'Feature Configuration',
  121. 'description' => 'Settings for Chado Features',
  122. 'page callback' => 'drupal_get_form',
  123. 'page arguments' => array('tripal_feature_admin'),
  124. 'access arguments' => array('administer site configuration'),
  125. 'type' => MENU_NORMAL_ITEM,
  126. );
  127. $items['admin/tripal/tripal_feature/fasta_loader'] = array(
  128. 'title' => 'Import a multi-FASTA file',
  129. 'description' => 'Load sequences from a multi-FASTA file into Chado',
  130. 'page callback' => 'drupal_get_form',
  131. 'page arguments' => array('tripal_feature_fasta_load_form'),
  132. 'access arguments' => array('administer site configuration'),
  133. 'type' => MENU_NORMAL_ITEM,
  134. );
  135. $items['admin/tripal/tripal_feature/gff3_load'] = array(
  136. 'title' => 'Import a GFF3 file',
  137. 'description' => 'Import a GFF3 file into Chado',
  138. 'page callback' => 'drupal_get_form',
  139. 'page arguments' => array('tripal_core_gff3_load_form'),
  140. 'access arguments' => array('access administration pages'),
  141. 'type' => MENU_NORMAL_ITEM,
  142. );
  143. // Adding Secondary Properties
  144. $items['node/%tf_node/tf_properties'] = array(
  145. 'title' => t('Add Properties & Synonyms'),
  146. 'description' => t('Settings for Features'),
  147. 'page callback' => 'tripal_feature_add_ALL_property_page',
  148. 'page arguments' => array(1),
  149. 'access arguments' => array('create chado_feature content'),
  150. 'type' => MENU_CALLBACK
  151. );
  152. $items['node/%tf_node/tf_db_references'] = array(
  153. 'title' => t('Add Database References'),
  154. 'description' => t('Settings for Features'),
  155. 'page callback' => 'tripal_feature_add_ALL_dbreferences_page',
  156. 'page arguments' => array(1),
  157. 'access arguments' => array('create chado_feature content'),
  158. 'type' => MENU_CALLBACK
  159. );
  160. $items['node/%tf_node/tf_relationships'] = array(
  161. 'title' => t('Add Relationships'),
  162. 'description' => t('Settings for Features'),
  163. 'page callback' => 'tripal_feature_add_ALL_relationships_page',
  164. 'page arguments' => array(1),
  165. 'access arguments' => array('create chado_feature content'),
  166. 'type' => MENU_CALLBACK
  167. );
  168. //Edit/Deleting Secondary Properties-------------
  169. /*
  170. $items['node/%tf_node/tf_edit_properties'] = array(
  171. 'title' => t('Edit Properties'),
  172. 'description' => t('Settings for Features'),
  173. 'page callback' => 'tripal_feature_edit_ALL_properties_page',
  174. 'page arguments' => array(1),
  175. 'access arguments' => array('edit chado_feature content'),
  176. 'type' => MENU_LOCAL_TASK,
  177. 'weight' => 8,
  178. );
  179. $items['node/%tf_node/tf_edit_relationships'] = array(
  180. 'title' => t('Edit Relationships'),
  181. 'description' => t('Settings for Feature'),
  182. 'page callback' => 'tripal_feature_edit_ALL_relationships_page',
  183. 'page arguments' => array(1),
  184. 'access arguments' => array('edit chado_feature content'),
  185. 'type' => MENU_LOCAL_TASK,
  186. 'weight' => 9,
  187. );
  188. */
  189. $items['node/%tf_node/tf_edit_db_references'] = array(
  190. 'title' => t('Edit References'),
  191. 'description' => t('Settings for Feature'),
  192. 'page callback' => 'tripal_feature_edit_ALL_dbreferences_page',
  193. 'page arguments' => array(1),
  194. 'access arguments' => array('edit chado_feature content'),
  195. 'type' => MENU_LOCAL_TASK,
  196. 'weight' => 10,
  197. );
  198. // managing relationship aggregates
  199. $items['admin/tripal/tripal_feature/aggregate'] = array(
  200. 'title' => 'Feature Relationship Aggegators',
  201. 'description' => t('Features have relationships with other features and it may be desirable to aggregate the content from one ore more child or parent feature.'),
  202. 'page callback' => 'tripal_feature_aggregator_page',
  203. 'access arguments' => array('manage chado_feature aggregator'),
  204. 'type' => MENU_NORMAL_ITEM,
  205. );
  206. $items['admin/tripal/tripal_feature/aggregate/new'] = array(
  207. 'title' => 'Add an Aggregator',
  208. 'page callback' => 'drupal_get_form',
  209. 'page arguments' => array('tripal_feature_aggregator_form'),
  210. 'access arguments' => array('manage chado_feature aggregator'),
  211. 'type' => MENU_NORMAL_ITEM,
  212. );
  213. $items['admin/tripal/tripal_feature/aggregate/edit/js'] = array(
  214. 'title' => 'Edit an Aggegator',
  215. 'page callback' => 'tripal_feature_aggregator_ajax_edit',
  216. 'access arguments' => array('manage chado_feature aggregator'),
  217. 'type' => MENU_CALLBACK,
  218. );
  219. return $items;
  220. }
  221. /*************************************************************************
  222. * Implements Menu wildcard_load hook
  223. * Purpose: Allows the node ID of a chado feature to be dynamically
  224. * pulled from the path. The node is loaded from this node ID
  225. * and supplied to the page as an arguement
  226. */
  227. function tf_node_load($nid) {
  228. if (is_numeric($nid)) {
  229. $node = node_load($nid);
  230. if ($node->type == 'chado_feature') {
  231. return $node;
  232. }
  233. }
  234. return FALSE;
  235. }
  236. /*************************************************************************
  237. *
  238. */
  239. function tripal_feature_block($op = 'list', $delta = 0, $edit=array()){
  240. switch($op) {
  241. case 'list':
  242. $blocks['references']['info'] = t('Tripal Feature References');
  243. $blocks['references']['cache'] = BLOCK_NO_CACHE;
  244. $blocks['base']['info'] = t('Tripal Feature Details');
  245. $blocks['base']['cache'] = BLOCK_NO_CACHE;
  246. $blocks['sequence']['info'] = t('Tripal Feature Sequence');
  247. $blocks['sequence']['cache'] = BLOCK_NO_CACHE;
  248. $blocks['synonyms']['info'] = t('Tripal Feature Synonyms');
  249. $blocks['synonyms']['cache'] = BLOCK_NO_CACHE;
  250. $blocks['properties']['info'] = t('Tripal Feature Properties');
  251. $blocks['properties']['cache'] = BLOCK_NO_CACHE;;
  252. $blocks['featureloc_sequences']['info'] = t('Tripal Formatted Location Sequence');
  253. $blocks['featureloc_sequences']['cache'] = BLOCK_NO_CACHE;
  254. $blocks['featurelocs_as_parent']['info'] = t('Tripal Feature Locations as Parent');
  255. $blocks['featurelocs_as_parent']['cache'] = BLOCK_NO_CACHE;
  256. $blocks['featurelocs_as_child']['info'] = t('Tripal Feature Locations as Child');
  257. $blocks['featurelocs_as_child']['cache'] = BLOCK_NO_CACHE;
  258. $blocks['relationships_as_object']['info'] = t('Tripal Feature Relationships as Object');
  259. $blocks['relationships_as_object']['cache'] = BLOCK_NO_CACHE;
  260. $blocks['relationships_as_subject']['info'] = t('Tripal Feature Relationships as Subject');
  261. $blocks['relationships_as_subject']['cache'] = BLOCK_NO_CACHE;
  262. $blocks['org_feature_counts']['info'] = t('Tripal Organism Feature Counts');
  263. $blocks['org_feature_counts']['cache'] = BLOCK_NO_CACHE;
  264. $blocks['org_feature_browser']['info'] = t('Tripal Organism Feature Browser');
  265. $blocks['org_feature_browser']['cache'] = BLOCK_NO_CACHE;
  266. return $blocks;
  267. case 'view':
  268. if(user_access('access chado_feature content') and arg(0) == 'node' and is_numeric(arg(1))) {
  269. $nid = arg(1);
  270. $node = node_load($nid);
  271. $block = array();
  272. switch($delta){
  273. case 'references':
  274. $block['subject'] = t('References');
  275. $block['content'] = theme('tripal_feature_references',$node);
  276. break;
  277. case 'base':
  278. $block['subject'] = t('Feature Details');
  279. $block['content'] = theme('tripal_feature_base',$node);
  280. break;
  281. case 'synonyms':
  282. $block['subject'] = t('Synonyms');
  283. $block['content'] = theme('tripal_feature_synonyms',$node);
  284. break;
  285. case 'properties':
  286. $block['subject'] = t('Properties');
  287. $block['content'] = theme('tripal_feature_properties',$node);
  288. break;;
  289. case 'sequence':
  290. $block['subject'] = t('Sequence');
  291. $block['content'] = theme('tripal_feature_sequence',$node);
  292. break;
  293. case 'featureloc_sequences':
  294. $block['subject'] = t('Formatted Sequences');
  295. $block['content'] = theme('tripal_feature_featureloc_sequences',$node);
  296. break;
  297. case 'featurelocs_as_parent':
  298. $block['subject'] = t('Parent Features');
  299. $block['content'] = theme('tripal_feature_featurelocs_as_parent',$node);
  300. break;
  301. case 'featurelocs_as_child':
  302. $block['subject'] = t('Child Features');
  303. $block['content'] = theme('tripal_feature_featurelocs_as_child',$node);
  304. break;
  305. case 'relationships_as_object':
  306. $block['subject'] = t('Object Relationships');
  307. $block['content'] = theme('tripal_feature_relationships_as_object',$node);
  308. break;
  309. case 'relationships_as_subject':
  310. $block['subject'] = t('Subject Relationships');
  311. $block['content'] = theme('tripal_feature_relationships_as_child',$node);
  312. break;
  313. case 'org_feature_counts':
  314. $block['subject'] = t('Feature Type Summary');
  315. $block['content'] = theme('tripal_organism_feature_counts', $node);
  316. break;
  317. case 'org_feature_browser':
  318. $block['subject'] = t('Feature Browser');
  319. $block['content'] = theme('tripal_organism_feature_browser', $node);
  320. break;
  321. default :
  322. }
  323. return $block;
  324. }
  325. }
  326. }
  327. /************************************************************************
  328. * When a new chado_feature node is created we also need to add information
  329. * to our chado_feature table. This function is called on insert of a new node
  330. * of type 'chado_feature' and inserts the necessary information.
  331. */
  332. function chado_feature_insert($node){
  333. // remove spaces, newlines from residues
  334. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  335. // If this feature already exists then don't recreate it in chado
  336. // TODO: the unique index in chado for this also includes the type_id. If the site
  337. // ever needs to have the same feature name for different types then this will break.
  338. $feature_sql = "SELECT *
  339. FROM {Feature} F
  340. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  341. WHERE uniquename = '%s' and organism_id = %d and CVT.name = '%s'";
  342. $previous_db = tripal_db_set_active('chado');
  343. $feature = db_fetch_object(db_query($feature_sql,$node->uniquename,$node->organism_id,$node->feature_type));
  344. tripal_db_set_active($previous_db);
  345. // if the feature doesn't exist then let's create it in chado.
  346. if(!$feature){
  347. $sql = "INSERT INTO {feature} (organism_id, name, uniquename, residues, seqlen,".
  348. " is_obsolete, type_id)".
  349. " VALUES(%d,'%s','%s','%s',%d, %s, ".
  350. " (SELECT cvterm_id ".
  351. " FROM {CVTerm} CVT ".
  352. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  353. " WHERE CV.name = 'sequence' and CVT.name = '%s'))";
  354. $obsolete = 'FALSE';
  355. if($node->is_obsolete){
  356. $obsolete = 'TRUE';
  357. }
  358. // use chado database
  359. $previous_db = tripal_db_set_active('chado');
  360. db_query($sql,$node->organism_id,$node->fname,$node->uniquename,
  361. $residues,strlen($residues),$obsolete,$node->feature_type);
  362. // now that we've added the feature, get the feature id for this feature
  363. $feature = db_fetch_object(db_query($feature_sql,$node->uniquename,$node->organism_id,$node->feature_type));
  364. // now use drupal database
  365. tripal_db_set_active($previous_db);
  366. }
  367. // add the genbank accession and synonyms
  368. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  369. // make sure the entry for this feature doesn't already exist in the chado_feature table
  370. // if it doesn't exist then we want to add it.
  371. $node_check_sql = "SELECT * FROM {chado_feature} ".
  372. "WHERE feature_id = '%s'";
  373. $node_check = db_fetch_object(db_query($node_check_sql,$feature->feature_id));
  374. if(!$node_check){
  375. // next add the item to the drupal table
  376. $sql = "INSERT INTO {chado_feature} (nid, vid, feature_id, sync_date) ".
  377. "VALUES (%d, %d, %d, " . time() . ")";
  378. db_query($sql,$node->nid,$node->vid,$feature->feature_id);
  379. }
  380. }
  381. /************************************************************************
  382. */
  383. function chado_feature_delete($node){
  384. // get feature_id so we can remove it from chado database
  385. $sql_drupal = "SELECT feature_id ".
  386. "FROM {chado_feature} ".
  387. "WHERE nid = %d AND vid = %d";
  388. $feature_id = db_result(db_query($sql_drupal, $node->nid, $node->vid));
  389. // remove the drupal content
  390. $sql_del = "DELETE FROM {chado_feature} ".
  391. "WHERE nid = %d ".
  392. "AND vid = %d";
  393. db_query($sql_del, $node->nid, $node->vid);
  394. $sql_del = "DELETE FROM {node} ".
  395. "WHERE nid = %d ".
  396. "AND vid = %d";
  397. db_query($sql_del, $node->nid, $node->vid);
  398. $sql_del = "DELETE FROM {node_revisions} ".
  399. "WHERE nid = %d ".
  400. "AND vid = %d";
  401. db_query($sql_del, $node->nid, $node->vid);
  402. // Remove data from feature tables of chado database. This will
  403. // cause a cascade delete and remove all data in referencing tables
  404. // for this feature
  405. $previous_db = tripal_db_set_active('chado');
  406. db_query("DELETE FROM {feature} WHERE feature_id = %d", $feature_id);
  407. tripal_db_set_active($previous_db);
  408. drupal_set_message("The feature and all associated data were removed from ".
  409. "chado");
  410. }
  411. /************************************************************************
  412. */
  413. function chado_feature_update($node){
  414. if($node->revision){
  415. // TODO -- decide what to do about revisions
  416. } else {
  417. // get the feature for this node:
  418. $sql = 'SELECT feature_id FROM {chado_feature} WHERE vid = %d';
  419. $feature = db_fetch_object(db_query($sql, $node->vid));
  420. // remove spaces, newlines from residues
  421. $residues = preg_replace("/[\n\r\s]/","",$node->residues);
  422. $sql = "UPDATE {feature} ".
  423. " SET residues = '%s', ".
  424. " name = '%s', ".
  425. " uniquename = '%s', ".
  426. " seqlen = %d, ".
  427. " organism_id = %d, ".
  428. " is_obsolete = %s, ".
  429. " type_id = (SELECT cvterm_id ".
  430. " FROM {CVTerm} CVT ".
  431. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  432. " WHERE CV.name = 'sequence' and CVT.name = '%s') ".
  433. "WHERE feature_id = %d ";
  434. $obsolete = 'FALSE';
  435. if($node->is_obsolete){
  436. $obsolete = 'TRUE';
  437. }
  438. $previous_db = tripal_db_set_active('chado'); // use chado database
  439. db_query($sql,$residues,$node->fname,$node->uniquename,
  440. strlen($residues),$node->organism_id,$obsolete,$node->feature_type,
  441. $feature->feature_id);
  442. tripal_db_set_active($previous_db); // now use drupal database
  443. // add the genbank accession & synonyms
  444. // chado_feature_add_gbaccession($node->gbaccession,$feature->feature_id);
  445. chado_feature_add_synonyms($node->synonyms,$feature->feature_id);
  446. }
  447. }
  448. /************************************************************************
  449. *
  450. */
  451. function chado_feature_add_synonyms($synonyms,$feature_id){
  452. drupal_set_message($synonyms);
  453. // make sure we only have a single space between each synonym
  454. $synonyms = preg_replace("/[\s\n\r]+/"," ",$synonyms);
  455. // split the synonyms into an array based on a space as the delimieter
  456. $syn_array = array();
  457. $syn_array = explode(" ",$synonyms);
  458. // use the chado database
  459. $previous_db = tripal_db_set_active('chado');
  460. // remove any old synonyms
  461. $feature_syn_dsql = "DELETE FROM {feature_synonym} WHERE feature_id = %d";
  462. if(!db_query($feature_syn_dsql,$feature_id)){
  463. $error .= "Could not remove synonyms from feature. ";
  464. }
  465. // return if we don't have any synonmys to add
  466. if(!$synonyms){
  467. tripal_db_set_active($previous_db);
  468. return;
  469. }
  470. // iterate through each synonym and add it to the database
  471. foreach($syn_array as $syn){
  472. // skip this item if it's empty
  473. if(!$syn){ break; }
  474. // check to see if we have this accession number already in the database
  475. // if so then don't add it again. it messes up drupal if the insert fails.
  476. // It is possible for the accession number to be present and not the feature
  477. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  478. "WHERE name = '%s'";
  479. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  480. if(!$synonym){
  481. $synonym_isql = "INSERT INTO {synonym} (name,synonym_sgml,type_id) ".
  482. "VALUES ('%s','%s', ".
  483. " (SELECT cvterm_id ".
  484. " FROM {CVTerm} CVT ".
  485. " INNER JOIN CV ON CVT.cv_id = CV.cv_id ".
  486. " WHERE CV.name = 'feature_property' and CVT.name = 'synonym'))";
  487. if(!db_query($synonym_isql,$syn,$syn)){
  488. $error .= "Could not add synonym. ";
  489. }
  490. // now get the synonym we just added
  491. $synonym_sql = "SELECT synonym_id FROM {synonym} ".
  492. "WHERE name = '%s'";
  493. $synonym = db_fetch_object(db_query($synonym_sql,$syn));
  494. }
  495. // now add in our new sysnonym
  496. $feature_syn_isql = "INSERT INTO {feature_synonym} (synonym_id,feature_id,pub_id) ".
  497. "VALUES (%d,%d,1)";
  498. if(!db_query($feature_syn_isql,$synonym->synonym_id,$feature_id)){
  499. $error .= "Could not add synonyms to feature. ";
  500. }
  501. }
  502. // return to the drupal database
  503. tripal_db_set_active($previous_db);
  504. return $error;
  505. }
  506. /************************************************************************
  507. *
  508. */
  509. function chado_feature_add_gbaccession($accession,$feature_id){
  510. // use chado database
  511. $previous_db = tripal_db_set_active('chado');
  512. // remove any old accession from genbank dbEST
  513. $fdbxref_dsql = "DELETE FROM {feature_dbxref} ".
  514. "WHERE feature_id = %d and dbxref_id IN ".
  515. " (SELECT DBX.dbxref_id FROM {dbxref} DBX ".
  516. " INNER JOIN DB ON DB.db_id = DBX.db_id ".
  517. " INNER JOIN feature_dbxref FDBX ON DBX.dbxref_id = FDBX.dbxref_id ".
  518. " WHERE DB.name = 'DB:Genbank' and FDBX.feature_id = %d)";
  519. if(!db_query($fdbxref_dsql,$feature_id,$feature_id)){
  520. $error .= "Could not remove accession from feature. ";
  521. }
  522. // if we don't have an accession number to add then just return
  523. if(!$accession){
  524. tripal_db_set_active($previous_db);
  525. return;
  526. }
  527. // get the db_id
  528. $db_sql = "SELECT db_id FROM {DB} ".
  529. "WHERE name = 'DB:Genbank_est'";
  530. $db = db_fetch_object(db_query($db_sql));
  531. // check to see if we have this accession number already in the database
  532. // if so then don't add it again. it messes up drupal if the insert fails.
  533. // It is possible for the accession number to be present and not the feature
  534. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  535. "WHERE db_id = %d and accession = '%s'";
  536. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  537. if(!$dbxref){
  538. // add the accession number
  539. $dbxref_isql = "INSERT INTO {dbxref} (db_id,accession) ".
  540. " VALUES (%d, '%s') ";
  541. if(!db_query($dbxref_isql,$db->db_id,$accession)){
  542. $error .= 'Could not add accession as a database reference ';
  543. }
  544. // get the dbxref_id for the just added accession number
  545. $dbxref_sql = "SELECT dbxref_id FROM {dbxref} ".
  546. "WHERE db_id = %d and accession = '%s'";
  547. $dbxref = db_fetch_object(db_query($dbxref_sql,$db->db_id,$accession));
  548. }
  549. // associate the accession number with the feature
  550. $feature_dbxref_isql = "INSERT INTO {feature_dbxref} (feature_id,dbxref_id) ".
  551. " VALUES (%d, %d) ";
  552. if(!db_query($feature_dbxref_isql,$feature_id,$dbxref->dbxref_id)){
  553. $error .= 'Could not add feature database reference. ';
  554. }
  555. tripal_db_set_active($previous_db);
  556. return $error;
  557. }
  558. /************************************************************************
  559. *
  560. */
  561. function chado_feature_form ($node,$param){
  562. $type = node_get_types('type', $node);
  563. $form = array();
  564. $feature = $node->feature;
  565. $synonyms = $node->synonyms;
  566. $analyses = $node->analyses;
  567. $references = $node->references;
  568. // We need to pass above variables for preview to show
  569. $form['feature'] = array(
  570. '#type' => 'value',
  571. '#value' => $feature
  572. );
  573. // This field is read when previewing a node
  574. $form['synonyms'] = array(
  575. '#type' => 'value',
  576. '#value' => $synonyms
  577. );
  578. // This field is read when previewing a node
  579. $form['analyses'] = array(
  580. '#type' => 'value',
  581. '#value' => $analyses
  582. );
  583. // This field is read when previewing a node
  584. $form['references'] = array(
  585. '#type' => 'value',
  586. '#value' => $references
  587. );
  588. // keep track of the feature id if we have one. If we do have one then
  589. // this would indicate an update as opposed to an insert.
  590. $form['feature_id'] = array(
  591. '#type' => 'value',
  592. '#value' => $feature->feature_id,
  593. );
  594. $form['title']= array(
  595. '#type' => 'textfield',
  596. '#title' => t('Title'),
  597. '#required' => TRUE,
  598. '#default_value' => $node->title,
  599. '#description' => t('The title must be a unique identifier for this feature. It is recommended to use a combination of uniquename, organism and feature type in the title as this is guranteed to be unique.'),
  600. '#weight' => 1,
  601. '#maxlength' => 255
  602. );
  603. $form['uniquename']= array(
  604. '#type' => 'textfield',
  605. '#title' => t('Unique Feature Name'),
  606. '#required' => TRUE,
  607. '#default_value' => $feature->uniquename,
  608. '#description' => t('Enter a unique name for this feature. This name must be unique for the organism and feature type.'),
  609. '#weight' => 1,
  610. '#maxlength' => 255
  611. );
  612. $form['fname']= array(
  613. '#type' => 'textfield',
  614. '#title' => t('Feature Name'),
  615. '#required' => TRUE,
  616. '#default_value' => $feature->featurename,
  617. '#description' => t('Enter the name used by humans to refer to this feature.'),
  618. '#weight' => 1,
  619. '#maxlength' => 255
  620. );
  621. // get the list of supported feature types
  622. $ftypes = array();
  623. $ftypes[''] = '';
  624. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  625. foreach($supported_ftypes as $ftype){
  626. $ftypes["$ftype"] = $ftype;
  627. }
  628. $form['feature_type'] = array (
  629. '#title' => t('Feature Type'),
  630. '#type' => t('select'),
  631. '#description' => t("Choose the feature type."),
  632. '#required' => TRUE,
  633. '#default_value' => $feature->cvname,
  634. '#options' => $ftypes,
  635. '#weight' => 2
  636. );
  637. // get the list of organisms
  638. $sql = "SELECT * FROM {Organism} ORDER BY genus, species";
  639. $previous_db = tripal_db_set_active('chado'); // use chado database
  640. $org_rset = db_query($sql);
  641. tripal_db_set_active($previous_db); // now use drupal database
  642. //
  643. $organisms = array();
  644. $organisms[''] = '';
  645. while($organism = db_fetch_object($org_rset)){
  646. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  647. }
  648. $form['organism_id'] = array (
  649. '#title' => t('Organism'),
  650. '#type' => t('select'),
  651. '#description' => t("Choose the organism with which this feature is associated "),
  652. '#required' => TRUE,
  653. '#default_value' => $feature->organism_id,
  654. '#options' => $organisms,
  655. '#weight' => 3,
  656. );
  657. // Get synonyms
  658. if ($synonyms) {
  659. if (is_array($synonyms)) {
  660. foreach ($synonyms as $synonym){
  661. $syn_text .= "$synonym->name\n";
  662. }
  663. } else {
  664. $syn_text = $synonyms;
  665. }
  666. }
  667. $form['synonyms']= array(
  668. '#type' => 'textarea',
  669. '#title' => t('Synonyms'),
  670. '#required' => FALSE,
  671. '#default_value' => $syn_text,
  672. '#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.'),
  673. '#weight' => 5,
  674. );
  675. $form['residues']= array(
  676. '#type' => 'textarea',
  677. '#title' => t('Residues'),
  678. '#required' => FALSE,
  679. '#default_value' => $feature->residues,
  680. '#description' => t('Enter the nucelotide sequences for this feature'),
  681. '#weight' => 6
  682. );
  683. $checked = '';
  684. if($feature->is_obsolete == 't'){
  685. $checked = '1';
  686. }
  687. $form['is_obsolete']= array(
  688. '#type' => 'checkbox',
  689. '#title' => t('Is Obsolete'),
  690. '#required' => FALSE,
  691. '#default_value' => $checked,
  692. '#description' => t('Check this box if this sequence should be retired and no longer included in further analysis.'),
  693. '#weight' => 8
  694. );
  695. return $form;
  696. }
  697. /************************************************************************
  698. *
  699. */
  700. function chado_feature_validate($node){
  701. $result = 0;
  702. // if this is an update, we want to make sure that a different feature for
  703. // the organism doesn't already have this uniquename. We don't want to give
  704. // two sequences the same uniquename
  705. if($node->feature_id){
  706. $sql = "SELECT *
  707. FROM {Feature} F
  708. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  709. WHERE uniquename = '%s'
  710. AND organism_id = %d AND CVT.name = '%s' AND NOT feature_id = %d";
  711. $previous_db = tripal_db_set_active('chado');
  712. $result = db_fetch_object(db_query($sql, $node->uniquename,$node->organism_id,$node->feature_type,$node->feature_id));
  713. tripal_db_set_active($previous_db);
  714. if($result){
  715. form_set_error('uniquename',t("Feature update cannot proceed. The feature name '$node->uniquename' is not unique for this organism. Please provide a unique name for this feature. "));
  716. }
  717. }
  718. // if this is an insert then we just need to make sure this name doesn't
  719. // already exist for this organism if it does then we need to throw an error
  720. else {
  721. $sql = "SELECT *
  722. FROM {Feature} F
  723. INNER JOIN {cvterm} CVT ON F.type_id = CVT.cvterm_id
  724. WHERE uniquename = '%s'
  725. AND organism_id = %d AND CVT.name = '%s'";
  726. $previous_db = tripal_db_set_active('chado');
  727. $result = db_fetch_object(db_query($sql, $node->uniquename,$node->organism_id,$node->feature_type));
  728. tripal_db_set_active($previous_db);
  729. if($result){
  730. form_set_error('uniquename',t("Feature insert cannot proceed. The feature name '$node->uniquename' already exists for this organism. Please provide a unique name for this feature. "));
  731. }
  732. }
  733. // we want to remove all characters except IUPAC nucleotide characters from the
  734. // the residues. however, residues are not required so if blank then we'll skip
  735. // this step
  736. if($node->residues){
  737. $residues = preg_replace("/[^\w]/",'',$node->residues);
  738. if(!preg_match("/^[ACTGURYMKSWBDHVN]+$/i",$residues)){
  739. form_set_error('residues',t("The residues in feature $node->name contains more than the nucleotide IUPAC characters. Only the following characters are allowed: A,C,T,G,U,R,Y,M,K,S,W,B,D,H,V,N: '" . $residues ."'"));
  740. }
  741. }
  742. // we don't allow a genbank accession number for a contig
  743. if($node->feature_type == 'contig' and $node->gbaccession){
  744. form_set_error('gbaccession',t("Contigs cannot have a genbank accession number. Please change the feature type or remove the accession number"));
  745. }
  746. }
  747. /************************************************************************
  748. * When a node is requested by the user this function is called to allow us
  749. * to add auxiliary data to the node object.
  750. */
  751. function chado_feature_load($node){
  752. // add the feature_id for this node:
  753. $sql = 'SELECT feature_id FROM {chado_feature} WHERE nid = %d';
  754. $feature = db_fetch_object(db_query($sql, $node->nid));
  755. $feature_id = $feature->feature_id;
  756. // get information about this feature and add it to the items in this node
  757. $sql = "SELECT F.feature_id, F.name as featurename, F.uniquename, ".
  758. "F.residues, F.seqlen, O.genus, O.species, O.common_name, ".
  759. " CVT.name as cvname, O.organism_id, F.type_id, F.is_obsolete ".
  760. "FROM {Feature} F ".
  761. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  762. " INNER JOIN CVterm CVT ON F.type_id = CVT.cvterm_id ".
  763. "WHERE F.feature_id = %d";
  764. $previous_db = tripal_db_set_active('chado'); // use chado database
  765. $feature = db_fetch_object(db_query($sql,$feature_id));
  766. tripal_db_set_active($previous_db); // now use drupal database
  767. $additions->feature = $feature;
  768. $additions->seqlen = $feature->seqlen;
  769. $organism_id = $feature->organism_id;
  770. // add organism node nid
  771. $sql = "SELECT nid FROM {chado_organism} WHERE organism_id = %d";
  772. $org_nid = db_result(db_query($sql, $additions->feature->organism_id));
  773. $additions->org_nid = $org_nid;
  774. $additions->accession = variable_get('chado_feature_accession_prefix','ID') . $feature->feature_id;
  775. // add details about the organism
  776. $additions->organism = tripal_feature_load_organism($organism_id);
  777. // add the list of synomyms
  778. $additions->synonyms = tripal_feature_load_synonyms($feature_id);
  779. return $additions;
  780. }
  781. /************************************************************************
  782. *
  783. */
  784. function tripal_feature_load_organism ($organism_id){
  785. // add organism details
  786. $sql = "SELECT * FROM {organism} WHERE organism_id = %d";
  787. $previous_db = tripal_db_set_active('chado'); // use chado database
  788. $organism = db_fetch_object(db_query($sql,$organism_id));
  789. tripal_db_set_active($previous_db); // now use drupal database
  790. return $organism;
  791. }
  792. /************************************************************************
  793. *
  794. */
  795. function tripal_feature_load_synonyms ($feature_id){
  796. $sql = "SELECT S.name ".
  797. "FROM {Feature_Synonym} FS ".
  798. " INNER JOIN {Synonym} S ".
  799. " ON FS.synonym_id = S.Synonym_id ".
  800. "WHERE FS.feature_id = %d ".
  801. "ORDER BY S.name ";
  802. $previous_db = tripal_db_set_active('chado'); // use chado database
  803. $results = db_query($sql,$feature_id);
  804. tripal_db_set_active($previous_db); // now use drupal database
  805. $synonyms = array();
  806. $i=0;
  807. while($synonym = db_fetch_object($results)){
  808. $synonyms[$i++] = $synonym;
  809. }
  810. return $synonyms;
  811. }
  812. /************************************************************************
  813. *
  814. */
  815. function tripal_feature_load_properties ($feature_id){
  816. $sql = "SELECT CVT.name as cvname, FS.type_id, FS.value, FS.rank,
  817. CVT.definition, CVT.is_obsolete,
  818. DBX.dbxref_id,DBX.accession,DB.name as dbname,
  819. DB.urlprefix, DB.description as db_description, DB.url
  820. FROM {featureprop} FS
  821. INNER JOIN {cvterm} CVT ON FS.type_id = CVT.cvterm_id
  822. INNER JOIN {dbxref} DBX ON CVT.dbxref_id = DBX.dbxref_id
  823. INNER JOIN {db} DB ON DB.db_id = DBX.db_id
  824. WHERE FS.feature_id = %d
  825. ORDER BY FS.rank ASC";
  826. $previous_db = tripal_db_set_active('chado'); // use chado database
  827. $results = db_query($sql,$feature_id);
  828. tripal_db_set_active($previous_db); // now use drupal database
  829. $i=0;
  830. $properties = array();
  831. while($property = db_fetch_object($results)){
  832. $properties[$i++] = $property;
  833. }
  834. return $properties;
  835. }
  836. /************************************************************************
  837. *
  838. */
  839. function tripal_feature_load_references ($feature_id){
  840. $sql = "SELECT F.uniquename,F.Feature_id,DBX.accession,DB.description as dbdesc, ".
  841. " DB.db_id, DB.name as db_name, DB.urlprefix,DBX.dbxref_id ".
  842. "FROM {feature} F ".
  843. " INNER JOIN {feature_dbxref} FDBX on F.feature_id = FDBX.feature_id ".
  844. " INNER JOIN {dbxref} DBX on DBX.dbxref_id = FDBX.dbxref_id ".
  845. " INNER JOIN {db} on DB.db_id = DBX.db_id ".
  846. "WHERE F.feature_id = %d ".
  847. "ORDER BY DB.name ";
  848. $previous_db = tripal_db_set_active('chado'); // use chado database
  849. $results = db_query($sql,$feature_id);
  850. tripal_db_set_active($previous_db); // now use drupal database
  851. $references = array();
  852. $i=0;
  853. while($accession = db_fetch_object($results)){
  854. $references[$i++] = $accession;
  855. }
  856. return $references;
  857. }
  858. /************************************************************************
  859. *
  860. */
  861. function tripal_feature_load_featurelocs ($feature_id,$side = 'as_parent',$aggregate = 1){
  862. $sql = "SELECT
  863. F.name, F.feature_id, F.uniquename,
  864. FS.name as src_name,
  865. FS.feature_id as src_feature_id,
  866. FS.uniquename as src_uniquename,
  867. CVT.name as cvname, CVT.cvterm_id,
  868. CVTS.name as src_cvname, CVTS.cvterm_id as src_cvterm_id,
  869. FL.fmin, FL.fmax, FL.is_fmin_partial, FL.is_fmax_partial,FL.strand,
  870. FL.phase
  871. FROM {featureloc} FL
  872. INNER JOIN {feature} F on FL.feature_id = F.feature_id
  873. INNER JOIN {feature} FS on FS.feature_id = FL.srcfeature_id
  874. INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id
  875. INNER JOIN {cvterm} CVTS on FS.type_id = CVTS.cvterm_id
  876. ";
  877. if(strcmp($side,'as_parent')==0){
  878. $sql .= "WHERE FL.srcfeature_id = %d ";
  879. }
  880. if(strcmp($side,'as_child')==0){
  881. $sql .= "WHERE FL.feature_id = %d ";
  882. }
  883. $previous_db = tripal_db_set_active('chado'); // use chado database
  884. $flresults = db_query($sql, $feature_id);
  885. tripal_db_set_active($previous_db); // now use drupal database
  886. // copy the results into an array
  887. $i=0;
  888. $featurelocs = array();
  889. while($loc = db_fetch_object($flresults)){
  890. // if a drupal node exists for this feature then add the nid to the
  891. // results object
  892. $sql = 'SELECT nid FROM {chado_feature} WHERE feature_id = %d';
  893. if(strcmp($side,'as_parent')==0){
  894. $feature = db_fetch_object(db_query($sql, $loc->feature_id));
  895. }
  896. if(strcmp($side,'as_child')==0){
  897. $feature = db_fetch_object(db_query($sql, $loc->src_feature_id));
  898. }
  899. $loc->nid = $feature->nid;
  900. // add the result to the array
  901. $featurelocs[$i++] = $loc;
  902. }
  903. // Add the relationship feature locs if aggregate is turned on
  904. if($aggregate and strcmp($side,'as_parent')==0){
  905. // get the relationships for this feature without substituting any children
  906. // for the parent. We want all relationships
  907. $relationships = tripal_feature_get_aggregate_relationships($feature_id,0);
  908. foreach($relationships as $rindex => $rel){
  909. // get the featurelocs for each of the relationship features
  910. $rel_featurelocs = tripal_feature_load_featurelocs ($rel->subject_id,'as_child',0);
  911. foreach($rel_featurelocs as $findex => $rfloc){
  912. $featurelocs[$i++] = $rfloc;
  913. }
  914. }
  915. }
  916. usort($featurelocs,'tripal_feature_sort_locations');
  917. return $featurelocs;
  918. }
  919. /************************************************************************
  920. * used to sort the feature locs by start position
  921. */
  922. function tripal_feature_sort_locations($a,$b){
  923. return strnatcmp($a->fmin, $b->fmin);
  924. }
  925. /************************************************************************
  926. *
  927. */
  928. function tripal_feature_load_relationships ($feature_id,$side = 'as_subject'){
  929. // get the relationships for this feature. The query below is used for both
  930. // querying the object and subject relationships
  931. $sql = "SELECT
  932. FS.name as subject_name,
  933. FS.uniquename as subject_uniquename,
  934. CVTS.name as subject_type,
  935. CVTS.cvterm_id as subject_type_id,
  936. FR.subject_id,
  937. FR.type_id as relationship_type_id,
  938. CVT.name as rel_type,
  939. FO.name as object_name,
  940. FO.uniquename as object_uniquename,
  941. CVTO.name as object_type,
  942. CVTO.cvterm_id as object_type_id,
  943. FR.object_id,
  944. FR.rank
  945. FROM {feature_relationship} FR
  946. INNER JOIN {cvterm} CVT ON FR.type_id = CVT.cvterm_id
  947. INNER JOIN {feature} FS ON FS.feature_id = FR.subject_id
  948. INNER JOIN {feature} FO ON FO.feature_id = FR.object_id
  949. INNER JOIN {cvterm} CVTO ON FO.type_id = CVTO.cvterm_id
  950. INNER JOIN {cvterm} CVTS ON FS.type_id = CVTS.cvterm_id
  951. ";
  952. if(strcmp($side,'as_object')==0){
  953. $sql .= " WHERE FR.object_id = %d";
  954. }
  955. if(strcmp($side,'as_subject')==0){
  956. $sql .= " WHERE FR.subject_id = %d";
  957. }
  958. $sql .= " ORDER BY FR.rank";
  959. // get the relationships
  960. $previous_db = tripal_db_set_active('chado'); // use chado database
  961. $results = db_query($sql, $feature_id);
  962. tripal_db_set_active($previous_db); // now use drupal database
  963. // iterate through the relationships, put these in an array and add
  964. // in the Drupal node id if one exists
  965. $i=0;
  966. $nodesql = "SELECT nid FROM {chado_feature} WHERE feature_id = %d";
  967. $relationships = array();
  968. while($rel = db_fetch_object($results)){
  969. $node = db_fetch_object(db_query($nodesql,$rel->subject_id));
  970. if($node){
  971. $rel->subject_nid = $node->nid;
  972. }
  973. $node = db_fetch_object(db_query($nodesql,$rel->object_id));
  974. if($node){
  975. $rel->object_nid = $node->nid;
  976. }
  977. $relationships[$i++] = $rel;
  978. }
  979. return $relationships;
  980. }
  981. /************************************************************************
  982. *
  983. */
  984. function tripal_feature_get_aggregate_types($feature_id){
  985. // get the feature details
  986. $sql = 'SELECT type_id FROM {feature} WHERE feature_id = %d';
  987. $previous_db = tripal_db_set_active('chado'); // use chado database
  988. $feature = db_fetch_object(db_query($sql, $feature_id));
  989. tripal_db_set_active($previous_db); // now use drupal database
  990. // check to see if this feature is of a type with an aggregate
  991. $sql = "SELECT * FROM {tripal_feature_relagg} WHERE type_id = %d";
  992. $types = array();
  993. $results = db_query($sql,$feature->type_id);
  994. while($agg = db_fetch_object($results)){
  995. $types[] = $agg->rel_type_id;
  996. }
  997. return $types;
  998. }
  999. /************************************************************************
  1000. *
  1001. */
  1002. function tripal_feature_get_aggregate_relationships($feature_id, $substitute=1,
  1003. $levels=0, $base_type_id=NULL, $depth=0)
  1004. {
  1005. // we only want to recurse to as many levels deep as indicated by the
  1006. // $levels variable, but only if this variable is > 0. If 0 then we
  1007. // recurse until we reach the end of the relationships tree.
  1008. if($levels > 0 and $levels == $depth){
  1009. return NULL;
  1010. }
  1011. // first get the relationships for this feature
  1012. $relationships = tripal_feature_load_relationships($feature_id,'as_object');
  1013. // next, iterate through these relationships and descend, adding in those
  1014. // that are specified by the aggregator.
  1015. $i=0;
  1016. $new_relationships = array();
  1017. foreach($relationships as $rindex => $rel){
  1018. // set the base type id
  1019. if(!$base_type_id){
  1020. $base_type_id = $rel->object_type_id;
  1021. }
  1022. // check to see if we have an aggregator for this base type
  1023. $sql = "SELECT * FROM {tripal_feature_relagg} WHERE type_id = %d and rel_type_id = %d";
  1024. $agg = db_fetch_object(db_query($sql,$base_type_id,$rel->subject_type_id));
  1025. if($agg){
  1026. // if we're not going to substitute the resulting relationships for the
  1027. // parent then we need to add the parent to our list
  1028. if(!$substitute){
  1029. $new_relationships[$i++] = $rel;
  1030. }
  1031. // recurse all relationships
  1032. $agg_relationships = tripal_feature_get_aggregate_relationships(
  1033. $rel->subject_id,$levels,$base_type_id,$depth++);
  1034. // if we have an aggregate defined but we have no relationships beyond
  1035. // this point then there's nothing we can substitute
  1036. if(!$agg_relationships and $substitute){
  1037. $new_relationships[$i++] = $rel;
  1038. }
  1039. // merge all relationships into one array
  1040. foreach($agg_relationships as $aindex => $arel){
  1041. $new_relationships[$i++] = $arel;
  1042. }
  1043. }
  1044. else {
  1045. // if we don't have an aggregate then keep the current relationship
  1046. $new_relationships[$i++] = $rel;
  1047. }
  1048. }
  1049. return $new_relationships;
  1050. }
  1051. /************************************************************************
  1052. *
  1053. */
  1054. function tripal_feature_load_featureloc_sequences($feature_id,$featurelocs){
  1055. // if we don't have any featurelocs then no point in continuing
  1056. if(!$featurelocs){
  1057. return array();
  1058. }
  1059. // get the list of relationships (including any aggregators) and iterate
  1060. // through each one to find information needed to color-code the reference sequence
  1061. $relationships = tripal_feature_get_aggregate_relationships($feature_id);
  1062. if(!$relationships){
  1063. return array();
  1064. }
  1065. foreach($relationships as $rindex => $rel){
  1066. // get the featurelocs for each of the relationship features
  1067. $rel_featurelocs = tripal_feature_load_featurelocs ($rel->subject_id,'as_child',0);
  1068. foreach($rel_featurelocs as $rfindex => $rel_featureloc){
  1069. // keep track of this unique source feature
  1070. $src = $rel_featureloc->src_feature_id ."-". $rel_featureloc->src_type_id;
  1071. // copy over the results to the relationship object. Since there can
  1072. // be more than one feature location for each relationship feature we
  1073. // use the '$src' variable to keep track of these.
  1074. $rel->featurelocs->$src->src_uniquename = $rel_featureloc->src_uniquename;
  1075. $rel->featurelocs->$src->src_type_id = $rel_featureloc->src_type_id;
  1076. $rel->featurelocs->$src->src_cvname = $rel_featureloc->src_cvname;
  1077. $rel->featurelocs->$src->fmin = $rel_featureloc->fmin;
  1078. $rel->featurelocs->$src->fmax = $rel_featureloc->fmax;
  1079. $rel->featurelocs->$src->src_name = $rel_featureloc->src_name;
  1080. // keep track of the individual parts for each relationship
  1081. $start = $rel->featurelocs->$src->fmin;
  1082. $end = $rel->featurelocs->$src->fmax;
  1083. $rel_locs[$src]['parts'][$start]['type'] = $rel->subject_type;
  1084. $rel_locs[$src]['parts'][$start]['start'] = $start;
  1085. $rel_locs[$src]['parts'][$start]['end'] = $end;
  1086. }
  1087. }
  1088. // now get the sequence for each featureloc and highlight the different
  1089. // relationships
  1090. $sql = "SELECT residues FROM {feature} WHERE feature_id = %d";
  1091. $floc_sequences = array();
  1092. foreach ($featurelocs as $findex => $featureloc){
  1093. // get the residues for this feature
  1094. $previous_db = tripal_db_set_active('chado'); // use chado database
  1095. $feature = db_fetch_object(db_query($sql,$featureloc->src_feature_id));
  1096. tripal_db_set_active($previous_db); // now use drupal database
  1097. $src = $featureloc->src_feature_id ."-". $featureloc->src_type_id;
  1098. // orient the parts to the beginning of the feature sequence
  1099. $parts = $rel_locs[$src]['parts'];
  1100. usort($parts, 'tripal_feature_sort_rel_parts');
  1101. foreach ($parts as $start => $attrs){
  1102. $parts[$start]['start'] = $parts[$start]['start'] - $featureloc->fmin;
  1103. $parts[$start]['end'] = $parts[$start]['end'] - $featureloc->fmin;
  1104. }
  1105. $floc_sequences[$src]['src'] = $src;
  1106. $floc_sequences[$src]['type'] = $featureloc->cvname;
  1107. $sequence = substr($feature->residues,$featureloc->fmin,$featureloc->fmax - $featureloc->fmin);
  1108. $floc_sequences[$src]['formatted_seq'] = tripal_feature_color_sequence (
  1109. $sequence,$parts);
  1110. }
  1111. return $floc_sequences;
  1112. }
  1113. /*******************************************************************************
  1114. *
  1115. */
  1116. function tripal_feature_load_organism_feature_counts($organism){
  1117. // don't show the browser if the settings in the admin page is turned off
  1118. // instead return the array indicating the status of the browser
  1119. $show_counts = variable_get('tripal_feature_summary_setting','show_feature_summary');
  1120. if(strcmp($show_counts,'show_feature_summary')!=0){
  1121. return array ('enabled' => false );
  1122. }
  1123. // get the feature counts. This is dependent on a materialized view
  1124. // installed with the organism module
  1125. $sql = "
  1126. SELECT OFC.num_features,OFC.feature_type,CVT.definition
  1127. FROM {organism_feature_count} OFC
  1128. INNER JOIN {cvterm} CVT on OFC.cvterm_id = CVT.cvterm_id
  1129. WHERE organism_id = %d
  1130. ORDER BY num_features desc
  1131. ";
  1132. $previous_db = tripal_db_set_active('chado'); // use chado database
  1133. $org_features = db_query($sql,$organism->organism_id);
  1134. tripal_db_set_active($previous_db); // now use drupal database
  1135. $i=0;
  1136. $types = array();
  1137. while($type = db_fetch_object($org_features)){
  1138. $types[$i++] = $type;
  1139. }
  1140. return array ( 'types' => $types, 'enabled' => true );
  1141. }
  1142. /************************************************************************
  1143. *
  1144. */
  1145. function tripal_feature_load_organism_feature_browser($organism){
  1146. // don't show the browser if the settings in the admin page is turned off
  1147. // instead return the array indicating the status of the browser
  1148. $show_browser = variable_get('tripal_feature_browse_setting','show_feature_browser');
  1149. if(strcmp($show_browser,'show_feature_browser')!=0){
  1150. return array ('enabled' => false);
  1151. }
  1152. # get the list of available sequence ontology terms for which
  1153. # we will build drupal pages from features in chado. If a feature
  1154. # is not one of the specified typse we won't build a node for it.
  1155. $allowed_types = variable_get('chado_feature_types','EST contig');
  1156. $allowed_types = preg_replace("/[\s\n\r]+/"," ",$allowed_types);
  1157. $so_terms = split(' ',$allowed_types);
  1158. $where_cvt = "";
  1159. foreach ($so_terms as $term){
  1160. $where_cvt .= "CVT.name = '$term' OR ";
  1161. }
  1162. $where_cvt = substr($where_cvt,0,strlen($where_cvt)-3); # strip trailing 'OR'
  1163. // get the features for this organism
  1164. $sql = "SELECT F.name,F.feature_id,F.uniquename,CVT.name as cvname ".
  1165. "FROM {feature} F ".
  1166. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1167. "WHERE organism_id = %s and ($where_cvt) ".
  1168. "ORDER BY feature_id ASC";
  1169. // the counting SQL
  1170. $csql = "SELECT count(*) ".
  1171. "FROM {feature} F".
  1172. " INNER JOIN {cvterm} CVT on F.type_id = CVT.cvterm_id ".
  1173. "WHERE organism_id = %s and ($where_cvt) ".
  1174. "GROUP BY organism_id ";
  1175. $previous_db = tripal_db_set_active('chado'); // use chado database
  1176. $org_features = pager_query($sql,10,0,$csql,$organism->organism_id);
  1177. tripal_db_set_active($previous_db); // now use drupal database
  1178. $pager = theme('pager');
  1179. // prepare the query that will lookup node ids
  1180. $sql = "SELECT nid FROM {chado_feature} ".
  1181. "WHERE feature_id = %d";
  1182. $i=0;
  1183. $features = array();
  1184. while($feature = db_fetch_object($org_features)){
  1185. $node = db_fetch_object(db_query($sql,$feature->feature_id));
  1186. $feature->nid = $node->nid;
  1187. $features[$i++] = $feature;
  1188. }
  1189. return array ( 'features' => $features, 'pager' => $pager, 'enabled' => true );
  1190. }
  1191. /************************************************************************
  1192. * used to sort the list of relationship objects by start position
  1193. */
  1194. function tripal_feature_sort_rel_objects($a,$b){
  1195. return strnatcmp($a->fmin, $b->fmin);
  1196. }
  1197. /************************************************************************
  1198. * used to sort the list of relationship parts by start position
  1199. */
  1200. function tripal_feature_sort_rel_parts($a,$b){
  1201. return strnatcmp($a['start'], $b['start']);
  1202. }
  1203. /************************************************************************
  1204. *
  1205. */
  1206. function tripal_feature_color_sequence ($sequence,$parts){
  1207. $types = array();
  1208. // first get the list of types so we can create a color legend
  1209. foreach ($parts as $index => $child){
  1210. $type = $child['type'];
  1211. if(!in_array($type,$types)){
  1212. $types[] = $type;
  1213. }
  1214. }
  1215. $newseq .= "<div id=\"tripal_feature-featureloc_sequence-legend\">Legend: ";
  1216. foreach($types as $type){
  1217. $newseq .= "<span class=\"tripal_feature-featureloc_sequence-$type\">$type</span>";
  1218. }
  1219. $newseq .= "</div>";
  1220. // set the background color of the rows based on the type
  1221. $pos = 0;
  1222. $newseq .= "<pre id=\"tripal_feature-featureloc_sequence\">";
  1223. foreach ($parts as $index => $child){
  1224. $type = $child['type'];
  1225. $start = $child['start'];
  1226. $end = $child['end'];
  1227. $class = "class=\"tripal_feature-featureloc_sequence-$type\"";
  1228. // iterate through the sequence up to the end of the child
  1229. for ($i = $pos; $i < $end; $i++){
  1230. // if we're at the beginning of the child sequence then set the
  1231. // appropriate text color
  1232. if($pos == $start){
  1233. $newseq .= "<span $class>";
  1234. $func = 'uc'; // nucleotides within the child should be uppercase
  1235. }
  1236. $newseq .= $sequence{$pos};
  1237. $seqcount++;
  1238. if($seqcount % 60 == 0){
  1239. $newseq .= "\n";
  1240. }
  1241. $pos++;
  1242. if($pos == $end){
  1243. $newseq .= "</span>";
  1244. $func = 'lc';
  1245. }
  1246. }
  1247. }
  1248. $newseq .= "</pre>";
  1249. return $newseq;
  1250. }
  1251. /************************************************************************
  1252. * This function customizes the view of the chado_feature node. It allows
  1253. * us to generate the markup.
  1254. */
  1255. function chado_feature_view ($node, $teaser = FALSE, $page = FALSE) {
  1256. if (!$teaser) {
  1257. // use drupal's default node view:
  1258. $node = node_prepare($node, $teaser);
  1259. // if we're building the node for searching then
  1260. // we want to handle this within the module and
  1261. // not allow theme customization. We don't want to
  1262. // index all items (such as DNA sequence).
  1263. if($node->build_mode == NODE_BUILD_SEARCH_INDEX){
  1264. $node->content['index_version'] = array(
  1265. '#value' => theme('tripal_feature_search_index',$node),
  1266. '#weight' => 1,
  1267. );
  1268. }
  1269. elseif($node->build_mode == NODE_BUILD_SEARCH_RESULT){
  1270. $node->content['index_version'] = array(
  1271. '#value' => theme('tripal_feature_search_results',$node),
  1272. '#weight' => 1,
  1273. );
  1274. }
  1275. else {
  1276. // do nothing here, let the theme derived template handle display
  1277. }
  1278. }
  1279. return $node;
  1280. }
  1281. /*******************************************************************************
  1282. * Display feature information for associated organisms. This function also
  1283. * provides contents for indexing
  1284. */
  1285. function tripal_feature_nodeapi(&$node, $op, $teaser, $page) {
  1286. switch ($op) {
  1287. // Note that this function only adds feature view to an organism node.
  1288. // The view of a feature node is controled by the theme *.tpl file
  1289. case 'view':
  1290. switch($node->type){
  1291. case 'chado_organism':
  1292. // Show feature browser
  1293. $types_to_show = array('chado_organism', 'chado_library');
  1294. if (in_array($node->type, $types_to_show, TRUE)) {
  1295. $node->content['tripal_organism_feature_counts'] = array(
  1296. '#value' => theme('tripal_organism_feature_counts', $node),
  1297. );
  1298. $node->content['tripal_organism_feature_browser'] = array(
  1299. '#value' => theme('tripal_organism_feature_browser', $node),
  1300. );
  1301. }
  1302. break;
  1303. case 'chado_library':
  1304. break;
  1305. default:
  1306. }
  1307. break;
  1308. }
  1309. }
  1310. /************************************************************************
  1311. * We need to let drupal know about our theme functions and their arguments.
  1312. * We create theme functions to allow users of the module to customize the
  1313. * look and feel of the output generated in this module
  1314. */
  1315. function tripal_feature_theme () {
  1316. return array(
  1317. 'tripal_feature_search_index' => array (
  1318. 'arguments' => array('node'),
  1319. ),
  1320. 'tripal_feature_search_results' => array (
  1321. 'arguments' => array('node'),
  1322. ),
  1323. 'tripal_organism_feature_browser' => array (
  1324. 'arguments' => array('node'=> null),
  1325. 'template' => 'tripal_organism_feature_browser',
  1326. ),
  1327. 'tripal_organism_feature_counts' => array (
  1328. 'arguments' => array('node'=> null),
  1329. 'template' => 'tripal_organism_feature_counts',
  1330. ),
  1331. 'tripal_feature_base' => array (
  1332. 'arguments' => array('node'=> null),
  1333. 'template' => 'tripal_feature_base',
  1334. ),
  1335. 'tripal_feature_sequence' => array (
  1336. 'arguments' => array('node'=> null),
  1337. 'template' => 'tripal_feature_sequence',
  1338. ),
  1339. 'tripal_feature_synonyms' => array (
  1340. 'arguments' => array('node'=> null),
  1341. 'template' => 'tripal_feature_synonyms',
  1342. ),
  1343. 'tripal_feature_featureloc_sequences' => array (
  1344. 'arguments' => array('node'=> null),
  1345. 'template' => 'tripal_feature_featureloc_sequences',
  1346. ),
  1347. 'tripal_feature_references' => array (
  1348. 'arguments' => array('node'=> null),
  1349. 'template' => 'tripal_feature_references',
  1350. ),
  1351. 'tripal_feature_properties' => array (
  1352. 'arguments' => array('node'=> null),
  1353. 'template' => 'tripal_feature_properties',
  1354. ),
  1355. 'tripal_feature_featurelocs_as_parent' => array (
  1356. 'arguments' => array('node'=> null),
  1357. 'template' => 'tripal_feature_featurelocs_as_parent',
  1358. ),
  1359. 'tripal_feature_featurelocs_as_child' => array (
  1360. 'arguments' => array('node'=> null),
  1361. 'template' => 'tripal_feature_featurelocs_as_child',
  1362. ),
  1363. 'tripal_feature_relationships_as_object' => array (
  1364. 'arguments' => array('node'=> null),
  1365. 'template' => 'tripal_feature_relationships_as_object',
  1366. ),
  1367. 'tripal_feature_relationships_as_subject' => array (
  1368. 'arguments' => array('node'=> null),
  1369. 'template' => 'tripal_feature_relationships_as_subject',
  1370. ),
  1371. );
  1372. }
  1373. /*******************************************************************************
  1374. *
  1375. */
  1376. function tripal_feature_preprocess(&$variables){
  1377. // if the template file is the default node template file then we want
  1378. // to add all of our variables.
  1379. if($variables['template_files'][0] == 'node-chado_feature'){
  1380. $feature = $variables['node']->feature;
  1381. $variables['tripal_feature']['synonyms'] = tripal_feature_load_synonyms($feature->feature_id);
  1382. $variables['tripal_feature']['object_relationships'] = tripal_feature_get_aggregate_relationships($feature->feature_id,0);
  1383. $variables['tripal_feature']['subject_relationships'] = tripal_feature_load_relationships($feature->feature_id,'as_subject');
  1384. $variables['tripal_feature']['featurelocs_as_child'] = tripal_feature_load_featurelocs($feature->feature_id,'as_child',0);
  1385. $variables['tripal_feature']['featurelocs_as_parent'] = tripal_feature_load_featurelocs($feature->feature_id,'as_parent');
  1386. $variables['tripal_feature']['references'] = tripal_feature_load_references($feature->feature_id);
  1387. $variables['tripal_feature']['featureloc_sequences'] = tripal_feature_load_featureloc_sequences ($feature->feature_id,$featurelocs);
  1388. }
  1389. }
  1390. function tripal_feature_preprocess_tripal_organism_feature_counts(&$variables){
  1391. $organism = $variables['node']->organism;
  1392. $variables['tripal_feature']['feature_counts'] = tripal_feature_load_organism_feature_counts($organism);
  1393. }
  1394. /*******************************************************************************
  1395. *
  1396. */
  1397. function tripal_feature_preprocess_tripal_organism_feature_browser(&$variables){
  1398. $organism = $variables['node']->organism;
  1399. $variables['tripal_feature']['browser'] = tripal_feature_load_organism_feature_browser($organism);
  1400. }
  1401. /*******************************************************************************
  1402. *
  1403. */
  1404. function tripal_feature_preprocess_tripal_feature_properties(&$variables){
  1405. $feature = $variables['node']->feature;
  1406. $variables['tripal_feature']['properties'] = tripal_feature_load_properties($feature->feature_id);
  1407. }
  1408. /*******************************************************************************
  1409. *
  1410. */
  1411. function tripal_feature_preprocess_tripal_feature_synonyms(&$variables){
  1412. $feature = $variables['node']->feature;
  1413. $variables['tripal_feature']['synonyms'] = tripal_feature_load_synonyms($feature->feature_id);
  1414. }
  1415. /*******************************************************************************
  1416. *
  1417. */
  1418. function tripal_feature_preprocess_tripal_feature_relationships_as_object(&$variables){
  1419. $feature = $variables['node']->feature;
  1420. $variables['tripal_feature']['object_relationships'] = tripal_feature_get_aggregate_relationships($feature->feature_id,0);
  1421. }
  1422. /*******************************************************************************
  1423. *
  1424. */
  1425. function tripal_feature_preprocess_tripal_feature_relationships_as_subject(&$variables){
  1426. $feature = $variables['node']->feature;
  1427. $variables['tripal_feature']['subject_relationships'] = tripal_feature_load_relationships($feature->feature_id,'as_subject');
  1428. }
  1429. /*******************************************************************************
  1430. *
  1431. */
  1432. function tripal_feature_preprocess_tripal_feature_featurelocs_as_child(&$variables){
  1433. $feature = $variables['node']->feature;
  1434. $variables['tripal_feature']['featurelocs_as_child'] = tripal_feature_load_featurelocs($feature->feature_id,'as_child',0);
  1435. }
  1436. /*******************************************************************************
  1437. *
  1438. */
  1439. function tripal_feature_preprocess_tripal_feature_featurelocs_as_parent(&$variables){
  1440. $feature = $variables['node']->feature;
  1441. $variables['tripal_feature']['featurelocs_as_parent'] = tripal_feature_load_featurelocs($feature->feature_id,'as_parent');
  1442. }
  1443. /*******************************************************************************
  1444. *
  1445. */
  1446. function tripal_feature_preprocess_tripal_feature_references(&$variables){
  1447. $feature = $variables['node']->feature;
  1448. $variables['tripal_feature']['references'] = tripal_feature_load_references($feature->feature_id);
  1449. }
  1450. /*******************************************************************************
  1451. *
  1452. */
  1453. function tripal_feature_preprocess_tripal_feature_featureloc_sequences(&$variables){
  1454. $feature = $variables['node']->feature;
  1455. $featurelocs = tripal_feature_load_featurelocs($feature->feature_id,'as_child',0);
  1456. $variables['tripal_feature']['featureloc_sequences'] = tripal_feature_load_featureloc_sequences ($feature->feature_id,$featurelocs);
  1457. }
  1458. /************************************************************************
  1459. *
  1460. */
  1461. function tripal_feature_cv_chart($chart_id){
  1462. // The CV module will create the JSON array necessary for buillding a
  1463. // pie chart using jgChart and Google Charts. We have to pass to it
  1464. // a table that contains count information, tell it which column
  1465. // contains the cvterm_id and provide a filter for getting the
  1466. // results we want from the table.
  1467. $organism_id = preg_replace("/^tripal_feature_cv_chart_(\d+)$/","$1",$chart_id);
  1468. $options = array(
  1469. count_mview => 'organism_feature_count',
  1470. cvterm_id_column => 'cvterm_id',
  1471. count_column => 'num_features',
  1472. size => '550x200',
  1473. filter => "CNT.organism_id = $organism_id",
  1474. );
  1475. return $options;
  1476. }
  1477. /************************************************************************
  1478. *
  1479. */
  1480. function tripal_feature_cv_tree($tree_id){
  1481. // The CV module will create the JSON array necessary for buillding a
  1482. // pie chart using jgChart and Google Charts. We have to pass to it
  1483. // a table that contains count information, tell it which column
  1484. // contains the cvterm_id and provide a filter for getting the
  1485. // results we want from the table.
  1486. $organism_id = preg_replace("/^tripal_feature_cv_tree_(\d+)$/","$1",$tree_id);
  1487. $options = array(
  1488. cv_id => tripal_cv_get_cv_id('sequence'),
  1489. count_mview => 'organism_feature_count',
  1490. cvterm_id_column => 'cvterm_id',
  1491. count_column => 'num_features',
  1492. filter => "CNT.organism_id = $organism_id",
  1493. label => 'Features',
  1494. );
  1495. return $options;
  1496. }
  1497. /************************************************************************
  1498. * This function is an extension of the chado_feature_view by providing
  1499. * the markup for the feature object THAT WILL BE INDEXED.
  1500. */
  1501. function theme_tripal_feature_search_index ($node) {
  1502. $feature = $node->feature;
  1503. $content = '';
  1504. // get the accession prefix
  1505. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1506. $content .= "<h1>$feature->uniquename</h1>. ";
  1507. $content .= "<strong>$aprefix$feature->feature_id.</strong> ";
  1508. $content .= "$feature->cvname ";
  1509. $content .= "$feature->common_name ";
  1510. // add the synonyms of this feature to the text for searching
  1511. $synonyms = $node->synonyms;
  1512. if(count($synonyms) > 0){
  1513. foreach ($synonyms as $result){
  1514. $content .= "$result->name ";
  1515. }
  1516. }
  1517. return $content;
  1518. }
  1519. /************************************************************************
  1520. * This function is an extension of the chado_feature_view by providing
  1521. * the markup for the feature object THAT WILL BE INDEXED.
  1522. */
  1523. function theme_tripal_feature_search_results ($node) {
  1524. $feature = $node->feature;
  1525. $content = '';
  1526. // get the accession prefix
  1527. $aprefix = variable_get('chado_feature_accession_prefix','ID');
  1528. $content .= "Feature Name: <h1>$feature->uniquename</h1>. ";
  1529. $content .= "<strong>Accession: $aprefix$feature->feature_id.</strong>";
  1530. $content .= "Type: $feature->cvname. ";
  1531. $content .= "Organism: $feature->common_name. ";
  1532. // add the synonyms of this feature to the text for searching
  1533. $synonyms = $node->synonyms;
  1534. if(count($synonyms) > 0){
  1535. $content .= "Synonyms: ";
  1536. foreach ($synonyms as $result){
  1537. $content .= "$result->name, ";
  1538. }
  1539. }
  1540. return $content;
  1541. }
  1542. /************************************************************************
  1543. *
  1544. */
  1545. function tripal_feature_set_vocabulary (){
  1546. //include the file containing the required functions for adding taxonomy vocabs
  1547. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1548. // get the vocabularies so that we make sure we don't recreate
  1549. // the vocabs that already exist
  1550. $vocabularies = taxonomy_get_vocabularies();
  1551. $ft_vid = NULL;
  1552. $op_vid = NULL;
  1553. $lb_vid = NULL;
  1554. $an_vid = NULL;
  1555. // These taxonomic terms are hard coded because we
  1556. // konw we have these relationships in the chado tables
  1557. // through foreign key relationships. The tripal
  1558. // modules that correspond to these chado "modules" don't
  1559. // need to be installed for the taxonomy to work.
  1560. foreach($vocabularies as $vocab){
  1561. if($vocab->name == 'Feature Type'){
  1562. $ft_vid = $vocab->vid;
  1563. }
  1564. if($vocab->name == 'Organism'){
  1565. $op_vid = $vocab->vid;
  1566. }
  1567. if($vocab->name == 'Library'){
  1568. $lb_vid = $vocab->vid;
  1569. }
  1570. if($vocab->name == 'Analysis'){
  1571. $an_vid = $vocab->vid;
  1572. }
  1573. }
  1574. if(!$ft_vid){
  1575. $form_state = array();
  1576. $values = array(
  1577. 'name' => t('Feature Type'),
  1578. 'nodes' => array('chado_feature' => 'chado_feature'),
  1579. 'description' => t('The feature type (or SO cvterm for this feature).'),
  1580. 'help' => t('Select the term that matches the feature '),
  1581. 'tags' => 0,
  1582. 'hierarchy' => 1,
  1583. 'relations' => 1,
  1584. 'multiple' => 0,
  1585. 'required' => 0,
  1586. 'weight' => 1,
  1587. );
  1588. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1589. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1590. }
  1591. if(!$op_vid){
  1592. $form_state = array();
  1593. $values = array(
  1594. 'name' => t('Organism'),
  1595. 'nodes' => array('chado_feature' => 'chado_feature'),
  1596. 'description' => t('The organism to which this feature belongs.'),
  1597. 'help' => t('Select the term that matches the feature '),
  1598. 'tags' => 0,
  1599. 'hierarchy' => 1,
  1600. 'relations' => 1,
  1601. 'multiple' => 0,
  1602. 'required' => 0,
  1603. 'weight' => 2,
  1604. );
  1605. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1606. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1607. }
  1608. if(!$lb_vid){
  1609. $form_state = array();
  1610. $values = array(
  1611. 'name' => t('Library'),
  1612. 'nodes' => array('chado_feature' => 'chado_feature'),
  1613. 'description' => t('Chado features associated with a library are assigned the term associated with the library'),
  1614. 'help' => t('Select the term that matches the feature '),
  1615. 'tags' => 0,
  1616. 'hierarchy' => 1,
  1617. 'relations' => 1,
  1618. 'multiple' => 0,
  1619. 'required' => 0,
  1620. 'weight' => 3,
  1621. );
  1622. drupal_execute('taxonomy_form_vocabulary', $form_state, $values);
  1623. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1624. }
  1625. if(!$an_vid){
  1626. $form_state = array();
  1627. $values = array(
  1628. 'name' => t('Analysis'),
  1629. 'nodes' => array('chado_feature' => 'chado_feature'),
  1630. 'description' => t('Any analysis to which this feature belongs.'),
  1631. 'help' => t('Select the term that matches the feature '),
  1632. 'tags' => 0,
  1633. 'hierarchy' => 1,
  1634. 'relations' => 1,
  1635. 'multiple' => 1,
  1636. 'required' => 0,
  1637. 'weight' => 4,
  1638. );
  1639. drupal_execute('taxonomy_form_vocabulary', $form_state,$values);
  1640. drupal_execute('taxonomy_form_vocabulary', $form_state);
  1641. }
  1642. }
  1643. /************************************************************************
  1644. *
  1645. */
  1646. function tripal_feature_del_vocabulary(){
  1647. //include the file containing the required functions for adding taxonomy vocabs
  1648. module_load_include('inc', 'taxonomy', 'taxonomy.admin');
  1649. // get the vocabularies
  1650. $vocabularies = taxonomy_get_vocabularies();
  1651. // These taxonomic terms are hard coded because we
  1652. // know we have these relationships in the chado tables
  1653. // through foreign key relationships. The tripal
  1654. // modules that correspond to these chado "modules" don't
  1655. // need to be installed for the taxonomy to work.
  1656. foreach($vocabularies as $vocab){
  1657. if($vocab->name == 'Feature Type'){
  1658. taxonomy_del_vocabulary($vocab->vid);
  1659. }
  1660. if($vocab->name == 'Organism'){
  1661. taxonomy_del_vocabulary($vocab->vid);
  1662. }
  1663. if($vocab->name == 'Library'){
  1664. taxonomy_del_vocabulary($vocab->vid);
  1665. }
  1666. if($vocab->name == 'Analysis'){
  1667. taxonomy_del_vocabulary($vocab->vid);
  1668. }
  1669. }
  1670. }
  1671. /************************************************************************
  1672. *
  1673. */
  1674. function tripal_features_set_taxonomy($max_sync = 0,$job_id = NULL){
  1675. // make sure our vocabularies are cleaned and reset before proceeding
  1676. tripal_feature_del_vocabulary();
  1677. tripal_feature_set_vocabulary();
  1678. // iterate through all drupal feature nodes and set the taxonomy
  1679. $results = db_query("SELECT * FROM {chado_feature}");
  1680. $nsql = "SELECT * FROM {node} ".
  1681. "WHERE nid = %d";
  1682. $i = 0;
  1683. // load into ids array
  1684. $count = 0;
  1685. $chado_features = array();
  1686. while($chado_feature = db_fetch_object($results)){
  1687. $chado_features[$count] = $chado_feature;
  1688. $count++;
  1689. }
  1690. // Iterate through features that need to be synced
  1691. $interval = intval($count * 0.01);
  1692. foreach($chado_features as $chado_feature){
  1693. // update the job status every 1% features
  1694. if($job_id and $i % $interval == 0){
  1695. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1696. }
  1697. $node = db_fetch_object(db_query($nsql,$chado_feature->nid));
  1698. tripal_feature_set_taxonomy($node,$chado_feature->feature_id);
  1699. $i++;
  1700. }
  1701. }
  1702. /************************************************************************
  1703. *
  1704. */
  1705. function tripal_feature_set_taxonomy ($node,$feature_id){
  1706. // iterate through the taxonomy classes that have been
  1707. // selected by the admin user and make sure we only set those
  1708. $tax_classes = variable_get('tax_classes', '');
  1709. $do_ft = 0;
  1710. $do_op = 0;
  1711. $do_lb = 0;
  1712. $do_an = 0;
  1713. foreach($tax_classes as $class){
  1714. if(strcmp($class ,'organism')==0){
  1715. $do_op = 1;
  1716. }
  1717. if(strcmp($class,'feature_type')==0){
  1718. $do_ft = 1;
  1719. }
  1720. if(strcmp($class,'library')==0){
  1721. $do_lb = 1;
  1722. }
  1723. if(strcmp($class,'analysis')==0){
  1724. $do_an = 1;
  1725. }
  1726. }
  1727. // get the list of vocabularies and find our two vocabularies of interest
  1728. $vocabularies = taxonomy_get_vocabularies();
  1729. $ft_vid = NULL;
  1730. $op_vid = NULL;
  1731. $lb_vid = NULL;
  1732. $an_vid = NULL;
  1733. foreach($vocabularies as $vocab){
  1734. if($vocab->name == 'Feature Type'){
  1735. $ft_vid = $vocab->vid;
  1736. }
  1737. if($vocab->name == 'Organism'){
  1738. $op_vid = $vocab->vid;
  1739. }
  1740. if($vocab->name == 'Library'){
  1741. $lb_vid = $vocab->vid;
  1742. }
  1743. if($vocab->name == 'Analysis'){
  1744. $an_vid = $vocab->vid;
  1745. }
  1746. }
  1747. // get the cvterm and the organism for this feature
  1748. $sql = "SELECT CVT.name AS cvname, O.genus, O.species ".
  1749. "FROM {CVTerm} CVT ".
  1750. " INNER JOIN Feature F on F.type_id = CVT.cvterm_id ".
  1751. " INNER JOIN Organism O ON F.organism_id = O.organism_id ".
  1752. "WHERE F.feature_id = $feature_id";
  1753. $previous_db = tripal_db_set_active('chado'); // use chado database
  1754. $feature = db_fetch_object(db_query($sql));
  1755. tripal_db_set_active($previous_db); // now use drupal database
  1756. // Set the feature type for this feature
  1757. if($do_ft && $ft_vid){
  1758. $tags["$ft_vid"] = "$feature->cvname";
  1759. }
  1760. // Set the organism for this feature type
  1761. if($do_op && $op_vid){
  1762. $tags["$op_vid"] = "$feature->genus $feature->species";
  1763. }
  1764. // get the library that this feature may belong to and add it as taxonomy
  1765. if($do_lb && $lb_vid){
  1766. $sql = "SELECT L.name ".
  1767. "FROM {Library} L ".
  1768. " INNER JOIN Library_feature LF ON LF.library_id = L.library_id ".
  1769. "WHERE LF.feature_id = %d ";
  1770. $previous_db = tripal_db_set_active('chado'); // use chado database
  1771. $library = db_fetch_object(db_query($sql,$feature_id));
  1772. tripal_db_set_active($previous_db); // now use drupal database
  1773. $tags["$lb_vid"] = "$library->name";
  1774. }
  1775. // now add the taxonomy to the node
  1776. $terms['tags'] = $tags;
  1777. taxonomy_node_save($node,$terms);
  1778. // print "Setting $node->name: " . implode(", ",$tags) . "\n";
  1779. // get the analysis that this feature may belong to and add it as taxonomy
  1780. // We'll add each one individually since there may be more than one analysis
  1781. if($do_an && $an_vid){
  1782. $sql = "SELECT A.name ".
  1783. "FROM {Analysis} A ".
  1784. " INNER JOIN Analysisfeature AF ON AF.analysis_id = A.analysis_id ".
  1785. "WHERE AF.feature_id = $feature_id ";
  1786. $results = db_query($sql);
  1787. $previous_db = tripal_db_set_active('chado'); // use chado database
  1788. $analysis_terms = array();
  1789. while($analysis=db_fetch_object($results)){
  1790. $tags2["$an_vid"] = "$analysis->name";
  1791. $terms['tags'] = $tags2;
  1792. taxonomy_node_save($node,$terms);
  1793. }
  1794. tripal_db_set_active($previous_db); // now use drupal database
  1795. }
  1796. }
  1797. /************************************************************************
  1798. *
  1799. */
  1800. function tripal_features_cleanup($dummy = NULL, $job_id = NULL) {
  1801. // build the SQL statments needed to check if nodes point to valid features
  1802. $dsql = "SELECT * FROM {node} WHERE type = 'chado_feature' order by nid";
  1803. $nsql = "SELECT * FROM {node} WHERE nid = %d";
  1804. $csql = "SELECT * FROM {chado_feature} where nid = %d ";
  1805. $cfsql= "SELECT * FROM {chado_feature}";
  1806. $tsql = "SELECT * FROM {feature} F ".
  1807. " INNER JOIN CVTerm CVT ON F.type_id = CVT.cvterm_id ".
  1808. "WHERE feature_id = %d AND (";
  1809. $supported_ftypes = split("[ \n]",variable_get('chado_feature_types','EST contig'));
  1810. foreach($supported_ftypes as $ftype){
  1811. $tsql .= " CVT.name = '$ftype' OR ";
  1812. }
  1813. $tsql .= " 0=1) "; // add a 0=1 just as a filler so we don't have to remove a trailing 'OR'
  1814. // load into nodes array
  1815. $results = db_query($dsql);
  1816. $count = 0;
  1817. $nodes = array();
  1818. while($node = db_fetch_object($results)){
  1819. $nodes[$count] = $node;
  1820. $count++;
  1821. }
  1822. // load the chado_features into an array
  1823. $results = db_query($cfsql);
  1824. $cnodes = array();
  1825. while($node = db_fetch_object($results)){
  1826. $cnodes[$count] = $node;
  1827. $count++;
  1828. }
  1829. $interval = intval($count * 0.01);
  1830. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1831. foreach($nodes as $nid){
  1832. // update the job status every 1% features
  1833. if($job_id and $i % $interval == 0){
  1834. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1835. }
  1836. // first check to see if the node has a corresponding entry
  1837. // in the chado_feature table. If not then delete the node.
  1838. $feature = db_fetch_object(db_query($csql,$nid->nid));
  1839. if(!$feature){
  1840. node_delete($nid->nid);
  1841. $message = "Missing in chado_feature table.... DELETING: $nid->nid\n";
  1842. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1843. continue;
  1844. }
  1845. // second check to see if the node is for a feature of an allowed type.
  1846. // if not, then delete the node. This check will also take care of the
  1847. // case when a node exists and an entry in the chado_feature table exists
  1848. // but no feature with a matching feature_id exists
  1849. $previous_db = tripal_db_set_active('chado'); // use chado database
  1850. $ftype = db_fetch_object(db_query($tsql,$feature->feature_id));
  1851. tripal_db_set_active($previous_db); // now use drupal database
  1852. if(!$ftype){
  1853. node_delete($nid->nid);
  1854. db_query("DELETE FROM {chado_feature} WHERE feature_id = $feature->feature_id");
  1855. $message = "Node of the wrong feature type.... DELETING: $nid->nid\n";
  1856. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1857. }
  1858. $i++;
  1859. }
  1860. // iterate through all of the chado_feature nodes and delete those that aren't valid
  1861. foreach($cnodes as $nid){
  1862. // update the job status every 1% features
  1863. if($job_id and $i % $interval == 0){
  1864. tripal_job_set_progress($job_id,intval(($i/$count)*100));
  1865. }
  1866. $node = db_fetch_object(db_query($nsql,$nid->nid));
  1867. if(!$node){
  1868. db_query("DELETE FROM {chado_feature} WHERE nid = $nid->nid");
  1869. $message = "chado_feature missing node.... DELETING: $nid->nid\n";
  1870. watchdog('tripal_feature',$message,array(),WATCHDOG_WARNING);
  1871. }
  1872. $i++;
  1873. }
  1874. return '';
  1875. }
  1876. /************************************************************************
  1877. *
  1878. */
  1879. function tripal_feature_return_fasta($feature,$desc){
  1880. $fasta = ">" . variable_get('chado_feature_accession_prefix','ID') . "$feature->feature_id|$feature->name";
  1881. $fasta .= " $desc\n";
  1882. $fasta .= wordwrap($feature->residues, 50, "\n", true);
  1883. $fasta .= "\n\n";
  1884. return $fasta;
  1885. }