File: converter.cpp

package info (click to toggle)
libncl 2.1.18%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 8,344 kB
  • ctags: 3,526
  • sloc: xml: 43,716; cpp: 30,400; sh: 10,658; python: 231; makefile: 190
file content (909 lines) | stat: -rw-r--r-- 33,669 bytes parent folder | download
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
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
//	Copyright (C) 2007-2008 Mark T. Holder
//
//	This file is part of NCL (Nexus Class Library).
//
//	NCL is free software; you can redistribute it and/or modify
//	it under the terms of the GNU General Public License as published by
//	the Free Software Foundation; either version 2 of the License, or
//	(at your option) any later version.
//
//	NCL is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU General Public License
//	along with NCL; if not, write to the Free Software Foundation, Inc.,
//	59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//

/*******************************************************************************
 * This file contains code for 4 executables:
 *		NEXUSnormalizer, NEXUSvalidator, NEXUSinspector, and NEX_us2ml
 *	with conditional compilation used to determine the behavior.
 *
 *		* NEXUSnormalizer - writes a NEXUS version of the file with consistent
 *			ordering of blocks and commands. Ideally 2 equivalent files will
 *			produce the same normalized output. This version of tthe program is
 *			less ambitious. The goal is to be able to run (for any valid NEXUS
 *			in.nex file):
 *				$ NEXUSnormalizer in.nex > outOrig.nex
 *				$ NEXUSnormalizer outOrig.nex > outSecond.nex
 *				$ diff outOrig.nex outSecond.nex
 *			and find no differences.
 *		* NEXUSvalidator - reports errors and warnings to stderr. Invalid files
 *			cause exit with a non-zero return code
 *		* NEXUSinspector - writes a brief report of every block parsed
 *		* NEXUS_us2ml - writes a nexml version of the input (partially
 *			implemented, note that the code to write nexml is in us2ml.cpp).
 * See the processFilepath() function for an example of how to deal with NCL
 *	to read a file using the new MultiFormatReader class. When the file
 *	is correctly read, the processContent() function is called.
 *
 * All other code has to do with reading command line arguments and other
 * 	user-interface concerns.
 */

#include <cassert>
#include "ncl/ncl.h"
#include "ncl/nxsblock.h"
#include "ncl/nxspublicblocks.h"
#include "ncl/nxsmultiformat.h"
using namespace std;

void writeAsNexml(PublicNexusReader & nexusReader, ostream & os);
void writeAsNexus(PublicNexusReader & nexusReader, ostream & os);
extern bool gQuietMode;



void exportData(PublicNexusReader & nexusReader, MultiFormatReader::DataFormatType f, long interleaveLen, std::string prefix, std::ostream * fp);
void exportCharacters(PublicNexusReader & nexusReader, MultiFormatReader::DataFormatType f, long interleaveLen, std::string prefix, std::ostream * fp);
void exportTrees(PublicNexusReader & nexusReader, MultiFormatReader::DataFormatType f, std::string prefix, std::ostream * fp);
const char * getFileExtension( MultiFormatReader::DataFormatType f);
std::vector<NxsNameToNameTrans> nameTranslationDict(const std::vector<std::string> & origTaxa, MultiFormatReader::DataFormatType f);
std::string getLegalTaxonName(const std::string & origName, const std::set<std::string> & used,  MultiFormatReader::DataFormatType f);
std::string getLegalPhylipTaxonName(const std::string & origName, const std::set<std::string> & used);
std::string getLegalRelaxedPhylipTaxonName(const std::string & origName, const std::set<std::string> & used);
bool formLegalPhylipName(const std::string & origName, const NxsString & numericExtension, const std::set<std::string> & used, std::string & toReturn);
bool formLegalRelaxedPhylipName(const std::string & origName, const NxsString & numericExtension, const std::set<std::string> & used, std::string & toReturn);

std::string purgeIllegalCharactersFromPhylipName(const std::string &origName);
std::string purgeIllegalCharactersFromRelaxedPhylipName(const std::string &origName);


void reverseTranslateNames(PublicNexusReader &/*reader*/,
                           std::string /*filepath*/) {
}


std::string assignUniqueName(const std::string & prefix, unsigned maxLen, const std::set<std::string> & safeLabelSet)
    {
    NxsString s;
    s << prefix;
    std::set<std::string>::const_iterator sIt = safeLabelSet.find(s);
    if (sIt == safeLabelSet.end())
        return s;
    unsigned i = 1;
    for (;;)
        {
        NxsString numAsStr;
        numAsStr << i++;
        s.clear(); 
        unsigned concatLen = numAsStr.length() + prefix.length();
        if (concatLen > maxLen) 
            s << prefix.substr(0, 2*maxLen - 1 - concatLen);
        else
            s << prefix;
        s << numAsStr;
        sIt = safeLabelSet.find(s);
        if (sIt == safeLabelSet.end())
            return s;
        }
    }
