Wrapper for Clustal Omega.
[jabaws.git] / binaries / src / clustalo / src / squid / eps.c
1 /*****************************************************************
2  * SQUID - a library of functions for biological sequence analysis
3  * Copyright (C) 1992-2002 Washington University School of Medicine
4  * 
5  *     This source code is freely distributed under the terms of the
6  *     GNU General Public License. See the files COPYRIGHT and LICENSE
7  *     for details.
8  *****************************************************************/
9
10 /* eps.c
11  * SRE, Thu Jun 21 18:02:31 2001 [St. Louis]
12  * 
13  * Some crude support for Encapsulated PostScript (EPS) output,
14  * DSC compliant.
15  * 
16  * CVS $Id: eps.c,v 1.4 2002/02/24 19:39:27 eddy Exp)
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include "squid.h"
24 #include "msa.h"       
25
26 /* Function: EPSWriteSmallMSA()
27  * Date:     SRE, Thu Jun 21 18:15:21 2001 [St. Louis]
28  *
29  * Purpose:  Write an alignment in singleblock, Stockholm/SELEX like
30  *           format to an open file. Very crude.
31  *           Currently fails if the alignment is >50 columns long, because
32  *           it doesn't think it will fit on a single page.
33  *
34  * Args:     fp  - open file for writing
35  *           msa - alignment to write     
36  *
37  * Returns:  (void)
38  */
39 void
40 EPSWriteSmallMSA(FILE *fp, MSA *msa)
41 {
42   int namewidth;                /* namewidth in PostScript units */
43   int fontwidth;                /* width of a character in this font */
44   int hspace;                   /* horizontal space between aligned chars */
45   int vspace;                   /* vertical space between sequences */
46   char *font;                   /* font name, e.g. "Courier" */
47   int fontsize;                 /* font size in pts */
48   int  i,j;                     /* counter over sequences, columns */
49   int  len;                     /* tmp var holding length of something */
50   int  width, height;           /* width and height of bounding box */
51   int  xpos, ypos;              /* x,y position */
52
53   /* Set some font characteristics; done here, so it'll
54    * be easy to change. Magic numbers for Courier 12 determined
55    * by trial and error.
56    */
57   fontwidth = 8;
58   hspace    = 9;
59   vspace    = 15;
60   font      = sre_strdup("Courier", -1);
61   fontsize  = 12;
62
63   /* Find the width of the longest sequence name in characters.
64    */
65   namewidth = 0;
66   for (i = 0; i < msa->nseq; i++)
67     if ((len = (int) strlen(msa->sqname[i])) > namewidth)
68       namewidth = len;
69   namewidth += 1;               /* add a space to separate name & aligned seq */
70   namewidth *= fontwidth;
71
72   /* Determine bounding box
73    */
74   if (msa->alen > 50) Die("No EPS fmt if alignment is >50 columns");
75   width = namewidth + hspace*msa->alen;
76   if (width > 612) Die("Alignment too wide to write in EPS");
77   height = vspace*msa->nseq;
78   if (height > 792) Die("Too many seqs to write in EPS");
79
80   /* Magic EPS header, bare-bones DSC-compliant.
81    */
82   fprintf(fp, "%%!PS-Adobe-3.0 EPSF-3.0\n");
83   fprintf(fp, "%%%%BoundingBox: %d %d %d %d\n", 0, 0, width, height);
84   fprintf(fp, "%%%%Pages: 1\n");
85   fprintf(fp, "%%%%EndComments\n");  
86
87   /* More postscript magic before we start the alignment
88    */
89   fprintf(fp, "/%s findfont\n", font);
90   fprintf(fp, "%d scalefont\n", fontsize);
91   fprintf(fp, "setfont\n");
92   fprintf(fp, "newpath\n");
93
94   /* Write the alignment in PostScript in a single block
95    */
96   for (i = 0; i < msa->nseq; i++)
97     {
98       ypos = (msa->nseq-i-1)*vspace;
99                                 /* name first */
100       fprintf(fp, "%d %d moveto\n", 0, ypos);
101       fprintf(fp, "(%s) show\n", msa->sqname[i]);
102                                 /* now seq */
103       xpos = namewidth;
104       for (j = 0; j < msa->alen; j++)
105         {
106           fprintf(fp, "%d %d moveto\n", xpos, ypos);
107           fprintf(fp, "(%c) show\n", msa->aseq[i][j]);
108           xpos+= hspace;
109         }
110     }
111
112   free(font);
113 }
114
115