Next version of JABA
[jabaws.git] / binaries / src / mafft / core / f2cl.c
1 #include "mltaln.h"
2
3 #define DEBUG 0
4
5
6 static char *comment;
7 static char *orderfile;
8 static int format;
9
10 static void fillspace( char *seq, int lenmax )
11 {
12         int len = strlen( seq );
13         seq += len;
14         lenmax -= len;
15         while( lenmax-- ) *seq++ = ' ';
16         *seq = 0;
17 }
18
19 void setmark_clustal( int nlen, int nseq, char **seq, char *mark )
20 {
21         int i, j, k;
22
23         char *strong[] = { 
24                                         "STA",
25                                         "NEQK",
26                                         "NHQK",
27                                         "NDEQ",
28                                         "QHRK",
29                                         "MILV",
30                                         "MILF",
31                                         "HY",
32                                         "FYW",
33                                   };
34         int nstrong = 9;
35         char *weaker[] = { 
36                                         "CSA",
37                                         "ATV",
38                                         "SAG",
39                                         "STNK",
40                                         "STPA",
41                                         "SGND",
42                                         "SNDEQK",
43                                         "NDEQHK",
44                                         "NEQHRK",
45                                         "FVLIM",
46                                         "HFY",
47                                   };
48         int nweaker = 11;
49
50         for( i=0; i<nlen; i++ )
51         {
52                 mark[i] = ' ';
53                 for( j=0; j<nseq; j++ )
54                         if( '-' == seq[j][i] ) break;
55                 if( j != nseq ) 
56                 {
57                         continue;
58                 }
59                 for( j=0; j<nseq; j++ )
60                         if( seq[0][i] != seq[j][i] ) break;
61                 if( j == nseq ) 
62                 {
63                         mark[i] = '*';
64                         continue;
65                 }
66                 for( k=0; k<nstrong; k++ )
67                 {
68                         for( j=0; j<nseq; j++ )
69                         {
70                                 if( !strchr( strong[k], seq[j][i] ) ) break;
71                         }
72                         if( j == nseq ) break;
73                 }
74                 if( k < nstrong )
75                 {
76                         mark[i] = ':';
77                         continue;
78                 }
79                 for( k=0; k<nweaker; k++ )
80                 {
81                         for( j=0; j<nseq; j++ )
82                         {
83                                 if( !strchr( weaker[k], seq[j][i] ) ) break;
84                         }
85                         if( j == nseq ) break;
86                 }
87                 if( k < nweaker )
88                 {
89                         mark[i] = '.';
90                         continue;
91                 }
92         }
93         mark[nlen] = 0;
94 }
95
96 void setmark( int nlen, int nseq, char **seq, char *mark )
97 {
98         int i, j;
99
100         for( i=0; i<nlen; i++ )
101         {
102                 mark[i] = ' ';
103                 for( j=0; j<nseq; j++ )
104                         if( '-' == seq[j][i] ) break;
105                 if( j != nseq ) 
106                 {
107                         continue;
108                 }
109                 for( j=0; j<nseq; j++ )
110                         if( seq[0][i] != seq[j][i] ) break;
111                 if( j == nseq ) 
112                 {
113                         mark[i] = '*';
114                         continue;
115                 }
116                 for( j=0; j<nseq; j++ )
117                         if( amino_grp[(int)seq[0][i]] != amino_grp[(int)seq[j][i]] ) break;
118                 if( j == nseq ) 
119                 {
120                         mark[i] = '.';
121                         continue;
122                 }
123         }
124         mark[nlen] = 0;
125 }
126
127 void arguments( int argc, char *argv[] )
128 {
129     int c;
130         scoremtx = 1;
131         nblosum = 62;
132         dorp = NOTSPECIFIED;
133         kimuraR = NOTSPECIFIED;
134         pamN = NOTSPECIFIED;
135         inputfile = NULL;
136         comment = NULL;
137         orderfile = NULL;
138         format = 'c';
139
140     while( --argc > 0 && (*++argv)[0] == '-' )
141         {
142         while ( (c = *++argv[0]) )
143                 {
144             switch( c )
145             {
146                                 case 'i':
147                                         inputfile = *++argv;
148                                         fprintf( stderr, "inputfile = %s\n", inputfile );
149                                         --argc;
150                                         goto nextoption;
151                                 case 'c':
152                                         comment = *++argv;
153                                         fprintf( stderr, "comment = %s\n", comment );
154                                         --argc;
155                                         goto nextoption;
156                                 case 'r':
157                                         orderfile = *++argv;
158                                         fprintf( stderr, "orderfile = %s\n", orderfile );
159                                         --argc;
160                                         goto nextoption;
161                                 case 'f':
162                                         format = 'f';
163                                         break;
164                                 case 'm':
165                                         format = 'm';
166                                         break;
167                 default:
168                     fprintf( stderr, "illegal option %c\n", c );
169                     argc = 0;
170                     break;
171             }
172                 }
173                 nextoption:
174                         ;
175         }
176     if( argc != 0 ) 
177     {
178         fprintf( stderr, "options: Check source file !\n" );
179         exit( 1 );
180     }
181 }
182
183
184 int main( int argc, char *argv[] )
185 {
186         static int  *nlen;      
187         static char **name, **seq, *mark;
188         static int *order;
189         int i;
190         FILE *infp;
191         FILE *orderfp;
192         char gett[B];
193
194         arguments( argc, argv );
195
196
197         if( inputfile )
198         {
199                 infp = fopen( inputfile, "r" );
200                 if( !infp )
201                 {
202                         fprintf( stderr, "Cannot open %s\n", inputfile );
203                         exit( 1 );
204                 }
205         }
206         else
207                 infp = stdin;
208
209         getnumlen( infp );
210         rewind( infp );
211
212         seq = AllocateCharMtx( njob, nlenmax*2+1 );
213         mark = AllocateCharVec( nlenmax*2+1 );
214         order = AllocateIntVec( njob );
215         name = AllocateCharMtx( njob, B+1 );
216     nlen = AllocateIntVec( njob );
217
218
219         if( orderfile )
220         {
221                 orderfp = fopen( orderfile, "r" );
222                 if( !orderfile )
223                 {
224                         fprintf( stderr, "Cannot open %s\n", orderfile );
225                         exit( 1 );
226                 }
227                 for( i=0; i<njob; i++ )
228                 {
229                         fgets( gett, B-1, orderfp );
230                         order[i] = atoi( gett );
231                 }
232                 fclose( orderfp );
233         }
234         else
235         {
236                 for( i=0; i<njob; i++ ) order[i] = i;
237         }
238
239         readData_pointer( infp, name, nlen, seq );
240         fclose( infp );
241
242         if( format == 'c' ) for( i=0; i<njob; i++ ) fillspace( seq[i], nlenmax );
243         constants( njob, seq );
244
245 //      initSignalSM();
246
247 //      initFiles();
248
249
250
251 //      setmark( nlenmax, njob, seq, mark );
252         setmark_clustal( nlenmax, njob, seq, mark );
253
254         if( format == 'f' )
255                 writeData_reorder_pointer( stdout, njob, name, nlen, seq, order );
256         else if( format == 'c' )
257                 clustalout_pointer( stdout, njob, nlenmax, seq, name, mark, comment, order );
258         else if( format == 'm' )
259                 miyataout_reorder_pointer( stdout, njob, nlenmax, name, nlen, seq, order );
260         else
261                 fprintf( stderr, "Unknown format\n" );
262
263 //      SHOWVERSION;
264         return( 0 );
265 }