|
string Class Examples - Genome Sequences
1. Declaration and initialization
% cat gen1.cpp
// string class example - genomics
#include<iostream>
using namespace std;
#include<string>
int main()
{
string seq1 = "GACCGCT", seq2 = "GATCACT", seq3;
cout << "Enter a sequence of nucleotides: ";
cin >> seq3;
cout << "Sequence 1 is " << seq1 <<
endl;
cout << "Sequence 3 is " << seq2 <<
endl;
cout << "Sequence 3 is " << seq3 <<
endl;
return 0;
}
% g++ gen1.cpp
% a.out
Enter a sequence of nucleotides: GAACCT
Sequence 1 is GACCGCT
Sequence 3 is GATCACT
Sequence 3 is GAACCT
2. Index (subscript) operation
% cat gen2.cpp
// string class example - genomics
#include<iostream>
using namespace std;
#include<string>
int main()
{
string seq1 = "GACCGCT", seq2 = "GATCACT", seq3;
cout << "Sequence 1 is " << seq1 <<
endl;
for( int i = 0; i < 7; i++)
cout << seq1[i] <<
" ";
cout << endl;
return 0;
}
% g++ gen2.cpp
% a.out
Sequence 1 is GACCGCT
G A C C G C T
3. Concatenation and size() fumction
% cat gen3.cpp
// string class example - genomics
#include<iostream>
using namespace std;
#include<string>
int main()
{
string seq1 = "GACCGCT", seq2 = "GATCACT", seq3;
seq3 = seq1 + seq2; // concatenation
cout << "Sequence 1 is " << seq1 <<
endl;
cout << "Sequence 2 is " << seq2 <<
endl;
cout << "Sequence 3 is " << seq3 <<
endl;
cout << "\nThe size of seq3 is " << seq3.size()
<< endl;
return 0;
}
% g++ gen3.cpp
% a.out
Sequence 1 is GACCGCT
Sequence 2 is GATCACT
Sequence 3 is GACCGCTGATCACT
The size of seq3 is 14
4. Member functions find() aand substr()
% cat gen4.cpp
// string class example - genomics
#include<iostream>
using namespace std;
#include<string>
int main()
{
string chrom = "ACTTCGGGGGGACTTCGGAATAAAAAAAGCTAA";
string gene;
int start, ending, num_chars;
// find the beginning index of the gene from the "marker"
start = chrom.find("GGGGGG", 0);
start += 6;
cout << "The start of the gene is at index
" << start << endl;
// find the beginning of the end marker
ending = chrom.find("AAAAAAA", start);
cout << "The end of the gene is at index " <<
ending-1 << endl;
// extract the gene
num_chars = ending - start;
gene = chrom.substr( start, num_chars );
cout << "The gene sequence is " << gene
<< endl;
return 0;
}
% g++ gen4.cpp
% a.out
The start of the gene is at index 11
The end of the gene is at index 20
The gene sequence is ACTTCGGAAT
5. Member functions replace() aand insert()
% cat gen5.cpp
// string class example - genomics
#include<iostream>
using namespace std;
#include<string>
int main()
{
string chrom = "ACTTCGGGGGGACTTCGGAATAAAAAAAGCTAA";
string gene;
int start, ending, num_chars;
// find the beginning index of the gene from the "marker"
start = chrom.find("GGGGGG", 0);
start += 6;
cout << "The start of the gene is at index
" << start << endl;
// find the beginning of the end marker
ending = chrom.find("AAAAAAA", start);
cout << "The end of the gene is at index " <<
ending-1 << endl;
// extract the gene
num_chars = ending - start;
gene = chrom.substr( start, num_chars );
cout << "The gene sequence is " << gene
<< endl;
// replace the gene in chrom1
string chrom1;
chrom1 = chrom;
chrom1.replace(start, num_chars, "ACTTTCGGAATT");
cout << "\nchrom1 with new replacement gene
is\n" << chrom1 << endl;
// insert new nucleotides in chrom2
string chrom2;
chrom2 = chrom;
chrom2.insert(start+4, "T");
chrom2.insert(ending, "T");
cout << "\nchrom2 with new inserted nucleotides
is\n" << chrom2 << endl;
if( chrom1 == chrom2 )
cout << "\nchrom1 and chrom2
are identical\n";
return 0;
}
% g++ gen5.cpp
% a.out
The start of the gene is at index 11
The end of the gene is at index 20
The gene sequence is ACTTCGGAAT
chrom1 with new replacement gene is
ACTTCGGGGGGACTTTCGGAATTAAAAAAAGCTAA
chrom2 with new inserted nucleotides is
ACTTCGGGGGGACTTTCGGAATTAAAAAAAGCTAA
chrom1 and chrom2 are identical
|