00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include "array.h"
00004 #include "format.h"
00005 #include "linestream.h"
00006 #include "sqvUtil.h"
00007 #include "sqvCircos.h"
00008 #include "log.h"
00009
00010 void conf_printHeader (FILE *fp,
00011 char *circos_dir,
00012 char *data_dir,
00013 char *sdata_dir,
00014 Chrdata_t *chromosomes,
00015 char *prefix,
00016 Locus locus,
00017 int scale)
00018 {
00019 fprintf (fp, "<colors>\n<<include %s/etc/colors.conf>>\n</colors>\n\n", circos_dir);
00020 fprintf (fp, "<fonts>\n<<include %s/etc/fonts.conf>>\n</fonts>\n\n", circos_dir);
00021
00022 fprintf (fp, "<<include %s/test/ideogram.conf>>\n<<include %s/ticks%i.conf>>\n\n", circos_dir, sdata_dir, scale);
00023
00024 if (chromosomes[0].instances == 1) {
00025 if (chromosomes[0].show == 23) {
00026 fprintf (fp, "karyotype = %s/karyotype.humanX.txt\n\n<image>\n", sdata_dir);
00027 }
00028 else if (chromosomes[0].show == 24) {
00029 fprintf (fp, "karyotype = %s/karyotype.humanY.txt\n\n<image>\n", sdata_dir);
00030 }
00031 else {
00032 fprintf (fp, "karyotype = %s/karyotype.human%i.txt\n\n<image>\n", sdata_dir, chromosomes[0].show);
00033 }
00034 }
00035 else {
00036
00037 fprintf (fp, "karyotype = %s/karyotype.human.txt\n\n<image>\n", sdata_dir);
00038 }
00039
00040 fprintf (fp, "dir = %s/tmp\nfile = %s_%s_%d_%d.png\n", data_dir, prefix, locus.chromosome, locus.start, locus.end);
00041 fprintf (fp, "svg = yes\npng = yes\n");
00042 fprintf (fp, "radius = 1500p\n");
00043 fprintf (fp, "background = white\n");
00044 fprintf (fp, "angle_offset = -90\n</image>\n\n");
00045 }
00046
00047 void conf_printUnits (FILE *fp, Array regions, Chrdata_t *chromosomes, int scale)
00048 {
00049 int i;
00050 SRegion_t *tmp;
00051 char *chrname;
00052 int sflag = 1;
00053 Stringa chrshow = stringCreate (50);
00054
00055 tmp = arrayp (regions, 0, SRegion_t);
00056 if (tmp->chromosome == 0) {
00057 fprintf (fp, "chromosomes_display_default = yes\n");
00058 fprintf (fp, "chromosomes_units = 1000000\n");
00059 }
00060 else {
00061 stringPrintf (chrshow, "chromosomes = ");
00062
00063 fprintf (fp, "chromosomes_units = %i\n", scale);
00064 fprintf (fp, "chromosomes_display_default = no\n");
00065
00066 for (i = 0; i < arrayMax (regions); i++) {
00067 tmp = arrayp (regions, i, SRegion_t);
00068 if (chromosomes[tmp->chromosome].show && tmp->show) {
00069 chrname = getHchrname (tmp->chromosome);
00070
00071 if (sflag == 0) {
00072 stringAppendf (chrshow, ";");
00073 }
00074 stringAppendf (chrshow, "%s:%i-%i", chrname, tmp->start/scale, tmp->end/scale);
00075 sflag = 0;
00076
00077 free (chrname);
00078 }
00079 }
00080 fprintf (fp, "%s\n", string (chrshow));
00081 }
00082
00083 stringDestroy (chrshow);
00084 }
00085
00086
00087
00088 void conf_printDataTracks (FILE *fp,
00089 char *prefix,
00090 Locus locus,
00091 char *sdata_dir,
00092 char *data_dir,
00093 float *rpos,
00094 Array regions,
00095 Chrdata_t *chromosomes,
00096 SVCfg_t *settings)
00097 {
00098 if (settings->tracks.expr || settings->tracks.genes || settings->tracks.exons || settings->rfilter.showtars) {
00099 fprintf (fp, "<plots>\nz = 5\nlayers_overflow = hide\n");
00100 if (settings->tracks.genes) {
00101 conf_printDatGenes (fp, sdata_dir, regions, chromosomes, rpos);
00102 if (settings->tracks.exons) {
00103 conf_printDatExons (fp, sdata_dir, regions, chromosomes, rpos);
00104 }
00105 }
00106 if (settings->rfilter.showtars) {
00107 conf_printFilterTars (fp, data_dir, prefix, locus, rpos);
00108 }
00109 fprintf (fp, "</plots>\n");
00110 }
00111 }
00112
00113 void conf_printDatGenes (FILE *fp, char *sdata_dir, Array regions, Chrdata_t *chromosomes, float *rpos)
00114 {
00115 float npos = *rpos - DATTRK_WIDTH;
00116
00117 fprintf (fp, "<plot>\nshow = yes\ntype = tile\n");
00118
00119 incl_getGeneHlightFile (fp, regions, sdata_dir);
00120
00121 fprintf (fp, "orientation = out\nlayers = %i\nmargin = 0.02u\n\n", TILE_LAYERS);
00122 fprintf (fp, "thickness = 20\npadding = 5\ncolor = blue\n\n");
00123 fprintf (fp, "stroke_thickness = 1\nstroke_color = blue\n");
00124 fprintf (fp, "r0 = %1.2fr\nr1 = %1.2fr\n\n", npos, *rpos);
00125
00126 fprintf (fp, "</plot>\n");
00127
00128 npos -= DATTRK_SPACER;
00129 *rpos = npos;
00130 }
00131
00132 void conf_printDatExons (FILE *fp, char *sdata_dir, Array regions, Chrdata_t *chromosomes, float *rpos)
00133 {
00134 float npos = *rpos - DATTRK_WIDTH;
00135
00136 fprintf (fp, "<plot>\nshow = yes\ntype = tile\n");
00137
00138 incl_getExonHlightFile (fp, regions, sdata_dir);
00139
00140 fprintf (fp, "orientation = in\nlayers = %i\nmargin = 0.02u\n\n", TILE_LAYERS);
00141 fprintf (fp, "thickness = 20\npadding = 5\n\n");
00142 fprintf (fp, "stroke_thickness = 1\nstroke_color = dgreen\ncolor = green\n\n");
00143
00144 fprintf (fp, "r0 = %1.2fr\nr1 = %1.2fr\n", npos, *rpos);
00145
00146 fprintf (fp, "</plot>\n");
00147
00148 npos -= DATTRK_SPACER;
00149 *rpos = npos;
00150 }
00151
00152 void conf_printFilterTars (FILE *fp, char *data_dir, char *prefix, Locus locus, float *rpos)
00153 {
00154 float npos = *rpos - DATTRK_WIDTH;
00155
00156 fprintf (fp, "<plot>\nshow = yes\ntype = tile\n");
00157 fprintf (fp, "file = %s/tmp/%s_%s_%d_%d.hlight.txt\n\n",
00158 data_dir, prefix, locus.chromosome, locus.start, locus.end );
00159 fprintf (fp, "orientation = in\nlayers = %i\nmargin = 0.02u\n\n", TILE_LAYERS);
00160 fprintf (fp, "thickness = 20\npadding = 5\n\n");
00161 fprintf (fp, "stroke_thickness = 1\nstroke_color = dred\ncolor = red\n\n");
00162
00163 fprintf (fp, "r0 = %1.2fr\nr1 = %1.2fr\n", npos, *rpos);
00164
00165 fprintf (fp, "</plot>\n");
00166
00167 npos -= DATTRK_SPACER;
00168 *rpos = npos;
00169 }
00170
00171 void conf_printLinks (FILE *fp, char *data_dir, float *rpos, char *prefix, Locus locus, int readlim)
00172 {
00173 fprintf (fp, "<links>\n\nz = 0\nradius = %1.2fr\nbezier_radius = 0.2r\n\n", *rpos);
00174
00175 fprintf (fp, "<link data>\nshow = yes\ncolor = vvdgrey\nthickness = 2\n");
00176 if (readlim) {
00177 fprintf (fp, "record_limit = %i\n", readlim);
00178 }
00179 fprintf (fp, "file = %s/tmp/%s_%s_%d_%d_s.data\n</link>\n\n",
00180 data_dir, prefix, locus.chromosome, locus.start, locus.end );
00181 fprintf (fp, "</links>\n\n");
00182 }
00183
00184 void conf_printFooter (FILE *fp)
00185 {
00186 fprintf (fp, "anglestep = 0.5\nminslicestep = 10\nbeziersamples = 40\ndebug = no\nwarnings = no\nimagemap = no\n\n");
00187 fprintf (fp, "units_ok = bupr\nunits_nounit = n\n\n");
00188 }
00189
00190 void incl_getGeneHlightFile (FILE *fp, Array regions, char *sdata_dir)
00191 {
00192 LineStream src;
00193 FILE *out;
00194 char *line;
00195 Texta entry;
00196 int i, astart, aend;
00197
00198 Stringa buffer = stringCreate (50);
00199
00200 stringPrintf (buffer, "%s/tmp/genes.hlight_s.txt", sdata_dir);
00201 if (!(out = fopen (string (buffer), "w"))) {
00202 fprintf (stderr, "Cannot open genes.hlight_s.txt\n");
00203 return;
00204 }
00205
00206 SRegion_t *tmp;
00207 tmp = arrayp (regions, 0, SRegion_t);
00208
00209 if (tmp->chromosome == 0) {
00210 fprintf (fp, "file = %s/genes.hlight.txt\n", sdata_dir);
00211 }
00212 else {
00213 for (i = 0; i < arrayMax (regions); i++) {
00214 tmp = arrayp (regions, i, SRegion_t);
00215
00216 if (tmp->chromosome == 23) {
00217 stringPrintf (buffer, "%s/X/genes.hlight.txt", sdata_dir);
00218 }
00219 else if (tmp->chromosome == 24) {
00220 stringPrintf (buffer, "%s/Y/genes.hlight.txt", sdata_dir);
00221 }
00222 else {
00223 stringPrintf (buffer, "%s/%i/genes.hlight.txt", sdata_dir, tmp->chromosome);
00224 }
00225
00226 if ((src = ls_createFromFile (string (buffer))) == NULL) {
00227 warn( "Cannot open genes.hlight.txt %s\n", string(buffer));
00228 return;
00229 }
00230
00231 while ((line = ls_nextLine (src)) != NULL) {
00232 entry = textFieldtokP (line, " ");
00233
00234 astart = atoi (textItem (entry, 1));
00235 aend = atoi (textItem (entry, 2));
00236
00237 if ((astart >= tmp->start && astart <= tmp->end) ||
00238 (aend >= tmp->start && aend <= tmp->end)) {
00239 fprintf (out, "%s\n", line);
00240 }
00241 textDestroy (entry);
00242 }
00243 }
00244
00245 fprintf (fp, "file = %s/tmp/genes.hlight_s.txt\n", sdata_dir);
00246 }
00247
00248 stringDestroy (buffer);
00249 ls_destroy (src);
00250 fclose (out);
00251 }
00252
00253 void incl_getExonHlightFile (FILE *fp, Array regions, char *sdata_dir)
00254 {
00255 LineStream src;
00256 FILE *out;
00257 char *line;
00258 Texta entry;
00259 int i, astart, aend;
00260
00261 Stringa buffer = stringCreate (50);
00262
00263 stringPrintf (buffer, "%s/tmp/exons.hlight_s.txt", sdata_dir);
00264 if (!(out = fopen (string (buffer), "w"))) {
00265 fprintf (stderr, "Cannot open exons.hlight_s.txt\n");
00266 return;
00267 }
00268
00269 SRegion_t *tmp;
00270 tmp = arrayp (regions, 0, SRegion_t);
00271
00272 if (tmp->chromosome == 0) {
00273 fprintf (fp, "file = %s/exons.hlight.txt\n", sdata_dir);
00274 }
00275 else {
00276 for (i = 0; i < arrayMax (regions); i++) {
00277 tmp = arrayp (regions, i, SRegion_t);
00278
00279 if (tmp->chromosome == 23) {
00280 stringPrintf (buffer, "%s/X/exons.hlight.txt", sdata_dir);
00281 }
00282 else if (tmp->chromosome == 24) {
00283 stringPrintf (buffer, "%s/Y/exons.hlight.txt", sdata_dir);
00284 }
00285 else {
00286 stringPrintf (buffer, "%s/%i/exons.hlight.txt", sdata_dir, tmp->chromosome);
00287 }
00288
00289 if ((src = ls_createFromFile (string (buffer))) == NULL) {
00290 fprintf (stderr, "Cannot open exons.hlight.txt\n");
00291 return;
00292 }
00293
00294 while ((line = ls_nextLine (src)) != NULL) {
00295 entry = textFieldtokP (line, " ");
00296
00297 astart = atoi (textItem (entry, 1));
00298 aend = atoi (textItem (entry, 2));
00299
00300 if ((astart >= tmp->start && astart <= tmp->end) ||
00301 (aend >= tmp->start && aend <= tmp->end)) {
00302 fprintf (out, "%s\n", line);
00303 }
00304 textDestroy (entry);
00305 }
00306 }
00307
00308 fprintf (fp, "file = %s/tmp/exons.hlight_s.txt\n", sdata_dir);
00309 }
00310
00311 stringDestroy (buffer);
00312 ls_destroy (src);
00313 fclose (out);
00314 }