File: input.c

package info (click to toggle)
dialign 2.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 912 kB
  • sloc: ansic: 4,885; xml: 401; makefile: 34
file content (396 lines) | stat: -rw-r--r-- 8,460 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

                 /*******************\
                 *                   *
                 *     DIALIGN 2     *
                 *                   *
                 *     input.c       *
                 *                   *
                 \*******************/



#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include "define.h"
#include "dialign.h"
#include "alig_graph_closure.h"

extern int max_dia , self_comparison ; 
extern int sim_score[21][21]; 
extern int max_sim_score ;
extern float av_sim_score_pep ;
extern float av_sim_score_nuc ;
extern char par_dir[ NAME_LEN ] ;
extern double **tp400_prot, **tp400_dna, **tp400_trans ; 
extern int *seqlen ;
extern int seqnum ; 


int word_count( char *str ) {

  short word = 0 ; 
  int i ; 
  int word_len = 0 ; 

  for( i = 0 ; i < strlen( str ) - 1  ; i++ ) { 
    if( ( str[i] != ' ' ) && ( str[i] != '\t' ) ) {  
      if( ! word ) { 
        word_len++ ; 
        word = 1 ; 
      }
    }
    else 
      word = 0 ; 

  }
  return( word_len ) ; 
} 


void exclude_frg_read( char *file_name , int ***exclude_list) {

  char exclude_file_name[ NAME_LEN ] ;
  FILE *fp;
  char line[ 10000 ] ;
  int i, len, beg1, beg2, seq1, seq2; 
  int sv = 0, hv, word_num  ;

  strcpy( exclude_file_name , file_name );
  strcat( exclude_file_name , ".xfr" );

  if( (fp = fopen( exclude_file_name, "r")) == NULL)
    erreur("\n\n cannot find file with excluded fragments \n\n\n");

  

  while( fgets( line , MLINE , fp ) != NULL ) {
    if( strlen( line ) > 4 ) {   
      sscanf(line,"%d %d %d %d %d", &seq1, &seq2, &beg1, &beg2 , &len  );

      if( seq1 > seqnum ){
        printf ("\n\n exclueded fragment makes no sense!\n\n");
        printf (" wrong sequence no %d in fragment\n\n", seq1 );
        printf ("%d %d %d %d %d \n\n ", seq1, seq2, beg1, beg2 , len  );
        exit(1) ;
      }
 
      if( seq2 > seqnum ){
        printf ("\n\n    excluded fragment makes no sense!\n\n");
        printf ("    wrong sequence no %d in fragment\n\n", seq2 );
        printf ("    %d %d %d %d %d \n\n", seq1, seq2, beg1, beg2 , len );
        exit(1) ;
      }

/*
      seq1 = seq1 - 1; 
      seq2 = seq2 - 1;
*/

      if( beg1 + len > seqlen[ seq1 - 1 ] + 1 ){
        printf ("\n\n    excluded fragment makes no sense!\n");
        printf ("    fragment");
        printf ("     \" %d %d %d %d %d \"\n", seq1, seq2, beg1, beg2 , len );
        printf ("    doesn't fit into sequence %d:\n", seq1 );
        printf ("    sequence %d has length =  %d\n\n", seq1 , seqlen[ seq1 - 1 ] );
        exit(1) ;
      }


 
      for( i = 0 ; i < len ; i++ ) {  
        exclude_list[ seq1 - 1 ][ seq2 - 1 ][ beg1 + i ] = beg2 + i ;
      }
    }
  }
  
} /* excluded_frg_read  */ 







void ws_remove( char *str ) {
  int pv = 0 ;

  while( ( str[ pv ] == ' ' ) || ( str[ pv ] == '\t' ) ) {
    pv++ ;
  }

  strcpy( str , str + pv );
}

void n_clean( char *str ) {
  int pv = 0 ;
  char *char_ptr ;

  while( ( str[ pv ] == ' ' ) || 
         ( str[ pv ] == '\t' ) || 
         ( str[ pv ] == '>' ) ) {
    pv++ ;
  }
  strcpy( str , str + pv ) ;

  if( ( char_ptr = strchr( str ,' ') ) != NULL)
    *char_ptr = '\0';
  if( ( char_ptr = strchr( str ,'\t') ) != NULL)
    *char_ptr = '\0';
  if( ( char_ptr = strchr( str ,'\n') ) != NULL)
    *char_ptr = '\0';




}


void fasta_test( char *seq_file ) {

  int test = 1;
  int pv = 0;
  FILE *fp;

  char line[ MAX_INPUT_LINE ] ;

  if( (fp = fopen( seq_file , "r")) == NULL) { 
    printf("\n\n Cannot find sequence file %s \n\n\n", seq_file );
    exit(1) ;
  }

  while( test ) {
    fgets( line , MAX_INPUT_LINE , fp );

    ws_remove( line );

    if( line[0] != '\n' )
    if( line[0] == '>' ) 
      test = 0;
    else
      erreur("\n\n  file not in FASTA format  \n\n");
  }
  
  fclose( fp );
}


