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