File: MultiSequence.cc

package info (click to toggle)
proda 1.0-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 584 kB
  • sloc: cpp: 8,549; xml: 163; makefile: 109; sh: 12
file content (758 lines) | stat: -rw-r--r-- 22,386 bytes parent folder | download | duplicates (3)
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//////////////////////////////////////////////////////////////////////
// MultiSequence.cc
//////////////////////////////////////////////////////////////////////

#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Assert.h"
#include "Utilities.h"
#include "MultiSequence.h"
#include "Types.h"

const int NUM_COLUMNS = 60;
const char *WHITE_SPACE_PLUS_GAPS = " \n\r\t\v\f-.";
const char *WHITE_SPACE = " \n\r\t\v\f";
const char *END_OF_LINE = "\n\r\f";

typedef Sequence *SequencePtr;
typedef char *charPtr;


//////////////////////////////////////////////////////////////////////
// Default constructor
//////////////////////////////////////////////////////////////////////

MultiSequence::MultiSequence() : sequences (NULL), numSequences (0) {}
  
//////////////////////////////////////////////////////////////////////
// Copy constructor
//////////////////////////////////////////////////////////////////////

MultiSequence::MultiSequence (const MultiSequence &rhs) : 
  sequences (NULL), numSequences (rhs.numSequences){
  
  if (rhs.sequences){
    sequences = new SequencePtr[numSequences];      
    ASSERT (sequences, "Out of memory.");
    
    for (int i = 0; i < numSequences; i++){
      sequences[i] = new Sequence (*rhs.sequences[i]);
      ASSERT (sequences[i], "Out of memory.");
    }
  }
}

//////////////////////////////////////////////////////////////////////
// Assignment operator
//////////////////////////////////////////////////////////////////////

const MultiSequence& MultiSequence::operator= (const MultiSequence &rhs){
  
  if (this != &rhs){
    numSequences = rhs.numSequences;
    sequences = NULL;
    
    if (rhs.sequences){
      sequences = new SequencePtr[numSequences];      
      ASSERT (sequences, "Out of memory.");
      
      for (int i = 0; i < numSequences; i++){
	sequences[i] = new Sequence (*rhs.sequences[i]);
	ASSERT (sequences[i], "Out of memory.");
      }
    }
  }
  
  return *this;
}

//////////////////////////////////////////////////////////////////////
// Destructor
//////////////////////////////////////////////////////////////////////

MultiSequence::~MultiSequence (){
  if (sequences){
    for (int i = 0; i < numSequences; i++)
      delete sequences[i];
    delete[] sequences;
  }
}

//////////////////////////////////////////////////////////////////////
// Return number of sequences
//////////////////////////////////////////////////////////////////////

const int MultiSequence::GetNumSequences() const {
  return numSequences;
}

//////////////////////////////////////////////////////////////////////
// Return length of first sequence
//////////////////////////////////////////////////////////////////////

const int MultiSequence::GetLength() const {
  ASSERT (numSequences > 0, "MultiSequence must have at least one sequence to retrieve length.");
  return sequences[0]->GetLength();
}

//////////////////////////////////////////////////////////////////////
// Rerieve sequence
//////////////////////////////////////////////////////////////////////
  
const Sequence &MultiSequence::GetSequence (int index) const {
  ASSERT (0 <= index && index < numSequences, "Invalid sequence index.");
  return *sequences[index];
}

//////////////////////////////////////////////////////////////////////
// Add new sequence
//////////////////////////////////////////////////////////////////////

void MultiSequence::AddSequence (Sequence *seq){
  Sequence **temp = new SequencePtr[numSequences+1];
  ASSERT (temp, "Out of memory.");
  
  if (sequences) memcpy (temp, sequences, sizeof(SequencePtr) * numSequences);
  temp[numSequences++] = seq;
  
  delete[] sequences;
  sequences = temp;
}

//////////////////////////////////////////////////////////////////////
// Auto-detect file format 
//
// 0 = MFA
// 1 = PILEUP
//////////////////////////////////////////////////////////////////////