bool constructSafeLabels(const NxsTaxaBlock & tb, std::vector<std::string> * vs, std::set<std::string> & safeLabelSet)
    {
    if (vs == 0L)
        return false;
    const unsigned numTaxa = tb.GetNTaxTotal();
    bool changed = false;
    for (unsigned i = 0; i < numTaxa; ++i) 
        {
        NxsString oldName = tb.GetTaxonLabel(i);
        unsigned oldOffset = 0;
        unsigned newOffset = 0;
        std::string prefix;
        for (; newOffset < 10 && oldOffset < oldName.length(); ++oldOffset)
            {
            char c = oldName[oldOffset];
            if ((isgraph(c)) && (strchr("!@#$%^&*.?|()[]{}/\\,;:=*\'\"`-+<>", c) == NULL))
                {
                if (newOffset == 0 && isdigit(c))
                    {
                    prefix.append(1, 't');
                    newOffset += 1;
                    }
                prefix.append(1, toupper(c));
                newOffset += 1;
                }
            }
        std::string newLabel = assignUniqueName(prefix, 10, safeLabelSet);
        safeLabelSet.insert(newLabel);
        vs->push_back(newLabel);
        if (newLabel != oldName)
            changed = true;
        
        }
    return changed;
    }

bool substituteSafeTaxaLabels(PublicNexusReader &nexusReader, std::string filepath, bool evenTrivial)
    {
    std::ostream * outStrPtr = 0L;
    std::ofstream outStream;
	const unsigned nTaxaBlocks = nexusReader.GetNumTaxaBlocks();
	bool someChanged = false;
	std::set<std::string> safeNameSet;
	try
	    {
	    for (unsigned t = 0; t < nTaxaBlocks; ++t)
            {
            NxsTaxaBlock * tb = nexusReader.GetTaxaBlock(t);
            std::vector<std::string> safeLabelsVec;
    
            bool changed = constructSafeLabels(*tb, &safeLabelsVec, safeNameSet);
            someChanged = someChanged | changed;  
            if (changed || evenTrivial)
                {
                if (outStrPtr == 0L) 
                    {
                    outStream.open(filepath.c_str());
                    if (!outStream.good())
                        {
                        NxsString eMessage;
                        eMessage << "Could not open the file \"" << filepath << "\"";
                        throw NxsException(eMessage);
                        }
                    outStrPtr = &outStream;
                    outStream << "#NEXUS\n";
                    }
                tb->WriteAsNexus(*outStrPtr);
                std::vector<NxsString> oldNameVec;
                const unsigned numTaxa = tb->GetNTaxTotal();
                for (unsigned i = 0; i < numTaxa; ++i) 
                    {
                    NxsString oldName = tb->GetTaxonLabel(i);
                    oldNameVec.push_back(oldName);
                    NxsString newName;
                    newName << safeLabelsVec[i];
                    tb->ChangeTaxonLabel(i, newName);
                    }
                NxsString title;
                title << tb->GetTitle();
                NxsString nt;
                nt << "Renamed " << title;
                tb->SetTitle(nt, false);
                tb->WriteAsNexus(*outStrPtr);
                
                // We can't just create a TaxaAssociationBlock instance here,
                //  and call WriteAsNexus because it first and second taxa block pointers
                //  would refer to the same instance...
                NxsString taTitle;
                taTitle << "Renaming " << title;
                *outStrPtr << "BEGIN TaxaAssociation;\n  Title " << NxsString::GetEscaped(taTitle) << " ;\n";
                *outStrPtr << "  Taxa " << NxsString::GetEscaped(title) << " , " << NxsString::GetEscaped(nt) << " ;\n";
                *outStrPtr << "  Associates ";
                for (unsigned i = 0; i < numTaxa; ++i)
                    {
                    if (i > 0) 
                        *outStrPtr << " ,\n    ";
                    *outStrPtr <<  NxsString::GetEscaped(oldNameVec[i]) << " / " << NxsString::GetEscaped(safeLabelsVec[i]);
                    }
                *outStrPtr << " ;\nEND;\n" ;
                }
            }
        }
    catch (...) 
        {
        if (outStrPtr != 0L)
            outStream.close();
        throw;
        }
        
    if (outStrPtr != 0L)
        outStream.close();
    return someChanged;
    }
        

