@@ -405,6 +405,52 @@ bool Sequences::loadSequencesFromFasta(const std::string& fastaFilePath,
405
405
return true ;
406
406
}
407
407
408
+ /* *
409
+ * @brief Read the first line of a Phylip Alignment file (which is the number of sequences,
410
+ * white space, and the length of all the sequences)
411
+ *
412
+ * @tparam S the stream type
413
+ * @param in the stream itself
414
+ * @param num_sequences reference - will have the number of sequences
415
+ * in the alignment assigned to it (on success)
416
+ * @param sequence_length reference - will have the length of the sequences
417
+ * (every sequence must have the same length)
418
+ * assigned to it (on success)
419
+ * @return true on success
420
+ * @return false on failure (an error message will have been written
421
+ * to standard output).
422
+ *
423
+ * @note it is assumed that failbit is not already set on the stream when this
424
+ * function is called.
425
+ */
426
+ template <class S > bool readFirstLineOfPhylipAlignmentFile (S& in, size_t & num_sequences, size_t & sequence_length) {
427
+ if (in.eof ()) {
428
+ std::cerr << " Sequence file was empty." ;
429
+ return false ;
430
+ }
431
+ std::string line;
432
+ safeGetLine (in, line);
433
+ // Read the header line
434
+ std::stringstream linestream (line);
435
+ linestream >> num_sequences;
436
+ if (in.fail ()) {
437
+ std::cerr << " Could not read number of sequences." ;
438
+ return false ;
439
+ }
440
+ linestream >> sequence_length;
441
+ if (in.fail ()) {
442
+ std::cerr << " Could not read sequence length." ;
443
+ return false ;
444
+ }
445
+ if (num_sequences < 1 || sequence_length < 1 ) {
446
+ std::cerr << " Number of sequences " << num_sequences
447
+ << " or Sequence length " << sequence_length
448
+ << " was invalid." ;
449
+ return false ;
450
+ }
451
+ return true ;
452
+ }
453
+
408
454
/* *
409
455
* @brief Load a sequence alignment from a phylip format file
410
456
* @param phylipFilePath - the path to the file
@@ -443,27 +489,19 @@ bool Sequences::loadSequencesFromPhylip(const std::string& phylipFilePath,
443
489
}
444
490
size_t num_sequences = 0 ;
445
491
size_t sequence_length = 0 ;
492
+ if (!readFirstLineOfPhylipAlignmentFile (in, num_sequences, sequence_length))
493
+ {
494
+ in.close ();
495
+ return false ;
496
+ }
497
+
446
498
bool have_read_names = 0 ;
447
499
size_t name_length = 0 ; // Number of characters to use for sequence name
448
500
size_t sequence_num = 0 ; // Ordinal sequence # to read next
449
501
450
502
for (; !in.eof (); ++line_num) {
451
503
std::string line;
452
504
safeGetLine (in, line);
453
- if (line_num == 1 ) {
454
- // Read the header line
455
- std::stringstream linestream (line);
456
- linestream >> num_sequences;
457
- linestream >> sequence_length;
458
- if (num_sequences < 1 || sequence_length < 1 ) {
459
- in.close ();
460
- std::cerr << " Number of sequences " << num_sequences
461
- << " or Sequence length " << sequence_length
462
- << " was invalid." ;
463
- return false ;
464
- }
465
- continue ;
466
- }
467
505
if (line == " " ) {
468
506
if (sequence_num!=0 && sequence_num!=num_sequences) {
469
507
in.close ();
@@ -817,7 +855,7 @@ SequenceLoader::SequenceLoader(char unknown, bool isDNA,
817
855
, is_site_variant(site_variant)
818
856
, report_progress(report_progress_while_loading), unkLen(0 )
819
857
, buffer(nullptr ), sequence_data(nullptr )
820
- , unk_buffer(nullptr ), unknown_data(nullptr ) {
858
+ , unk_buffer(nullptr ), unknown_data(nullptr ), num_states( 4 ) {
821
859
rank = sequences.size ();
822
860
rawSeqLen = sequences.front ().sequenceLength ();
823
861
seqLen = 0 ;
@@ -982,6 +1020,7 @@ bool SequenceLoader::writeDistanceMatrixToFile(bool numbered_names,
982
1020
progress_display& show_progress;
983
1021
public:
984
1022
typedef FlatMatrix super;
1023
+ FakeMatrix (const FakeMatrix& rhs) = delete ;
985
1024
FakeMatrix (SequenceLoader& my_owner, progress_display& progress_bar)
986
1025
: super(), owner(my_owner), show_progress(progress_bar) {}
987
1026
virtual void setSize (intptr_t rows) { rowCount=rows;}
0 commit comments