const int MultiSequence::AutoDetectFileFormat (const char *filename) const {
  int fileType = -1;
  char *header;
  
  FILE *file = fopen (filename, "r");
  ASSERT (file, "Unable to open input file!");
  
  int length = GetData (file, header, END_OF_LINE, "");
  if (length){
    if (header[0] == '>'){
      fileType = 0;
    } else {
      if (!strncmp (header, "PileUp", 6))
	fileType = 1;
    }
  }
  
  fclose (file);
  
  return fileType;
}

//////////////////////////////////////////////////////////////////////
// Load sequences from MFA file
//////////////////////////////////////////////////////////////////////

void MultiSequence::LoadMFA (const char *filename, bool compressGaps){
  FILE *file = fopen (filename, "r");
  bool firstSequence = true;
  
  ASSERT (file, "Unable to open input file!");
  
  while (true){
    int length;
    
    // read MFA header
    
    char *name;
    length = GetData (file, name, END_OF_LINE, "");
    if (length == 0) break;
    
    if (firstSequence){
      ASSERT (name[0] == '>', "MFA sequence header should begin with '>'.");
      char *temp = new char[length];
      ASSERT (temp, "Out of memory.");
      
      memcpy (temp, name+1, sizeof(char) * length);
      delete[] name;
      name = temp;
      firstSequence = false;
    }
    
    // read MFA character data
    
    char *data;      
    length = GetData (file, data, ">", (compressGaps ? WHITE_SPACE_PLUS_GAPS : WHITE_SPACE));
    for (int i = 0; i < length; i++){
      if (data[i] == '.') data[i] = '-';
      ASSERT (('A' <= data[i] && data[i] <= 'Z') ||
	      ('a' <= data[i] && data[i] <= 'z') ||
	      (data[i] == '*' || data[i] == '-'),
	      "Unknown character encountered in MFA sequence data.");
    }
    
    // insert '@' at the beginning of the sequence
    
    char *temp = new char[length+2];
    ASSERT (temp, "Out of memory.");
    
    memcpy (temp+1, data, sizeof(char) * (length+1));
    temp[0] = '@';
    delete[] data;
    data = temp;	
    
    // add sequence
    
    Sequence *seq = new Sequence (data, name, length, numSequences);
    ASSERT (seq, "Out of memory.");
    
    AddSequence (seq);
  }
  
  ASSERT (!firstSequence, "No sequences read.");
  
  fclose (file);  
}

//////////////////////////////////////////////////////////////////////
// Load sequences from PILEUP file
//////////////////////////////////////////////////////////////////////

