File: BCReader.m

package info (click to toggle)
biococoa 2.2.2-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,184 kB
  • sloc: objc: 21,348; makefile: 17; ansic: 4
file content (844 lines) | stat: -rw-r--r-- 32,730 bytes parent folder | download | duplicates (5)
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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
#import "BCReader.h"
#import "../BCUtils/BCUtilStrings.h"


@implementation BCReader


- (NSDictionary *)readFile:(NSString *)textFile
{
    NSMutableDictionary *theContents;
    NSString *lineBreak;
	
    lineBreak = [self detectLineBreak:textFile];
	
    if ([textFile hasCaseInsensitivePrefix:@"#NEXUS"] || [textFile hasCaseInsensitivePrefix:@"#PAUP"])
    {
        theContents =  (NSMutableDictionary*) [self readNexusFileAndBlocks:textFile];
    }
    else if ([textFile hasCaseInsensitivePrefix:@"CLUSTAL"])
    {
        theContents =  (NSMutableDictionary*) [self readClustalFile:textFile];
    }
    else if ([textFile hasCaseInsensitivePrefix:@"Pileup"])
    {
        theContents =  (NSMutableDictionary*) [self readMSFFile:textFile];
    }
    else if ([textFile hasCaseInsensitivePrefix:@">DL"])
    {
        theContents =  (NSMutableDictionary*) [self readPirFile:textFile];
    }
    else if ([textFile hasPrefix:@">"])
    {
        theContents =  (NSMutableDictionary*) [self readFastaFile:textFile];
    }
    else if ([textFile hasPrefix:@"HEADER"])
    {
        theContents =  (NSMutableDictionary*) [self readPDBFile:textFile];
    }
    else if ([textFile hasPrefix:@"LOCUS"])
    {
        theContents =  (NSMutableDictionary*) [self readNCBIFile:textFile];
    }
    else if ([textFile hasPrefix:@"#"])
    {
        theContents = (NSMutableDictionary*)  [self readGDEFile:textFile];
    }
    else if ([textFile hasPrefix:@"ID"])	// also works for EMBL files, both have the same prefix in the first line
    {
        theContents =  (NSMutableDictionary*) [self readSwissProtFile:textFile];
    }
    else if ([textFile stringBeginsWithTwoNumbers])	
	{
        theContents =  (NSMutableDictionary*) [self readPhylipFile:textFile];
    }
	else
	{
		return nil;		// should we have a error message here?
	}
	
    [theContents setObject:lineBreak forKey:@"lineBreak"];
    
	return theContents;
}


- (NSDictionary *)readPhylipFile:(NSString *)textFile
{
    int i, j;
    NSScanner *itemScanner;
    NSString *stringWithMacLineBreaks;
    NSMutableString *item, *sequence, *tempSequence;
    NSMutableArray *linesArray;
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *phylipDictionary = [NSMutableDictionary dictionary];
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:10];
    j = 0;

    //Convert all line breaks to Mac line breaks (\r)
    stringWithMacLineBreaks = [self convertLineBreaksToMac:textFile];
    //Isolate individual lines based on \r
    linesArray = (NSMutableArray*)[stringWithMacLineBreaks componentsSeparatedByString:@"\r"];

    //Remove empty lines
    for (i = 0; i < [linesArray count]; i++) {
        if ([(NSString *)[linesArray objectAtIndex:i]length] < 1 || [[linesArray objectAtIndex:i] isEqualTo:@""]) {
            [linesArray removeObjectAtIndex:i];
            i--;
        }
    }

    //Read item names and sequences and put them in the matrixDictionary. We can ignore the first line.
    for (i = 1; i < [linesArray count]; i++) {

        if ([[linesArray objectAtIndex:i] hasPrefix:@" "]) {	//If the item name is not present
            sequence = [linesArray objectAtIndex:i];
            tempSequence = (NSMutableString*)[[matrixDictionary objectForKey:[itemArray objectAtIndex:j]] stringByAppendingString:sequence];
            [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:j]];
            j++;

            if (j == [itemArray count]) { j = 0;}
        }
        else {							//If the item name precedes the sequence
            itemScanner = [[NSScanner alloc] initWithString:[linesArray objectAtIndex:i]];
            [itemScanner scanUpToString:@" " intoString:&item];
            sequence = (NSMutableString*)[[linesArray objectAtIndex:i]substringFromIndex:[itemScanner scanLocation]];
            [matrixDictionary setObject:sequence forKey:item];
            [itemArray addObject:item];
            //NSLog(@"Adding item %@", item);
            [itemScanner release];
        }
    }

    //Remove spaces and tabs from the sequences in the matrixDictionary
    for (i = 0; i < [itemArray count]; i++) {
        tempSequence = [NSMutableString stringWithString:[matrixDictionary objectForKey:[itemArray objectAtIndex:i]]];
        [tempSequence replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\t" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:i]];
    }

    //Put the matrixDictionary and the items Array in the phylipDictionary
    [phylipDictionary setObject:matrixDictionary forKey:@"matrix"];
    [phylipDictionary setObject:itemArray forKey:@"items"];
    [phylipDictionary setObject:@"phylip" forKey:@"fileType"];

    return phylipDictionary;
}



