Protocol Online logo
Top : Forum Archives: : Bioinformatics and Biostatistics

FOR C++ EXPERTS - need your help (Jan/06/2007 )

Pages: Previous 1 2 3 4 Next

I think if you are just starting programming then you should look into perl. The way you wrote the program initially is valid, and has the syntax of perl/c. Perl is easy to learn, and you shouldn't have a problem with it if you already know some c++. Trying to wrap your head around the object-orientated c++, which is what perlmunky is showing how to write, is not the best starting point.

-jaknight-

hi jaknight...i told u previously that i'm just a beginner in programming, i chose to work on C++ because i have a small background about it from one course i studied years ago..so i need more practice to refresh my memory and become used to it, and since perl is not widely different from C++ as u said, i will start learning it as soon as i can...smile.gif

-strawberry-

QUOTE (strawberry @ Jan 9 2007, 05:56 AM)
hi jaknight...i told u previously that i'm just a beginner in programming, i chose to work on C++ because i have a small background about it from one course i studied years ago..so i need more practice to refresh my memory and become used to it, and since perl is not widely different from C++ as u said, i will start learning it as soon as i can...smile.gif

I actually totally disagree with you (jaknight). The best place to start is with OO and a language like C++/JAVA. This is because it is strict, you can't get away with hacks (as you can with perl). The rule set is much better defined. I started out with python using it in an semi-object orientated way (the way python is written it is alway OO) I didn't create my own objects. I then did perl before C/C++/JAVA - I found the transition far harder than my friends who had experience in OO languages.

However, this task is a perl/scripting language job - 'berry however has some experience of c/c++ and wants to continue down that route so I don't see the point of either of us trying to turn 'berry to the light side.

If you can get the basics of c/c++ you will find perl a walk in the park. IMHO

-perlmunky-

For an easy-to-learn, strongly-typed, OOP language, how about Ruby?

My preference would be Perl, because I know it, and it seems to be a very good choice for bioinformatics -- there are a lot of modules around geared toward bio, and many people in biology use it. Also, since Per is a scripted language (as is Ruby), there are no compiler issues to deal with, so it's easier for cross-platform compatability.

But, I also agree with perlmunky to some extent -- learn what you're most comfortable with...

-HomeBrew-

oops... I forgot ruby (i am trying to find time to learn it). From the little I have used it, it seems very nice. Not too far from standard english (unlike C, but like java).

see: RUBY - from a Digg link. There is also ruby on rails for web development and so on.

-perlmunky-

i modified the program, using one array only by calling a function applying switch:

#include<iostream>
using namespace std;

void rna(char seq[], int size)
{
for(int j=0;j<=size;j++)
{

switch(seq[j]){
case 'A':
seq[j]='U';
break;

case 'T':
seq[j]='A';
break;

case 'G':
seq[j]='C';
break;

case 'C':
seq[j]='G';
break;

default :
cout<<" "<<endl;
break;

}
}
}