void MultiSequence::LoadPILEUP (const char *filename, bool compressGaps){
  FILE *file = fopen (filename, "r");
  ASSERT (file, "Unable to open input file!");
  int numRead = 0;
  
  // process header
  
  while (true){
    char *text;
    int length = GetData (file, text, END_OF_LINE, "");
    if (length == 0 && feof (file)) break;
    if (strstr (text, "//")) break;
    
    // parse sequence description 
    
    char *ptr = strstr (text, "Name:");
    if (ptr){
      int res;
      char *temp = new char[length];
      ASSERT (temp, "Out of memory.");
      
      res = sscanf (ptr + 5, "%s", temp);
      ASSERT (res != EOF && res > 0, "Failed to read sequence name in PILEUP file.");
      char *name = new char[strlen(temp)+1];
      ASSERT (name, "out of memory.");
      memcpy (name, temp, sizeof(char) * (strlen(temp)+1));
      for (int i = 0; i < numSequences; i++)
	ASSERT (strcmp (sequences[i]->GetName(), name), "Duplicate sequence name found.");
      
      
      ptr = strstr (text, "Len:");
      ASSERT (ptr, "Length field expected for sequence in PILEUP file.");
      sscanf (ptr + 4, "%d", &length);
      ASSERT (res != EOF && res > 0, "Failed to read sequence length in PILEUP file.");
      ASSERT (length > 0, "Length of sequence must be positive in PILEUP file.");
      
      delete[] temp;
      
      Sequence *seq = new Sequence (NULL, name, length, numSequences);
      ASSERT (seq, "Out of memory.");
      
      numRead++;
      AddSequence (seq);
    }
  }
  
  // prepare buffers for reading
  
  int *charsRead = new int[numRead];
  ASSERT (charsRead, "Out of memory.");
  char **data = new charPtr[numRead];
  ASSERT (data, "Out of memory.");
  for (int i = 0; i < numRead; i++){
    int index = i + numSequences - numRead;
    charsRead[i] = 0;
    data[i] = new char[sequences[index]->GetLength()+2];
    ASSERT (data[i], "Out of memory.");
    strcpy (data[i], "@");
  }
  
  // read sequences 
  
  while (true){
    char *text;
    int length = GetData (file, text, WHITE_SPACE, "");
    if (length == 0 && feof (file)) break;
    
    // search for sequence name
    
    int foundSequence = -1;
    for (int i = numSequences - numRead; i < numSequences; i++){
      if (!strcmp (text, sequences[i]->GetName())){
	foundSequence = i; 
	break;
      }
    }      
    delete[] text;
    if (foundSequence == -1) continue;
    
    // read sequence data
    
    length = GetData (file, text, END_OF_LINE, (compressGaps ? WHITE_SPACE_PLUS_GAPS : WHITE_SPACE));
    {for (int i = 0; i < length; i++){
      if (text[i] == '.') text[i] = '-';
      ASSERT (('A' <= text[i] && text[i] <= 'Z') ||
	      ('a' <= text[i] && text[i] <= 'z') ||
	      (text[i] == '*' || text[i] == '-'),
	      "Unknown character encountered in PILEUP sequence data.");
    }}
    
    // add to sequence data
    
    charsRead[foundSequence - numSequences + numRead] += length;
    ASSERT (charsRead[foundSequence - numSequences + numRead] <= sequences[foundSequence]->GetLength(),
	    "Sequence longer than reported length in PILEUP file.");
    strcat (data[foundSequence - numSequences + numRead], text);
  }
  
  {for (int i = numSequences - numRead; i < numSequences; i++){
    sequences[i]->SetData (data[i - numSequences + numRead]);
    if (!compressGaps){
      ASSERT (sequences[i]->GetLength() == charsRead[i],
	      "Actual sequence length inconsistent with reported length in PILEUP file");
    }
  }}
  
  ASSERT (numRead > 0, "No sequences read.");
  
  delete[] charsRead;
  delete[] data;
  
  fclose (file);  
}

//////////////////////////////////////////////////////////////////////
// Load data from file.
//////////////////////////////////////////////////////////////////////

void MultiSequence::LoadData (const char *filename, bool compressGaps){
  int fileFormat = AutoDetectFileFormat (filename);
  
  switch (fileFormat){
  case 0: LoadMFA (filename, compressGaps); break;
  case 1: LoadPILEUP (filename, compressGaps); break;
  default: ASSERT (false, "Unrecognized input file type.");
  }
}

//////////////////////////////////////////////////////////////////////
// Load sequences from file
//////////////////////////////////////////////////////////////////////

void MultiSequence::LoadSequences (const char *filename){
  LoadData (filename, true);
}

//////////////////////////////////////////////////////////////////////
// Load alignment from file
//////////////////////////////////////////////////////////////////////
  
void MultiSequence::LoadAlignment (const char *filename){
  LoadData (filename, false);
}

//////////////////////////////////////////////////////////////////////
// Print sequence in MFA format
//////////////////////////////////////////////////////////////////////
  
void MultiSequence::WriteMFA (FILE *file) const {
  
  for (int i = 0; i < numSequences; i++){
    fprintf (file, ">%s\n", sequences[i]->GetName());
    int length = sequences[i]->GetLength();
    const char *data = sequences[i]->GetData();
    
    for (int j = 1; j <= length; j++){
      fprintf (file, "%c", data[j]);
      if (j % NUM_COLUMNS == 0) fprintf (file, "\n");
    }
    if (length % NUM_COLUMNS != 0) fprintf (file, "\n");
  }
}

//////////////////////////////////////////////////////////////////////
// Compute CLUSTALW annotation character for alignment column
//////////////////////////////////////////////////////////////////////

