tripal_feature.module 79 KB

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