tripal_feature.module 80 KB

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