DNA Sequence Analysis program very slow, suggestions to improve speed?

I fixed the bug I asked assistance for previously I now would like to optimize this program to run as quickly as possible. The most important parts are the analysis and file-writing portion. I'm not very experienced and would like some advice on how to make something like this more efficient.

-edit I'm removing msot of the code, leaving the most costly parts, to make it a bit easier to read.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
//==============================Sequence Analysis==============================	
#define ANALYSIS
	cursize=maxsize;
	//==================Initialize head and set pointers to NULL===================
	head=new matchnode*[maxsize-minsize+1];
	curr=new matchnode*[maxsize-minsize+1];
	if(!head||!curr)
	{
		cout<<"could not allocate memory for operation, terminating"<<endl;
		return MEM_ALLOC_ERROR;
	}
	num1=0;
	while(num1<=(maxsize-minsize+1))
	{
		head[num1]=NULL;
		curr[num1]=NULL;
		num1++;
	}
	
	while(!(cursize<minsize))
	{
		for(num1=0;num1<(size1-cursize);num1++)
		{
			temp=0;
			temp2=size2-cursize;
			if(flags&0x200&&(num1-leeway)<=num1)
			{
				temp=num1-leeway;
			}
			else
			{
				temp=0;
			}
			if(flags&0x200&&(num1+leeway)>=num1&&(num1+leeway)<size2-cursize)
			{
				temp2=leeway+num1;
			}
			else
			{
				temp2=size2-cursize;
			}
			for(num2=temp;num2<temp2;num2++)
			{
				//==========Check if regions are homologous==========
				if(flags&0x100)
				{
					for(count=0,homnum=0;(count<cursize);count++)
					{
						homnum+=D_equiv(seq1[num1+count],seq2[num2+count]);
					}
					matchpercent=(homnum*100)/(cursize);
				}
				else
				{
					for(count=0,homnum=0;(count<cursize);count++)
					{
						homnum+=equiv(seq1[num1+count],seq2[num2+count]);
					}
					matchpercent=(homnum*100)/cursize;
				}
				//==========Check if regions are encompassed by Larger regions already found==========
				if(!(matchpercent<percent)&&(cursize<maxsize))
					{
#if 1	
						ch=backcheck(head,curr,&num1,&num2,cursize,maxsize);
#else
						ch=1;
#endif
					}
				//==========Store info about region in a matchnode==========
				if(ch)
				{
					newmatchnode=new matchnode();
					if(!newmatchnode)
					{
						puts("could not allocate memory for operation, terminating\n ");
						return MEM_ALLOC_ERROR;
					}
					newmatchnode->next=NULL;
					newmatchnode->start1=num1;
					newmatchnode->start2=num2;
					if(head[maxsize-cursize]==NULL)
					{
						head[maxsize-cursize]=newmatchnode;
						curr[maxsize-cursize]=newmatchnode;
					}
					else
					{
						curr[maxsize-cursize]->next=newmatchnode;
						curr[maxsize-cursize]=newmatchnode;
					}
					//num1=num1+count;
					num2=num2+count;
					ch=0;
				}
			}
		}
#ifdef HOMOLOGY_TEST
		cout<<"cursize: "<<cursize<<",num1: "<<num1<<",num2: "<<num2<<endl;
#endif
		curr[maxsize-cursize]=head[maxsize-cursize];
		cursize--;
	}
	for(count=0;count<(maxsize-minsize+1);count++)
	{
#ifdef HOMOLOGY_TEST
		if(!curr[count])
		{
			cout<<"Nothing found at size: "<<maxsize-count;
		}
#endif
		while(curr[count])
		{
#ifdef HOMOLOGY_TEST
			cout<<"Homology found, size: "<<maxsize-count<<", start1: "<<curr[count]->start1+1<<",start2: "<<curr[count]->start2+1<<endl;
#endif
			curr[count]=curr[count]->next;
		}
		curr[count]=head[count];
#ifdef HOMOLOGY_TEST
		ch=0;
		cin >> noskipws;
		while(ch!='\n')
		{
			cin >> ch;
		}
		cin >> skipws;
#endif
	}
	//==============================File Writing==============================
