tripal_feature.module 72 KB

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