const char MultiSequence::ComputeAnnotation (const char *data, const int size) const {
  static const char *groups[47] = {
    
    // Identities
    "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", 
    "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "*",
    
    // Strong groups
    "STA", "NEQK", "NHQK", "NDEQ", "QHRK", "MILV", "MILF", "HY", "FYW",
    
    // Weaker groups
    "CSA", "ATV", "SAG", "STNK", "STPA", "SGND", "SNDEQK", "NDEQHK", "NEQHRK", "FVLIM", "HFY"
  };
  
  for (int m = 0; m < 47; m++){
    bool isConserved = true;
    for (int j = 0; isConserved && j < size; j++)
      isConserved = strchr (groups[m], toupper(data[j]));
    if (isConserved){
      if (m < 27) return '*';
      if (m < 36) return ':';
      return '.';
    }
  }
  
  return ' ';
}

//////////////////////////////////////////////////////////////////////
// Print sequence in CLUSTALW format
//////////////////////////////////////////////////////////////////////
  
void MultiSequence::WriteCLUSTALW (FILE *file) const {
  
//  fprintf (file, "PROBCONS version %s multiple sequence alignment\n\n", "2.0");
  
  if (numSequences == 0) return;
  
  // Get sequence length and length of longest sequence name
  
  int length = sequences[0]->GetLength();
  int nameLength = strlen(sequences[0]->GetName());
  for (int i = 1; i < numSequences; i++){
    ASSERT (sequences[i]->GetLength() == length, 
	    "ERROR: Sequences of unequal length in CLUSTALW output.");
    nameLength = max (nameLength, (int) strlen(sequences[i]->GetName()));
  }
  
  // Print out sequences
  
  char *buffer = new char[numSequences];
  ASSERT (buffer, "Out of memory.");
  
  {for (int i = 1; i <= length; i += NUM_COLUMNS){
    for (int j = 0; j < numSequences; j++){
      fprintf (file, "%*s    ", nameLength, sequences[j]->GetName());
      const char *data = sequences[j]->GetData();
      for (int k = i; k <= min (i + NUM_COLUMNS - 1, length); k++)
	fprintf (file, "%c", data[k]);
      fprintf (file, "\n");
    }
    
    // Compute annotation line
    
    fprintf (file, "%*s    ", nameLength, "");
    for (int k = i; k <= min (i + NUM_COLUMNS - 1, length); k++){
      for (int j = 0; j < numSequences; j++)
	buffer[j] = sequences[j]->GetData()[k];
      char ch = ComputeAnnotation (buffer, numSequences);
      fprintf (file, "%c", ch);
    }
    fprintf (file, "\n");
    
    if (i + NUM_COLUMNS <= length)
      fprintf (file, "\n");
  }}
}

//////////////////////////////////////////////////////////////////////
// Sort sequences by ID number
//////////////////////////////////////////////////////////////////////

void MultiSequence::Sort(){
  for (int i = 0; i < numSequences; i++){
    for (int j = i+1; j < numSequences; j++){
      if (sequences[i]->GetID() > sequences[j]->GetID()){
	Sequence *temp = sequences[i];
	sequences[i] = sequences[j];
	sequences[j] = temp;
      }
    }
  }
}


//////////////////////////////////////////////////////////////////////
// Retrieve pointer to a sequence
//////////////////////////////////////////////////////////////////////
  

Sequence * MultiSequence::GetSequencePtr(int index)
{
  ASSERT (0 <= index && index < numSequences, "Invalid sequence index.");
  return sequences[index];
}


void MultiSequence::AddAlignPosition(Fragment * frag)
{
	sequences[frag->id]->AddAlignPosition(frag->begin,frag->end);
}