const char * getFileExtension( MultiFormatReader::DataFormatType f) {
	if (f == MultiFormatReader::NEXUS_FORMAT)
		return ".nex";
	else if (f == MultiFormatReader::FASTA_DNA_FORMAT
			|| f == MultiFormatReader::FASTA_AA_FORMAT
			|| f == MultiFormatReader::FASTA_RNA_FORMAT)
		return ".fasta";
	else if (f == MultiFormatReader::PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::PHYLIP_DISC_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DISC_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_DISC_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DISC_FORMAT)
		return ".phy";
	else if (f == MultiFormatReader::PHYLIP_TREE_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_TREE_FORMAT)
		return ".tre";
	else if (f == MultiFormatReader::NEXML_FORMAT)
		return ".xml";
	else {
		throw NxsException("export requested for unsupported format");
	}
}

std::string purgeIllegalCharactersFromPhylipName(const std::string &origName)
{
	std::string cleanedName;
	cleanedName.reserve(origName.length());
	for (std::string::const_iterator it = origName.begin(); it != origName.end(); ++it)
		{
		if (strchr("()[];,:\n", *it) == NULL)
			cleanedName.append(1, *it);
		}
	return cleanedName;
}

std::string purgeIllegalCharactersFromRelaxedPhylipName(const std::string &origName)
{
	std::string cleanedName;
	cleanedName.reserve(origName.length());
	for (std::string::const_iterator it = origName.begin(); it != origName.end(); ++it)
		{
		if (strchr(" \n\t", *it) != NULL)
			cleanedName.append(1, '_');
		else if (strchr("()[];,:.", *it) == NULL) // RAxML (at least in the past) has not been happy with .'s in names -- since it is one of the targetted "relaxed phylip" programs, we will suppress .'s
			cleanedName.append(1, *it);
		}
	return cleanedName;
}

bool formLegalPhylipName(const std::string & origName, const NxsString & numericExtension, const std::set<std::string> & used, std::string & toReturn)
{
	const unsigned MAX_PHYLIP_NAME_LENGTH = 10;
	unsigned postLen = numericExtension.length();
	if (postLen > MAX_PHYLIP_NAME_LENGTH)
		throw NxsException("Number of duplicate names exceed the capacity of our poorly thought out mechanism for avoiding name clashes");
	const unsigned unPaddedLen = postLen + origName.length();
	if (unPaddedLen <= MAX_PHYLIP_NAME_LENGTH)
		toReturn = origName;
	else if (postLen == MAX_PHYLIP_NAME_LENGTH)
		toReturn.clear();
	else
		toReturn.assign(origName.c_str(), MAX_PHYLIP_NAME_LENGTH - postLen);
	toReturn.append(numericExtension.c_str());
	if (unPaddedLen < MAX_PHYLIP_NAME_LENGTH)
		toReturn.append(MAX_PHYLIP_NAME_LENGTH - unPaddedLen, ' ');
	const std::string cap = NxsString::get_upper(toReturn);
	return (used.find(cap) == used.end());
}

bool formLegalRelaxedPhylipName(const std::string & origName, const NxsString & numericExtension, const std::set<std::string> & used, std::string & toReturn)
{
	toReturn = origName;
	toReturn.append(numericExtension.c_str());
	const std::string cap = NxsString::get_upper(toReturn);
	return (used.find(cap) == used.end());
}

std::string getLegalPhylipTaxonName(const std::string & origName, const std::set<std::string> & used)
{
	NxsString numericExtension;
	std::string toReturn;
	const std::string cleanedName(purgeIllegalCharactersFromPhylipName(origName));

	for (unsigned i = 1;; ++i)
		{
		if (formLegalPhylipName(cleanedName, numericExtension, used, toReturn))
			return toReturn;
		numericExtension.clear();
		numericExtension << i;
		}
}

std::string getLegalRelaxedPhylipTaxonName(const std::string & origName, const std::set<std::string> & used)
{
	NxsString numericExtension;
	std::string toReturn;
	const std::string cleanedName(purgeIllegalCharactersFromRelaxedPhylipName(origName));

	for (unsigned i = 1;; ++i)
		{
		if (formLegalRelaxedPhylipName(cleanedName, numericExtension, used, toReturn))
			return toReturn;
		numericExtension.clear();
		numericExtension << i;
		}
}

