tripal_feature.module 73 KB

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