- (NSDictionary *)readPirFile:(NSString *)textFile
{
    int i;
    NSScanner *itemScanner;
    NSString *stringWithMacLineBreaks;
    NSMutableString *item, *sequence, *tempSequence;
    NSMutableArray *linesArray;
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *pirDictionary = [NSMutableDictionary dictionary];
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:10];

    //Convert all line breaks to Mac line breaks (\r)
    stringWithMacLineBreaks = [self convertLineBreaksToMac:textFile];

    //Isolate individual lines based on >DL;
    linesArray = (NSMutableArray*)[textFile componentsSeparatedByString:@">DL;"];

    //Trim lines from surrounding whitespaces and remove empty lines
    for (i = [linesArray count]-1; i >= 0 ; i--) {
        [linesArray insertObject:[[linesArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] atIndex:i];
        [linesArray removeObjectAtIndex:i+1];
        if ([[linesArray objectAtIndex:i]isEqualTo:@""]) {
            [linesArray removeObjectAtIndex:i];
        }
    }


    //Read item names and sequences and put them in the matrixDictionary
    for (i = 0; i < [linesArray count]; i++) {
        itemScanner = [[NSScanner alloc] initWithString:[linesArray objectAtIndex:i]];
        [itemScanner scanUpToString:@"\r" intoString:&item];
        [itemScanner scanUpToString:@"\r" intoString:NULL];  //there is an additional line of information containing a textual description before the sequence begins, not sure how to store with sequence
        sequence = (NSMutableString*)[[linesArray objectAtIndex:i]substringFromIndex:[itemScanner scanLocation]];
        [matrixDictionary setObject:sequence forKey:item];
        [itemArray addObject:item];
        [itemScanner release];
    }


    //Remove spaces, carriage returns and * from the sequences in the matrixDictionary
    for (i = 0; i < [itemArray count]; i++) {
        tempSequence = [NSMutableString stringWithString:[matrixDictionary objectForKey:[itemArray objectAtIndex:i]]];
        [tempSequence replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"*" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\r" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:i]];
    }

    //Put the matrixDictionary and the items Array in the fastaDictionary
    [pirDictionary setObject:matrixDictionary forKey:@"matrix"];
    [pirDictionary setObject:itemArray forKey:@"items"];
    [pirDictionary setObject:@"pir" forKey:@"fileType"];

    return pirDictionary;
}




- (NSDictionary *)readFastaFile:(NSString *)textFile
{
    int i;
    NSMutableArray *linesArray;
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *fastaDictionary = [NSMutableDictionary dictionary];
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:10];

#if 0
    NSMutableString *item, *sequence, *tempSequence;
    NSScanner *itemScanner;
    NSString *stringWithMacLineBreaks;

    //Convert all line breaks to Mac line breaks (\r)
    stringWithMacLineBreaks = [self convertLineBreaksToMac:textFile];

    //Isolate individual lines based on >
    linesArray = (NSMutableArray*)[textFile componentsSeparatedByString:@">"];

    //Trim lines from surrounding whitespaces and remove empty lines
    for (i = [linesArray count]; i = 0 ; i--) {
        if ([[linesArray objectAtIndex:i]isEqualTo:@""]) {
            [linesArray removeObjectAtIndex:i];
        }
        [linesArray insertObject:[[linesArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] atIndex:i];
        [linesArray removeObjectAtIndex:i+1];
    }


    //Read item names and sequences and put them in the matrixDictionary
    for (i = 1; i < [linesArray count]; i++) {
        itemScanner = [[NSScanner alloc] initWithString:[linesArray objectAtIndex:i]];
        [itemScanner scanUpToString:@"\r" intoString:&item];
        sequence = (NSMutableString*)[[linesArray objectAtIndex:i]substringFromIndex:[itemScanner scanLocation]];
        [matrixDictionary setObject:sequence forKey:item];
        [itemArray addObject:item];
        [itemScanner release];
    }


    //Remove spaces, carriage returns and tabs from the sequences in the matrixDictionary
    for (i = 0; i < [itemArray count]; i++) {
        tempSequence = [NSMutableString stringWithString:[matrixDictionary objectForKey:[itemArray objectAtIndex:i]]];
        [tempSequence replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\t" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\r" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:i]];
    }

#else

	NSString	*line, *item, *sequence;
	
	linesArray = [textFile splitLines];
	
    for (i = 0; i < [linesArray count]; i++)
	{
		line = [linesArray objectAtIndex: i];
		
		if ([line hasPrefix: @">"] )
		{
			item = [line substringFromIndex:1];
			line = [linesArray objectAtIndex: ++i];
			
			sequence = @"";
			
			while (![line hasPrefix:@">"] )
			{			
				sequence = [sequence stringByAppendingString:[line stringByRemovingWhitespace]];
				
				i++;
				if ( i < [linesArray count])
					line = [linesArray objectAtIndex: i];
				else
					break;
			}
			
			[matrixDictionary setObject:sequence forKey:item];
			[itemArray addObject: item];
			
			i--;
		}
	}

#endif

    //Put the matrixDictionary and the items Array in the fastaDictionary
    [fastaDictionary setObject:matrixDictionary forKey:@"matrix"];
    [fastaDictionary setObject:itemArray forKey:@"items"];
    [fastaDictionary setObject:@"fasta" forKey:@"fileType"];

    return fastaDictionary;
}


