Compare commits

...

7 commits

Author SHA1 Message Date
247224a3e4 fix: tnt format 2023-10-11 21:49:54 +08:00
bbbf71374c add: tcl GUI 2023-04-06 04:27:09 +08:00
1051374b13 fix: format style 2023-03-21 01:36:27 +08:00
d81bfbd8d9 fix: fasta space issue; polish: readfas, readnex 2023-03-21 01:19:39 +08:00
c32f318f85 polish: get rid of extra new 2023-03-20 21:40:40 +08:00
0cbec45015 polish: rm extra endl 2023-03-20 19:36:26 +08:00
26e358ffef fix: Wall warning, OR symbol 2023-03-20 18:34:38 +08:00
3 changed files with 240 additions and 110 deletions

BIN
logo.gif Normal file

Binary file not shown.

After

(image error) Size: 367 KiB

216
main.cpp
View file

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector>
using namespace std; using namespace std;
@ -24,7 +25,7 @@ class Sample {
}; };
}; };
Basic_arg procargs(int nargs, char** arg, char* itn, char* otn); Basic_arg procargs(int nargs, char** arg);
Sample read_input(char* itn, int intype); Sample read_input(char* itn, int intype);
void show_help(int help_num); void show_help(int help_num);
Sample readFas(char* itn); Sample readFas(char* itn);
@ -40,6 +41,8 @@ bool isNum(string strnum);
bool checkalign(class Sample sam); bool checkalign(class Sample sam);
string to_lower(string stri); string to_lower(string stri);
string add_space(char x, string str_old); string add_space(char x, string str_old);
string rep_space(string str_old);
string del_space(string str_old);
string checktype(string str); string checktype(string str);
int countfre(string str, char c); int countfre(string str, char c);
int checkextension(string str); int checkextension(string str);
@ -59,7 +62,7 @@ Sample readPhy(char* itn) {
nchar = stoi(snseq); // string to int nchar = stoi(snseq); // string to int
Sample sam(ntax, nchar); Sample sam(ntax, nchar);
// read sequence // read sequence
int lennum; unsigned 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);
@ -75,55 +78,53 @@ Sample readFas(char* itn) {
int ntax, nchar, lnum; int ntax, nchar, lnum;
ifstream matrixfile; ifstream matrixfile;
matrixfile.open(itn); matrixfile.open(itn);
// use vector to read once use serveral times
vector<string> file_content;
// check line number and taxa number // check line number and taxa number
ntax = 0; ntax = 0;
string temln; string temln;
for (lnum = 0; getline(matrixfile, temln); lnum++) { for (lnum = 0; getline(matrixfile, temln); lnum++) {
file_content.push_back(temln);
if (temln[0] == '>') { if (temln[0] == '>') {
ntax++; ntax++;
} }
} }
matrixfile.clear(); matrixfile.close();
matrixfile.seekg(0);
// check the nchar // check the nchar
string* str_a = new string; string str_i;
string* str_b = new string; int r;
int r = lnum / ntax; if (file_content.size() != 0) {
r = file_content.size() / ntax;
} else {
cout << "MiMi:\tInput file contains 0 line" << endl;
exit(0);
}
for (int i = 0; i < r; i++) { for (int i = 0; i < r; i++) {
getline(matrixfile, *str_a);
if (i > 0) { if (i > 0) {
*str_b = *str_b + *str_a; file_content[i] = del_space(file_content[i]);
str_i = str_i + file_content[i];
} }
} }
nchar = (*str_b).length(); nchar = str_i.length();
delete str_a;
str_a = nullptr;
delete str_b;
str_b = nullptr;
matrixfile.clear();
matrixfile.seekg(0);
// create class // create class
Sample sam(ntax, nchar); Sample sam(ntax, nchar);
// get class // get class
string* str_c = new string; for (int a = 0, b = 0; a < (int)file_content.size(); a++) {
for (int a = 1, b = 0; a <= lnum; a++) { if ((a + 1) % r == 1) {
if (a % r == 1) { sam.taxas[b] = file_content[a];
getline(matrixfile, sam.taxas[b]);
sam.taxas[b].erase(0, 1); sam.taxas[b].erase(0, 1);
sam.taxas[b] = rep_space(sam.taxas[b]);
} }
if (a % r > 1) { if ((a + 1) % r > 1) {
getline(matrixfile, *str_c); file_content[a] = del_space(file_content[a]);
sam.chars[b] = sam.chars[b] + *str_c; sam.chars[b] = sam.chars[b] + file_content[a];
} }
if (a % r == 0) { if ((a + 1) % r == 0) {
getline(matrixfile, *str_c); file_content[a] = del_space(file_content[a]);
sam.chars[b] = sam.chars[b] + *str_c; sam.chars[b] = sam.chars[b] + file_content[a];
b++; b++;
} }
} }
delete str_c;
str_c = nullptr;
matrixfile.close();
return sam; return sam;
} }
@ -147,7 +148,7 @@ Sample readTnt(char* itn) {
// create class // create class
Sample sam(ntax, nchar); Sample sam(ntax, nchar);
// get class // get class
int lennum; unsigned int lennum;
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);
@ -169,22 +170,26 @@ Sample readNex(char* itn) {
// open file // open file
ifstream matrixfile; ifstream matrixfile;
matrixfile.open(itn); matrixfile.open(itn);
// some tem // some var
string snall, stri, str_a, str_b; string snall, stri, str_a, str_b;
bool found = false, found_ntax = false, found_nchar = false, bool found_ntax = false, found_nchar = false, found_equal = false;
found_equal = false;
char x = '='; char x = '=';
int lnum, eulnum; // get line number and read line to vector
int lnum;
unsigned int eulnum;
vector<string> file_content;
while (getline(matrixfile, snall)) {
file_content.push_back(snall);
}
matrixfile.close();
// getline line by line // getline line by line
for (lnum = 0; getline(matrixfile, snall); lnum++) { for (lnum = 0; lnum < (int)file_content.size(); lnum++) {
str_a = to_lower(snall); str_a = to_lower(file_content[lnum]);
str_b = add_space(x, str_a); str_b = add_space(x, str_a);
istringstream istr(str_b); istringstream istr(str_b);
// convert to words // convert to words
while (istr >> stri) { while (istr >> stri) {
if (stri == "dimensions") { if (stri == "ntax") {
found = true;
} else if (stri == "ntax") {
found_ntax = true; found_ntax = true;
} else if (stri == "nchar") { } else if (stri == "nchar") {
found_nchar = true; found_nchar = true;
@ -193,7 +198,6 @@ Sample readNex(char* itn) {
} else if (found_ntax && found_equal) { } else if (found_ntax && found_equal) {
if (stri.back() == ';') { if (stri.back() == ';') {
stri.pop_back(); stri.pop_back();
found = false;
} }
ntax = stoi(stri); ntax = stoi(stri);
found_equal = false; found_equal = false;
@ -201,7 +205,6 @@ Sample readNex(char* itn) {
} else if (found_nchar && found_equal) { } else if (found_nchar && found_equal) {
if (stri.back() == ';') { if (stri.back() == ';') {
stri.pop_back(); stri.pop_back();
found = false;
} }
nchar = stoi(stri); nchar = stoi(stri);
found_equal = false; found_equal = false;
@ -211,32 +214,24 @@ Sample readNex(char* itn) {
} }
} }
} }
// go back
matrixfile.clear();
matrixfile.seekg(0);
// create class // create class
Sample sam(ntax, nchar); Sample sam(ntax, nchar);
// some temp, z is line number, l is the string arrary number // read line by line, limit line number
int z = 0;
int l = 0; int l = 0;
// read line by line for (unsigned int z = eulnum; z < (eulnum + sam.ntax); z++) {
while (getline(matrixfile, snall)) {
// convert to word // convert to word
istringstream istr(snall); istringstream istr(file_content[z]);
// limit the read line number
if (z > (eulnum - 1) && z < (eulnum + sam.ntax)) {
istr >> sam.taxas[l]; istr >> sam.taxas[l];
istr >> sam.chars[l]; istr >> sam.chars[l];
l++; l++;
} }
z++;
}
return sam; return sam;
} }
string add_space(char x, string str_old) { string add_space(char x, string str_old) {
int i; int i;
string str_new; string str_new;
for (i = 0; i < str_old.length(); i++) { for (i = 0; i < (int)str_old.length(); i++) {
if (str_old[i] != x) { if (str_old[i] != x) {
str_new = str_new + str_old[i]; str_new = str_new + str_old[i];
} else { } else {
@ -246,6 +241,32 @@ string add_space(char x, string str_old) {
return str_new; return str_new;
} }
string del_space(string str_old) {
int i;
string str_new;
char x = ' ';
for (i = 0; i < (int)str_old.length(); i++) {
if (str_old[i] != x) {
str_new = str_new + str_old[i];
}
}
return str_new;
}
string rep_space(string str_old) {
int i;
string str_new;
char x = ' ', c = '_';
for (i = 0; i < (int)str_old.length(); i++) {
if (str_old[i] != x) {
str_new = str_new + str_old[i];
} else {
str_new = str_new + c;
}
}
return str_new;
}
string to_lower(string stri) { string to_lower(string stri) {
transform(stri.begin(), stri.end(), stri.begin(), ::tolower); transform(stri.begin(), stri.end(), stri.begin(), ::tolower);
return stri; return stri;
@ -253,7 +274,7 @@ string to_lower(string stri) {
void writeFas(class Sample sam, char* otn) { void writeFas(class Sample sam, char* otn) {
ofstream matrixfile(otn); ofstream matrixfile(otn);
for (int i = 0; i < sam.ntax; i++) { for (unsigned int i = 0; i < sam.ntax; i++) {
matrixfile << ">" << sam.taxas[i] << endl; matrixfile << ">" << sam.taxas[i] << endl;
matrixfile << sam.chars[i] << endl; matrixfile << sam.chars[i] << endl;
} }
@ -263,7 +284,7 @@ void writeFas(class Sample sam, char* otn) {
void writePhy(class Sample sam, char* otn) { void writePhy(class Sample sam, char* otn) {
ofstream matrixfile(otn); ofstream matrixfile(otn);
matrixfile << sam.ntax << " " << sam.nchar << endl; matrixfile << sam.ntax << " " << sam.nchar << endl;
for (int i = 0; i < sam.ntax; i++) { for (unsigned int i = 0; i < sam.ntax; i++) {
matrixfile << sam.taxas[i] << "\t" << sam.chars[i] << endl; matrixfile << sam.taxas[i] << "\t" << sam.chars[i] << endl;
} }
matrixfile.close(); matrixfile.close();
@ -273,16 +294,14 @@ void writeNex(class Sample sam, char* otn) {
ofstream matrixfile(otn); ofstream matrixfile(otn);
string datatype; string datatype;
datatype = checktype(sam.chars[0]); datatype = checktype(sam.chars[0]);
matrixfile << "#NEXUS" << endl; matrixfile << "#NEXUS\nBegin data;\n\tDimensions nchar=" << sam.nchar
matrixfile << "Begin data;" << endl << " ntax=" << sam.ntax << ";\n\tFormat datatype=" << datatype
<< "\tDimensions nchar=" << sam.nchar << " ntax=" << sam.ntax << " missing=? gap=-;\n\tMatrix" << endl;
<< ";" << endl
<< "\tFormat datatype=" << datatype << " missing=? gap=-;" << endl for (unsigned int i2 = 0; i2 < sam.ntax; i2++) {
<< "\tMatrix" << endl;
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;
} }
matrixfile << "\t;" << endl << "End;" << endl; matrixfile << "\t;\nEnd;" << endl;
matrixfile.close(); matrixfile.close();
} }
@ -323,18 +342,18 @@ int countfre(string str, char c) {
void writeTnt(class Sample sam, char* otn) { void writeTnt(class Sample sam, char* otn) {
ofstream matrixfile(otn); ofstream matrixfile(otn);
matrixfile << "xread" << endl << "\' \'" << endl; matrixfile << "xread\n\' \'\n" << sam.nchar << " " << sam.ntax << endl;
matrixfile << sam.nchar << " " << sam.ntax << endl; for (unsigned int i = 0; i < sam.ntax; i++) {
for (int i = 0; i < sam.ntax; i++) {
matrixfile << sam.taxas[i] << "\t" << sam.chars[i] << endl; matrixfile << sam.taxas[i] << "\t" << sam.chars[i] << endl;
} }
matrixfile << "proc / ;" << endl; matrixfile << "\n;\nproc / ;" << endl;
matrixfile.close(); matrixfile.close();
} }
Basic_arg procargs(int nargs, char** arg, char* itn, char* otn) { Basic_arg procargs(int nargs, char** arg) {
int i, sta = 0, intype = 0, outype = 0; int i, sta = 0, intype = 0, outype = 0;
string para, inputfile, outputfile; string para, inputfile, outputfile;
char *itn, *otn;
// no arg, show help // no arg, show help
if (nargs == 1) { if (nargs == 1) {
show_help(0); show_help(0);
@ -345,28 +364,36 @@ Basic_arg procargs(int nargs, char** arg, char* itn, char* otn) {
for (i = 1; i < nargs; i++) { for (i = 1; i < nargs; i++) {
// to string // to string
string para(arg[i]); string para(arg[i]);
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")) {
if ((i + 1) < nargs) {
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 {
cout << "MiMi:\tOInput file name must be defined" << endl;
}
} else if (((para == "-o") || (para == "--output"))) {
if ((i + 1) < nargs) {
i++; i++;
otn = arg[i]; otn = arg[i];
string outputfile(arg[i]); string outputfile(arg[i]);
outype = checkextension(outputfile); outype = checkextension(outputfile);
sta++; sta++;
} else { } else {
cout << "MiMi\tUnknown arguments, please use -h to check" << endl; cout << "MiMi:\tOutput file name must be defined" << endl;
}
} else {
cout << "MiMi:\tUnknown arguments, please use -h to check" << endl;
exit(0); exit(0);
} }
} }
if (sta != 2) { if (sta != 2) {
cout << "MiMi\tInput and Output can't be empty" << endl; cout << "MiMi:\tInput and Output can't be empty" << endl;
exit(0); exit(0);
} }
Basic_arg arguvar(intype, outype, itn, otn); Basic_arg arguvar(intype, outype, itn, otn);
@ -380,20 +407,20 @@ int checkextension(string str) {
if (loc) { if (loc) {
extension = str.substr(loc + 1); extension = str.substr(loc + 1);
} else { } else {
cout << "MiMi\tPlease sepecifc the extension name" << endl; cout << "MiMi:\tPlease sepecifc the extension name" << endl;
exit(0); exit(0);
} }
extension = to_lower(extension); extension = to_lower(extension);
if (extension == "fas" | extension == "fasta") { if ((extension == "fas") || (extension == "fasta")) {
type = 1; type = 1;
} else if (extension == "nex" | extension == "nexus") { } else if ((extension == "nex") || (extension == "nexus")) {
type = 2; type = 2;
} else if (extension == "phy" | extension == "phylip") { } else if ((extension == "phy") || (extension == "phylip")) {
type = 3; type = 3;
} else if (extension == "tnt" | extension == "ss") { } else if ((extension == "tnt") || (extension == "ss")) {
type = 4; type = 4;
} else { } else {
cout << "MiMi\tUnknown format" << endl; cout << "MiMi:\tUnknown format" << endl;
exit(0); exit(0);
} }
return type; return type;
@ -405,22 +432,17 @@ void show_help(int help_num) {
<< endl; << endl;
} else { } else {
cout << "\n l、 \t. . . .\n(゚、 。 \t|\\/|*|\\/|*\n l ~ヽ " cout << "\n l、 \t. . . .\n(゚、 。 \t|\\/|*|\\/|*\n l ~ヽ "
" \t| ||| ||\n じしf_,)\t| ||| ||\n" " \t| ||| ||\n じしf_,)\t| ||| ||\n\nMorphology into "
<< endl; "Molecules into\nGPL;\tGuoyi "
cout << "Morphology into Molecules into\n" "Zhang;\t2023\n\nArguments:\n-h\t--help;\n-i\t--input\t\t${"
<< "GPL;\tGuoyi Zhang;\t2023\n" "filename};\n-o\t--output\t${filename};\n\nAccepted "
<< endl; "formats:\nfas\tfasta;\nnex\tnexus;\nphy\tphylip;\ntnt\tss;"
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; << endl;
} }
} }
Sample read_input(char* itn, int intype) { Sample read_input(char* itn, int intype) {
int ntax, nchar; int ntax = 0, nchar = 0;
Sample sam(ntax, nchar); Sample sam(ntax, nchar);
ifstream matrixfile; ifstream matrixfile;
matrixfile.open(itn); matrixfile.open(itn);
@ -430,7 +452,7 @@ Sample read_input(char* itn, int intype) {
if (intype == 3) sam = readPhy(itn); if (intype == 3) sam = readPhy(itn);
if (intype == 4) sam = readTnt(itn); if (intype == 4) sam = readTnt(itn);
} else { } else {
cout << "MiMi\tInput file can't be open" << endl; cout << "MiMi:\tInput file can't be open" << endl;
exit(0); exit(0);
} }
return sam; return sam;
@ -444,16 +466,17 @@ void write_output(class Sample sam, char* otn, int outype) {
if (outype == 3) writePhy(sam, otn); if (outype == 3) writePhy(sam, otn);
if (outype == 4) writeTnt(sam, otn); if (outype == 4) writeTnt(sam, otn);
} else { } else {
cout << "MiMi\tOutput file can't be open" << endl; cout << "MiMi:\tOutput file can't be open" << endl;
exit(0); exit(0);
} }
} }
bool checkalign(class Sample sam) { bool checkalign(class Sample sam) {
int a = 0, b = 0, x = 0; int a = 0, b = 0;
unsigned int x = 0;
a = sam.nchar; a = sam.nchar;
bool aligned = true; bool aligned = true;
for (int i = 0; i < sam.ntax; i++) { for (unsigned int i = 0; i < sam.ntax; i++) {
b = sam.chars[i].length(); b = sam.chars[i].length();
if (a == b) { if (a == b) {
x++; x++;
@ -466,8 +489,7 @@ bool checkalign(class Sample sam) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
char *itn, *otn; Basic_arg arguvar = procargs(argc, argv);
Basic_arg arguvar = procargs(argc, argv, itn, otn);
if (arguvar.intype != 0 && arguvar.outype != 0) { if (arguvar.intype != 0 && arguvar.outype != 0) {
Sample sam = read_input(arguvar.itn, arguvar.intype); Sample sam = read_input(arguvar.itn, arguvar.intype);
cout << "MiMi:\tInput\tfinished" << endl; cout << "MiMi:\tInput\tfinished" << endl;

108
mimi.tcl Normal file
View file

@ -0,0 +1,108 @@
package require Tk
proc ? L {lindex $L [expr {int (rand ()* [llength $L])}]}
proc validateFile name {
set ext [file extension $name]
if {$ext ni {.fas .fasta .nex .nexus .tnt .ss .phy .phylip}} {
tk_messageBox -icon error -type ok -message "extension of file must be fas fasta nex nexus tnt ss phy phylip"
return 0
} else {
return 1
}
}
proc runMimi {} {
global input output mimi
if {[validateFile $input] && [validateFile $output]} {
if {[file exists $mimi]} {
exec $mimi -i $input -o $output
tk_messageBox -icon info -type ok -message "Success"
} else {
tk_messageBox -icon error -type ok -message "Can't find the path of MiMi"
}
}
}
proc clearAll {} {
global input output mimi
set input ""
set output ""
}
proc chooseMimi {} {
global mimi
set file [tk_getOpenFile]
if {$file ne ""} {
set mimi $file
}
}
proc chooseInput {} {
global input
set file [tk_getOpenFile]
if {$file ne ""} {
set input $file
}
}
proc chooseOutput {} {
global output
set file [tk_getSaveFile]
if {$file ne ""} {
set output $file
}
}
proc quitApp {} {
exit
}
proc helpApp {} {
tk_messageBox -icon info -type ok -message "Thanks to my beloved daughter MiMi (Ragdoll cat) for accompanying me in my life, mom loves you forever.\n\nGuoyi"
}
#bind . <Escape> {quitApp}
wm protocol . WM_DELETE_WINDOW {quitApp}
wm iconphoto . [image create photo -file logo.gif]
set input ""
set output ""
set mimi ""
wm title . "Mimi GUI"
#
menu .menubar
.menubar add cascade -label "Settings" -menu .menubar.file
menu .menubar.file
.menubar.file add command -label "Select MiMi Path" -command chooseMimi
.menubar.file add separator
.menubar.file add command -label "Quit" -command quitApp
.menubar add cascade -label "Help" -menu .menubar.help
menu .menubar.help
.menubar.help add command -label "About" -command helpApp
#
. configure -menu .menubar
#
grid [ttk::frame .c -padding "3 3 12 12"] -column 0 -row 0 -sticky nwes
grid columnconfigure . 0 -weight 1; grid rowconfigure . 0 -weight 1
grid [ttk::label .c.ilbl -text "Input"] -column 1 -row 1 -sticky e
grid [ttk::entry .c.input -width 20 -textvariable input] -column 2 -row 1 -sticky we
grid [ttk::button .c.ibtn -text "Select" -command chooseInput] -column 3 -row 1
grid [ttk::label .c.olbl -text "Output"] -column 1 -row 2 -sticky e
grid [ttk::entry .c.output -width 20 -textvariable output] -column 2 -row 2 -sticky we
grid [ttk::button .c.obtn -text "Select" -command chooseOutput] -column 3 -row 2
grid [ttk::button .c.run -text "Run" -command runMimi] -column 2 -row 3
grid [ttk::button .c.clear -text "Clean" -command clearAll] -column 3 -row 3
foreach w [winfo children .c] {grid configure $w -padx 5 -pady 5 }
vwait forever