tripal_feature.module 70 KB

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