Commit 3584d7b6 authored by Tony Cheung's avatar Tony Cheung

Upload New File

parent 4dc62394
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int GetTotalchar(string filename)
{
ifstream inFile(filename);
int size = 0;
char c;
while (inFile.get(c))
{
size++;
}
inFile.close();
return size;
}
char* GetSequence(string filename, int total)
{
ifstream inFile(filename);
// allocate memory for storing sequence
char* sequence = new char[total];
char c;
int i = 0;
while (inFile.get(c))
{
sequence[i] = c;
i++;
}
inFile.close();
return sequence;
}
void CountSequence(char *sequence, int totalchar)
{
int count_A = 0;
int count_T = 0;
int count_C = 0;
int count_G = 0;
char c;
for (int i = 0; i < totalchar; i++)
{
c = sequence[i];
if (c == 'A')
count_A++;
if (c == 'T')
count_T++;
if (c == 'C')
count_C++;
if (c == 'G')
count_G++;
}
cout << "Total number of A is: " << count_A << endl;
cout << "Total number of T is: " << count_T << endl;
cout << "Total number of C is: " << count_C << endl;
cout << "Total number of G is: " << count_G << endl;
cout << "The length of the sequence is: " << totalchar << endl;
cout << endl;
}
void AnalyseSequence(char *sequence1, char *sequence2, int totalchar1, int totalchar2)
{
char c1, c2;
int CountIdenticalChar = 0;
double similarity = 0.;
for (int i = 0; i < totalchar1; i++)
{
c1 = sequence1[i];
c2 = sequence2[i];
if (c1 == c2)
CountIdenticalChar++;
}
similarity = ((double) CountIdenticalChar / (double) totalchar1) * 100;
cout << "The similarity of two sequences is: " << similarity << "%" << endl;
}
int main()
{
int totalchar1 = 0;
int totalchar2 = 0;
char* sequence1 = nullptr;
char* sequence2 = nullptr;
string filename1 = "Sequence1.txt";
string filename2 = "Sequence2.txt";
totalchar1 = GetTotalchar(filename1);
sequence1 = GetSequence(filename1, totalchar1);
totalchar2 = GetTotalchar(filename2);
sequence2 = GetSequence(filename2, totalchar2);
cout << "Counting for Sequence 1" << endl;
CountSequence(sequence1, totalchar1);
cout << "Counting for Sequence 2" << endl;
CountSequence(sequence2, totalchar2);
AnalyseSequence(sequence1, sequence2, totalchar1, totalchar2);
// release the memory
delete[] sequence1;
delete[] sequence2;
sequence1 = nullptr;
sequence2 = nullptr;
return 0;
}
//output
/*
Counting for Sequence 1
Total number of A is: 256
Total number of T is: 244
Total number of C is: 240
Total number of G is: 260
The length of the sequence is: 1000
Counting for Sequence 2
Total number of A is: 239
Total number of T is: 267
Total number of C is: 257
Total number of G is: 237
The length of the sequence is: 1000
The similarity of two sequences is: 25.4%
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment