944,208 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3667
  • C++ RSS
Jul 4th, 2005
0

2D-Array, switches, and importing from a file

Expand Post »
Hi all,

My first, but likely not my last post. I’m teaching myself C++ with basically no programming background. I’ve been using Borland, reading books (such as teach yourself in 21 days, etc.) and reading this website, but now I’ve run into a problem and after three days of trying to figure it out, I decided to seek your help. My code so far is below.

Here’s the deal, I’m trying to write a program that imports a file of DNA sequences (basically A T C and Gs) from one format called FASTA and export those sequences into a new format called PHASE. There is a lot of nonsense in the FASTA file that I need to skip over and the sequence length may change file to file; hence, I’m using a switch to get to the good parts of the FASTA file. That seemed to work out well. But now putting the data into the array is a problem. It does not seem to work like the more simple examples that I’ve seen and practiced.

To start: I want to bring in the first and second DNA sequences, after a bunch of junk characters, into a 2D array. My program nearly works in that it goes to the correct part of the FASTA file before entering characters into the array, but then for some reason it gets stuck in a loop. I’m only seeking help on getting these sequences into a 2D array. But if you have other tips for a beginner working a project like this, let me know.

Here the code and an abridged example of a FASTA file.

#include <iostream>
#include <string>
#include <fstream> //Provides input and output classes
using namespace std;
#pragma hdrstop

#pragma argsused
int main(int argc, char* argv[])
{
ifstream FASTAin("FASTA2.txt");
if (!FASTAin)
{
cout << "File not found.\n";
cout << "Press enter to exit.\n";
}

ofstream PHASEout("PHASE2.txt");
if (!PHASEout)
{
cout << "Unable to export the new PHASE.txt file.";
cout << "Press enter to exit.\n";
}

cout << "File found.\n";
cout << "Press enter continue.\n";

getchar();

const int M=2;
const int N=5000;

char seq1[M][N];
char IDname[80]="";
char ch='a';
unsigned short int row=0;
unsigned short int col=0;

int flipper=-9;
unsigned short int t=0;

while (FASTAin.get(ch))
{
if (ch==10)
{
flipper=0;
}

if (ch=='>') //10 = ascii code for <enter> character
{
flipper=1;
}

if (flipper==-9)
{
int ignore=0;
cout << "."; ignore++;
}

if (flipper==0)
{
for (row=0; row<=M; row++)
{
for (col=0; col<=N; col++)
{
seq1[row][col] = ch;
//Here is the problem!
}
}
}

if (flipper==1)
{
IDname[t]=ch; t++;
}

}
if (FASTAin.eof ()) //ignore this
{
for (int k=1;k<(M+1);k++) //for all int up to i
{
cout << seq1[k]; //print idname
PHASEout << seq1[k];
}
}

cout << seq1[row][col];
cout << "\n***End of FASTA file contents.***\n";
getchar();
FASTAin.close();
PHASEout.close();

getchar();
return 0;
}
------------THE FILE: FASTA.txt--Important stuff starts at BAGATT...

[oi]
>'1_{GreeNlANd}' [Jun 10, 2005]
BAGATTTGGGTACCACCCAAGTATTGACTCACCCATCAACGACCGCTATGTATTTCGTAC
ATTACTGCCAGTCACCATGAATATTGTACGGTACCATAAATACTTGACCACCTGTAGTAC
ATAAAAACCCAATCCACATCAAAACCCCCTCCCCATGCTTACAAGCAAGTACAGCAATCA
ACCTTCAACTATCACACATCAACTGCAACTCCAAAGCCACCCCTCGCCCACTAGGATACC
AACAAACCTATCCACCCTTAACAGTACATAGTACATAAAACCATTTACCGTACATAGCAC
ATTACAGTCAAATCCCTTCTCE
>'2_{GreeNlANd}'
bAGATTTGGGTACCACCCAAGTATTGACTCACCCATCAACAACCGCTATGTATCTCGTAC
ATTACTGCCAGTCACCATGAATATTGTACGGTACCATAAATACTTGACCACCTGTAGTAC
ATAAAAACCCAATCCACATCAAAACCCCCTCCCCATGCTTACAAGCAAGTACAGCAATCA
ACCTTCAACTATCACACATCAACTGCAACTCCAAAGCCACCCCTCGCCCACTAGGATACC
AACAAACCTATCCACCCTTAACAGTACATAGTACATAAAACCATTTACCGTACATAGCAC
ATTACAGTCAAATCCCTTCTCe
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BioTechNoob is offline Offline
5 posts
since Jun 2005
Jul 4th, 2005
0

Re: 2D-Array, switches, and importing from a file

well, as a general tip for this forum, put your code in
BB tags so one can see whitespaces.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 4th, 2005
0

Re: 2D-Array, switches, and importing from a file

