tripal_feature.module 73 KB

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