1

I am not sure about the correct way of passing the vector to multiple files. The code may look a complete mess for you, indeed i tried to put the minimum reproducible code. In file match_read.cpp , i am trying to use the vector elements std::vector<int> intref, but i could not find anything in the vector. The files perm.cpp, index_read.cpp, and index_ref.cpp work fine. While calling match_read.cpp, the code does not work for 4th file. I am not sure of passing the vector as an argument to other cpp file.

main.cpp

#include "index_read.h"
#include "perm.h"
#include "index_ref.h"
#include "match_read.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>


int main()

{
    int lenght = 3;

    char str[] = {'A', 'G', 'C', 'T'};

    int pos; // to hold positions of a vector element

    int n = sizeof str;

    for (int k = 1; k <= lenght; k++) //here we loop through all the possible lenghts 1, 2 and 3

               {

                   print_str(str, "", n, k);  //Note: this function works on all cases and not just the case above
                }


    std::string* permut_array =  new std::string[NumberOfPermutations]; // the array that we will use to store the permutations in

    std::copy(permutations.begin(), permutations.end(), permut_array); // here we copy the vector into the array 



    for (int k = 0; k < NumberOfPermutations; k++) //if you want you can use your array to print the permutation.

               {
                   std::cout << permut_array[k] << std::endl;
                }

    std::cout<<"Total number of permutations is: "<<NumberOfPermutations<<std::endl;

    index(permutations, str, read, intread); // calling index function

    refindex(permutations, ref, refarr, intref); // calling reference index function

    align_read(intref);

    //align_read(permutations, refarr, intread, intref, matchedread, found); // matching DNA read with reference genome

        return 0;
}

perm.cpp

#include "perm.h"
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

int NumberOfPermutations = 0;

std::vector<std::string> permutations;

// The main recursive method to print all possible strings of length "length"
    void print_str(const char str[],std::string prefix, const int n, const int lenght)

    {

        if (lenght == 1)

            {

                for (int j = 0; j < n; j++)

               {
                   // i commented this ligne so that if you want to use your array to print your permutations you will not get a screnn with permutations printed 2 times
                   //std::cout << prefix + str[j] << std::endl; 
                   permutations.push_back(prefix + str[j]); // the vector that we will use to store the permutations in
                }

            }//Base case: lenght = 1, print the string "lenght" times + the remaining letter

        else

            {


               // One by one add all characters from "str" and recursively call for "lenght" equals to "lenght"-1
                for (int i = 0; i < n; i++)

                // Next character of input added
                 print_str(str, prefix + str[i], n, lenght - 1);
                // "lenght" is decreased, because we have added a new character

            }
        NumberOfPermutations = permutations.size();
    }

perm.h

#ifndef perm_h
#define perm_h

#include <iostream>
#include <string>
#include <vector>

void print_str(const char*,std::string,const int, const int);

extern int NumberOfPermutations;// = 0;

extern std::vector<std::string> permutations;

#endif

index_read.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "index_read.h"


std::string str = "AGCTGTACGCG";
int read[4];

std::vector<int> intread;


void index(std::vector<std::string> permutations, std::string, int read[4], std::vector<int> intread) // to get the index of a string from the permutation.
{

    for( int i= 0; i<str.length(); i +=3)
    {
        std::cout<<"i: "<<i<<std::endl;
        std::string str2 = str.substr (i,3);
        std::cout<<"substring : "<<str2<<std::endl;
        int pos1 = std::find(permutations.begin(), permutations.end(), str2) - permutations.begin();
        intread.push_back(pos1); // pushing index of string into vector i.e. storing read into integer representation

        read[i] = pos1;
        std::cout<<"Read elements:"<<read[i]<<std::endl;
        //std::size_t pos = str.find(str2);
        std::cout<<"Position of "<<str2<< " : "<<pos1<<std::endl;

    }

}

index_read.h

#ifndef index_read_h
#define index_read_h

#include <iostream>
#include <string>
#include <vector>

void index(std::vector<std::string> permutations, std::string, int read[4], std::vector<int> intread);

extern std::vector<int> intread;

extern std::string str; 

extern int read[]; 

#endif

index_ref.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "index_ref.h"
//#include "variables.h"

//std::vector<std::string> permutations ;

std::string ref = "AAGCAGCTGTACGCT";

int refarr[3][6];

std::vector<int> intref;