#define FILE_WRITE
#ifndef NO_FSTREAM_WRITING
	//equalline:"================================================================================\n"
	// hashline:"################################################################################\n"
	file.open(argv[output],ios::out|ios::trunc);
	delete[] curr;
	curr=NULL;
	count=0;
	num1=0;
	num2=0;
	selector=0;
	file<<starline;
	file<<"Results of Comparison Between "<<argv[input1]<<" and "<<argv[input2]<<"\n";
	file<<starline;
	file<<"Size of "<<argv[input1]<<": "<<size1<<"\tSize of "<<argv[input2]<<": "<<size2<<"\n";
	file<<starline;
	file<<"Settings Used\n";
	file<<"   minsize: "<<minsize<<"\n";
	file<<"   maxsize: "<<maxsize<<"\n";
	file<<"   percent: "<<(int)percent<<"\n";
	if(flags&0x100)
	{
		file<<"degeneracy: "<<"YES"<<"\n";
	}
	else
	{
		file<<"degeneracy: "<<"NO"<<"\n";
	}
	if(flags&0x200)
	{
		file<<"    leeway: "<<leeway<<"\n";
	}
	else
	{
		file<<"    leeway: N/A\n";
	}
	file<<starline;
	while(selector<maxsize-minsize+1)
	{
		file<<hashline;
		file<<"Region Size: "<<maxsize-selector<<"\n";
		newmatchnode=head[selector];
		if(!newmatchnode)
		{
			file<<"No matching sequences of this size were found.\n";
		}
		while(newmatchnode)
		{
			file<<equalline;
			for(count=0;count<maxsize-selector;count++)
			{
				file<<seq1[newmatchnode->start1+count];
			}
			file<<'\n';
			file<<"Start1: "<<newmatchnode->start1+1<<"\t"<<"End1: "<<newmatchnode->start1+maxsize-selector<<"\n";
			for(count=0;count<maxsize-selector;count++)
			{
				file<<seq2[newmatchnode->start2+count];
			}
			file<<'\n';
			file<<"Start2: "<<newmatchnode->start2+1<<"\t"<<"End2: "<<newmatchnode->start2+maxsize-selector<<"\n";
			newmatchnode=newmatchnode->next;
			delete head[selector];
			head[selector]=newmatchnode;
		}
		selector++;
	}
	delete [] head;
	delete [] seq1;
	delete [] seq2;
	file<<flush;
	file.close();
	return 0;
#else
	return 0;
#endif

}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

