Next version of JABA
[jabaws.git] / binaries / src / mafft / core / regtable2seq.c
1 #include "mltaln.h"
2
3 #define DEBUG 0
4
5 char *regfile;
6 char *eregfile;
7
8 void arguments( int argc, char *argv[] )
9 {
10     int c;
11
12     inputfile = NULL;
13     regfile = NULL;
14     eregfile = NULL;
15
16     while( --argc > 0 && (*++argv)[0] == '-' )
17         {
18         while ( (c = *++argv[0]) )
19                 {
20             switch( c )
21             {
22                                 case 'e':
23                                         eregfile = *++argv;
24                                         fprintf( stderr, "eregfile = %s\n", eregfile );
25                                         --argc;
26                                         goto nextoption;
27                                 case 'r':
28                                         regfile = *++argv;
29                                         fprintf( stderr, "regfile = %s\n", regfile );
30                                         --argc;
31                                         goto nextoption;
32                                 case 'i':
33                                         inputfile = *++argv;
34                                         fprintf( stderr, "inputfile = %s\n", inputfile );
35                                         --argc;
36                                         goto nextoption;
37                 default:
38                     fprintf( stderr, "illegal option %c\n", c );
39                     argc = 0;
40                     break;
41             }
42                 }
43                 nextoption:
44                         ;
45         }
46     if( argc != 0 ) 
47     {
48         fprintf( stderr, "options: Check source file !\n" );
49         exit( 1 );
50     }
51 }
52
53 void readereg( FILE *regfp, int **regtable, char **revtable, int *outtable, int *noutpt, int *loutpt )
54 {
55         char gett[1000];
56         int c, j;
57         int mem, begin, end;
58         char reg[5][100];
59         char rev[100], out[100];
60         int startpos, endpos;
61
62         *noutpt = 0;
63         *loutpt = 0;
64         fgets( gett, 999, regfp );
65         sscanf( gett, "%c %s %s %s %s %s", &mem, reg[0], reg[1], reg[2], reg[3], reg[4] );
66         if( mem != 'e' )
67         {
68                 fprintf( stderr, "Format error\n" );
69                 exit( 1 );
70         }
71         for( j=0; j<5; j++ )
72         {
73                 sscanf( reg[j], "%d-%d-%c", regtable[0]+(j*2), regtable[0]+(j*2)+1, revtable[0]+j );
74                 fprintf( stderr, "%d %d-%d\n", 0, regtable[0][j*2], regtable[0][j*2+1] );
75                 startpos = regtable[0][j*2];
76                 endpos   = regtable[0][j*2+1];
77                 if( startpos > endpos )
78                 {
79                         endpos   = regtable[0][j*2];
80                         startpos = regtable[0][j*2+1];
81                 }
82                 if( startpos != -1 && endpos != -1 )
83                         *loutpt += endpos - startpos + 1;
84         }
85
86         while( 1 )
87         {
88                 fgets( gett, 999, regfp );
89                 if( feof( regfp ) ) break;
90                 sscanf( gett, "%d o=%s", &mem, out );
91                 if( mem >= njob )
92                 {
93                         fprintf( stderr, "Out of range\n" );
94                         exit( 1 );
95                 }
96                 outtable[mem] = atoi( out );
97                 if( outtable[mem] ) *noutpt += 1;
98         }
99 }
100
101 void readreg( FILE *regfp, int **regtable, char **revtable, int *outtable )
102 {
103         char gett[1000];
104         int c, j;
105         int mem, begin, end;
106         char reg[5][100];
107         char out[100];
108
109         while( 1 )
110         {
111                 fgets( gett, 999, regfp );
112                 if( feof( regfp ) ) break;
113                 sscanf( gett, "%d %s %s %s %s %s o=%s", &mem, reg[0], reg[1], reg[2], reg[3], reg[4], out );
114                 if( mem >= njob )
115                 {
116                         fprintf( stderr, "Out of range\n" );
117                         exit( 1 );
118                 }
119                 for( j=0; j<5; j++ )
120                 {
121                         sscanf( reg[j], "%d-%d-%c", regtable[mem]+(j*2), regtable[mem]+(j*2)+1, revtable[mem]+j );
122                         fprintf( stderr, "%d %d-%d\n", mem, regtable[mem][j*2], regtable[mem][j*2+1] );
123                 }
124                 outtable[mem] = atoi( out );
125         }
126 }
127
128 int main( int argc, char *argv[] )
129 {
130         FILE *infp;
131         FILE *regfp;
132         int nlenmin;
133         int **regtable;
134         char **revtable;
135         int *outtable;
136         int i, nout, lout;
137         char **outseq;
138         char **name;
139
140         arguments( argc, argv );
141
142         if( inputfile )
143         {
144                 infp = fopen( inputfile, "r" );
145                 if( !infp )
146                 {
147                         fprintf( stderr, "Cannot open %s\n", inputfile );
148                         exit( 1 );
149                 }
150         }
151         else
152                 infp = stdin;
153
154         dorp = NOTSPECIFIED;
155         getnumlen_nogap( infp, &nlenmin );
156
157         if( regfile )
158         {
159                 regfp = fopen( regfile, "r" );
160                 if( !regfp )
161                 {
162                         fprintf( stderr, "Cannot open %s\n", regfile );
163                         exit( 1 );
164                 }
165                 regtable = AllocateIntMtx( njob, 5*2 );
166                 revtable = AllocateCharMtx( njob, 5 );
167                 outtable = AllocateIntVec( njob );
168                 readreg( regfp, regtable, revtable, outtable );
169                 cutData( infp, regtable, revtable, outtable );
170         }
171         else if( eregfile )
172         {
173                 regfp = fopen( eregfile, "r" );
174                 if( !regfp )
175                 {
176                         fprintf( stderr, "Cannot open %s\n", eregfile );
177                         exit( 1 );
178                 }
179                 regtable = AllocateIntMtx( 1, 5*2 );
180                 revtable = AllocateCharMtx( 1, 5 );
181                 outtable = AllocateIntVec( njob );
182                 readereg( regfp, regtable, revtable, outtable, &nout, &lout );
183                 fprintf( stderr, "nout = %d, lout = %d\n", nout, lout );
184
185                 outseq = AllocateCharMtx( nout, lout+1 );
186                 name = AllocateCharMtx( nout, B );
187
188                 cutAlignment( infp, regtable, revtable, outtable, name, outseq );
189                 fprintf( stderr, "gappick! nout = %d\n", nout );
190                 commongappick( nout, outseq );
191                 for( i=0; i<nout; i++ )
192                 {
193                         fprintf( stdout, "%s\n", name[i] );
194                         fprintf( stdout, "%s\n", outseq[i] );
195                 }
196         }
197         else
198         {
199                 catData( infp );
200         }
201
202         fprintf( stderr, "Strategy:\n" );
203         fprintf( stderr, " Not-Aligned\n" );
204
205 //      fprintf( stdout, "%d x %d - %d %c\n", njob, nlenmax, nlenmin, dorp );
206         return( 0 );
207 }