tripal_feature.module 69 KB

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