File: compalign_main.c

package info (click to toggle)
biosquid 1.9g%2Bcvs20050121-15.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,632 kB
  • sloc: ansic: 12,750; sh: 1,412; perl: 243; makefile: 231
file content (217 lines) | stat: -rw-r--r-- 7,652 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*****************************************************************
 * @LICENSE@
 *****************************************************************/

/* main for compalign
 * 
 * Compalign -- a program to compare two sequence alignments
 * SRE, Tue Nov  3 07:38:03 1992
 * RCS $Id: compalign_main.c,v 1.6 2003/04/14 16:00:16 eddy Exp $
 *
 * incorporated into SQUID, Thu Jan 26 16:52:41 1995
 * 
 * Usage: compalign <trusted-alignment> <test-alignment>
 * 
 * Calculate the fractional "identity" between the trusted alignment
 * and the test alignment. The two files must contain exactly the same
 * sequences, in exactly the same order.
 * 
 * The identity of the multiple sequence alignments is defined as
 * the averaged identity over all N(N-1)/2 pairwise alignments. 
 * 
 * The fractional identity of two sets of pairwise alignments
 * is in turn defined as follows (for aligned known sequences k1 and k2,
 * and aligned test sequences t1 and t2):
 * 
 *           matched columns / total columns, 
 *       
 *       where total columns = the total number of columns in
 *        which there is a valid (nongap) symbol in k1 or k2;
 *        
 *       matched columns = the number of columns in which one of the
 *         following is true:
 *         
 *          k1 and k2 both have valid symbols at a given column; t1 and t2
 *             have the same symbols aligned in a column of the t1/t2
 *             alignment;
 *             
 *          k1 has a symbol aligned to a gap in k2; that symbol in t1
 *             is also aligned to a gap;
 *             
 *          k2 has a symbol aligned to a gap in k1; that symbol in t2
 *             is also aligned to a gap.
 * 
 * Because scores for all possible pairs are calculated, the
 * algorithm is of order (N^2)L for N sequences of length L;
 * large sequence sets will take a while.
 * 
 * Sean Eddy, Tue Nov  3 07:46:59 1992
 * 
 */

#include "squidconf.h"

#include <stdio.h>
#include <string.h>
#include "squid.h"
#include "msa.h"

static char banner[] = "compalign - compare two multiple alignments";

static char usage[]  = "\
Usage: compalign [-options] <trusted.ali> <test.ali>\n\
  Available options:\n\
   -c       : only compare under marked #=CS consensus structure\n\
   -h       : print short help and usage info\n\
";

static char experts[] = "\
   --informat <s> : specify that both alignments are in format <s> (MSF, for instance)\n\
   --quiet        : suppress verbose header (used in regression testing)\n\
"; 

struct opt_s OPTIONS[] = {
  { "-c", TRUE, sqdARG_NONE },     
  { "-h", TRUE, sqdARG_NONE },     
  { "--informat", FALSE, sqdARG_STRING },
  { "--quiet",    FALSE, sqdARG_NONE   },
};
#define NOPTIONS (sizeof(OPTIONS) / sizeof(struct opt_s))


