#include "muscle.h"
#include "alpha3.h"
#include "sort.h"
#include <errno.h>
void MultiSequence::LoadMFA(const string& filename, bool stripGaps)
	{
	// try opening file
	FileBuffer infile(filename.c_str());

	if (infile.fail())
		Die("Cannot open %s, errno=%d %s",
		  filename.c_str(), errno, strerror(errno));

	// if successful, then load using other LoadMFA() routine
	LoadMFA(infile, stripGaps);

	infile.close();
	}

#if SEQ_TRACE
void MultiSequence::AssertSeqIds() const
	{
	const uint SeqCount = GetSeqCount();
	asserta(SIZE(m_Owners) == SIZE(m_Seqs));
	for (uint i = 0; i < SeqCount; ++i)
		m_Seqs[i]->AssertId();
	}
#endif

void MultiSequence::Clear()
	{
	const uint SeqCount = GetSeqCount();
	asserta(SIZE(m_Owners) == SIZE(m_Seqs));
	for (uint i = 0; i < SeqCount; ++i)
		{
		if (m_Owners[i])
			DeleteSequence(m_Seqs[i]);
		}
	m_Seqs.clear();
	m_Owners.clear();
	}

void MultiSequence::Copy(const MultiSequence &rhs)
	{
	Clear();
	const uint SeqCount = rhs.GetSeqCount();
	for (uint i = 0; i < SeqCount; ++i)
		{
		const Sequence *r = rhs.GetSequence(i)->Clone();
		AddSequence(r, true);
		}
	}

void MultiSequence::LogMe() const
	{
	Log("\n");
	Log("MultiSequence::LogMe(%p), %u seqs\n", this, GetSeqCount());
	for (uint i = 0; i < GetSeqCount(); ++i)
		m_Seqs[i]->LogMe();
	}

uint MultiSequence::GetGSI(uint SeqIndex) const
	{
	const Sequence *Seq = GetSequence(SeqIndex);
	uint L = (uint) Seq->GetGSI();
	return L;
	}

uint MultiSequence::GetSeqLength(uint SeqIndex) const
	{
	const Sequence *Seq = GetSequence(SeqIndex);
	uint L = (uint) Seq->GetLength();
	return L;
	}

void MultiSequence::GetLengthOrder(vector<uint> &SeqIndexes) const
	{
	const uint SeqCount = GetSeqCount();
	vector<uint> Ls;
	for (uint SeqIndex = 0; SeqIndex < SeqCount; ++SeqIndex)
		{
		uint L = (uint) GetSequence(SeqIndex)->GetLength();
		Ls.push_back(L);
		}
	SeqIndexes.resize(SeqCount);
	const uint *PtrLs = Ls.data();
	uint *PtrOrder = SeqIndexes.data();
	QuickSortOrderDesc<uint>(PtrLs, SeqCount, PtrOrder);
	}

void MultiSequence::AssertGSIs() const
	{
	const uint GlobalSeqCount = GetGlobalMSSeqCount();
	const uint SeqCount = GetSeqCount();
	for (uint i = 0; i < SeqCount; ++i)
		{
		const Sequence *Seq = GetSequence(i);
		asserta(Seq != 0);
		uint GSI = Seq->GetGSI();
		asserta(GSI < GlobalSeqCount);
		}
	}

void MultiSequence::LogGSIs(const char *Msg) const
	{
	const MultiSequence &GlobalMS = GetGlobalInputMS();
	const uint GlobalSeqCount = GlobalMS.GetSeqCount();
	Log("\n");
	Log("LogGSIs()");
	if (Msg != 0)
		Log("  %s  ", Msg);
	const uint SeqCount = GetSeqCount();
	Log("%u seqs\n", SeqCount);
	for (uint i = 0; i < SeqCount; ++i)
		{
		Log("[%5u]", i);
		const Sequence *Seq = GetSequence(i);
		uint GSI = (uint) Seq->GetGSI();
		string Label = Seq->GetLabel();
		asserta(GSI < GlobalSeqCount);
		const Sequence *GlobalSeq = GlobalMS.GetSequence(GSI);
		string GlobalLabel = GlobalSeq->GetLabel();

		Log("  %5d", GSI);
		Log("  >%s", Label.c_str());
		Log("  (%s)", GlobalLabel.c_str());
		if (Label != GlobalLabel)
			Log("  <<< ERROR");
		Log("\n");
		}
	}

