add: function checkaligned, format the arg, clean code

This commit is contained in:
kuoi 2023-03-20 14:45:56 +08:00
parent 5a337fd47a
commit b038025948
2 changed files with 329 additions and 271 deletions

47
README.md Normal file
View file

@ -0,0 +1,47 @@
# MiMi
```
l、 . . . .
(゚、 。 |\/|*|\/|*
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.

View file

@ -1,8 +1,8 @@
#include <iostream> #include <algorithm>
#include <fstream> #include <fstream>
#include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <algorithm>
using namespace std; using namespace std;
@ -10,18 +10,15 @@ class Basic_arg{
public: public:
int intype = 0, outype = 0; int intype = 0, outype = 0;
char *itn, *otn; char *itn, *otn;
Basic_arg(int intype, int outype, char* itn, char *otn): Basic_arg(int intype, int outype, char* itn, char* otn)
intype(intype), outype(outype), : intype(intype), outype(outype), itn(itn), otn(otn){};
itn(itn), otn(otn)
{};
}; };
class Sample { class Sample {
public: public:
unsigned ntax, nchar; unsigned ntax, nchar;
string *taxas, *chars; string *taxas, *chars;
Sample(unsigned ntax, unsigned nchar): Sample(unsigned ntax, unsigned nchar) : ntax(ntax), nchar(nchar) {
ntax(ntax), nchar(nchar) {
taxas = new string[ntax]; taxas = new string[ntax];
chars = new string[ntax]; chars = new string[ntax];
}; };
@ -55,16 +52,19 @@ Sample readPhy(char* itn){
string sntax, snseq, snall; string sntax, snseq, snall;
getline(matrixfile, snall); getline(matrixfile, snall);
istringstream istr(snall); istringstream istr(snall);
istr >> sntax; istr >> snseq; istr >> sntax;
istr >> snseq;
int ntax, nchar; int ntax, nchar;
ntax = stoi (sntax); nchar = stoi (snseq); // string to int ntax = stoi(sntax);
nchar = stoi(snseq); // string to int
Sample sam(ntax, nchar); Sample sam(ntax, nchar);
// read sequence // read sequence
int lennum; int lennum;
for (lennum = 0; lennum < sam.ntax; lennum++) { for (lennum = 0; lennum < sam.ntax; lennum++) {
getline(matrixfile, snall); getline(matrixfile, snall);
istringstream istr(snall); istringstream istr(snall);
istr >> sam.taxas[lennum]; istr >> sam.chars[lennum]; istr >> sam.taxas[lennum];
istr >> sam.chars[lennum];
} }
matrixfile.close(); matrixfile.close();
// return to class // return to class
@ -136,7 +136,8 @@ Sample readTnt(char* itn){
for (int i = 0; i < 1;) { for (int i = 0; i < 1;) {
getline(matrixfile, stri); getline(matrixfile, stri);
istringstream istr(stri); istringstream istr(stri);
istr >> snchar; istr >> sntax; istr >> snchar;
istr >> sntax;
if (isNum(sntax) && isNum(snchar)) { if (isNum(sntax) && isNum(snchar)) {
ntax = stoi(sntax); ntax = stoi(sntax);
nchar = stoi(snchar); nchar = stoi(snchar);
@ -150,7 +151,8 @@ Sample readTnt(char* itn){
for (lennum = 0; lennum < sam.ntax; lennum++) { for (lennum = 0; lennum < sam.ntax; lennum++) {
getline(matrixfile, stri); getline(matrixfile, stri);
istringstream istr(stri); istringstream istr(stri);
istr >> sam.taxas[lennum]; istr >> sam.chars[lennum]; istr >> sam.taxas[lennum];
istr >> sam.chars[lennum];
} }
matrixfile.close(); matrixfile.close();
return sam; return sam;
@ -169,7 +171,8 @@ Sample readNex(char* itn){
matrixfile.open(itn); matrixfile.open(itn);
// some tem // some tem
string snall, stri, str_a, str_b; 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 = '='; char x = '=';
int lnum, eulnum; int lnum, eulnum;
// getline line by line // getline line by line
@ -214,7 +217,8 @@ Sample readNex(char* itn){
// create class // create class
Sample sam(ntax, nchar); Sample sam(ntax, nchar);
// some temp, z is line number, l is the string arrary number // 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 // read line by line
while (getline(matrixfile, snall)) { while (getline(matrixfile, snall)) {
// convert to word // convert to word
@ -270,7 +274,11 @@ void writeNex(class Sample sam, char* otn){
string datatype; string datatype;
datatype = checktype(sam.chars[0]); datatype = checktype(sam.chars[0]);
matrixfile << "#NEXUS" << endl; matrixfile << "#NEXUS" << endl;
matrixfile << "Begin data;" << endl << "\tDimensions nchar=" << sam.nchar << " ntax=" << sam.ntax << ";" << endl << "\tFormat datatype=" << datatype << " missing=? gap=-;" << endl << "\tMatrix" << endl; matrixfile << "Begin data;" << endl
<< "\tDimensions nchar=" << sam.nchar << " ntax=" << sam.ntax
<< ";" << endl
<< "\tFormat datatype=" << datatype << " missing=? gap=-;" << endl
<< "\tMatrix" << endl;
for (int i2 = 0; i2 < sam.ntax; i2++) { for (int i2 = 0; i2 < sam.ntax; i2++) {
matrixfile << "\t\t" << sam.taxas[i2] << "\t" << sam.chars[i2] << endl; matrixfile << "\t\t" << sam.taxas[i2] << "\t" << sam.chars[i2] << endl;
} }
@ -281,7 +289,8 @@ void writeNex(class Sample sam, char* otn){
string checktype(string str) { string checktype(string str) {
// some var // some var
float a, c, t, g, zero, one, two, dna, standard; float a, c, t, g, zero, one, two, dna, standard;
char ca='a', cc='c', ct='t', cg='g', czero='0', cone='1', ctwo='2'; char ca = 'a', cc = 'c', ct = 't', cg = 'g', czero = '0', cone = '1',
ctwo = '2';
string datatype; string datatype;
// count fre // count fre
a = countfre(str, ca); a = countfre(str, ca);
@ -323,7 +332,6 @@ void writeTnt(class Sample sam, char* otn){
matrixfile.close(); matrixfile.close();
} }
Basic_arg procargs(int nargs, char** arg, char* itn, char* otn) { Basic_arg procargs(int nargs, char** arg, char* itn, char* otn) {
int i, sta = 0, intype = 0, outype = 0; int i, sta = 0, intype = 0, outype = 0;
string para, inputfile, outputfile; string para, inputfile, outputfile;
@ -340,15 +348,13 @@ Basic_arg procargs (int nargs, char ** arg, char* itn, char* otn){
if (para == "-h" | para == "--help") { if (para == "-h" | para == "--help") {
show_help(1); show_help(1);
sta = 2; sta = 2;
} } else if (para == "-i" | para == "--input") {
else if ( para =="-i"| para=="--input"){
i++; i++;
itn = arg[i]; itn = arg[i];
string inputfile(arg[i]); string inputfile(arg[i]);
intype = checkextension(inputfile); intype = checkextension(inputfile);
sta++; sta++;
} } else if (para == "-o" | para == "--output") {
else if ( para =="-o"| para=="--output"){
i++; i++;
otn = arg[i]; otn = arg[i];
string outputfile(arg[i]); string outputfile(arg[i]);
@ -394,16 +400,22 @@ int checkextension(string str){
} }
void show_help(int help_num) { void show_help(int help_num) {
if (help_num == 0) { if (help_num == 0) {
cout << "MiMi, GPL, Guoyi Zhang, 2023.\nPlease use -h to see more help" << endl; cout << "MiMi, GPL, Guoyi Zhang, 2023.\nPlease use -h to see more help"
<< endl;
} else { } else {
cout << "\n l、 \t. . . .\n(゚、 。 \t|\\/|*|\\/|*\n l ~ヽ \t| ||| ||\n じしf_,)\t| ||| ||\n" << endl; cout << "\n l、 \t. . . .\n(゚、 。 \t|\\/|*|\\/|*\n l ~ヽ "
cout << "Morphology into Molecules into\n" << "GPL\tGuoyi\tZhang,\t2023\n" << endl; " \t| ||| ||\n じしf_,)\t| ||| ||\n"
cout << "-h\t--help;\n-i\t--input\t\t${filename};\n-o\t--output\t${filename};\n" << endl; << endl;
cout << "Accepted format:\nfas\tfasta;\nnex\tnexus\nphy\tphylip\ntnt\tss" << endl; cout << "Morphology into Molecules into\n"
<< "GPL;\tGuoyi Zhang;\t2023\n"
<< endl;
cout << "-h\t--help;\n-i\t--input\t\t${filename};\n-o\t--output\t${"
"filename};\n"
<< endl;
cout
<< "Accepted formats:\nfas\tfasta;\nnex\tnexus;\nphy\tphylip;\ntnt\tss;"
<< endl;
} }
} }
@ -453,7 +465,6 @@ bool checkalign(class Sample sam){
return aligned; return aligned;
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
char *itn, *otn; char *itn, *otn;
Basic_arg arguvar = procargs(argc, argv, itn, otn); Basic_arg arguvar = procargs(argc, argv, itn, otn);