iVar
get_common_consensus.cpp
1 #include<iostream>
2 #include<iterator>
3 #include<sstream>
4 #include<vector>
5 #include<fstream>
6 
7 const uint32_t MAX_SIZE = 100000;
8 
9 bool check_if_equal(char** n, int n_size, std::ifstream *f){
10  if(*n[0] == '\n' || *n[0] == '\0' || *n[0]==EOF || f[0].eof())
11  return false;
12  for(int i = 1; i < n_size;i++){
13  if(*n[i] != *n[0] || *n[i] == '\0' || *n[i] == '\n' || *n[0]==EOF || f[i].eof())
14  return false;
15  }
16  return true;
17 }
18 
19 int compare_consensus(char** f, int n_string, std::string out){
20  std::ifstream *cns = new std::ifstream[n_string];
21  std::ofstream o(out+".fa");
22  o << ">Common_Consensus" << std::endl;
23  int i, j;
24  char** l = new char*[n_string];
25  for(i = 0; i < n_string;i++){
26  std::cout << f[i+1] << std::endl;
27  l[i] = new char;
28  cns[i].open(f[i + 1]);
29  cns[i].read(l[i], 1);
30  if(*l[i]!='>'){
31  std::cout << "Error " << f[i] << " is not a fasta file." << std::endl;
32  return 0;
33  }
34  while(*l[i] !='\n'){
35  cns[i].read(l[i], 1);
36  }
37  cns[i].read(l[i], 1); // Read next character after \n
38  }
39  // Get to beginning of matching substring
40  while(!check_if_equal(l, n_string, cns)){
41  for(j = 0; j<n_string;j++){
42  cns[j].read(l[j], 1);
43  }
44  }
45  for(j = 0; j < n_string; j++)
46  if(*l[j] == EOF){
47  std::cout << "Warning: No common regions between consensus sequences" << std::endl;
48  return 0;
49  }
50  while(check_if_equal(l, n_string, cns)){
51  o << *l[0];
52  for(j = 0; j<n_string;j++){
53  cns[j].read(l[j], 1);
54  }
55  }
56  for(i = 0; i < n_string; i++){
57  cns[i].close();
58  }
59  o.close();
60  delete[] cns;
61  delete[] l;
62  return 0;
63 }
64 
65 int main(int argv, char** argc){
66  compare_consensus(argc, argv - 1, "test");
67 }
int main(int argc, char *argv[])
Definition: ivar.cpp:144