////////////////////////////////////////////////////////////////////////
// Return block of all aligned sequences
////////////////////////////////////////////////////////////////////////
typedef const char * pchar;
typedef int * pint;
void MultiSequence::FindBlock(Block &block, int &start, int &end, int minlength)
{
	int i,j;
	if (numSequences <=0) {start = end = 0;return;}

	start = end = -1;
	int length = sequences[0]->GetLength();

	const char **data = new pchar[numSequences];
	for ( i = 0; i < numSequences; i++)
		data[i] = sequences[i]->GetData();
	
	for( i = 1; i <= length; i++){
		for ( j = 0; j < numSequences && data[j][i] != '-';j++);
		if(j == numSequences){
			start = i;
			break;
		}
	}
	for( i = length; i > 0; i--){
		for ( j = 0; j < numSequences && data[j][i] != '-';j++);
		if(j == numSequences){
			end = i;
			break;
		}
	}

	int numFrag = numSequences ;//gradually decreases number of fragments

		int **map = new pint[numSequences];
		for (i = 0; i < numSequences; i++)
			map[i] = new int[length+1];
		for (i = 0; i < numSequences; i++){
			int count = 0;
			for (j = 1; j <= length; j++){
				if(data[i][j] != '-') count++;
				map[i][j] = count;
			}
		}
		int ml = end - start +1;
		for(i=0; i < numSequences && ml >= minlength; i++)
			if (ml > map[i][end] - map[i][start]+1) ml = map[i][end] - map[i][start]+1;
		
		if (ml < minlength){
			int *counts = new int[length+1];
			for (i = 1 ; i <= length; i++) counts[i] = 0;
			for (j = 0; j < numSequences; j++){
				for (i = 1 ; i <= length; i++)			
					if(data[j][i] != '-') counts[i]++;

			}
			for (numFrag = numSequences - 1; numFrag >=2 ; numFrag--){
				for (i = 1 ; i <= length - minlength && counts[i] < numFrag; i++);
				start = i;
				if(start > length - minlength) continue;
				for (i = length; i > start && counts[i] < numFrag; i--);
				end = i;
			
				int good = 0;
				for(i=0; i < numSequences; i++)
					if (map[i][end] - map[i][start]+1 >= minlength) good++;
				if (good >= numFrag){
					int match,bindex,eindex,flag = 0;;	
					for (bindex = start; bindex < end-minlength && flag ==0;bindex++){
						for(eindex = end; eindex > bindex+minlength;eindex--){
							for(match=0,j=0;j<numSequences;j++){
								if(data[j][bindex] != '-' && data[j][eindex] != '-' &&
									map[j][eindex] - map[j][bindex]+1 >= minlength) 
									match++;
							}
							if(match == numFrag && eindex - bindex >= minlength){
								start = bindex; end = eindex; flag = 1;
								break;
							}
						}
					}
					if(flag) break;
				}
			}

		}
		
	

	
	if(numFrag >= 2 && end-start+1 >= minlength && data[0][start] != '-' && data[0][end] != '-' &&
			map[0][end] - map[0][start]+1 >= minlength){
		int b,e;
	Block tmp;
	int ok = 1;
	tmp.seed = block.seed;
	for (i = 0,j=0; i < numSequences; i++){
		if(data[i][start] == '-' || data[i][end] == '-' ||
			map[i][end] - map[i][start]+1 < minlength) continue;
		for(b = block[i].begin, j = 0; j < start; j++)
			if (data[i][j] != '-') b++;
		for (e = b,j = start; j < end; j++)
			if (data[i][j] != '-') e++;
		Fragment fr(b-1,e-1,0,block[i].id);//Not the original ID
		tmp.AddFragment(fr);
	}
	if(numFrag < numSequences){ //remove some sequences
		Sequence **temp = new SequencePtr[numFrag];
		ASSERT (temp, "Out of memory.");
		for (i = 0,j=0; i < numSequences; i++){
			if(data[i][start] == '-' || data[i][end] == '-'  ||
				map[i][end] - map[i][start]+1 < minlength)
				delete sequences[i];
			else
				temp[j++] = sequences[i];
		}
		delete[] sequences;
		sequences = temp;
		numSequences = j;
	}
	if(ok)
		block = tmp;
	else
		start = end = 0;
	}
	else{
		start = end = 0;
	}
	
	if(start == 0) block.part = 1;
	
	for (i = 0; i < numSequences; i++)
		delete map[i];
	delete map;
	delete data;
	
}