std::string getLegalTaxonName(const std::string & origName, const std::set<std::string> & used,  MultiFormatReader::DataFormatType f)
{
	if (f == MultiFormatReader::NEXUS_FORMAT
		|| f == MultiFormatReader::FASTA_DNA_FORMAT
		|| f == MultiFormatReader::FASTA_AA_FORMAT
		|| f == MultiFormatReader::FASTA_RNA_FORMAT
		|| f == MultiFormatReader::NEXML_FORMAT)
		return origName;
	if (f == MultiFormatReader::PHYLIP_DNA_FORMAT
		|| f == MultiFormatReader::PHYLIP_RNA_FORMAT
		|| f == MultiFormatReader::PHYLIP_AA_FORMAT
		|| f == MultiFormatReader::PHYLIP_DISC_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DNA_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_PHYLIP_RNA_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_PHYLIP_AA_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DISC_FORMAT
		|| f == MultiFormatReader::PHYLIP_TREE_FORMAT) {
		return getLegalPhylipTaxonName(origName, used);
	}
	if (f == MultiFormatReader::RELAXED_PHYLIP_DNA_FORMAT
		|| f == MultiFormatReader::RELAXED_PHYLIP_RNA_FORMAT
		|| f == MultiFormatReader::RELAXED_PHYLIP_AA_FORMAT
		|| f == MultiFormatReader::RELAXED_PHYLIP_DISC_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DNA_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_RNA_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_AA_FORMAT
		|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DISC_FORMAT
		|| f == MultiFormatReader::RELAXED_PHYLIP_TREE_FORMAT) {
		return getLegalRelaxedPhylipTaxonName(origName, used);
	}
	throw NxsException("export requested for unsupported format");
}

/// returns an empty vector if no translation of names is needed or a vector
///	 of the internal name to the getLegaldName
std::vector<NxsNameToNameTrans> nameTranslationDict(const std::vector<std::string> & origTaxa, MultiFormatReader::DataFormatType f)
{
	bool transNeeded = false;
	std::vector<NxsNameToNameTrans> translationTable;
	std::set<std::string> usedNames;
	for (std::vector<std::string>::const_iterator origIt = origTaxa.begin(); origIt != origTaxa.end(); ++origIt) {
		std::string e = getLegalTaxonName(*origIt, usedNames, f);
		if (e != *origIt)
			transNeeded = true;
		translationTable.push_back(NxsNameToNameTrans(*origIt, e));
		NxsString::to_upper(e);
		usedNames.insert(e);
	}
	if (transNeeded)
		return translationTable;
	return std::vector<NxsNameToNameTrans>();
}

bool IsPhylipType(MultiFormatReader::DataFormatType f)
{
	return (f == MultiFormatReader::PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::PHYLIP_DISC_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DISC_FORMAT);
}

bool IsRelaxedPhylipType(MultiFormatReader::DataFormatType f)
{
	return (f == MultiFormatReader::RELAXED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::RELAXED_PHYLIP_DISC_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DISC_FORMAT);
}

bool IsInterleaveType(MultiFormatReader::DataFormatType f)
{
	return (f == MultiFormatReader::INTERLEAVED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DISC_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_RNA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_AA_FORMAT
			|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DISC_FORMAT);
}

bool IsFastaType(MultiFormatReader::DataFormatType f)
{
	return (f == MultiFormatReader::FASTA_DNA_FORMAT
			|| f == MultiFormatReader::FASTA_RNA_FORMAT
			|| f == MultiFormatReader::FASTA_AA_FORMAT);
}



void writeUnalignedBlockToStream(
  const NxsUnalignedBlock & cb,
  ostream & outf,
  const std::vector<std::string> & taxaNames,
  MultiFormatReader::DataFormatType f,
  long interleaveLen)
{
	const unsigned nt = taxaNames.size();
    if (IsFastaType(f))
		{
		if (interleaveLen < 1)
			interleaveLen = 60; // default FASTA line length
		for (unsigned i = 0; i < nt; ++i)
			{
			const std::string & name = taxaNames[i];
			std::string seq = cb.GetMatrixRowAsStr(i);
			outf << '>' << name << '\n';
			const unsigned nc = seq.length();
			for (unsigned currIndex = 0; currIndex < nc; currIndex += (unsigned)interleaveLen)
				{
				unsigned nCharsToWrite = ((nc - currIndex) > (unsigned)interleaveLen ? (unsigned)interleaveLen : (nc - currIndex));
				outf << seq.substr(currIndex, nCharsToWrite) << '\n';
				}
			}
		}
	else
		{
		throw NxsException("writeCharactersBlockToStream requested for unsupported format");
		}
}