void refindex (std::vector<std::string> permutations, std::string, int refarr[3][6], std::vector<int> intref) // to get the index of a string from the permutation.
{
    for(  int j = 0; j < 3; j++) // looping for maning n frames, where n is number of characters together
    {   int k = 0; // Tracking the columns of array
        std::string str4 = ref.substr(0, j+1);
        std::cout<<"substring first : "<<str4<<std::endl;
        int fstpos = std::find(permutations.begin(), permutations.end(), str4) - permutations.begin();
        intref.push_back(fstpos);
        std::cout<<"Fst element:"<<fstpos<<std::endl;
        refarr[j][0] = fstpos;
        std::cout<<"pos of fst element:"<<refarr[j][0]<<std::endl;
        for( int i = j+1; i < ref.length(); i +=3)  //loop for breaking the string into substrings
        {   k = k+1;
            std::cout<<"i: "<<i<<std::endl;
            std::string str3 = ref.substr (i,3);
            std::cout<<"substring : "<<str3<<std::endl;
            int pos2 = std::find(permutations.begin(), permutations.end(), str3) - permutations.begin();
            refarr[j][k] = pos2;
            std::cout<<"printing array elements: "<<refarr[j][k]<<std::endl;
            intref.push_back(pos2); // pushing index of string into vector i.e. storing reference into integer representation
            //std::size_t pos = str.find(str2);
            std::cout<<"Position of "<<str3<< " : "<<pos2<<std::endl;

        }
    }

}

index_ref.h

#ifndef index_ref_h
#define index_ref_h

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

void refindex (std::vector<std::string> permutations, std::string,  int refarr[3][6], std::vector<int> intref); // function decleration for indexing the reference. string

extern std::string ref;

extern std::vector<int> intref;

extern int refarr[3][6];

#endif

match_read.cpp

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "match_read.h"


//std::vector<std::string> matchedread; // vector to store the matched read.

//bool found = false;

void align_read(std::vector<int> intref)

{
    int offset;
    std::cout<<"testing the error file: "<<std::endl;

    for (std::vector<int>::const_iterator i = intref.begin(); 
    i != intref.end(); ++i)
    {
        std::cout<<"Integer representation of reference: "<<*i<<std::endl;
    }
}

match_read.h

#ifndef match_read_h
#define match_read_h

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

extern std::vector<int> intref;

void align_read(std::vector<int> intref);

#endif
AwaitedOne
  • 992
  • 3
  • 19
  • 42
  • I suggest you to use event & raise and pass vector. extern not a good way. when you define a extern when you run your program your vector will be initialize and will be stay untill program close. – Farhad May 02 '16 at 12:37
  • 1
    Also, you need to spend some time learning about C++ references. You do realize that you're passing the vector by value, right? You do realize that this involves duplicating the entire contents of the vector, for the sole purpose of making a function call, right? And if these functions' purpose is to modify the contents of the vector, you'll be back here on stackoverflow.com at some point later, wondering why these functions don't work. Seeing `std::vector` passed by value as a function parameter makes my eyes bleed. – Sam Varshavchik May 02 '16 at 12:38
  • @SamVarshavchik Kindly don't mind about my wrong coding as I am learning `cpp` – AwaitedOne May 02 '16 at 12:44
  • Nope, not at all, just giving you some friendly advice: learn about references, and don't ever, ever [use namespace std](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice). Completely forget that such a thing ever existed. – Sam Varshavchik May 02 '16 at 12:46
  • @SamVarshavchik Thanks for your advice. I have put a minimum reproducible code . Please find the updates. – AwaitedOne May 02 '16 at 16:11
  • @user2079303. Thanks for your comment. Kindly find the updated question. – AwaitedOne May 02 '16 at 16:12

2 Answers2

2

The problem with global variables is that since every function has access to these, it becomes increasingly hard to figure out which functions actually read and write these variables. extern variables are initialize since program run, and not close until you close your application. what is your IDE ?

Farhad
  • 4,119
  • 8
  • 43
  • 66
2

Your vector is defined both as a global variable and as an argument to your two functions. Those are different variables, so when filling one and accessing the other, you don't see any content.

You should either use only the global variable and get rid of the arguments of both functions, or pass the vector as an argument, which means you no longer need the extern declaration of your vector.

Karsten Koop
  • 2,475
  • 1
  • 18
  • 23