- (NSDictionary *)readGDEFile:(NSString *)textFile
{
    int i;
    NSScanner *itemScanner;
    NSString *stringWithMacLineBreaks;
    NSMutableString *item, *sequence, *tempSequence;
    NSMutableArray *linesArray;
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *gdeDictionary = [NSMutableDictionary dictionary];
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:10];

    //Convert all line breaks to Mac line breaks (\r)
    stringWithMacLineBreaks = [self convertLineBreaksToMac:textFile];

    //Isolate individual lines based on #
    linesArray = (NSMutableArray*)[stringWithMacLineBreaks componentsSeparatedByString:@"#"];

    //Trim lines from surrounding whitespaces and remove empty lines
    for (i = [linesArray count]; i = 0 ; i--) {
        if ([[linesArray objectAtIndex:i]isEqualTo:@""]) {
            [linesArray removeObjectAtIndex:i];
        }
        [linesArray insertObject:[[linesArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] atIndex:i];
        [linesArray removeObjectAtIndex:i+1];
    }


    //Read item names and sequences and put them in the matrixDictionary
    for (i = 1; i < [linesArray count]; i++) {
        itemScanner = [[NSScanner alloc] initWithString:[linesArray objectAtIndex:i]];
        [itemScanner scanUpToString:@"\r" intoString:&item];
        sequence = (NSMutableString*)[[linesArray objectAtIndex:i]substringFromIndex:[itemScanner scanLocation]];
        [matrixDictionary setObject:sequence forKey:item];
        [itemArray addObject:item];
        [itemScanner release];
    }


    //Remove spaces, carriage returns and tabs from the sequences in the matrixDictionary
    for (i = 0; i < [itemArray count]; i++) {
        tempSequence = [NSMutableString stringWithString:[matrixDictionary objectForKey:[itemArray objectAtIndex:i]]];
        [tempSequence replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\t" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\r" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:i]];
    }

    //Put the matrixDictionary and the items Array in the fastaDictionary
    [gdeDictionary setObject:matrixDictionary forKey:@"matrix"];
    [gdeDictionary setObject:itemArray forKey:@"items"];
    [gdeDictionary setObject:@"gde" forKey:@"fileType"];

    return gdeDictionary;
}



