diff --git a/README.md b/README.md new file mode 100644 index 0000000..0b37b0b --- /dev/null +++ b/README.md @@ -0,0 +1,47 @@ +# MiMi + +``` + /l、 . . . . +(゚、 。 7 |\/|*|\/|* + l ~ヽ | ||| || + じしf_,)ノ | ||| || + +Morphology into Molecules into +GPL Guoyi Zhang, 2023 +``` + +## Functions + +Convert any formats listed in `Accepted formats` to each other. + +## Arguments + +- -h --help; + +- -i --input ${filename}; + +- -o --output ${filename}; + +## Accepted formats + +- fas fasta + +- nex nexus + +- phy phylip + +- tnt ss + +## Compile + +``` +g++ main.cpp -o mimi +``` + +## License + +You should note that the license is (GPL)[https://www.gnu.org/licenses/gpl-3.0.html]. + +## Acknowledgements + +Thanks to my beloved daughter MiMi (Ragdoll cat) for accompanying me in my life, mom loves you forever. diff --git a/main.cpp b/main.cpp index 81ce952..caa6ac5 100644 --- a/main.cpp +++ b/main.cpp @@ -1,33 +1,30 @@ -#include -#include -#include -#include #include +#include +#include +#include +#include using namespace std; -class Basic_arg{ -public: - int intype=0, outype=0; - char *itn, *otn; - Basic_arg(int intype, int outype, char* itn, char *otn): - intype(intype), outype(outype), - itn(itn), otn(otn) - {}; +class Basic_arg { + public: + int intype = 0, outype = 0; + char *itn, *otn; + Basic_arg(int intype, int outype, char* itn, char* otn) + : intype(intype), outype(outype), itn(itn), otn(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]; - }; + public: + unsigned ntax, nchar; + string *taxas, *chars; + Sample(unsigned ntax, unsigned nchar) : ntax(ntax), nchar(nchar) { + taxas = new string[ntax]; + chars = new string[ntax]; + }; }; -Basic_arg procargs (int nargs, char ** arg, char* itn, char* otn); +Basic_arg procargs(int nargs, char** arg, char* itn, char* otn); Sample read_input(char* itn, int intype); void show_help(int help_num); Sample readFas(char* itn); @@ -46,54 +43,57 @@ string add_space(char x, string str_old); string checktype(string str); int countfre(string str, char c); int checkextension(string str); - -Sample readPhy(char* itn){ - //read file + +Sample readPhy(char* itn) { + // read file ifstream matrixfile; matrixfile.open(itn); - //read number of taxa and charcter + // read number of taxa and charcter string sntax, snseq, snall; - getline(matrixfile,snall); - istringstream istr(snall); - istr >> sntax; istr >> snseq; + getline(matrixfile, snall); + istringstream istr(snall); + istr >> sntax; + istr >> snseq; int ntax, nchar; - ntax = stoi (sntax); nchar = stoi (snseq); // string to int - Sample sam(ntax, nchar); - //read sequence + ntax = stoi(sntax); + nchar = stoi(snseq); // string to int + Sample sam(ntax, nchar); + // read sequence int lennum; - for(lennum=0;lennum> sam.taxas[lennum]; istr >> sam.chars[lennum]; + istr >> sam.taxas[lennum]; + istr >> sam.chars[lennum]; } matrixfile.close(); // return to class return sam; } -Sample readFas(char* itn){ +Sample readFas(char* itn) { int ntax, nchar, lnum; ifstream matrixfile; matrixfile.open(itn); // check line number and taxa number ntax = 0; string temln; - for(lnum=0;getline(matrixfile,temln);lnum++){ - if(temln[0]=='>'){ + for (lnum = 0; getline(matrixfile, temln); lnum++) { + if (temln[0] == '>') { ntax++; } } matrixfile.clear(); matrixfile.seekg(0); // check the nchar - 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; - } + if (i > 0) { + *str_b = *str_b + *str_a; + } } nchar = (*str_b).length(); delete str_a; @@ -105,183 +105,192 @@ Sample readFas(char* itn){ // create class Sample sam(ntax, nchar); // get class - string *str_c = new string; - for (int a=1, b=0;a<=lnum;a++){ - if(a%r==1){ - getline(matrixfile,sam.taxas[b]); - sam.taxas[b].erase(0,1); + string* str_c = new string; + for (int a = 1, b = 0; a <= lnum; a++) { + if (a % r == 1) { + getline(matrixfile, sam.taxas[b]); + sam.taxas[b].erase(0, 1); } - if(a%r>1){ - getline(matrixfile,*str_c); + if (a % r > 1) { + getline(matrixfile, *str_c); sam.chars[b] = sam.chars[b] + *str_c; } - if(a%r==0){ - getline(matrixfile,*str_c); + if (a % r == 0) { + getline(matrixfile, *str_c); sam.chars[b] = sam.chars[b] + *str_c; b++; - } + } } delete str_c; - str_c = nullptr; + str_c = nullptr; matrixfile.close(); return sam; } -Sample readTnt(char* itn){ +Sample readTnt(char* itn) { int ntax, nchar; ifstream matrixfile; matrixfile.open(itn); // get nchar and ntax string stri, sntax, snchar; - for(int i=0;i<1;){ - getline(matrixfile,stri); - istringstream istr(stri); - istr >> snchar; istr >> sntax; - if(isNum(sntax) && isNum(snchar)){ - ntax=stoi(sntax); - nchar=stoi(snchar); - i++; - } + for (int i = 0; i < 1;) { + getline(matrixfile, stri); + istringstream istr(stri); + istr >> snchar; + istr >> sntax; + if (isNum(sntax) && isNum(snchar)) { + ntax = stoi(sntax); + nchar = stoi(snchar); + i++; + } } // create class - Sample sam(ntax,nchar); + Sample sam(ntax, nchar); // get class int lennum; - for(lennum=0;lennum> sam.taxas[lennum]; istr >> sam.chars[lennum]; + istr >> sam.taxas[lennum]; + istr >> sam.chars[lennum]; } matrixfile.close(); return sam; } bool isNum(string strnum) { - char* p; - strtol(strnum.c_str(), &p, 10); - return *p == 0; + char* p; + strtol(strnum.c_str(), &p, 10); + return *p == 0; } -Sample readNex(char* itn){ +Sample readNex(char* itn) { int ntax, nchar; - // open file + // open file ifstream matrixfile; matrixfile.open(itn); - // some tem + // some tem string snall, stri, str_a, str_b; - bool found = false, found_ntax = false, found_nchar = false, found_equal = false; + bool found = false, found_ntax = false, found_nchar = false, + found_equal = false; char x = '='; int lnum, eulnum; // getline line by line - for(lnum=0;getline(matrixfile,snall);lnum++){ - str_a = to_lower(snall); - str_b = add_space(x,str_a); - istringstream istr(str_b); - // convert to words - while(istr>> stri){ - if(stri=="dimensions"){ - found = true; - } else if(stri=="ntax"){ - found_ntax = true; - } else if(stri=="nchar"){ - found_nchar = true; - } else if(stri=="="){ - found_equal = true; - } else if (found_ntax&&found_equal){ - if(stri.back()==';'){ - stri.pop_back(); - found = false; - } - ntax = stoi(stri); - found_equal = false; - found_ntax = false; - } else if (found_nchar&&found_equal){ - if(stri.back()==';'){ - stri.pop_back(); - found = false; - } - nchar = stoi(stri); - found_equal = false; - found_nchar = false; - } else if (stri=="matrix"){ - eulnum = lnum+1; - } + for (lnum = 0; getline(matrixfile, snall); lnum++) { + str_a = to_lower(snall); + str_b = add_space(x, str_a); + istringstream istr(str_b); + // convert to words + while (istr >> stri) { + if (stri == "dimensions") { + found = true; + } else if (stri == "ntax") { + found_ntax = true; + } else if (stri == "nchar") { + found_nchar = true; + } else if (stri == "=") { + found_equal = true; + } else if (found_ntax && found_equal) { + if (stri.back() == ';') { + stri.pop_back(); + found = false; + } + ntax = stoi(stri); + found_equal = false; + found_ntax = false; + } else if (found_nchar && found_equal) { + if (stri.back() == ';') { + stri.pop_back(); + found = false; + } + nchar = stoi(stri); + found_equal = false; + found_nchar = false; + } else if (stri == "matrix") { + eulnum = lnum + 1; + } } } // go back matrixfile.clear(); matrixfile.seekg(0); // create class - Sample sam(ntax,nchar); + Sample sam(ntax, nchar); // some temp, z is line number, l is the string arrary number - int z=0; int l=0; + int z = 0; + int l = 0; // read line by line - while(getline(matrixfile,snall)){ - // convert to word - istringstream istr(snall); - // limit the read line number - if(z>(eulnum-1)&&z<(eulnum+sam.ntax)){ - istr >> sam.taxas[l]; - istr >> sam.chars[l]; - l++; - } - z++; + while (getline(matrixfile, snall)) { + // convert to word + istringstream istr(snall); + // limit the read line number + if (z > (eulnum - 1) && z < (eulnum + sam.ntax)) { + istr >> sam.taxas[l]; + istr >> sam.chars[l]; + l++; + } + z++; } return sam; } -string add_space (char x, string str_old) { - int i; - string str_new; - for (i=0; i" << sam.taxas[i] << endl; - matrixfile << sam.chars[i] << endl; - } + for (int i = 0; i < sam.ntax; i++) { + matrixfile << ">" << sam.taxas[i] << endl; + matrixfile << sam.chars[i] << endl; + } matrixfile.close(); } -void writePhy(class Sample sam, char* otn){ +void writePhy(class Sample sam, char* otn) { ofstream matrixfile(otn); - matrixfile << sam.ntax << " " << sam.nchar << endl; - for(int i=0;i0.7){ - datatype = "dna"; - } else if ((standard/str.length())>0.7){ - datatype = "standard"; + if ((dna / str.length()) > 0.7) { + datatype = "dna"; + } else if ((standard / str.length()) > 0.7) { + datatype = "standard"; } else { - datatype = "protein"; + datatype = "protein"; } return datatype; } -int countfre(string str, char c){ +int countfre(string str, char c) { int num; str = to_lower(str); - num = count(str.begin(),str.end(),c); - return num; + num = count(str.begin(), str.end(), c); + return num; } -void writeTnt(class Sample sam, char* otn){ +void writeTnt(class Sample sam, char* otn) { ofstream matrixfile(otn); - matrixfile << "xread" << endl << "\' \'" << endl; - matrixfile << sam.nchar << " " << sam.ntax << endl; - for(int i=0;i