Sorry.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. using namespace std;
  5. #pragma hdrstop
  6.  
  7. #pragma argsused
  8. int main(int argc, char* argv[])
  9. {
  10. ifstream FASTAin("FASTA2.txt");
  11. if (!FASTAin)
  12. {
  13. cout << "File not found.\n";
  14. cout << "Press enter to exit.\n";
  15. }
  16. ofstream PHASEout("PHASE2.txt");
  17.  
  18. if (!PHASEout)
  19. {
  20. cout << "Unable to export the new PHASE.txt file.";
  21. cout << "Press enter to exit.\n";
  22. }
  23. cout << "File found.\n";
  24. cout << "This program will create or write over the file PHASE.txt.\n";
  25. cout << "Press enter continue.\n";
  26. getchar();
  27.  
  28. const int M=2;
  29. const int N=5000;
  30.  
  31. char seq1[M][N];
  32. char IDname[80]="";
  33. char ch='a';
  34. unsigned short int row=0;
  35. unsigned short int col=0;
  36.  
  37. int flipper=-9;
  38. unsigned short int t=0;
  39.  
  40. while (FASTAin.get(ch))
  41. {
  42. if (ch==10)
  43. {
  44. flipper=0;
  45. }
  46.  
  47. if (ch=='>')
  48. {
  49. flipper=1;
  50. }
  51.  
  52. if (flipper==0)
  53. {
  54. for (row=0; row<=M; row++)
  55. {
  56. for (col=0; col<=N; col++)
  57. {
  58. seq1[row][col] = ch;
  59. //Problem area
  60. }
  61. }
  62. }
  63.  
  64. if (flipper==1)
  65. {
  66. IDname[t]=ch; t++;
  67. }
  68.  
  69. }
  70.  
  71. cout << seq1[row][col];
  72. cout << "\n***End of FASTA file contents.***\n";
  73. getchar();
  74.  
  75. FASTAin.close();
  76. PHASEout.close();
  77.  
  78. getchar();
  79.  
  80. return 0;
  81. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BioTechNoob is offline Offline
5 posts
since Jun 2005
Jul 4th, 2005
0

Re: 2D-Array, switches, and importing from a file

ok, the reson your program crash is this:
C++ Syntax (Toggle Plain Text)
  1. for (row=0; row<=M; row++)
  2. {
  3. for (col=0; col<=N; col++)
  4. {
the row, and col both goes 1 val to far.
if you change it to
C++ Syntax (Toggle Plain Text)
  1. for (row=0; row<=M-1; row++)
  2. {
  3. for (col=0; col<=N-1; col++)
  4. {
it will work the way you have writen it. but I think that it aint going to work the way
you want to, since all you do is enter the ch into the whole array. so you wil get a array filld with 1 char.
I dont think you want the for loops, at least not there.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 4th, 2005
0

Re: 2D-Array, switches, and importing from a file

I see what your saying. I tried using the break; command at the end of this loop assuming it would take me back to the top of the program, get another character, and continue filling the array, but now it still hits this for loop twice before moving on to another character. Very strange. Clearly I'm thinking about this wrong. If you have any suggestions let me know, otherwise I'll simply hit the books again and come up with a different logic. Thanks for the tip though.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
BioTechNoob is offline Offline
5 posts
since Jun 2005
Jul 4th, 2005
0

Re: 2D-Array, switches, and importing from a file

I think this might help you a bit, it uses string instead of char to simplefie it a bit, but if I aint totaly wrong it does wath you want.
it only get the data from the file, and print it to screen, so you still need to make the saving work.

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int M=3; // number of enteries
  8. string store[M]; // global string array. store all enteries
  9.  
  10. void getfile() // used to get all enteries from file and put it in the store array
  11. {
  12. char ch; // used for reading the data.
  13. ifstream FASTAin("fasta.txt");
  14. if (!FASTAin)
  15. {
  16. cout << "Error: no file";
  17. }
  18. FASTAin.get(ch); // get the first char in file
  19. int row = -1;
  20. // in this while loop we get the data from the file, and divide it by enteries.
  21. while (!FASTAin.eof())
  22. {
  23. if (ch == '>') // '>' is the start of a new enterie. SO one need to change row.
  24. {
  25. row++;
  26. }
  27.  
  28. if (row > -1)
  29. {
  30. store[row] += ch; // add the char to the string.
  31. }
  32.  
  33. FASTAin.get(ch); // get next char
  34. }
  35. FASTAin.close(); // we have no more user for the file annymore.
  36. }
  37.  
  38. string getname (string data)
  39. {
  40. int start = data.find('{')+1;
  41. int end = data.find('}') - start;
  42. if (start == 0)// dident find the {
  43. {
  44. return "";
  45. }
  46. return data.substr(start, end);
  47. }
  48. string getdata (string data)
  49. {
  50. int start = data.find('\n')+1;
  51. int end = data.length() - start;
  52. if (start == -0)// dident find newline
  53. {
  54. return "";
  55. }
  56.  
  57. return data.substr(start, end);
  58. }
  59.  
  60. string getdate (string data)
  61. {
  62. int start = data.find('[')+1;
  63. int end = data.find(']') - start;
  64. if (start == 0)
  65. {
  66. return "";
  67. }
  68.  
  69. return data.substr(start, end);
  70. }
  71. // main
  72. int main()
  73. {
  74. getfile();
  75. for (int i = 0; i<M; i++)
  76. {
  77. cout << getname (store[i]) << " - " << getdate(store[i]) << endl << getdata(store[i]) << endl << endl;
  78. }
  79. int i;
  80. cin>>i;
  81. }
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: void sort2(char* &p, char* &q)
Next Thread in C++ Forum Timeline: Anonymous Objects





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC