tripal_feature.module 80 KB

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