int main()
{


char s1[20];

int t,n,mp;
int i, count1=0,count2=0,count3=0,count4=0;



cout<<"enter your dna sequence: \n";
cin>>s1;


for (i=0;s1[i]!='\';i++)
{
if (s1[i]=='A')
count1=count1+1;
else if (s1[i]=='T')
count2=count2+1;
else if(s1[i]=='G')
count3=count3+1;
else
count4=count4+1;

}


n=count1+count2+count3+count4;
cout<<"total length of the sequence= "<<n<<endl;

cout<<"number of amino acids= "<<n/3<<endl;

cout<<"%A= "<<count1*100/n<<endl;
cout<<"%T= "<<count2*100/n<<endl;
cout<<"%G= "<<count3*100/n<<endl;
cout<<"%C= "<<count4*100/n<<endl;

t=(count3+count4)*100/n;
cout<<"GC%= "<<t<<endl;

mp=2*(count1+count2)+4*(count3+count4);
cout<<"melting point of this sequence= "<<mp<<endl;

rna( s1, n);
cout<<s1<<endl;

return 0;
}

smile.gif

-strawberry-

Hi,

that looks good, however you still have another pointless loop.

CODE
for (i=0;s1[i]!='\';i++)
{
if (s1[i]=='A')
count1=count1+1;
else if (s1[i]=='T')
count2=count2+1;
else if(s1[i]=='G')
count3=count3+1;
else
count4=count4+1;

Rather than use a function you should build an object. Remember and object contains a group of functions.
Because of the scale of this bit of work you are probably best using a single c style program that could later be used as an object.
Rather than having individual counters (count 1 count 2) and so on, create an array to hold counters or better yet a structure.
where:
int [] counters = new int [4]
where counters[0] => the number of Ts
counters[1] => the number of As and so on.

Again in your switch case setup, increment the correct array position
CODE
switch(seq[j]){
case 'A':
seq[j]='U';
counters[1]++;
break;

case 'T':
seq[j]='A';
counters[0]++;
break;


This will save you more time in more complex tasks.
Also, to make you code easier to read, I find that using proto-types is good. This means that you define your function at the top of the program, but build the major code after the main(){ }

CODE
#include <iostream>

void dumpCharArray(char array []);

int main {
char [] array = new char [40];
}

##########################
#                                                #
#               FUNCTIONS            #
#                                               #
##########################

void dumpCharArray(char array []) {
    for (int i = 0; i < array.size; i++) { //size might be a python or java method not sure//
        std::cout << "Current base is " << array[i] << endl;
    }
}


Something like that.

-perlmunky-

THANX perlmunky for your consideration smile.gif
i am still a beginner , still have to learn objects , structures and files and that's why i use simple design
be patient with me laugh.gif

CODE
#include<iostream>
using namespace std;



int main()
{


char seq[20];
int size=20;

int t,n,mp;
int  counta=0,countt=0,countg=0,countc=0;

cout<<"enter your dna sequence: \n";
cin>>seq;

for(int j=0;j<=size;j++)
{

switch(seq[j]){
case 'A':
seq[j]='U';
counta++;
break;

case 'T':
seq[j]='A';
countt++;
break;

case 'G':
seq[j]='C';
countg++;
break;

case 'C':
seq[j]='G';
countc++;
break;

default :
cout<<" "<<endl;
break;
}
}

n=counta+countt+countg+countc;
cout<<"total length of the sequence= "<<n<<endl;

cout<<"number of amino acids= "<<n/3<<endl;

cout<<"%A= "<<counta*100/n<<endl;
cout<<"%T= "<<countt*100/n<<endl;
cout<<"%G= "<<countg*100/n<<endl;
cout<<"%C= "<<countc*100/n<<endl;

t=(countg+countc)*100/n;
cout<<"GC%= "<<t<<endl;

mp=2*(counta+countt)+4*(countg+countc);
cout<<"melting point of this sequence= "<<mp<<endl;
cout<<"mRNA sequence is "<<seq;

return 0;
}

-strawberry-

That's exactly what I meant.
You could maybe create an array instead of the individual counts. Then rather than having four individual print statments (to show % contribution of each acid) you could have a function that takes an array and for each element calculates the percentage contribution of said element.

Don't forget to annotate your code. I find it helps. If you do get around to using perl have a look at perldoc, I am not sure if there is a C/C++ version.

I am happy to help where I can: basically perl, c/c++/ , java, python, bash and ruby (soon I hope)

oh also, get yourself a copy of eclipse and the language plug-ins it makes life organised and writing far easier.

-perlmunky-

here is my modified small program happy.gif

CODE
#include<iostream>
using namespace std;

void mycalculation(int counter[], int size)
{
    int n,mpoint,sum=0;
    float i;
    for(int j=0;j<size;j++){
        sum=sum+counter[j];}
    cout<<"\nthis sequence has a length of "<<sum<<endl;

    cout<<"\nthe % of A,T,G,C ARESPECTIVELY is"<<endl;
    for(j=0;j<size;j++){
        n=counter[j]*100/sum;
      
        cout<<n<<"%"<<endl;}
    i=(counter[2]+counter[3])*100/sum;
    cout<<"\nCG%= "<<i<<endl;

    mpoint=(counter[0]+counter[1])*2+(counter[2]+counter[3])*4;
    cout<<"\nmelting point for this sequence= "<<mpoint<<endl;
    cout<<"\nexpected number of amino acids translated= "<<sum/3<<endl;
    
}




int main()
{

char  sdnaseq[100];char sdnaseq2[100];char rnaseq[100];char doubls[2][100];
int i,base=4;int size=100;
int countbase[4]={0};


cout<<"this is a single stranded DNA sequence:"<<endl;
cin>>sdnaseq;


for(i=0;i<size;i++)
{
    
    switch(sdnaseq[i]){
    case 'A':
        sdnaseq2[i]='T';
        rnaseq[i]='A';
        countbase[0]++;
        break;

    case 'T':
        sdnaseq2[i]='A';
        rnaseq[i]='U';
        countbase[1]++;
        break;

    case 'G':
        sdnaseq2[i]='C';
        rnaseq[i]='G';
        countbase[2]++;
        break;

    case 'C':
        sdnaseq2[i]='G';
        rnaseq[i]='C';
        countbase[3]++;
        break;

    default:
        sdnaseq2[i]=0;
        rnaseq[i]=0;
        break;
    }
}




for(i=0;i<100;i++){
    doubls[0][i]=sdnaseq[i];
    doubls[1][i]=sdnaseq2[i];}


cout<<"\ndouble stranded DNA: "<<endl;
for(i=0;i<2;i++){
    for(int j=0;j<100;j++){
        cout<<doubls[i][j];}
}

cout<<"\nif the coding DNA strand is: "<<sdnaseq2<<" \ntherefore the mRNA transcript would be: "<<rnaseq<<endl;
mycalculation(countbase,base);

return 0;
}

-strawberry-

Pages: Previous 1 2 3 4 Next