I've been working on a program that stores the data from a fasta file to a vector. The user can then search the vector by searching for a line number and the program will output the header line and the sequence line that comes beneath the header line. My code is nowhere near finished so I apologize in advance for that. My question is, how would I be able to search by line number as opposed to line content. For example if I type "55", the program will output ">55" and then the sequence beneath it. Right now, I have to type in the whole header line to search for it.
Here's a portion of my input file (fasta file):
seq1
GGGCCCAU-A
//Homework #5 - Fasta File Reader Program
//Anneliese Schorpp-Dettmann
#include <iostream> //directives
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <cstdlib>
usingnamespace std;
int main (){ //main program begins
ifstream fastafile; //variables
string content, line, filename;
vector <string> myvector;
string name;
int seqLength=0;
vector <string>:: iterator it;
cout<<"Input fasta file name: " << endl; //prompt user to input a fasta file name
getline(cin, filename);
fastafile.open(filename.c_str ()); //open fasta file
while (!fastafile.is_open ()) {
cout <<"Fasta file could not be opened. Session terminated."<<endl;
exit (EXIT_FAILURE); //ends program if fasta file cannot be opened
break;
}
while(getline (fastafile, line)) {
seqLength= line.length();
fastafile >> line; //store data from fasta file to vector
myvector.push_back(line);
cout <<line<<endl;
}
cout<<"Select sequence number."<<endl; //prompt user to enter a sequence number
getline (cin, name);
it = find (myvector.begin(), myvector.end (), name);
if (it != myvector.end()) {
cout<<"The name of your molecule is:"<<endl;
cout<<*it<<endl;
}
else {
cout <<"The name of the sequence number you entered could not be found."<<endl;
exit (EXIT_FAILURE);
}
cout<<"The sequence of your molecule is:"<<endl;
cout<<"The length of your sequence is:"<<seqLength<<endl;
cout<<"There are a total of "<< myvector.size ()/2 <<" sequences in your alignment."<<endl;
return 0;
}
If you want to extract from the vector by using a number you could usemyvector[x] with 'x' being a subscript just as you would use with an array. Otherwise the search that you have will work.