tripal_feature.module 78 KB

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