void writeCharactersBlockToStream(
  const NxsCharactersBlock & cb,
  ostream & outf,
  const std::vector<std::string> & taxaNames,
  MultiFormatReader::DataFormatType f,
  long interleaveLen)
{
	const unsigned nt = taxaNames.size();
	const unsigned nc = cb.GetNChar();
	unsigned nCharsToWrite;
	unsigned seqStartColumn = 0;

	if (IsRelaxedPhylipType(f))
		{
		for (unsigned i = 0; i < nt; ++i)
			{
			const std::string & name = taxaNames[i];
			if (name.length() > seqStartColumn)
				seqStartColumn = name.length();
			}
		seqStartColumn += 1;
		}


	if (IsPhylipType(f) || IsRelaxedPhylipType(f))
		{
		std::string sep;

		outf << nt << ' ' << nc << '\n';

		if (IsInterleaveType(f) && interleaveLen > 0)
			{
			std::vector<std::string> storedSeqs;
			storedSeqs.reserve(nt);
			std::string *sp;
			nCharsToWrite = (nc > (unsigned) interleaveLen ? (unsigned)interleaveLen : nc);
			for (unsigned i = 0; i < nt; ++i)
				{
				const std::string & name = taxaNames[i];
				storedSeqs.push_back(cb.GetMatrixRowAsStr(i));
				sp = &(storedSeqs[i]);
				if (IsRelaxedPhylipType(f))
					{
					sep.clear();
					sep.append(seqStartColumn - name.length(), ' ');
					}
				outf << name << sep << sp->substr(0, nCharsToWrite) << '\n';
				}

			for (unsigned currIndex = (unsigned)interleaveLen; currIndex < nc; currIndex += (unsigned)interleaveLen)
				{
				outf << '\n';
				nCharsToWrite = ((nc - currIndex) > (unsigned)interleaveLen ? (unsigned)interleaveLen : (nc - currIndex));
				for (unsigned i = 0; i < nt; ++i)
					{
					sp = &(storedSeqs[i]);
					outf << sp->substr(currIndex, nCharsToWrite) << '\n';
					}
				}
			}
		else
			{
			if (interleaveLen > 0)
				{
				// not interleaved, but wrapping at interleaveLen
				for (unsigned i = 0; i < nt; ++i)
					{
					nCharsToWrite = (nc > (unsigned)interleaveLen ? (unsigned)interleaveLen : nc);
					const std::string & name = taxaNames[i];
					std::string seq = cb.GetMatrixRowAsStr(i);
					if (IsRelaxedPhylipType(f))
						{
						sep.clear();
						sep.append(seqStartColumn - name.length(), ' ');
						}
					outf << name << sep << seq.substr(0, nCharsToWrite) << '\n';
					for (unsigned currIndex = (unsigned)interleaveLen; currIndex < nc; currIndex += (unsigned)interleaveLen)
						{
						nCharsToWrite = ((nc - currIndex) > (unsigned)interleaveLen ? (unsigned)interleaveLen : (nc - currIndex));
						outf << seq.substr(currIndex, nCharsToWrite) << '\n';
						}
					}
				}
			else
				{
				// not interleaved, and not wrapping
				for (unsigned i = 0; i < nt; ++i)
					{
					const std::string & name = taxaNames[i];
					std::string seq = cb.GetMatrixRowAsStr(i);
					if (IsRelaxedPhylipType(f))
						{
						sep.clear();
						sep.append(seqStartColumn - name.length(), ' ');
						}
					outf << name << sep << seq << '\n';
					}
				}
			}
		}
	else if (IsFastaType(f))
		{
		if (interleaveLen < 1)
			interleaveLen = 60; // default FASTA line length
		for (unsigned i = 0; i < nt; ++i)
			{
			nCharsToWrite = (nc > (unsigned)interleaveLen ? (unsigned)interleaveLen : nc);
			const std::string & name = taxaNames[i];
			std::string seq = cb.GetMatrixRowAsStr(i);
			outf << '>' << name << '\n' << seq.substr(0, nCharsToWrite) << '\n';
			for (unsigned currIndex = (unsigned)interleaveLen; currIndex < nc; currIndex += (unsigned)interleaveLen)
				{
				nCharsToWrite = ((nc - currIndex) > (unsigned)interleaveLen ? (unsigned)interleaveLen : (nc - currIndex));
				outf << seq.substr(currIndex, nCharsToWrite) << '\n';
				}
			}
		}
	else
		{
		throw NxsException("writeCharactersBlockToStream requested for unsupported format");
		}
}



void openOrThrow(std::ofstream & outf, const std::string &fn)
    {
    outf.open(fn.c_str());
    if (!outf.good())
        {
        NxsString msg;
        msg << "Could not open the file " << fn;
        throw NxsException(msg);
        }
    if (!gQuietMode)
        std::cerr << "Writing " << fn << '\n';
    }
    
