tripal_feature.module 72 KB

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