int
main(int argc, char **argv)
{
  char  *kfile;                 /* name of file of trusted (known) alignment */
  char  *tfile;                 /* name of file of test alignment            */
  MSAFILE *kfp;			/* open ptr into trusted (known) alignfile   */
  MSAFILE *tfp;			/* open ptr into test alignment file         */
  int    format;		/* expected format of alignment files        */
  MSA   *kmsa;                  /* a trusted (known) alignment               */     
  MSA   *tmsa;                  /* a test alignment                          */     
  char **kraw;			/* dealigned trusted seqs                    */
  char **traw;			/* dealigned test sequences                  */
  int    idx;			/* counter for sequences                     */
  int    apos;			/* position in alignment                     */
  float  score;			/* RESULT: score for the comparison          */

  int    cs_only;		/* TRUE to compare under #=CS annotation only */
  int   *ref = NULL;	        /* init only to silence gcc warning */		
  int    be_quiet;		/* TRUE to suppress verbose header  */

  char *optname;
  char *optarg;
  int   optind;

  /***********************************************
   * Parse command line
   ***********************************************/

  format   = MSAFILE_UNKNOWN;
  cs_only  = FALSE;
  be_quiet = FALSE;

  while (Getopt(argc, argv, OPTIONS, NOPTIONS, usage,
                &optind, &optname, &optarg))
    {
      if      (strcmp(optname, "-c")      == 0)  cs_only = TRUE; 
      else if (strcmp(optname, "--quiet") == 0)  be_quiet  = TRUE; 
      else if (strcmp(optname, "--informat") == 0) {
	format = String2SeqfileFormat(optarg);
	if (format == MSAFILE_UNKNOWN) 
	  Die("unrecognized sequence file format \"%s\"", optarg);
	if (! IsAlignmentFormat(format))
	  Die("%s is an unaligned format, can't read as an alignment", optarg);
      }
      else if (strcmp(optname, "-h") == 0) {
	SqdBanner(stdout, banner);
	puts(usage);
	puts(experts);
        exit(EXIT_SUCCESS);
      }
    }

  if (argc - optind != 2)
    Die("Incorrect number of command line arguments.\n%s\n", usage); 

  kfile = argv[optind++];
  tfile = argv[optind];
		 
  if (! be_quiet) SqdBanner(stdout, banner);

  /***********************************************
   * Read in the alignments
   * Capable of handling full Stockholm: >1 alignment/file
   ***********************************************/

  if ((kfp = MSAFileOpen(kfile, format, NULL)) == NULL)
    Die("Trusted alignment file %s could not be opened for reading", kfile);
  if ((tfp = MSAFileOpen(tfile, format, NULL)) == NULL)
    Die("Test alignment file %s could not be opened for reading", tfile);

  while ((kmsa = MSAFileRead(kfp)) != NULL)
    {
      if ((tmsa = MSAFileRead(tfp)) == NULL)
	Die("Failed to get a test alignment to match with the trusted alignment");

				/* test that they're the same! */
      if (kmsa->nseq != tmsa->nseq)
	Die("files %s and %s do not contain same number of seqs!\n", kfile, tfile);

      for (idx = 0; idx < kmsa->nseq; idx++)
	{
	  s2upper(kmsa->aseq[idx]);
	  s2upper(tmsa->aseq[idx]);
	}
				/* another sanity check */
      for (idx = 0; idx < kmsa->nseq; idx++)
	if (strcmp(kmsa->sqname[idx], tmsa->sqname[idx]) != 0)
	  Die("seqs in %s and %s don't seem to be in the same order\n  (%s != %s)",
	      kfile, tfile, kmsa->sqname[idx], tmsa->sqname[idx]);

				/* and *another* sanity check */
      DealignAseqs(kmsa->aseq, kmsa->nseq, &kraw);
      DealignAseqs(tmsa->aseq, tmsa->nseq, &traw);
      for (idx = 0; idx < kmsa->nseq; idx++)
	if (strcmp(kraw[idx], traw[idx]) != 0)
	  Die("raw seqs in %s and %s are not the same (died at %s, number %d)\n",
	      kfile, tfile, kmsa->sqname[idx], idx);
      Free2DArray((void **) kraw, kmsa->nseq);
      Free2DArray((void **) traw, tmsa->nseq);

      if (cs_only)
	{
	  if (kmsa->ss_cons == NULL)
	    Die("Trusted alignment %s has no consensus structure annotation\n  -- can't use -c!\n",
		kfile);
	  ref = (int *) MallocOrDie (sizeof(int) * kmsa->alen);
	  for (apos = 0; apos < kmsa->alen; apos++)
	    ref[apos] = (isgap(kmsa->ss_cons[apos])) ? FALSE : TRUE;
	}	

      /***********************************************
       * Compare the alignments, print results
       ***********************************************/

      if (cs_only)
	score = CompareRefMultAlignments(ref, kmsa->aseq, tmsa->aseq, kmsa->nseq);
      else
	score = CompareMultAlignments(kmsa->aseq, tmsa->aseq, kmsa->nseq);

      printf("Trusted alignment:   %s\n", kmsa->name != NULL ? kmsa->name : kfile);
      printf("Test alignment:      %s\n", tmsa->name != NULL ? tmsa->name : tfile);
      printf("Total sequences:     %d\n", kmsa->nseq);
      printf("Alignment identity:  %.4f\n", score);
      puts("//");

      if (cs_only) free(ref);
      MSAFree(kmsa);
      MSAFree(tmsa);
    }

  MSAFileClose(kfp);
  MSAFileClose(tfp);
  return 0;
}