tripal_feature.module 73 KB

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