tripal_feature.module 77 KB

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