From b0e35fa3d188181b73572717626927f5b56ccabf Mon Sep 17 00:00:00 2001 From: Kuoi Date: Sun, 19 Mar 2023 02:17:39 +0800 Subject: [PATCH] add more function --- main.cpp | 216 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 185 insertions(+), 31 deletions(-) diff --git a/main.cpp b/main.cpp index 3afbb22..6778275 100644 --- a/main.cpp +++ b/main.cpp @@ -1,21 +1,46 @@ #include #include -#include +#include #include #include #include using namespace std; -int intype=0, outype=0, ntax, nseq; -char * fn; +int intype=0, outype=0; +char *fn; +char *otn; + +class Sample { +public: + unsigned ntax, nchar; + string *taxas, *chars; + Sample(unsigned ntax, unsigned nchar): + ntax(ntax), nchar(nchar) { + taxas = new string[ntax]; + chars = new string[ntax]; + }; +// ~Sample(){ +// delete[] taxas; +// delete[] chars; +// } +}; void show_help(int help_num); -void read_input(void); +Sample read_input(); +Sample readFas(); +Sample readPhy(); +void write_output(class Sample sam); +void writeFas(class Sample sam); +void writePhy(class Sample sam); +void writeTnt(class Sample sam); +void writeNexus(class Sample sam); -void readPhy(){ +Sample readPhy(){ +// unsigned ntax, nchar; +// string *taxas, *chars; +// Sample sam(ntax, nchar); int num=0; - cout << "readphy is working, filename is " << fn << endl; //read file ifstream seqfile; seqfile.open(fn); @@ -25,31 +50,154 @@ void readPhy(){ getline(seqfile,snall); istringstream istr(snall); istr >> sntax; istr >> snseq; - ntax = stoi (sntax); nseq = stoi (snseq); // string to int - cout << "ntax=" << ntax << " nseq=" << nseq << endl; + int ntax, nchar; + ntax = stoi (sntax); nchar = stoi (snseq); // string to int + cout << "ntax=" << ntax << " nseq=" << nchar << endl; + Sample sam(ntax, nchar); //read sequence - string taxa[ntax], sequ[ntax]; - //taxa[ntax] = new string*[]; int lennum; - for(lennum=0;lennum> taxa[lennum]; istr >> sequ[lennum]; - cout << "tax" << lennum << " is " << taxa << "; seq" << lennum << " is " << sequ << endl; + istr >> sam.taxas[lennum]; istr >> sam.chars[lennum]; + cout << "tax" << lennum << " is " << sam.taxas[lennum] << "; seq" << lennum << " is " << sam.chars[lennum] << endl; } seqfile.close(); -// return taxa; -// return sequ; + // return to class + return sam; } -void writeFas(){ - +Sample readFas(){ + int ntax, nchar, lnum; + ifstream seqfile; + seqfile.open(fn); + ntax = 0; + string temln; + for(lnum=0;getline(seqfile,temln);lnum++){ + if(temln[0]=='>'){ + ntax++; + } + } + cout << "lnum= " << lnum << ", ntax= " << ntax << endl; + seqfile.clear(); + seqfile.seekg(0); + + string *str_a = new string; + string *str_b = new string; + int r = lnum/ntax; + for (int i=0; i0){ + *str_b = *str_b + *str_a; + } + } + nchar = (*str_b).length(); + delete str_a; + str_a = nullptr; + delete str_b; + str_b = nullptr; + + cout << "ntax= " << ntax << ", nchar= " << nchar << endl; + seqfile.clear(); + seqfile.seekg(0); + + Sample sam(ntax, nchar); + string *str_c = new string; + for (int a=1, b=0;a<=lnum;a++){ + if(a%r==1){ + getline(seqfile,sam.taxas[b]); + sam.taxas[b].erase(0,1); + } + if(a%r>1){ + getline(seqfile,*str_c); + sam.chars[b] = sam.chars[b] + *str_c; + } + if(a%r==0){ + getline(seqfile,*str_c); + sam.chars[b] = sam.chars[b] + *str_c; + b++; + } + } + delete str_c; + str_c = nullptr; + seqfile.close(); + int lennum; + for(lennum=0;lennum" << sam.taxas[i] << endl; + outputFile << sam.chars[i] << endl; + } + } else { + cout << "File can't be written" << endl; + } + outputFile.close(); +} + +void writePhy(class Sample sam){ + ofstream outputFile(otn); + if (outputFile.is_open()) { + outputFile << sam.ntax << " " << sam.nchar << endl; + for(int i=0;i