tripal_feature.module 80 KB

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