int seq_read( char *seq_file , char *sq[MAX_SEQNUM] , char **sqn , char **fsqn ) {
 
  char line[ MAX_INPUT_LINE ] ;

  char *nom_seq;
  char *char_ptr;
  int  sn, i, j, k , pv , crc ;
  FILE *fp;
  int max_char[ MAX_SEQNUM ] ;

  if( (fp = fopen( seq_file , "r")) == NULL) { 
    printf("\n\n Cannot find sequence file %s \n\n\n", seq_file );
    exit(1) ;
  }
  fasta_test( seq_file );

  sn = -1 ;
  while( fgets( line , MAX_INPUT_LINE , fp ) != NULL ) {


  ws_remove( line );

    if( line[0] == '>' ) {
      sn++;

   
      n_clean( line );


      fsqn[ sn ] = ( char * ) calloc( strlen( line ) + 3 , sizeof ( char ) ); 

      strcpy( fsqn[ sn ] , line ) ; 


      max_char[ sn ] = 0;
      sqn[ sn ]  = ( char * ) calloc( SEQ_NAME_LEN + 3 , sizeof ( char ) );

      for( crc = 0 ; crc < SEQ_NAME_LEN ; crc++ )  
        if( crc < strlen(line) ) 
          sqn[ sn ][ crc ] =  line[ crc ] ;
        else 
          sqn[ sn ][ crc ] =  ' ';

      sqn[ sn ][ SEQ_NAME_LEN ] = '\0';  

       

    }

 
    else  
      max_char[ sn ] = max_char[ sn ] + strlen( line ) - 1 ; 
  }

  for( i = 0 ; i <= sn ; i++ ) {
    sq[ i ]  = ( char * ) calloc( max_char[ i ] + 1 , sizeof ( char ) );
  }

  if( (seqlen = (int *) calloc( ( sn + 1 ) , sizeof(int) )) == NULL)
    erreur("\n\n problems with memory allocation for `seqlen' \n\n");


  fclose( fp );


  /******************************************/

  if( self_comparison == 1 ) {
    if( sn != 0 ) {
      printf("\n\n With option \"self comparison\" input file must contain one single sequence \n\n" ); 
      exit(1) ;
    }

    sq[ 1 ]  = ( char * ) calloc( max_char[ 0 ] + 1 , sizeof ( char ) );

    sqn[ 1 ]  = ( char * ) calloc( strlen( line ) + 3 , sizeof ( char ) );
    strcpy( sqn[ 1 ] , sqn[ 0 ] ) ;
  }

  /******************************************/


  if( (fp = fopen( seq_file , "r")) == NULL) 
    erreur("\n\n no seq file \n\n");
 
  sn = -1 ;
  while( fgets( line , MAX_INPUT_LINE , fp ) != NULL ) {
    ws_remove( line ); 
    if( line[0] == '>' ) {
      sn++;
      j = 0;
    }
    else
      for( k = 0 ; k < strlen( line )  ; k++ )
      if( 
          ( line[ k ] >= 65 ) && ( line[ k ] <= 90 ) || 
          ( line[ k ] >= 97 ) && ( line[ k ] <= 122 ) 
        ) 
      sq[ sn ][ j++ ] = toupper( line[ k ] ) ; 
  }
  
  sn++; 

  for( i = 0 ; i <  sn ; i++ ) {
    seqlen[ i ] = strlen ( sq[ i ] ) ;
  }

  if( self_comparison ) {
    seqlen[ 1 ] = seqlen[ 0 ] ;  
    for( i = 0 ; i <=  seqlen[ 0 ] ; i++ ) 
      sq[ 1 ][ i ] = sq[ 0 ][ i ] ;    
    sn++; 
  }

  fclose( fp );

  return( sn );
}



void matrix_read( FILE *fp_mat ) {
  int i, j;
  char line[MLINE], dummy[MLINE];
 
  fgets( line , MLINE , fp_mat );
  fgets( line , MLINE , fp_mat );


  for( i = 1 ; i <= 20 ; i++ ) {
    for(j=i;j<=20;j++) {
      fscanf( fp_mat , "%d" , &sim_score[i][j]);
      sim_score[j][i] = sim_score[i][j];  
      if ( sim_score[i][j] > max_sim_score )
        max_sim_score = sim_score[i][j] ;
    }

    fscanf( fp_mat, "%s\n", dummy);
  }

  fclose(fp_mat);

  for( i = 0 ; i <= 20 ; i++ ) {
    sim_score[i][0] = 0 ;
    sim_score[0][i] = 0 ;
  }

/*
 sim_score[0][0] = max_sim_score ;
*/

}
 


void tp400_read( int w_type , double **pr_ptr ) {  
 
  /* reads probabilities from file */
   /* w_type = 0 (protein), 1 (dna w/o transl.), 2 (dna with transl.) */  

  char line[MLINE], file_name[MLINE], suffix[10], str[MLINE] ;
  int sum, len, max_sim, i ;
  double pr;

  FILE *fp;
 
  if ( w_type == 0 ) {
    strcpy( suffix , "prot" );
  }

  if ( w_type == 1 ) {
    strcpy( suffix , "dna" );
  }  

  if ( w_type == 2 ) {
    strcpy( suffix , "trans" );
  }
 
  strcpy( file_name , par_dir ); 
  strcat( file_name , "/tp400_" );
  strcat( file_name , suffix );


 if ( ( fp = fopen( file_name , "r" ) ) == NULL ) { 
   printf("\n\n Cannot find the file %s \n\n", file_name );    
   printf(" Make sure the environment variable DIALIGN2_DIR points\n");
   printf(" to a directory containing the files \n\n");
   printf("   BLOSUM \n   tp400_dna\n   tp400_prot \n   tp400_trans \n\n" );
   printf(" These files should be contained in the DIALIGN package \n\n\n" ) ;
   exit(1) ;
 }


  if ( fgets( line , MLINE , fp ) == NULL ) 
    erreur("\n\n problem with file %s  \n\n", file_name );
  else
    if( w_type % 2 )  
      av_sim_score_nuc = atof( line );
    else
      av_sim_score_pep = atof( line );
     

  while( fgets( line , MLINE , fp ) != NULL )
   {
      sscanf(line,"%d %d %s", &len, &sum, str  );

      pr = atof(str);
      pr_ptr[len][sum] = pr;

    }


}    /*  tp400_read  */