Click to See Complete Forum and Search --> : Comparision Error ----


Sarika73
January 18th, 2003, 02:06 AM
Hi

I am a beginner in C++. I am trying towrite a program where the user enters the number of sequences which can be more than 1 and then compare each letter of the sequence with each element of char array.but it falls over on line 80.


Any help is appreciated.


------------------------------------------------------------------------------
// This program asks the user to enter the no of sequences
// and read the sequences as string and then compare them

// Assumption
// The char of sequences should be A,C,T,G,U

#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool checkSequenceChar(string);

int main()
{

int input = 0; // sequence

vector<string> sequences;

cout << "Enter The Number Of Sequence in PTree: ";


while(!(cin >> input))
{
string buffer;
cin.clear(); //clear failbit
cin >> buffer; //flush stream
cout << "Invalid Input.. Please Enter An Interger Only !!!" << endl;
cout << "Enter The Number Of Sequence : ";

}

for (int i = 0; i < input; i++)
{
string temp;

// store the elements in array.
cout << "Please Enter Sequence [" << i << "] : " ;

cin >> temp;
sequences.push_back(temp);
}

for (int i = 0; i < input; i++)
{


for (int j = 0; j < sequences[i].length(); j++)
{

string seqChar;

seqChar = sequences[i].substr(j,1);

if ( checkSequenceChar(seqChar) )
{
cout << "Sequence [" << i << "] Matches " << sequences[i] <<endl;
}
else
{
cout << "Sequence Doesn't [" << i << "] Matches " << sequences[i] <<endl;
}
}
}


return 0;
}

// checks every char of the sequence entered
bool checkSequenceChar(string c)
{
char seqFilter[] = "ACTGU";

cout << " The Comparision Char is : " << c << endl;
for (int i = 0; i < 5; i++)
{
cout << "Sequence Pick : " << " " << i << " " << seqFilter[1] << endl;

// THE PROGRAM FALLS HERE
if ( seqFilter[i] != c )
{
cout << "Sequence Char NOT Matches : " << c << " " << seqFilter[i] << endl;
//return false;
}
}
return true;
}

Philip Nicoletti
January 18th, 2003, 08:18 AM
First, the reason for the compile error: you are
comparing a "char" with a "std::string". To fix this
problem, change :

string seqChar;
seqChar = sequences[i].substr(j,1);

to

char seqChar = sequences[i].at(j); // or sequences[i][j]

and modify checkSequenceChar() to take a char as its argument.

What are you trying to check here ? There is probably an
easier way. For example, are you trying to check that the only
characters entered by the user in the string are A,C,T,G,U ?

You can do this as follows:


const string seqFilter = "ACTGU";

std::string testString = "AGGGTUTACC";

std::string::size_type position = testString.find_first_not_of(seqFilter);

if (position != std::string::npos)
{
std::cout << " a character other than A,C,T,T,G, or U was found " << std::endl;
}

KevinHall
January 21st, 2003, 12:11 PM
Philip, I bet that's exactly what he is doing. It appears he is trying to write a program for the user to enter in Nucleic Acid sequences (DNA and RNA). ACTGU correspond to adenine, cytosine, thymine, guanine, and uracil.

CheckSequenceChar will always fail as it is written. Philip has a good suggestion as how to use the STL. A slightly more complicated approach that will run much more quickly -- and that is to copy the (most compilers') C library's implementations of the isXXX functions:


static bool NABase[256]; // static initializes entire array to false

void InitNABase()
{
&nbsp; &nbsp;NABase['A'] = true;
&nbsp; &nbsp;NABase['C'] = true;
&nbsp; &nbsp;NABase['T'] = true;
&nbsp; &nbsp;NABase['G'] = true;
&nbsp; &nbsp;NABase['U'] = true;
}

inline bool IsNABase(char c)
{
&nbsp; &nbsp;return NABase[c];
}

#define checkSequenceChar(c) IsNABase(c)


All you have to do is call InitNABase at the beginning of the program. This method does use 256 bytes of data memory and requires initialization, but is quicker, is compatible with C, and is not that difficult to understand (I'm not saying that this is any more or less difficult to understand compared to other methods).

You can avoid the initialization function, by initializing in the source. Something like:


static bool NABase[256] =
{
false, false, false, false,
// ...
false, true, false, false,
// ...
false, false, true // last true ends the sequence. The rest
// will automatically be initialized to false by being declared static
};

Sarika73
January 25th, 2003, 12:38 AM
Hi


What Kevin Hall says is right.I am trying to draw a Phylogenetic Tree.
Kevin if you got the source code will you mind passing it to me.

Thanks

KevinHall
January 25th, 2003, 02:03 AM
Sorry, I don't have any code :( -- I just remember my biochem.