lib/seqOut.c

Go to the documentation of this file.
00001 /* seqOut - stuff to output sequences and alignments in web 
00002  * or ascii viewable form. 
00003  *
00004  * This file is copyright 2002 Jim Kent, but license is hereby
00005  * granted for all use - public, private or commercial. */
00006 
00007 #include "common.h"
00008 #include "obscure.h"
00009 #include "dnautil.h"
00010 #include "fuzzyFind.h"
00011 #include "seqOut.h"
00012 #include "htmshell.h"
00013 #include "axt.h"
00014 
00015 static char const rcsid[] = "$Id: seqOut.c,v 1.27 2007/03/26 18:11:48 angie Exp $";
00016 
00017 struct cfm *cfmNew(int wordLen, int lineLen, 
00018         boolean lineNumbers, boolean countDown, FILE *out, int numOff)
00019 /* Set up colored sequence formatting for html. */
00020 {
00021 struct cfm *cfm;
00022 AllocVar(cfm);
00023 cfm->inWord = cfm->inLine = cfm->charCount = 0;
00024 cfm->color = 0;
00025 cfm->wordLen = wordLen;
00026 cfm->lineLen = lineLen;
00027 cfm->lineNumbers = lineNumbers;
00028 cfm->countDown = countDown;
00029 cfm->out = out;
00030 cfm->numOff = numOff;
00031 cfm->bold = cfm->underline = cfm->italic = FALSE;
00032 return cfm;
00033 }
00034 
00035 static void cfmPopFormat(struct cfm *cfm)
00036 /* Restore format to default. */
00037 {
00038 if (cfm->color != 0)
00039    fprintf(cfm->out, "</FONT>");
00040 if (cfm->underline)
00041   fprintf(cfm->out, "</U>");
00042 if (cfm->bold)
00043   fprintf(cfm->out, "</B>");
00044 if (cfm->italic)
00045   fprintf(cfm->out, "</I>");
00046 }
00047 
00048 static void cfmPushFormat(struct cfm *cfm)
00049 /* Set format. */
00050 {
00051 if (cfm->italic)
00052   fprintf(cfm->out, "<I>");
00053 if (cfm->bold)
00054   fprintf(cfm->out, "<B>");
00055 if (cfm->underline)
00056   fprintf(cfm->out, "<U>");
00057 if (cfm->color != 0)
00058   fprintf(cfm->out, "<FONT COLOR=\"#%06X\">", cfm->color);
00059 }
00060 
00061 void cfmOutExt(struct cfm *cfm, char c, int color, boolean underline, boolean bold, boolean italic)
00062 /* Write out a byte, and formatting extras  */
00063 {
00064 if (color != cfm->color || underline != cfm->underline
00065    || bold != cfm->bold || italic != cfm->italic)
00066    {
00067    cfmPopFormat(cfm);
00068    cfm->color = color;
00069    cfm->underline = underline;
00070    cfm->bold = bold;
00071    cfm->italic = italic;
00072    cfmPushFormat(cfm);
00073    }
00074 
00075 ++cfm->charCount;
00076 fputc(c, cfm->out);
00077 if (cfm->wordLen)
00078     {
00079     if (++cfm->inWord >= cfm->wordLen)
00080         {
00081         cfmPopFormat(cfm);
00082         fputc(' ', cfm->out);
00083         cfmPushFormat(cfm);
00084         cfm->inWord = 0;
00085         }
00086     }
00087 if (cfm->lineLen)
00088     {
00089     if (++cfm->inLine >= cfm->lineLen)
00090         {
00091         if (cfm->lineNumbers)
00092             {
00093             int pos = cfm->charCount;
00094             if (cfm->countDown)
00095                 {
00096                 pos = 1-pos;
00097                 }
00098             pos += cfm->numOff;
00099             cfmPopFormat(cfm);
00100             fprintf(cfm->out, " %d", pos);
00101             cfmPushFormat(cfm);
00102             }
00103         fprintf(cfm->out, "\n");
00104         cfm->inLine = 0;
00105         }
00106     }
00107 }
00108 
00109 void cfmOut(struct cfm *cfm, char c, int color)
00110 /* Write out a byte, and depending on color formatting extras  */
00111 {
00112 cfmOutExt(cfm, c, color, FALSE, FALSE, FALSE);
00113 }
00114 
00115 void cfmFree(struct cfm **pCfm)
00116 /* Finish up cfm formatting job. */
00117 {
00118 struct cfm *cfm = *pCfm;
00119 if (cfm != NULL)
00120     {
00121     cfmPopFormat(cfm);
00122     freez(pCfm);
00123     }
00124 }
00125 
00126 int seqOutColorLookup[] = 
00127     {
00128     0x000000,
00129     0x3300FF,
00130     0x22CCEE,
00131     0xFF0033,
00132     0xFFcc22,
00133     0x00aa00,
00134     0xFF0000,
00135     };
00136 
00137 
00138 void bafInit(struct baf *baf, DNA *needle, int nNumOff,  boolean nCountDown,
00139         DNA *haystack, int hNumOff, boolean hCountDown, FILE *out, 
00140         int lineSize, boolean isTrans )
00141 /* Initialize block alignment formatter. */
00142 {
00143 baf->cix = 0;
00144 baf->needle = needle;
00145 baf->nCountDown = nCountDown;
00146 baf->haystack = haystack;
00147 baf->nNumOff = nNumOff;
00148 baf->hNumOff = hNumOff;
00149 baf->hCountDown = hCountDown;
00150 baf->out = out;
00151 baf->lineSize = lineSize;
00152 baf->isTrans = isTrans;
00153 baf->nCurPos = baf->hCurPos = 0;
00154 baf->nLineStart = baf->hLineStart = 0;
00155 }
00156 
00157 void bafSetAli(struct baf *baf, struct ffAli *ali)
00158 /* Set up block formatter around an ffAli block. */
00159 {
00160 baf->nCurPos = ali->nStart - baf->needle;
00161 baf->hCurPos = ali->hStart - baf->haystack;
00162 }
00163 
00164 void bafSetPos(struct baf *baf, int nStart, int hStart)
00165 /* Set up block formatter starting at nStart/hStart. */
00166 {
00167 if (baf->isTrans)
00168     nStart *= 3;
00169 baf->nCurPos = nStart;
00170 baf->hCurPos = hStart;
00171 }
00172 
00173 void bafStartLine(struct baf *baf)
00174 /* Set up block formatter to start new line at current position. */
00175 {
00176 baf->nLineStart = baf->nCurPos;
00177 baf->hLineStart = baf->hCurPos;
00178 }
00179 
00180 static int maxDigits(int x, int y)
00181 {
00182 int xDigits = digitsBaseTen(x);
00183 int yDigits = digitsBaseTen(y);
00184 return (xDigits > yDigits ? xDigits : yDigits);
00185 }
00186 
00187 void bafWriteLine(struct baf *baf)
00188 /* Write out a line of an alignment (which takes up
00189  * three lines on the screen. */
00190 {
00191 int i;
00192 int count = baf->cix;
00193 int nStart = baf->nLineStart + 1 + baf->nNumOff;
00194 int hStart = baf->hLineStart + 1 + baf->hNumOff;
00195 int nEnd = baf->nCurPos + baf->nNumOff;
00196 int hEnd = baf->hCurPos + baf->hNumOff;
00197 int startDigits = maxDigits(nStart, hStart);
00198 int endDigits = maxDigits(nEnd, hEnd);
00199 int hStartNum, hEndNum;
00200 int nStartNum, nEndNum;
00201 static struct axtScoreScheme *ss = 0;  /* Scoring scheme. */
00202 struct cfm cfm;
00203 extern char blosumText[];
00204 extern struct axtScoreScheme *axtScoreSchemeFromProteinText(char *text, char *fileName);
00205 boolean revArrows = (baf->nCountDown ^ baf->hCountDown);
00206 char arrowChar = (revArrows ? '<' : '>');
00207 
00208 ZeroVar(&cfm);
00209 cfm.out = baf->out;
00210 if (ss == 0)
00211     ss = axtScoreSchemeFromProteinText(blosumText, "fake");
00212 
00213 if (baf->nCountDown)
00214     {
00215     nStartNum = baf->nNumOff - baf->nLineStart;
00216     nEndNum = 1+baf->nNumOff - baf->nCurPos;
00217     }
00218 else
00219     {
00220     nStartNum = 1+baf->nNumOff + baf->nLineStart;
00221     nEndNum = baf->nNumOff + baf->nCurPos;
00222     }
00223 fprintf(baf->out, "%0*d ", startDigits, nStartNum);
00224 for (i=0; i<count; ++i)
00225     fputc(baf->nChars[i], baf->out);
00226 fprintf(baf->out, " %0*d\n", endDigits, nEndNum);
00227 
00228 for (i=0; i<startDigits; ++i)
00229     fputc(arrowChar, baf->out);
00230 fputc(' ', baf->out);
00231 for (i=0; i<count; ++i)
00232     {
00233     char n,h,c =  ' ';
00234 
00235     n = baf->nChars[i];
00236     h = baf->hChars[i];
00237     if (baf->isTrans)
00238         {
00239         if (n != ' ')
00240             {
00241             DNA codon[4];
00242             codon[0] = baf->hChars[i-1];
00243             codon[1] = h;
00244             codon[2] = baf->hChars[i+1];
00245             codon[3] = 0;
00246             tolowers(codon);
00247             c  = lookupCodon(codon);
00248             cfmPushFormat(&cfm);
00249             if (toupper(n) == c)
00250                 cfmOut(&cfm, '|', seqOutColorLookup[0]);
00251             else
00252                 {
00253                 int color;
00254 
00255                 if (c == 0) 
00256                     c = 'X';
00257                 if (ss->matrix[(int)toupper(n)][(int)c] > 0)
00258                     color = 5;
00259                 else
00260                     color = 6;
00261                 cfmOut(&cfm, c, seqOutColorLookup[color]);
00262                 }
00263             cfmPopFormat(&cfm);
00264             }
00265         else
00266             {
00267             fputc(c, baf->out);
00268             }
00269         }
00270     else 
00271         {
00272         if (toupper(n) == toupper(h))
00273              c = '|';
00274         fputc(c, baf->out);
00275         }
00276     }
00277 fputc(' ', baf->out);
00278 for (i=0; i<endDigits; ++i)
00279     fputc(arrowChar, baf->out);
00280 fprintf(baf->out, "\n");
00281 
00282 if (baf->hCountDown)
00283     {
00284     hStartNum = baf->hNumOff - baf->hLineStart;
00285     hEndNum = 1+baf->hNumOff - baf->hCurPos;
00286     }
00287 else
00288     {
00289     hStartNum = 1+baf->hNumOff + baf->hLineStart;
00290     hEndNum = baf->hNumOff + baf->hCurPos;
00291     }
00292 fprintf(baf->out, "%0*d ", startDigits, hStartNum);
00293 for (i=0; i<count; ++i)
00294     fputc(baf->hChars[i], baf->out);
00295 fprintf(baf->out, " %0*d\n\n", endDigits, hEndNum);
00296 }
00297 
00298 void bafOut(struct baf *baf, char n, char h)
00299 /* Write a pair of character to block alignment. */
00300 {
00301 baf->nChars[baf->cix] = n;
00302 baf->hChars[baf->cix] = h;
00303 if (n != '.' && n != '-')
00304     baf->nCurPos += 1;
00305 if (h != '.' && h != '-')
00306     baf->hCurPos += 1;
00307 if (++(baf->cix) >= baf->lineSize)
00308     {
00309     bafWriteLine(baf);
00310     baf->cix = 0;
00311     baf->nLineStart = baf->nCurPos;
00312     baf->hLineStart = baf->hCurPos;
00313     }
00314 }
00315 
00316 void bafFlushLine(struct baf *baf)
00317 /* Write out alignment line if it has any characters in it. */
00318 {
00319 if (baf->cix > 0)
00320     bafWriteLine(baf);
00321 fflush(baf->out);
00322 fprintf(baf->out, "<HR ALIGN=\"CENTER\">");
00323 baf->cix = 0;
00324 }
00325 

Generated on Tue Dec 25 18:39:31 2007 for blat by  doxygen 1.5.2