Compare commits

...

7 Commits

Author SHA1 Message Date
kuoi 247224a3e4 fix: tnt format 2023-10-11 21:49:54 +08:00
kuoi bbbf71374c add: tcl GUI 2023-04-06 04:27:09 +08:00
kuoi 1051374b13 fix: format style 2023-03-21 01:36:27 +08:00
kuoi d81bfbd8d9 fix: fasta space issue; polish: readfas, readnex 2023-03-21 01:19:39 +08:00
kuoi c32f318f85 polish: get rid of extra new 2023-03-20 21:40:40 +08:00
kuoi 0cbec45015 polish: rm extra endl 2023-03-20 19:36:26 +08:00
kuoi 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

Width:  |  Height:  |  Size: 367 KiB

242
main.cpp
View File

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