PDA

View Full Version : how do can I use my C++ code in qt and can it be translated into QT code



ApacheOmega
6th September 2012, 02:59
I have a bioinformatics program that i want a GUI added to it. I just discoverd QT and I'm told it uses C++ code but when I analyze QT code it looks a heck of alot different from C++. where can I go to find the QT equivalent of my code or where is the best place to learn?

this is my code I'm trying to convert
does anybody have any tips on how to or how can I make this happen?



#include<iostream>
#include<fstream>
#include<cstdlib>
#include <string>
using namespace std;
int input;
int main()
{

cout<<"please enter a file name"<<endl;
char filename[50];
ifstream seq;
cin.getline(filename, 50);
seq.open(filename);

if(!seq.is_open()){
exit(EXIT_FAILURE);
}

char DNA[50];
char RNA[50];
seq>>DNA;

cout<< DNA <<" "<<endl;

int len=0;
for(int i=0; i<=sizeof(DNA); i++){

if (DNA[i] == 'A')
{cout<<"U";
RNA[i]='U';
len++;
}
if (DNA[i] == 'G')
{cout<<"C";
RNA[i]='C';
len++;
}
if (DNA[i] == 'C')
{cout<<"G";
RNA[i]='G';
len++;
}

if (DNA[i] == 'T')
{cout<<"A";
RNA[i]='A';
len++;
}
}
cout<<endl;
for(int i=0; i<len; i++){
cout<<RNA[i];
}
cout<<endl<<sizeof(DNA)<<endl;
cout<<len<<endl;
fstream file;
file.open("RNA_DNASeq.txt",ios::out);
file<<DNA<<endl<<RNA<<endl;
file.close();

system("PAUSE");
return 0;
}

ChrisW67
6th September 2012, 03:17
Qt is a library not a language. Qt is written in C++ and designed to be used from C++ programs (although there are bindings for Python). All you require is a knowledge of C++ to learn how to use the Qt library. There are plenty of tutorials, examples and demos in the Qt documentation. That documentation is installed with the Qt library: run "assistant" or look at the Help in Qt Creator (if you have that).

Your program is not designed for a GUI, whether it is Qt or any other kind, so you will need to think about how your GUI program should work.

(I also suspect your program does not do what you expect)

ApacheOmega
6th September 2012, 03:34
thanks for the reply


(I also suspect your program does not do what you expect)

My program works perfectly



Your program is not designed for a GUI, whether it is Qt or any other kind, so you will need to think about how your GUI program should work.


what I want it to do as far as it working with the GUI is I want the DNA sequence typed in a text field and then press a button and the RNA complement to print out in a second text field.

How should my program be set up?

stereoMatching
6th September 2012, 04:10
but when I analyze QT code it looks a heck of alot different from C++
It is because Qt is not a standard of C++, Qt is one of the library of C++


where can I go to find the QT equivalent of my code
Hard to tell that which part of Qt you need to use.Who knows what kind of gui you want to design?


where is the best place to learn?
Go to the website of Qthttp://qt.nokia.com/learning
Study the documents come with QtCreator, or find a book
I would recommend "Foundation of Qt development" for you.

Besides, about your codes, there are too many ways to get the same results,
but I would show you one of the solution which almost base on the component of Qt



/*
* you could use QMap to replace std::map, I use std::map because
* QMap do not support std::initializer_list in Qt4.8.2 yet.If QString
* is too expensive for your case, change it back to char
*/
inline std::map<QString, QString> const create_table()
{
return std::map<QString, QString>() = { {"A", "U"}, {"G", "C"}, {"C", "G"}, {"T", "A"} };
}

inline void help2()
{
qDebug() << "please enter a file name";
QString filename;
QTextStream qtin(stdin);
qtin >> filename;
QFile file(filename);
if(!file.open(QIODevice::WriteOnly) )
{
qFatal("can't open the file");
}

QString dna;
qDebug() << "please enter dna";
qtin >> dna;
qDebug() << dna;

auto table = create_table();
QString rna;

int const dna_size = dna.size();
for(int i = 0; i != dna_size; ++i)
{
qDebug() << table[QString(dna[i])];
rna += table[QString(dna[i])];
}

qDebug() << rna;
qDebug() << endl<< dna_size <<endl;

QTextStream fileStream(&file);
fileStream << dna << endl << rna << endl;
}


source codes
http://www.sendspace.com/file/qam63o

Besides, try don't use system to pause the codes, you could try std::cin.get() instead.

ps : I declare inline not because it should be but lazy to separate the declaration and implementation
My codes are not a good example, please split the codes into different parts as your requirement.

ApacheOmega
6th September 2012, 05:07
Thanks alot
I see your way more advanced the me and I like your style of coding
I will study this and thanks for the links
I guess I have a long way to go if I'm gonna learn this but you gave my some real positive hope!!!!

Thank you again

ChrisW67
6th September 2012, 05:34
With two QLineEdits (or a QLineEdit and a QLabel) and a QPushButton. You will need to put them into a parent QWidget with a layout. The QPushButton::clicked() signal can be used to trigger a slot that does your DNA->RNA translation on the text of the input widget and places the result into the result widget. You can use a QValidator to ensure only the character TAGC are accepted and upper case is forced. You use QString instead of your fixed C-style char arrays.

Take a look at the Line Edits Example and the Address Book tutorial.


Your current code assumes that DNA and RNA have been zeroed. This is not a safe assumption: the array contents are undefined. Here is what the array contents were immediately after declaring the arrays:


DNA d8 20 60 00 00 00 00 00 24 23 60 00 00 00 00 00 a0 0e 40 00 00 00 00 00 44 a6 4b 06 c8 7f 00 00 00 00 00 00 00 00 00 00 f0 f7 20 51 ff 7f 00 00 c0 1d
RNA 00 a3 01 07 c8 7f 00 00 00 00 00 00 00 00 00 00 f0 f7 20 51 ff 7f 00 00 c0 1d 60 00 00 00 00 00 01 00 00 00 00 00 00 00 c5 b0 04 07 c8 7f 00 00 a0 0e

Reading in from the file will put a zero terminator after the input data. However, since you do not limit yourself to the valid characters in the input string, or zero-terminate the RNA string after you build it you sometimes get rubbish characters in the output. Never mind, using either std::string or QString will make this problem go away.

ApacheOmega
6th September 2012, 06:29
Thanks Chris & Stereo
I'm going through the learn QT documentation and I'm really like WOW you can do alot with QT, more than I expected. the QT quick is awesome to it looks like you can design with QML and program with QT C++

Thanks for the help you guys are really inspiring me right now!!!!

Chris it looks like you turned the transcription portion of my code into the translation portion howd you do that???

ChrisW67
6th September 2012, 07:07
Chris it looks like you turned the transcription portion of my code into the translation portion howd you do that???

By having an MSc is astronomy, not genetics, I free myself from the need to understand that transcription and translation have specific meaning in genetics. I meant translation in the computing sense of T translates (or maps) to A, G to C etc. I was not proposing determine the entire gene expression and protein folding result for a given DNA sequence ;)