void exportUnalignedBlocks(
  PublicNexusReader & nexusReader,
  MultiFormatReader::DataFormatType f,
  long interleaveLen,
  std::string prefix, 
  std::ostream * fp)
{
    std::ostream * fpToUse = fp;
	const unsigned nTaxaBlocks = nexusReader.GetNumTaxaBlocks();
	for (unsigned t = 0; t < nTaxaBlocks; ++t)
		{
		const NxsTaxaBlock * tb = nexusReader.GetTaxaBlock(t);
		const unsigned nUnalignedBlocks = nexusReader.GetNumUnalignedBlocks(tb);
		if (nUnalignedBlocks == 0)
			continue;

		NxsString tbSpecificPrefix;
		if (t > 0)
			tbSpecificPrefix << (1 + t);
		tbSpecificPrefix << prefix << "-unaligned";

		typedef std::pair<const NxsUnalignedBlock *, std::string> PairUBAndString;
		typedef std::vector< PairUBAndString > VecPairUBAndString;
		VecPairUBAndString ubToWrite;
		for (unsigned i = 0; i < nUnalignedBlocks; ++i)
			{
			NxsString fn = tbSpecificPrefix;
			if (i > 0)
				fn << (1 + i);
			fn << getFileExtension(f);

			const NxsUnalignedBlock * ub = nexusReader.GetUnalignedBlock(tb, i);

			NxsCharactersBlock::DataTypesEnum dt = ub->GetDataType();
			bool writeBlock = false;
            if ((dt == NxsCharactersBlock::dna || dt == NxsCharactersBlock::nucleotide) && (f == MultiFormatReader::FASTA_DNA_FORMAT))
				writeBlock = true;
			else if ((dt == NxsCharactersBlock::rna) && (f == MultiFormatReader::FASTA_RNA_FORMAT))
				writeBlock = true;
			else if ((dt == NxsCharactersBlock::protein) && (f == MultiFormatReader::FASTA_AA_FORMAT))
				writeBlock = true;

			if (writeBlock)
				ubToWrite.push_back(std::pair<const NxsUnalignedBlock *, std::string>(ub, fn));
			}
		if (!ubToWrite.empty())
			{
			std::vector<std::string> namesToPrint(tb->GetAllLabels());
			std::vector<NxsNameToNameTrans> nameTrans = nameTranslationDict(namesToPrint, f);
			if (!nameTrans.empty())
				{
				namesToPrint.clear();
				for (std::vector<NxsNameToNameTrans>::const_iterator nIt = nameTrans.begin(); nIt != nameTrans.end(); ++nIt)
					namesToPrint.push_back(nIt->second);
				if (nexusReader.conversionOutputRecord.writeNameTranslationFile)
				    nexusReader.conversionOutputRecord.writeNameTranslation(nameTrans, tb);
				}

			for (VecPairUBAndString::const_iterator vIt = ubToWrite.begin(); vIt != ubToWrite.end(); ++vIt)
				{
				const NxsUnalignedBlock * ubP = vIt->first;
				std::string fn = vIt->second;
				ofstream outf;
				if (fp == 0L)
				    {
				    openOrThrow(outf, fn);
				    fpToUse = &outf;
                    }
                
				writeUnalignedBlockToStream(*ubP, *fpToUse, namesToPrint, f, interleaveLen);
				}
			}
		}
}