//////////////////////////////////////////////////////////////////////////
//  Output from start to end
//////////////////////////////////////////////////////////////////////////
void MultiSequence::WriteCLUSTALW(FILE *file, int start, int end)
{
  if (numSequences == 0) return;
  
  // Get sequence length and length of longest sequence name
  
  int length = sequences[0]->GetLength();
  int nameLength = strlen(sequences[0]->GetName());
  for (int i = 1; i < numSequences; i++){
    ASSERT (sequences[i]->GetLength() == length, 
	    "ERROR: Sequences of unequal length in CLUSTALW output.");
    nameLength = max (nameLength, (int) strlen(sequences[i]->GetName()));
  }
  
  // Print out sequences
  
  char *buffer = new char[numSequences];
  ASSERT (buffer, "Out of memory.");
  
  {for (int i = start; i <= end; i += NUM_COLUMNS){
    for (int j = 0; j < numSequences; j++){
      fprintf (file, "%*s    ", nameLength, sequences[j]->GetName());
      const char *data = sequences[j]->GetData();
      for (int k = i; k <= min (i + NUM_COLUMNS - 1, end); k++)
	fprintf (file, "%c", data[k]);
      fprintf (file, "\n");
    }
    
    // Compute annotation line
    
    fprintf (file, "%*s    ", nameLength, "");
    for (int k = i; k <= min (i + NUM_COLUMNS - 1, end); k++){
      for (int j = 0; j < numSequences; j++)
	buffer[j] = sequences[j]->GetData()[k];
      char ch = ComputeAnnotation (buffer, numSequences);
      fprintf (file, "%c", ch);
    }
    fprintf (file, "\n");
    
    if (i + NUM_COLUMNS <= length)
      fprintf (file, "\n");
  }}

}

void MultiSequence::ClearAlignPosition()
{
	for (int i = 0; i < numSequences; i++)
		sequences[i]->ClearAlignPosition();
}



void MultiSequence::WriteFASTA(FILE *file, Block *block, MultiSequence *result, int start, int end)
{
	int i,j;
	if(block->size() > numSequences){
		fprintf(stderr,"\nBlock contains repeats\n\n");
		exit(1);
	}
	int *map = new int[numSequences];
	for (i = 0 ; i < numSequences; i++)
		map[i] = -1;
	for (i =0; i < block->size(); i++)
		map[(*block)[i].id] = i;

	int maxBegin = 0;
	int maxEnd = 0;
	for (i = 0; i < block->size(); i++){
		if(maxBegin < (*block)[i].begin)
			maxBegin = (*block)[i].begin;
	}

	for(i=0;i<numSequences;i++){
		int len;
		if(map[i] != -1)
			len = maxBegin + end-start+sequences[i]->GetLength()-(*block)[map[i]].end;
		else
			len = sequences[i]->GetLength()+end-start+1;
		if(maxEnd < len)
			maxEnd = len;
	}

	for (i = 0; i < numSequences; i++){
		fprintf(file,">%s\n",sequences[i]->GetName());
		int cur = map[i];
		const char *data = sequences[i]->GetData();
		int count = 0;
		if(cur != -1){
			for (j = 1; j < (*block)[cur].begin; j++,count++)
				fprintf(file,"%c",data[j]+'a'-'A');
			for (j = (*block)[cur].begin; j < maxBegin; j++,count++)
				fprintf(file,".");
			const char *alignData = result->GetSequence(cur).GetData();
			for (j = start; j <= end; j++,count++)
				fprintf(file,"%c",alignData[j]);
			for (j = (*block)[cur].end+1; j <= sequences[i]->GetLength();j++,count++)
				fprintf(file,"%c",data[j]+'a'-'A');
		}
		else{
			int beginGap = min (maxBegin, sequences[i]->GetLength()+1);
			for (j = 1; j <beginGap;j++,count++)
				fprintf(file,"%c",data[j]+'a'-'A');
			if (beginGap < sequences[i]->GetLength()+1){
				for (j = start; j <= end; j++,count++)
					fprintf(file,"-");
				for (j = beginGap; j <= sequences[i]->GetLength(); j++,count++)
					fprintf (file,"%c",data[j]+'a'-'A');
			}
		}
		for (; count < maxEnd; count++)
			fprintf(file,".");
		fprintf(file,"\n");
	}
	fprintf(file,"#\n");
	delete map;
}