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 <iostream>
#include <sstream>
#include <string>
#include <algorithm>
using namespace std;
@ -10,18 +10,15 @@ 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)
{};
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) {
Sample(unsigned ntax, unsigned nchar) : ntax(ntax), nchar(nchar) {
taxas = new string[ntax];
chars = new string[ntax];
};
@ -55,16 +52,19 @@ Sample readPhy(char* itn){
string sntax, snseq, snall;
getline(matrixfile, snall);
istringstream istr(snall);
istr >> sntax; istr >> snseq;
istr >> sntax;
istr >> snseq;
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);
// read sequence
int lennum;
for (lennum = 0; lennum < sam.ntax; lennum++) {
getline(matrixfile, snall);
istringstream istr(snall);
istr >> sam.taxas[lennum]; istr >> sam.chars[lennum];
istr >> sam.taxas[lennum];
istr >> sam.chars[lennum];
}
matrixfile.close();
// return to class
@ -136,7 +136,8 @@ Sample readTnt(char* itn){
for (int i = 0; i < 1;) {
getline(matrixfile, stri);
istringstream istr(stri);
istr >> snchar; istr >> sntax;
istr >> snchar;
istr >> sntax;
if (isNum(sntax) && isNum(snchar)) {
ntax = stoi(sntax);
nchar = stoi(snchar);
@ -150,7 +151,8 @@ Sample readTnt(char* itn){
for (lennum = 0; lennum < sam.ntax; lennum++) {
getline(matrixfile, stri);
istringstream istr(stri);
istr >> sam.taxas[lennum]; istr >> sam.chars[lennum];
istr >> sam.taxas[lennum];
istr >> sam.chars[lennum];
}
matrixfile.close();
return sam;
@ -169,7 +171,8 @@ Sample readNex(char* itn){
matrixfile.open(itn);
// 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
@ -214,7 +217,8 @@ Sample readNex(char* itn){
// create class
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
@ -270,7 +274,11 @@ void writeNex(class Sample sam, char* otn){
string datatype;
datatype = checktype(sam.chars[0]);
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++) {
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) {
// some var
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;
// count fre
a = countfre(str, ca);
@ -323,7 +332,6 @@ void writeTnt(class Sample sam, char* otn){
matrixfile.close();
}
Basic_arg procargs(int nargs, char** arg, char* itn, char* otn) {
int i, sta = 0, intype = 0, outype = 0;
string para, inputfile, outputfile;
@ -340,15 +348,13 @@ Basic_arg procargs (int nargs, char ** arg, char* itn, char* otn){
if (para == "-h" | para == "--help") {
show_help(1);
sta = 2;
}
else if ( para =="-i"| para=="--input"){
} else if (para == "-i" | para == "--input") {
i++;
itn = arg[i];
string inputfile(arg[i]);
intype = checkextension(inputfile);
sta++;
}
else if ( para =="-o"| para=="--output"){
} else if (para == "-o" | para == "--output") {
i++;
otn = arg[i];
string outputfile(arg[i]);
@ -394,16 +400,22 @@ int checkextension(string str){
}
void show_help(int help_num) {
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 {
cout << "\n l、 \t. . . .\n(゚、 。 \t|\\/|*|\\/|*\n l ~ヽ \t| ||| ||\n じしf_,)\t| ||| ||\n" << endl;
cout << "Morphology into Molecules into\n" << "GPL\tGuoyi\tZhang,\t2023\n" << endl;
cout << "-h\t--help;\n-i\t--input\t\t${filename};\n-o\t--output\t${filename};\n" << endl;
cout << "Accepted format:\nfas\tfasta;\nnex\tnexus\nphy\tphylip\ntnt\tss" << endl;
cout << "\n l、 \t. . . .\n(゚、 。 \t|\\/|*|\\/|*\n l ~ヽ "
" \t| ||| ||\n じしf_,)\t| ||| ||\n"
<< 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;
}
int main(int argc, char** argv) {
char *itn, *otn;
Basic_arg arguvar = procargs(argc, argv, itn, otn);