-3

Here is what the input file looks like:

1-1_Sample 1 GCCCATGGCT 2-1_Sample 1 GAGTGTATGT 3-1_Sample 1 TGTTCTATCT 1-1_Sample 2 GCTTAGCCAT 2-1_Sample 2 TGTAGTCAGT 3-1_Sample 2 GGGAACCAAG 1-1_Sample 3 TGGAAGCGGT 2-1_Sample 3 CGGGAGGAGA 3-1_Sample 3 CTTCAGTTTT


#include <cstdlib>
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>


using namespace std;

const int pops = 10;
const int sequence = 100;
string w;
string popname;
string lastpop;
int totalpops;
string ind;
int i;
int j;
char c;
float dna[pops][4][sequence];
float Af[1][1][1];

int main(int argc, char *argv[])
{
ifstream fin0("dnatest.txt");
lastpop = "nonsense";
totalpops = -1;

if (fin0)
{
do
{           
getline(fin0, w);
cout << w<<endl;
i=0;
ind = "";
     popname = "";

    do  {c = w [i];
         i++;
         if ((c != '>')&(c!='-'))  ind=ind+c; } while (c != '-');
     do {c = w [i];
         i++; } while (c != ' ');
     do {c = w [i];
         i++;
        if (c!= '\n') popname=popname+c; } while (i< w.length());
        if (popname != lastpop) { totalpops++;
        lastpop=popname;
        }

     getline (fin0, w);
     cout << w<<endl << w.length()<<endl;         
     for (i=0; i<w.length(); i++)
     {if (w[i]=='A') dna[totalpops][0][i]++;
      if (w[i]=='C') dna[totalpops][1][i]++;
      if (w[i]=='G') dna[totalpops][2][i]++;
      if (w[i]=='T') dna[totalpops][3][i]++;
      }

      for(int k=0;k<1;k++)
      {for(int j=0; j<1;j++)
        {for (int i=0;i<1;i++)
         Af[0] = Af[0][0][0]+dna[i][j][k]; //RETURNS THE ERROR "INCOMPATIBLE TYPES IN ASSIGNMENT OF 'FLOAT' TO 'FLOAT[1][1]'
         cout<<Af<<endl;}
    }

       while (!fin0.eof());

     }


system("PAUSE");
return EXIT_SUCCESS;
}

Background: I am very new to C++ and trying to teach myself to use it to supplement my graduate research. I am genetics PhD candidate trying to model different evolutionary histories, and how they affect the frequency of alleles across populations.

Question: I am trying to extract certain portions of data from the "dna" array that I created from the input file. For example, here I have created another array "Af" where I am trying to extract counts for the first "cell," so to speak, of the dna array. The purpose of doing this, is so that I can calculate a frequency by comparing the counts in certain groups of cells to the entire dna array. I can't figure out how to do this. I keep getting the error message: "INCOMPATIBLE TYPES IN ASSIGNMENT OF 'FLOAT' TO 'FLOAT[1][1]'"

I have spent a great deal of time researching this on different forums, but I cannot seem to understand what this error means, and how else to achieve what I'm trying to achieve.

So the dna array I'm visualizing is a made from the input file such that there are 4 rows (A,C,G,T). and then 10 columns (one column for each nucleotide in the series). This "grid" is then stacked 3 times (one "sheet" for each Sample (here sample means population, and there are three individuals per population) as listed on the input file). So from this stack of grids I want to extract, for example, the first cell (the number of A's in Sample 1 at position 1. I would then want to compare this number to the total number of A's at position 1 across all samples. This frequency would then be a meaningful number for the model I'm testing.

The problem is, I don't know how to extract portions of the dna array - once I figure out this condensed example, I will be applying it to very large input files, and will want to extract more than one cell at a time.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Sara
  • 1
  • 4
    `float Af[1][1][1];` - Why do you have a 3-dimensional array with only 1 element in it? – Joseph Mansfield Feb 26 '13 at 22:51
  • moreover, what do you expect this assignment to do? `Af[0] = Af[0][0][0] + dna[i][j][k];` You're summing up two values and assigning them to a bi-dimensional array. What's your intention? – Andy Prowl Feb 26 '13 at 22:52
  • You should try to read a book before jumping into writing code. – Bartek Banachewicz Feb 26 '13 at 23:09
  • @sftrabbit, I want to use the Af array to contain portions of the DNA array so that I can calculate allele distributions for different portions of my data. I intend to define different ranges of the DNA array to be added to the Af[0][0][0] array for different analyses I'm running. As it stands right now, the DNA array houses all of my data. I want to be able to subset that data and then compare it to the full set of data. I'm really just asking why I'm getting the error message that I am. I don't understand what the incompatible types error means in this context. – Sara Mar 04 '13 at 20:19

1 Answers1

0

Af is a 3-dimensional array:

float Af[1][1][1];

However, it contains only a single element. It has one row, one column, and one "layer" (or however you want to name the 3rd dimension). That makes it a bit pointless. You might as well just have this:

float Af;

Nonetheless, you don't have that - you have a 3D array. Now let's look at this line:

Af[0] = Af[0][0][0] + dna[i][j][k];

So first it takes the (0, 0, 0)th element from Af (which as we've just seen is the only element in A and adds the (i, j, j)th element from dna to it. That bit is fine because both of these elements are of type float. That is:

Af[0] = Af[0][0][0] + dna[i][j][k];
//      ^^^^^^^^^^^   ^^^^^^^^^^^^
//        These are both floats

So the result of this addition is also a float. Then what do you try to assign this result to? Well you try to assign it to Af[0], but that is not a float. You've simplify specified the 0th index in the first dimension. There's still two other dimensions to specify. The type of Af[0] is actually a float[1][1] (a two dimensional array of floats). This would work, for example:

Af[0][0][0] = Af[0][0][0] + dna[i][j][k];
// Or equivalently:
Af[0][0][0] += dna[i][j][k];

Whether that's what you want to do or not is completely dependent on the problem, which I can't begin to understand. However, as I said, it makes very little sense to have Af as a 3 dimensional array with only a single element in it. If it's just one float, make it a float, not an array. Then you would do the above line as:

Af += dna[i][j][k];
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324