// File: /workspace/github_cpp_makefile_star2_O1/giovannarosone[P]BCR_LCP_GSA/TransposeFasta.cpp
// Function: TransposeFasta::findLengthNseq(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
bool TransposeFasta::findLengthNseq( const string& input, const string& fileOutput, string BCRprefPrev){
	//#if BCR_FROMCYC==1
	for (dataTypedimAlpha z = 0 ; z < SIZE_ALPHA-1; z++)
		freq[z]=0;
	freq[SIZE_ALPHA-1]=0;
	freq[(unsigned int)(TERMINATE_CHAR)]=1;
	//#endif

	//lengthRead = CYCLENUM;
    
	char *fileLen = new char[fileOutput.length()+10];
	sprintf(fileLen, "%s.len", fileOutput.c_str());
	
	

	string bufChar;
	
	bool lenSeq = false;
   	vector <dataTypelenSeq> lengthSeqVector;

	//Find max length and number of reads
	dataTypelenSeq charsNumber=0;
	lengthRead=0;
	nSeq = 0;
	lengthTexts = 0;       //Total length of all texts without $-symbols
	
	#if KSEQ_PARSER==0
	
		std::ifstream infile(input.c_str());
		
		if (infile.is_open() == false) {
			std::cerr << "TransposeFasta::findLengthNseq: could not open file \"" << input.c_str() << "\"!"<< std::endl;	
			exit (EXIT_FAILURE);
		}
		while (getline(infile, bufChar))  {
			if ( (bufChar.length() > 0) && ((bufChar[bufChar.length()-2] == '\r') || (bufChar[bufChar.length()-2] == '\n')) )
				bufChar[bufChar.length()-2] = '\0';
			else if ( (bufChar.length() > 0) && ((bufChar[bufChar.length()-1] == '\r') || (bufChar[bufChar.length()-1] == '\n')) )
				bufChar[bufChar.length()-1] = '\0';
			
			
			//std::cerr << "Nuova lettura: seq:" << bufChar << ". Length " << bufChar.length() << " the last char is "<< (char)bufChar[bufChar.length()-1] << std::endl;
					
			if( bufChar[0] == '>' ) {     //it is the title of fasta file
				nSeq++;
				//std::cerr << "--Length Sequence N. " << (int)(nSeq) << " is " << (int)charsNumber << std::endl;
				if (charsNumber != 0) {
					//charsNumber--;
					if (lengthRead != charsNumber) {
						if (lengthRead < charsNumber)
							lengthRead = charsNumber;
						if (nSeq > 2)
							lenSeq = true;
					}
					
					//dataTypeNSeq numchar = fwrite (&charsNumber, sizeof(dataTypelenSeq), 1 , OutFileLen);
					//assert( numchar == 1); // we should always read the same number of characters
					lengthSeqVector.push_back (charsNumber);
					lengthTexts += charsNumber;
					//if ((verboseEncode==1) || (verboseDistance ==1))
						//std::cerr << "Length Sequence N. " << (int)(nSeq-1) << " is " << (int)charsNumber << " nSeq= "<< nSeq << std::endl;
				}
									
				charsNumber = 0;
			}
			else   //no title
			{

				#if BCR_FROMCYC==1
					for (dataTypelenSeq z = 0 ; z < bufChar.length(); z++)
						freq[(unsigned int)(bufChar[z])]=1;
				#endif

				// increase the counter of chars
				charsNumber = charsNumber + bufChar.length();
				//std::cerr << "Stessa sequenza --Length Sequence N. " << (int)(nSeq) << " is " << (int)charsNumber << std::endl;
			}
				
		}   //end-while
		//Update the max length of the reads
		if (lengthRead != charsNumber) {
			if (lengthRead < charsNumber)
				lengthRead = charsNumber;
			lenSeq = true;
		}
		lengthSeqVector.push_back (charsNumber);
		lengthTexts += charsNumber;
		infile.close();
	//charsNumber--;
	
	#else
		//kseq 2019-11-08
		gzFile fp;
		kseq_t *seq;
		//int l;
		//cout << input.c_str() << "\n";
		#if  STORE_TITLE_FASTQ == 1
		    char *fnAux = new char[fileOutput.length()+10];
		    sprintf (fnAux,"%s%s",fileOutput.c_str(),".title\0");
		    FILE* OutFile = fopen(fnAux, "wb");
		    if (OutFile==NULL) {
			std::cerr << "TransposeFasta: (title) Error opening " << fnAux << std::endl;
			exit (EXIT_FAILURE);
		    }
		#endif
	
		fp = gzopen(input.c_str(), "r");
		seq = kseq_init(fp);
		while ((kseq_read(seq)) >= 0) {
			nSeq++;
			//printf("name: %s\n", seq->name.s);
			#if  STORE_TITLE_FASTQ == 1
                		fprintf(OutFile, "%s\n", seq->name.s);
            		#endif
			//if (seq->comment.l) 
				//printf("comment: %s\n", seq->comment.s);
			
			//printf("seq: %s\n", seq->seq.s);
			
			charsNumber = strlen(seq->seq.s);
			if (lengthRead != charsNumber) {
				if (lengthRead < charsNumber)
					lengthRead = charsNumber;
				if (nSeq > 2)
					lenSeq = true;
			}
			lengthSeqVector.push_back (charsNumber);
			lengthTexts += charsNumber;
					
			//if (seq->qual.l) 
			//	printf("qual: %s\n", seq->qual.s);
		} //end-while
	
		#if  STORE_TITLE_FASTQ == 1
            		fclose(OutFile);
        	#endif
	
		kseq_destroy(seq);
		gzclose(fp);
	#endif
	
	
	#if verboseEncode==1
	   	dataTypeNChar sum=0;
		for (dataTypeNSeq m=0; m < lengthSeqVector.size(); m++) {
           		sum += lengthSeqVector[m];
       }
	#endif


	if (lenSeq == true)
		cerr << "The  (new and-or old) reads have a different length." << endl;
	else
        cerr << "The (new and-or old) reads have the same length." << endl;

	static FILE *OutFileLen;                  // output file of the end positions;
	OutFileLen = fopen(fileLen, "wb");
	if (OutFileLen==NULL) {
			std::cerr << "TransposeFasta::findLengthNseq: could not open file \"" << fileLen << "\"!"<< std::endl;
			exit (EXIT_FAILURE);
	}

	//Store vector in file
	dataTypeNSeq numchar = fwrite (&lengthSeqVector[0], sizeof(dataTypelenSeq), lengthSeqVector.size(), OutFileLen);
	
//	std::cerr << "lengthTexts= " << (lengthTexts) << " lengthRead= " << (int)lengthRead << std::endl;
//	std::cerr << "lengthSeqVector.size= " << (lengthSeqVector.size()) << " nSeq= " << nSeq << std::endl;
	assert (lengthSeqVector.size() == numchar);
	assert (lengthSeqVector.size() == nSeq);
	
	lengthSeqVector.clear();
	//lengthSeqVector.shrink_to_fit();
			
	#if ( (BUILD_BCR_FROM_BCRpartials==1) && (STORE_LENGTH_IN_FILE==1)  )   //We have to add the length of the sequences from BCR partial files
		char *fnOutLenOld = new char[BCRprefPrev.length()+100];
		sprintf(fnOutLenOld, "%s.len", BCRprefPrev.c_str());
		FILE* OutFileLenOld = fopen(fnOutLenOld, "rb");
		if (OutFileLenOld==NULL) {
			std::cerr << "TransposeFasta::findLengthNseq (BUILD_BCR_FROM_BCRpartials): Error " << fnOutLenOld << "\n";
			exit (EXIT_FAILURE);
		}
		numchar =0;
		dataTypeNChar p;
		numchar = fread (&p, sizeof(dataTypelenSeq), 1 , OutFileLenOld);
		if (numchar > 0) {
			fwrite (&p, sizeof(dataTypelenSeq), numchar , OutFileLen);
			nAddedTextEGSA_transp=1;
		}
		while (numchar != 0) {
			numchar = fread (&p, sizeof(dataTypelenSeq), 1 , OutFileLenOld);
			if (numchar > 0) {
				fwrite (&p, sizeof(dataTypelenSeq), numchar , OutFileLen);
				nAddedTextEGSA_transp++;
			}
		}
		delete [] fnOutLenOld;
		fclose(OutFileLenOld);
	#endif

	delete [] fileLen;
	fclose(OutFileLen);
	
	cerr << "Number of sequences reading/writing: " << nSeq << "\n";
	cerr << "Number of characters reading/writing: " << lengthTexts << "\n";
	
	return true;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
movq %rdx, %r12
movq %rsi, %r15
movq %rdi, %rbx
addq $0x18, %rdi
xorl %r14d, %r14d
movl $0x7f8, %edx            # imm = 0x7F8
xorl %esi, %esi
callq 0x3170
movq %r14, 0x810(%rbx)
movq $0x1, 0x138(%rbx)
movq 0x8(%r12), %rdi
addq $0xa, %rdi
callq 0x3040
movq (%r12), %rdx
leaq 0x4d93(%rip), %rsi      # 0xd078
movq %rax, 0x28(%rsp)
movq %rax, %rdi
xorl %eax, %eax
callq 0x3070
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
movq %r14, -0x8(%rax)
movb %r14b, (%rax)
movq %r14, 0x20(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rsp)
movb %r14b, 0xf(%rsp)
movb %r14b, (%rbx)
movl $0x0, 0x10(%rbx)
movq %r14, 0x8(%rbx)
movq (%r15), %rdi
leaq 0x4e10(%rip), %rsi      # 0xd13e
callq 0x3380
movq %rax, 0x30(%rsp)
movq %rax, %rdi
callq 0x8631
movq %rax, %r12
xorl %r14d, %r14d
leaq 0xf(%rsp), %rbp
movl $0x1, %r15d
movq %r12, %rdi
callq 0x868b
testl %eax, %eax
js 0x83bd
movl 0x10(%rbx), %r13d
incl %r13d
movl %r13d, 0x10(%rbx)
movq 0x28(%r12), %rdi
callq 0x3120
movb %al, 0xf(%rsp)
movzbl (%rbx), %ecx
movzbl %al, %edx
cmpl %ecx, %edx
je 0x8390
jbe 0x8384
movb %al, (%rbx)
cmpl $0x3, %r13d
movzbl %r14b, %r14d
cmovael %r15d, %r14d
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x83a5
movb %al, (%rsi)
incq 0x18(%rsp)
jmp 0x83b2
leaq 0x10(%rsp), %rdi
movq %rbp, %rdx
callq 0xa5bc
movzbl 0xf(%rsp), %eax
addq %rax, 0x8(%rbx)
jmp 0x8351
movq %r12, %rdi
callq 0x89a9
movq 0x30(%rsp), %rdi
callq 0x3420
leaq 0x4ca9(%rip), %rax      # 0xd07f
leaq 0x4cd7(%rip), %rsi      # 0xd0b4
testb $0x1, %r14b
cmovneq %rax, %rsi
movzbl %r14b, %eax
andl $0x1, %eax
leaq 0x30(,%rax,4), %rdx
movq 0x8bf5(%rip), %r15      # 0x10ff0
movq %r15, %rdi
callq 0x32d0
movq (%r15), %rax
addq -0x18(%rax), %r15
movq %r15, %rdi
movl $0xa, %esi
callq 0x3280
movsbl %al, %esi
movq 0x8bcf(%rip), %rdi      # 0x10ff0
callq 0x3050
movq %rax, %rdi
callq 0x31e0
leaq 0x46d4(%rip), %rsi      # 0xcb09
movq 0x28(%rsp), %r14
movq %r14, %rdi
callq 0x32e0
movq %rax, 0x8ed7(%rip)      # 0x11320
testq %rax, %rax
je 0x855c
movq 0x10(%rsp), %rdi
movq 0x18(%rsp), %rdx
subq %rdi, %rdx
movl $0x1, %esi
movq %rax, %rcx
callq 0x34c0
movq 0x10(%rsp), %rcx
movq 0x18(%rsp), %rdx
movq %rdx, %rsi
subq %rcx, %rsi
movl %eax, %eax
cmpq %rax, %rsi
jne 0x85c1
movl 0x10(%rbx), %eax
cmpq %rax, %rsi
jne 0x85e0
cmpq %rcx, %rdx
je 0x849d
movq %rcx, 0x18(%rsp)
movq %r14, %rdi
callq 0x3330
movq 0x8e74(%rip), %rdi      # 0x11320
callq 0x32a0
movq 0x8b38(%rip), %rdi      # 0x10ff0
leaq 0x4cff(%rip), %rsi      # 0xd1be
movl $0x25, %edx
callq 0x32d0
movl 0x10(%rbx), %esi
movq 0x8b1d(%rip), %rdi      # 0x10ff0
callq 0x31d0
leaq 0x4557(%rip), %rsi      # 0xca36
movl $0x1, %edx
movq %rax, %rdi
callq 0x32d0
movq 0x8afd(%rip), %rdi      # 0x10ff0
leaq 0x4cea(%rip), %rsi      # 0xd1e4
movl $0x26, %edx
callq 0x32d0
movq 0x8(%rbx), %rsi
movq 0x8ae1(%rip), %rdi      # 0x10ff0
callq 0x31d0
leaq 0x451b(%rip), %rsi      # 0xca36
movl $0x1, %edx
movq %rax, %rdi
callq 0x32d0
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x8537
callq 0x3260
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x854b
callq 0x3260
movb $0x1, %al
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8a8d(%rip), %rdi      # 0x10ff0
leaq 0x4b7b(%rip), %rsi      # 0xd0e5
movl $0x35, %edx
callq 0x32d0
movq 0x28(%rsp), %rbx
movq %rbx, %rdi
callq 0x3120
movq 0x8a68(%rip), %rdi      # 0x10ff0
movq %rbx, %rsi
movq %rax, %rdx
callq 0x32d0
movq 0x8a56(%rip), %rdi      # 0x10ff0
leaq 0x4b7a(%rip), %rsi      # 0xd11b
movl $0x2, %edx
callq 0x32d0
movq 0x8a3e(%rip), %rdi      # 0x10ff0
callq 0x3140
movl $0x1, %edi
callq 0x3300
leaq 0x4b56(%rip), %rdi      # 0xd11e
leaq 0x4b71(%rip), %rsi      # 0xd140
leaq 0x4b7d(%rip), %rcx      # 0xd153
movl $0xea, %edx
callq 0x31a0
leaq 0x4bb8(%rip), %rdi      # 0xd19f
leaq 0x4b52(%rip), %rsi      # 0xd140
leaq 0x4b5e(%rip), %rcx      # 0xd153
movl $0xeb, %edx
callq 0x31a0
jmp 0x8603
jmp 0x8603
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x8615
callq 0x3260
movq 0x38(%rsp), %rdi
leaq 0x48(%rsp), %rax
cmpq %rax, %rdi
je 0x8629
callq 0x3260
movq %rbx, %rdi
callq 0x3460