void MultiSequence::LoadMFA(FileBuffer& infile, bool stripGaps)
	{
	if (infile.fail())
		Die("LoadMFA read error");

	for (;;)
		{
		Sequence *seq = Sequence::NewSequence();
		bool Ok = seq->FromFileBuffer(infile, stripGaps);
		if (!Ok)
			{
			DeleteSequence(seq);
			break;
			}

		m_Seqs.push_back(seq);
		m_Owners.push_back(true);
		}
	}

uint MultiSequence::GetColCount() const
	{
	assert(IsAligned());
	const Sequence *Seq = GetSequence(0);
	uint L = Seq->GetLength();
	return L;
	}

bool MultiSequence::IsAligned() const
	{
	int N = GetNumSequences();
	if (N == 0)
		return false;
	uint ColCount0 = GetSequence(0)->GetLength();
	for (int i = 1; i < N; ++i)
		{
		int ColCount = GetSequence(i)->GetLength();
		if (ColCount != ColCount0)
			return false;
		}
	return true;
	}

uint MultiSequence::GetSeqIndex(const string &Label, bool FailOnError) const
	{
	int N = GetNumSequences();
	for (int i = 0; i < N; ++i)
		{
		const Sequence *Seq = GetSequence(i);
		if (Seq->GetLabel() == Label)
			return i;
		}
	if (FailOnError)
		Die("Label not found >%s", Label.c_str());
	return UINT_MAX;
	}

bool MultiSequence::GuessIsNucleo() const
	{
// If at least MIN_NUCLEO_PCT of the first CHAR_COUNT non-gap
// letters belong to the nucleotide alphabet, guess nucleo.
// Otherwise amino.
	const unsigned CHAR_COUNT = 100;
	const unsigned MIN_NUCLEO_PCT = 95;

	const unsigned SeqCount = GetSeqCount();
	uint NucleoCount = 0;
	for (uint i = 0; i < 100; ++i)
		{
		uint SeqIndex = randu32()%SeqCount;
		const Sequence &seq = *GetSequence(SeqIndex);
		const uint L = seq.GetLength();
		uint r = randu32();
		const uint Pos = r%L;
		byte c = (byte) GetChar(SeqIndex, Pos);
		uint Letter = g_CharToLetterNucleo[c];
		if (Letter < 4)
			++NucleoCount;
		}
	if (NucleoCount > 75)
		return true;
	return false;
	}

void MultiSequence::FromStrings(const vector<string> &Labels,
  const vector<string> &Seqs)
	{
	Clear();
	const uint N = SIZE(Seqs);
	asserta(SIZE(Labels) == N);
	for (uint i = 0; i < N; ++i)
		{
		const string &Label = Labels[i];
		const string &Str = Seqs[i];
		Sequence *Seq = Sequence::NewSequence();
		Seq->FromString(Label, Str);
		AddSequence(Seq, true);
		}
	}

void MultiSequence::ToMSA(MSA &msa) const
	{
	uint SeqCount = GetSeqCount();
	uint ColCount = GetColCount();
	msa.SetSize(SeqCount, ColCount);
	for (uint SeqIndex = 0; SeqIndex < SeqCount; ++SeqIndex)
		{
		const Sequence *Seq = GetSequence(SeqIndex);
		const byte *ByteSeq = Seq->GetBytePtr();
		const char *Label = Seq->GetLabel().c_str();
		uint L = Seq->GetLength();

		char *CharSeq = myalloc(char, L+1);
		memcpy(CharSeq, ByteSeq, L);
		CharSeq[L] = 0;
		msa.m_szSeqs[SeqIndex] = CharSeq;
		msa.m_szNames[SeqIndex] = mystrsave(Label);
		}
	}