void exportCharacters(
  PublicNexusReader & nexusReader,
  MultiFormatReader::DataFormatType f,
  long interleaveLen,
  std::string prefix, 
  std::ostream * fp)
{
    std::ostream * fpToUse = fp;
	const unsigned nTaxaBlocks = nexusReader.GetNumTaxaBlocks();
	for (unsigned t = 0; t < nTaxaBlocks; ++t)
		{
		const NxsTaxaBlock * tb = nexusReader.GetTaxaBlock(t);
		const unsigned nCharBlocks = nexusReader.GetNumCharactersBlocks(tb);
		if (nCharBlocks == 0)
			continue;

		NxsString tbSpecificPrefix;
		if (t > 0)
			tbSpecificPrefix << (1 + t);
		tbSpecificPrefix << prefix;

		typedef std::pair<const NxsCharactersBlock *, std::string> PairCBAndString;
		typedef std::vector< PairCBAndString > VecPairCBAndString;
		VecPairCBAndString cbToWrite;
		for (unsigned i = 0; i < nCharBlocks; ++i)
			{
			NxsString fn = tbSpecificPrefix;
			if (i > 0)
				fn << (1 + i);
			fn << getFileExtension(f);

			const NxsCharactersBlock * cb = nexusReader.GetCharactersBlock(tb, i);

			NxsCharactersBlock::DataTypesEnum dt = cb->GetDataType();
			bool writeBlock = false;
			if ((dt == NxsCharactersBlock::standard)
				&& (f == MultiFormatReader::PHYLIP_DISC_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DISC_FORMAT
					|| f == MultiFormatReader::RELAXED_PHYLIP_DISC_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DISC_FORMAT))
				writeBlock = true;
			else if ((dt == NxsCharactersBlock::dna || dt == NxsCharactersBlock::nucleotide)
				&& (f == MultiFormatReader::PHYLIP_DNA_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_PHYLIP_DNA_FORMAT
					|| f == MultiFormatReader::FASTA_DNA_FORMAT
					|| f == MultiFormatReader::RELAXED_PHYLIP_DNA_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DNA_FORMAT))
				writeBlock = true;
			else if ((dt == NxsCharactersBlock::rna)
				&& (f == MultiFormatReader::PHYLIP_RNA_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_PHYLIP_RNA_FORMAT
					|| f == MultiFormatReader::FASTA_RNA_FORMAT
					|| f == MultiFormatReader::RELAXED_PHYLIP_RNA_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_RNA_FORMAT))
				writeBlock = true;
			else if ((dt == NxsCharactersBlock::protein)
				&& (f == MultiFormatReader::PHYLIP_AA_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_PHYLIP_AA_FORMAT
					|| f == MultiFormatReader::FASTA_AA_FORMAT
					|| f == MultiFormatReader::RELAXED_PHYLIP_AA_FORMAT
					|| f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_AA_FORMAT))
				writeBlock = true;

			if (writeBlock)
				cbToWrite.push_back(std::pair<const NxsCharactersBlock *, std::string>(cb, fn));
			}
		if (!cbToWrite.empty())
			{
			std::vector<std::string> namesToPrint(tb->GetAllLabels());
			std::vector<NxsNameToNameTrans> nameTrans = nameTranslationDict(namesToPrint, f);
			if (!nameTrans.empty())
				{
				namesToPrint.clear();
				for (std::vector<NxsNameToNameTrans>::const_iterator nIt = nameTrans.begin(); nIt != nameTrans.end(); ++nIt)
					namesToPrint.push_back(nIt->second);
				if (nexusReader.conversionOutputRecord.writeNameTranslationFile)
                    nexusReader.conversionOutputRecord.writeNameTranslation(nameTrans, tb);
				}

			for (VecPairCBAndString::const_iterator vIt = cbToWrite.begin(); vIt != cbToWrite.end(); ++vIt)
				{
				const NxsCharactersBlock * cbP = vIt->first;
				std::string fn = vIt->second;
				ofstream outf;
				if (fp == 0L)
				    {
				    openOrThrow(outf, fn);
                    fpToUse = &outf;
                    }
                
				writeCharactersBlockToStream(*cbP, *fpToUse, namesToPrint, f, interleaveLen);
				}
			}
		}
}

void exportTrees(
  PublicNexusReader & nexusReader,
  MultiFormatReader::DataFormatType f,
  std::string prefix,
  std::ostream * fp)
{
    std::ostream * fpToUse = fp;
	const unsigned nTaxaBlocks = nexusReader.GetNumTaxaBlocks();
	for (unsigned t = 0; t < nTaxaBlocks; ++t)
		{
		const NxsTaxaBlock * tb = nexusReader.GetTaxaBlock(t);
		const unsigned nTreesBlocks = nexusReader.GetNumTreesBlocks(tb);
		if (nTreesBlocks == 0)
			continue;

		NxsString tbSpecificPrefix;
		if (t > 0)
			tbSpecificPrefix << (1 + t);
		tbSpecificPrefix << prefix;
		std::vector<std::string> namesToPrint(tb->GetAllLabels());
		std::vector<NxsNameToNameTrans> nameTrans = nameTranslationDict(namesToPrint, f);
		if (!nameTrans.empty())
			{
			namesToPrint.clear();
			for (std::vector<NxsNameToNameTrans>::const_iterator nIt = nameTrans.begin(); nIt != nameTrans.end(); ++nIt)
				namesToPrint.push_back(nIt->second);
			if (nexusReader.conversionOutputRecord.writeNameTranslationFile)
                nexusReader.conversionOutputRecord.writeNameTranslation(nameTrans, tb);
			}

		for (unsigned i = 0; i < nTreesBlocks; ++i)
			{
			NxsString fn = tbSpecificPrefix;
			if (i > 0)
				fn << (1 + i);
			fn << getFileExtension(f);

			const NxsTreesBlock * trb = nexusReader.GetTreesBlock(tb, i);

			ofstream outf;
			if (fp == 0L)
			    {
			    openOrThrow(outf, fn);
                fpToUse = &outf;
                }
            
			trb->ProcessAllTrees();
			for (unsigned j = 0; j < trb->GetNumTrees(); ++j)
				{
				const NxsFullTreeDescription & ftd = trb->GetFullTreeDescription(j);
				NxsSimpleTree tree(ftd, -1, -1.0);
				std::vector<NxsSimpleNode *> & leaves = tree.GetLeavesRef();
				for (std::vector<NxsSimpleNode *>::const_iterator leafIt = leaves.begin(); leafIt != leaves.end(); ++leafIt)
					{
					NxsSimpleNode * leaf = *leafIt;
					assert(leaf);
					const std::string name = namesToPrint[leaf->GetTaxonIndex()];
					if (!gQuietMode)
                        std::cerr << "Setting name=" << name << '\n';
					leaf->SetName(name);
					}
				tree.WriteAsNewick(*fpToUse, true, true, false, tb);
				*fpToUse << ";\n";
				}
			}
		}
}