int backcheck(matchnode **head, matchnode **curr,unsigned long long *num1,unsigned long long *num2, unsigned long long cursize,unsigned long long maxsize)
{
	unsigned long long selector,start1,end1,start2,end2,enum1,enum2;
	enum1=*num1+cursize-1;
	enum2=*num2+cursize-1;
	selector=0;
	while(cursize<maxsize-selector)
	{
		while(curr[selector])
		{
			start1=curr[selector]->start1;
			start2=curr[selector]->start2;
			end1=start1+maxsize-(selector+1);
			end2=start2+maxsize-(selector+1);
			if(
					(
						((*num1>start1)&&(*num1<end1))
						||
						((*num1==start1)||(*num1==end1))
						||
						((enum1>start1)&&(enum1<end1))
						||
						((enum1==start1)||(enum1==end1))
					)
					&&
					(
						((*num2>start2)&&(*num2<end2))
						||
						((*num2==start2)||(*num2==end2))
						||
						((enum2>start2)&&(enum2<end2))
						||
						((enum2==start2)||(enum2==end2))
					)
					
				)
			{
				curr[selector]=head[selector];
				return 0;
			}
			curr[selector]=curr[selector]->next;
		}
		curr[selector]=head[selector]; 
		selector++;
	}
	return 1;
}
int equiv(char c1,char c2)
{
	/*
	A 65> adenosine           M 77> A C (amino)
	C 67> cytidine            S 83> G C (strong)
	G 71> guanine             W 87> A T (weak)
	T 84> thymidine           B 66> G T C (NOT A)
	U 85> uridine             D 68> G A T (NOT C)
	R 82> G A (purine)        H 72> A C T (NOT G)
	Y 89> T C (pyrimidine)    V 86> G C A (NOT T)
	K 75> G T (keto)          N 78> A G C T (any)
	- 45>gap of indeterminate length
	a<->z ==97<->122
	a-A=32
	*/
	switch(c1)
	{
		case 'A':
		switch(c2)
		{
			default:return 0;
			case 'A':
			case 'R':
			case 'M':
			case 'W':
			case 'N':
			return 1;
		}
		case 'C':
		switch(c2)
		{
			default:return 0;
			case 'C':
			case 'Y':
			case 'M':
			case 'S':
			case 'N':
			return 1;
		}
		case 'G':
		switch(c2)
		{
			default:return 0;
			case 'G':
			case 'R':
			case 'K':
			case 'S':
			case 'N':
			return 1;
		}
		case 'T':
		switch(c2)
		{
			default:return 0;
			case 'T':
			case 'U':
			case 'Y':
			case 'K':
			case 'W':
			case 'N':
			return 1;
		}
		case 'U':
		switch(c2)
		{
			default:return 0;
			case 'U':
			case 'T':
			case 'Y':
			case 'K':
			case 'W':
			case 'N':
			return 1;
		}
		case 'R':
		switch(c2)
		{
			default:return 0;
			case 'R':
			case 'G':
			case 'A':
			case 'N':
			return 1;
		}
		case 'Y':
		switch(c2)
		{
			default:return 0;
			case 'Y':
			case 'T':
			case 'U':
			case 'C':
			case 'N':
			return 1;
		}
		case 'K':
		switch(c2)
		{
			default:return 0;
			case 'K':
			case 'G':
			case 'T':
			case 'U':
			case 'N':
			return 1;
		}
		case 'M':
		switch(c2)
		{
			default:return 0;
			case 'M':
			case 'A':
			case 'C':
			case 'N':
			return 1;
		}
		case 'S':
		switch(c2)
		{
			default:return 0;
			case 'S':
			case 'G':
			case 'C':
			case 'N':
			return 1;
		}
		case 'W':
		switch(c2)
		{
			default:return 0;
			case 'W':
			case 'A':
			case 'T':
			case 'U':
			case 'N':
			return 1;
		}
		case 'N':
		return 1;
		default:return 0;
	}
}
int D_equiv(char c1,char c2)//A=G,C=T/U
{...}//essentially the same as equiv, with different cases in the switch 
Last edited on
would it be faster to use a two dimensional array char[26][26] and store zeroes and ones where they need to be and use character-'A' as the index or is that equivalent or slower to the switch method I used to compare symbols?
I just removed most of the code that doesn't really need optimizing (since its negligible in comparison to the effects of the code I left in the previous posts on the running time). I'm most interested in reducing the time taken to compare the DNA symbols (its not a simple== comparison since there are symbols like R that would match BOTH purines and S that would match G and C.
Have you tried running this through a profiler? I use AQTime at work and its brilliant. It can tell you how long each piece of code is taking to run, and where your bottlenecks are.

Best Practices for Developing and Optimizing Threaded Applications, Part 1
http://www.devx.com/go-parallel/Article/33534

There is no golden rule for optimising your code. We are not able to look at the code and fine tune it. There will be speed differences even between compilers. It's upto you to run and performance tune it based upon your results.
int equiv(char c1,char c2)

can be compress!
But it just have little effect!
I changed a bit of the program, using an STL container type and changed some other things and it seems to be faster now than it had been.
Topic archived. No new replies allowed.