- (NSDictionary *)readClustalFile:(NSString *)textFile
{
    int i;
    NSScanner *itemScanner;
    NSString *stringWithMacLineBreaks;
    NSMutableString *item, *sequence, *tempSequence;
    NSMutableArray *linesArray;
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *clustalDictionary = [NSMutableDictionary dictionary];
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:10];

    //Convert all line breaks to Mac line breaks (\r)
    stringWithMacLineBreaks = [self convertLineBreaksToMac:textFile];
    //Isolate individual lines based on \r
    linesArray = (NSMutableArray *)[stringWithMacLineBreaks componentsSeparatedByString:@"\r"];


    //Trim lines from surrounding whitespaces and remove empty lines
    for (i = 0; i < [linesArray count]; i++) {
        if ([[linesArray objectAtIndex:i]isEqualTo:@""] || [[linesArray objectAtIndex:i]hasPrefix:@"  "]) {
            [linesArray removeObjectAtIndex:i];
            i--;
        }
        [linesArray insertObject:[[linesArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] atIndex:i];
        [linesArray removeObjectAtIndex:i+1];
    }


    //Read item names and sequences and put them in the matrixDictionary
    for (i = 1; i < [linesArray count]; i++) {
        itemScanner = [[NSScanner alloc] initWithString:[linesArray objectAtIndex:i]];
        [itemScanner scanUpToString:@" " intoString:&item];
        sequence = (NSMutableString*)[[linesArray objectAtIndex:i]substringFromIndex:[itemScanner scanLocation]];

        if ([matrixDictionary objectForKey:item]) {	//If the item already exists
            tempSequence = (NSMutableString*)[[matrixDictionary objectForKey:item] stringByAppendingString:sequence];
            [matrixDictionary setObject:tempSequence forKey:item];
        }
        else {						//If the item does not yet exist
            [matrixDictionary setObject:sequence forKey:item];
            [itemArray addObject:item];
        }
        [itemScanner release];
    }


    //Remove spaces and tabs from the sequences in the matrixDictionary
    //itemArray = [matrixDictionary allKeys];
    for (i = 0; i < [itemArray count]; i++) {
        tempSequence = [NSMutableString stringWithString:[matrixDictionary objectForKey:[itemArray objectAtIndex:i]]];
        [tempSequence replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\t" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:i]];
    }

    //Put the matrixDictionary and the items Array in the clustalDictionary
    [clustalDictionary setObject:matrixDictionary forKey:@"matrix"];
    [clustalDictionary setObject:itemArray forKey:@"items"];
    [clustalDictionary setObject:@"clustal" forKey:@"fileType"];

    return clustalDictionary;
}


- (NSDictionary *)readMSFFile:(NSString *)textFile
{
    int i;
    NSScanner *itemScanner;
    NSString *stringWithMacLineBreaks, *trimmedString;
    NSMutableString *item, *sequence, *tempSequence;
    NSMutableArray *linesArray;
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *MSFDictionary = [NSMutableDictionary dictionary];
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:10];

    //Convert all line breaks to Mac line breaks (\r)
    stringWithMacLineBreaks = [self convertLineBreaksToMac:textFile];
    //Trim the first part of the string
    trimmedString = [[stringWithMacLineBreaks componentsSeparatedByString:@"//"]objectAtIndex:1];

    //Isolate individual lines based on \r
    linesArray = (NSMutableArray *)[trimmedString componentsSeparatedByString:@"\r"];


    //Trim lines from surrounding whitespaces and remove empty lines
    for (i = [linesArray count]-1; i >= 0 ; i--) {
        [linesArray insertObject:[[linesArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] atIndex:i];
        [linesArray removeObjectAtIndex:i+1];
        if ([[linesArray objectAtIndex:i]isEqualTo:@""] || [[linesArray objectAtIndex:i]hasPrefix:@"  "]) {
            [linesArray removeObjectAtIndex:i];
        }
    }


    //Read item names and sequences and put them in the matrixDictionary
    for (i = 0; i < [linesArray count]; i++) {
        itemScanner = [[NSScanner alloc] initWithString:[linesArray objectAtIndex:i]];
        [itemScanner scanUpToString:@" " intoString:&item];
        sequence = (NSMutableString*)[[linesArray objectAtIndex:i]substringFromIndex:[itemScanner scanLocation]];

        if ([matrixDictionary objectForKey:item]) {	//If the item already exists
            tempSequence = (NSMutableString*)[[matrixDictionary objectForKey:item] stringByAppendingString:sequence];
            [matrixDictionary setObject:tempSequence forKey:item];

        }
        else {						//If the item does not yet exist
            [matrixDictionary setObject:sequence forKey:item];
            [itemArray addObject:item];
        }
        [itemScanner release];
    }


    //Remove spaces and tabs from the sequences in the matrixDictionary
    //itemArray = [matrixDictionary allKeys];
    for (i = 0; i < [itemArray count]; i++) {
        tempSequence = [NSMutableString stringWithString:[matrixDictionary objectForKey:[itemArray objectAtIndex:i]]];
        [tempSequence replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\t" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:i]];
    }

    //Put the matrixDictionary and the items Array in the clustalDictionary
    [MSFDictionary setObject:matrixDictionary forKey:@"matrix"];
    [MSFDictionary setObject:itemArray forKey:@"items"];
    [MSFDictionary setObject:@"msf" forKey:@"fileType"];

    return MSFDictionary;
}


- (NSDictionary *)readNexusFile:(NSString *)textFile
{
    int i;
    NSScanner *matrixScanner, *itemScanner, *commentScanner, *treeScanner;
    NSString *comments = @"";
    NSString *treeString = @"";
    NSString *stringWithMacLineBreaks;
    NSMutableString *item, *sequence, *tempSequence, *matrix, *bracketsString;
    NSMutableArray *linesArray, *bracketsArray;
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *nexusDictionary = [NSMutableDictionary dictionary];
    NSMutableArray *itemArray = [NSMutableArray arrayWithCapacity:10];
    NSMutableArray *treesArray = [NSMutableArray arrayWithCapacity:10];

    //Extract the Nexus file's main comments
    commentScanner = [[NSScanner alloc] initWithString:textFile];
    [commentScanner scanUpToString:@"[!" intoString:nil];
    [commentScanner scanString:@"[!" intoString:nil];
    [commentScanner scanUpToString:@"]" intoString:&comments];
    [commentScanner release];
    //Put the comments in the nexusDictionary
    if (![comments isEqualTo:@""]) {
        [nexusDictionary setObject:comments forKey:@"comments"];
    }


    //Check whether a treeblock exists and extract it
    if ([textFile rangeOfString:@"begin trees;" options:NSCaseInsensitiveSearch||NSBackwardsSearch].length != 0) {
        treeScanner = [[NSScanner alloc] initWithString:textFile];
        [treeScanner scanUpToString:@"begin trees;" intoString:nil];
        [treeScanner scanString:@"begin trees;" intoString:nil];
        [treeScanner scanUpToString:@"end;" intoString:&treeString];
        [treeScanner release];
        treesArray = [NSMutableArray arrayWithArray:[treeString componentsSeparatedByString:@";"]];
        //Put the trees in the treesArray
        if ([treesArray count] > 0) {
            [treesArray removeObjectAtIndex:0];
            [treesArray removeObjectAtIndex:[treesArray count]-1];
            //NSLog(@"%@", treesArray);
            [nexusDictionary setObject:treesArray forKey:@"trees"];
        }
    }


    //Remove other comments (in sequences) between the []
    bracketsString = [NSMutableString stringWithString:textFile];
    [bracketsString replaceOccurrencesOfString:@"[" withString:@"!!!*" options:NULL range:NSMakeRange(0, [bracketsString length])];
    [bracketsString replaceOccurrencesOfString:@"]" withString:@"!!!" options:NULL range:NSMakeRange(0, [bracketsString length])];
    bracketsArray = (NSMutableArray *)[bracketsString componentsSeparatedByString:@"!!!"];
    for (i = 0; i < [bracketsArray count]; i++) {
        if ([[bracketsArray objectAtIndex:i]hasPrefix:@"*"]){
            [bracketsArray removeObjectAtIndex:i];
        }
    }
    matrix = (NSMutableString *)[bracketsArray componentsJoinedByString:@""];


    //Isolate the matrix
    matrixScanner = [[NSScanner alloc] initWithString:matrix];
    [matrixScanner scanUpToString:@"matrix" intoString:nil];
    [matrixScanner scanUpToString:@";" intoString:&matrix];
    [matrixScanner release];


    //Convert all line breaks to Mac line breaks (\r)
    stringWithMacLineBreaks = [self convertLineBreaksToMac:matrix];
    //Isolate individual lines based on \r
    linesArray = (NSMutableArray *)[stringWithMacLineBreaks componentsSeparatedByString:@"\r"];


    //Trim lines from surrounding whitespaces and remove empty lines
    for (i = 0; i < [linesArray count]; i++) {
        [linesArray insertObject:[[linesArray objectAtIndex:i]stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] atIndex:i];
        [linesArray removeObjectAtIndex:i+1];
        if ([[linesArray objectAtIndex:i]isEqualTo:@""]) {
            [linesArray removeObjectAtIndex:i];
            i--;
        }
    }


    //Read item names and sequences and put them in the matrixDictionary
    for (i = 1; i < [linesArray count]; i++) {
        itemScanner = [[NSScanner alloc] initWithString:[linesArray objectAtIndex:i]];
        [itemScanner scanUpToString:@" " intoString:&item];
        sequence = (NSMutableString *)[[linesArray objectAtIndex:i]substringFromIndex:[itemScanner scanLocation]];

        if ([matrixDictionary objectForKey:item]) {	//If the item already exists
            tempSequence = [NSMutableString stringWithString:[[matrixDictionary objectForKey:item] stringByAppendingString:sequence]];
            [matrixDictionary setObject:tempSequence forKey:item];

        }
        else {						//If the item does not yet exist
            [matrixDictionary setObject:sequence forKey:item];
            [itemArray addObject:item]; //Put the item name in the itemArray
        }
        [itemScanner release];
    }


    //Remove spaces and tabs from the sequences in the matrixDictionary
    for (i = 0; i < [itemArray count]; i++) {
        tempSequence = [NSMutableString stringWithString:[matrixDictionary objectForKey:[itemArray objectAtIndex:i]]];
        [tempSequence replaceOccurrencesOfString:@" " withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [tempSequence replaceOccurrencesOfString:@"\t" withString:@"" options:NULL range:NSMakeRange(0, [tempSequence length])];
        [matrixDictionary setObject:tempSequence forKey:[itemArray objectAtIndex:i]];
    }

    //Put the matrixDictionary and the items Array in the nexusDictionary
    [nexusDictionary setObject:matrixDictionary forKey:@"matrix"];
    [nexusDictionary setObject:itemArray forKey:@"items"];
    [nexusDictionary setObject:@"nexus" forKey:@"fileType"];

    return nexusDictionary;
}


- (NSDictionary *)readNexusFileAndBlocks:(NSString *)textFile
{
    int i;
    NSMutableDictionary *dict;
    NSScanner *scanner;
    NSArray *blocksArray;
    NSString *blockName, *blockContents;
    NSMutableDictionary *blocksDictionary = [NSMutableDictionary dictionary];
    dict = (NSMutableDictionary*)[self readNexusFile:textFile];

    //Separate every block
    blocksArray = [textFile componentsSeparatedByString:@"BEGIN "];
    //Use the block name as the key and the contents as the value
    for (i = 1; i < [blocksArray count]; i++) {
        scanner = [[NSScanner alloc] initWithString:[blocksArray objectAtIndex:i]];
        [scanner scanUpToString:@";" intoString:&blockName];
        [scanner scanUpToString:@"end;" intoString:&blockContents];
        [scanner release];
        [blocksDictionary setObject:blockContents forKey:blockName];
    }
    [dict setObject:blocksDictionary forKey:@"blocks"];
    return dict;
}


- (NSDictionary *)readSwissProtFile:(NSString *)textFile
{
    NSString			*line, *sequence, *ID, *description, *organism;
    NSMutableArray		*linesArray;
    NSMutableDictionary *swissProtDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableArray		*itemArray = [NSMutableArray arrayWithCapacity:10];
	int					i;
	
	linesArray = [textFile splitLines];
	    
 // to be sure, make all strings empty

    ID = @"";
	organism = @"";
	sequence = @"";
	description = @"";
    
	for (i = 0; i < [linesArray count]; i++)
	{
		line = [linesArray objectAtIndex:i];
		
		if ( [line hasPrefix:@"ID"] )
		{
			ID = [ID stringByAppendingString: [line substringFromIndex:2]];
		}
		
		else if ( [line hasPrefix:@"OS"] )
		{
			organism = [organism stringByAppendingString: [line substringFromIndex:2]];
		}

		else if ( [line hasPrefix:@"DE"] )
		{
			description = [description stringByAppendingString: [line substringFromIndex:2]];
		}

		else if ([line hasPrefix:@"SQ"] )
        {
			line = [linesArray objectAtIndex: ++i];
			
			while (![line hasPrefix:@"//"] )
			{			
				sequence = [sequence stringByAppendingString:[line stringByRemovingWhitespace]];
				line = [linesArray objectAtIndex: ++i];
			}
        }
	}

 // Put the key-value pairs in the swissProtDictionary
	
	[itemArray addObject:ID];
	[matrixDictionary setObject:sequence forKey:ID];
	
    [swissProtDictionary setObject:matrixDictionary forKey:@"matrix"];
    [swissProtDictionary setObject:itemArray forKey:@"items"];
    [swissProtDictionary setObject:description forKey:@"comments"];
    [swissProtDictionary setObject:organism forKey:@"organism"];
    [swissProtDictionary setObject:@"swissprot" forKey:@"fileType"];

	return swissProtDictionary;
}

- (NSDictionary *)readPDBFile:(NSString *)textFile
{
    NSString			*line, *sequence, *header, *title, *source;
    NSMutableArray		*linesArray;
    NSMutableDictionary *pdbDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableArray		*itemArray = [NSMutableArray arrayWithCapacity:10];
	int					i;
	
	linesArray = [textFile splitLines];
	    
  // to be sure, make all strings empty

    header = @"";
	source = @"";
	sequence = @"";
	title = @"";
    
	for (i = 0; i < [linesArray count]; i++)
	{
		line = [linesArray objectAtIndex:i];
		
		if ( [line hasPrefix:@"HEADER"] )
		{
			header = [header stringByAppendingString: [line substringFromIndex:10]];
		}
		
		else if ( [line hasPrefix:@"SOURCE"] )
		{
			source = [source stringByAppendingString: [line substringFromIndex:10]];
		}

		else if ( [line hasPrefix:@"TITLE"] )
		{
			title = [title stringByAppendingString: [line substringFromIndex:10]];
		}

		else if ([line hasPrefix:@"SEQRES"] )
        {
			sequence = [sequence stringByAppendingString:[line substringWithRange:NSMakeRange(19, 52)]];
        }
	}

 // Put the key-value pairs in the ncbiDictionary
	
	[itemArray addObject:header];
	[matrixDictionary setObject:sequence forKey:header];

    [pdbDictionary setObject:matrixDictionary forKey:@"matrix"];
    [pdbDictionary setObject:itemArray forKey:@"items"];
    [pdbDictionary setObject:title forKey:@"comments"];
    [pdbDictionary setObject:source forKey:@"organism"];
    [pdbDictionary setObject:@"pdb" forKey:@"fileType"];

	return pdbDictionary;
}


- (NSDictionary *)readNCBIFile:(NSString *)textFile
{
    NSString			*line, *sequence, *locus, *definition, *organism;
    NSMutableArray		*linesArray;
    NSMutableDictionary *ncbiDictionary = [NSMutableDictionary dictionary];
    NSMutableDictionary *matrixDictionary = [NSMutableDictionary dictionary];
    NSMutableArray		*itemArray = [NSMutableArray arrayWithCapacity:10];
	int					i;
	
	linesArray = [textFile splitLines];

 // to be sure, make all strings empty
 
    locus = @"";
	organism = @"";
	sequence = @"";
	definition = @"";
    
	for (i = 0; i < [linesArray count]; i++)
	{
		line = [linesArray objectAtIndex:i];
		
		if ( [line hasPrefix:@"LOCUS"] )
		{
			locus = [locus stringByAppendingString: [line substringFromIndex:11]];
		}
		
		else if ( [line hasPrefix:@"ORGANISM"] )
		{
			organism = [organism stringByAppendingString: [line substringFromIndex:11]];
		}

		else if ( [line hasPrefix:@"DEFINITION"] )
		{
			definition = [definition stringByAppendingString: [line substringFromIndex:11]];
		}

		else if ([line hasPrefix:@"ORIGIN"] )
        {
			line = [linesArray objectAtIndex: ++i];
			
			while (![line hasPrefix:@"//"] )
			{			
				sequence = [sequence stringByAppendingString:[[line substringFromIndex:10] stringByRemovingWhitespace]];
				line = [linesArray objectAtIndex: ++i];
			}
        }
	}

 // Put the key-value pairs in the ncbiDictionary
	
	[itemArray addObject:locus];
	[matrixDictionary setObject:sequence forKey:locus];

    [ncbiDictionary setObject:matrixDictionary forKey:@"matrix"];
    [ncbiDictionary setObject:itemArray forKey:@"items"];
    [ncbiDictionary setObject:definition forKey:@"comments"];
    [ncbiDictionary setObject:organism forKey:@"organism"];
    [ncbiDictionary setObject:@"ncbi" forKey:@"fileType"];

	return ncbiDictionary;
}


- (NSMutableString *)convertLineBreaksToMac:(NSString *)textFile
{
    // \r\n (Windows) becomes \r\r - \n (Unix) becomes \r
    NSMutableString *theString = [NSMutableString stringWithString:textFile];
    [theString replaceOccurrencesOfString:@"\r\n" withString:@"\r" options:NULL range:NSMakeRange(0, [theString length])];
    [theString replaceOccurrencesOfString:@"\n" withString:@"\r" options:NULL range:NSMakeRange(0, [theString length])];
    return theString;
}


- (NSString *)detectLineBreak:(NSString *)srcNStr
{
    // search for dos
    if ([srcNStr rangeOfString: @"\r\n"].location != NSNotFound)
        return @"\r\n";
    // search for mac
    else if ([srcNStr rangeOfString: @"\n"].location != NSNotFound)
        return @"\n";
    // search for unix
    else if ([srcNStr rangeOfString: @"\n"].location != NSNotFound)
        return @"\n";
    // otherwise unknown
    else
        return @"\r";
}


// Memory management

- (id)init
{
    if (self = [super init]) {
    }
    return self;
}


- (void)dealloc
{
    [super dealloc];
}



@end