void exportData(
  PublicNexusReader & nexusReader,
  MultiFormatReader::DataFormatType f,
  long interleaveLen,
  std::string prefix,
  std::ostream * fp)
{
    std::ostream * fpToUse = fp;
	std::string fullName = prefix;
	if (f == MultiFormatReader::NEXUS_FORMAT) {
		// hack-alert: we don't need to pass in the interleaveLen because it is set as a global in the calling code
		fullName.append(".nex");
		std::ofstream nexOut;
		if (fp == 0L)
		    {
		    nexOut.open(fullName.c_str());
            if (!gQuietMode)
                std::cerr << "Writing " << fullName << '\n';
    		fpToUse = &nexOut;
    		}
		writeAsNexus(nexusReader, *fpToUse);
		if (fp == 0L)
    		nexOut.close();
	}
	else if (f == MultiFormatReader::FASTA_DNA_FORMAT
			 || f == MultiFormatReader::FASTA_AA_FORMAT
			 || f == MultiFormatReader::FASTA_RNA_FORMAT
			 || f == MultiFormatReader::PHYLIP_DNA_FORMAT
			 || f == MultiFormatReader::PHYLIP_RNA_FORMAT
			 || f == MultiFormatReader::PHYLIP_AA_FORMAT
//			 || f == MultiFormatReader::PHYLIP_DISC_FORMAT
			 || f == MultiFormatReader::INTERLEAVED_PHYLIP_DNA_FORMAT
			 || f == MultiFormatReader::INTERLEAVED_PHYLIP_RNA_FORMAT
			 || f == MultiFormatReader::INTERLEAVED_PHYLIP_AA_FORMAT
//			 || f == MultiFormatReader::INTERLEAVED_PHYLIP_DISC_FORMAT
			 || f == MultiFormatReader::RELAXED_PHYLIP_DNA_FORMAT
			 || f == MultiFormatReader::RELAXED_PHYLIP_RNA_FORMAT
			 || f == MultiFormatReader::RELAXED_PHYLIP_AA_FORMAT
//			 || f == MultiFormatReader::RELAXED_PHYLIP_DISC_FORMAT
			 || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DNA_FORMAT
			 || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_RNA_FORMAT
//			 || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_DISC_FORMAT
			 || f == MultiFormatReader::INTERLEAVED_RELAXED_PHYLIP_AA_FORMAT)
	    {
		exportCharacters(nexusReader, f, interleaveLen, prefix, fp);
		if (f == MultiFormatReader::FASTA_DNA_FORMAT
			 || f == MultiFormatReader::FASTA_AA_FORMAT
			 || f == MultiFormatReader::FASTA_RNA_FORMAT)
			 {
			 exportUnalignedBlocks(nexusReader, f, interleaveLen, prefix, fp);
			 }
		}
	else if (f == MultiFormatReader::PHYLIP_TREE_FORMAT
			 || f == MultiFormatReader::RELAXED_PHYLIP_TREE_FORMAT)
		exportTrees(nexusReader, f, prefix, fp);
	else if (f == MultiFormatReader::NEXML_FORMAT) {
		fullName.append(".xml");
		std::ofstream nexOut(fullName.c_str());
		if (!gQuietMode)
		    std::cerr << "Writing " << fullName << '\n';
		writeAsNexml(nexusReader, nexOut);
		nexOut.close();
	}
	else {
		throw NxsException("export requested for unsupported format");
	}
}