T O P I C R E V I E W |
Vidyashankara |
Posted - 01/10/2012 : 6:32:55 PM Origin Ver. and Service Release (Select Help-->About Origin): 8.0 Operating System: Windows 7
Hi All, I am really not a programming guy. I am a protein scientist and my knowledge with C is very close to nill. A few years ago, we had a guy in our lab write us the following code. It works very well with Origin 7. It was written to peak the peak maxima for a bunch of peak and report them in a separate window with the position of peak and its peak height. For some reason, this does not work with origin 8. Is there anyone who can help me with this? Any suggestions will be appreciated. -Vidyashankara
void calc_peaks(string strWks, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
clean_null_rows(strWks);
Worksheet data(strWks);
Worksheet wdata;
Worksheet idata;
wdata.Create();
wdata.GetPage().Rename(strWks+type+"W");
wdata.AddCol();
idata.Create();
idata.GetPage().Rename(strWks+type+"I");
printf("Calculating peaks for %s...\n", strWks);
for(int i = 1; i < data.GetNumCols(); i++) {
/*
float Min_y = atof(data.TCell(0, i));
float Min_x = atof(data.TCell(0, 0));
float Max_y = 0;
float Max_x = 0;
for(int j = 0; j < data.GetNumRows(); j++) {
double intensity = atof(data.TCell(j, i));
double wavelength = atof(data.TCell(j, 0));
if(intensity < Min_y) {
Min_y = intensity;
Min_x = wavelength;
}
if((intensity > Max_y) && (wavelength > Min_x) && (wavelength > minWavelength)) {
Max_y = intensity;
Max_x = wavelength;
}
}
*/
vector<double> peak(2);
if(type.Compare("max") == 0) {
peak = max_peak(data, i, minWavelength);
} else if (type.Compare("msm") == 0) {
peak = msm(data.GetPage().GetName(), i);
} else if (type.Compare("dpoly") == 0) {
peak = dpoly(data.GetPage().GetName(), i, minWavelength);
} else if (type.Compare("dsmooth") == 0) {
peak = dsmooth(data.GetPage().GetName(), i, minWavelength);
} else {
printf("Incorrect peak calculation type.\n");
return;
}
double Max_x = peak[0];
double Max_y = peak[1];
double hpw = half_peak_width(data.GetPage().GetName(), i, Max_y);
float temp = (i-1)*tempinc+tempbegin;
//float temp = (i-1)*2.5+10;
if((Max_x > 0) || (Max_y > 0)) {
wdata.SetCell(i-1, 0, temp);
idata.SetCell(i-1, 0, temp);
wdata.SetCell(i-1, 1, Max_x);
wdata.SetCell(i-1, 2, hpw);
idata.SetCell(i-1, 1, Max_y);
}
}
plot_cols(wdata.GetPage().GetName());
plot_cols(idata.GetPage().GetName());
}
|
7 L A T E S T R E P L I E S (Newest First) |
Vidyashankara |
Posted - 01/27/2012 : 11:13:53 AM I sent my project file via the general contact form. Hoping for a solution soon! |
Penn |
Posted - 01/18/2012 : 9:32:30 PM Hi,
I am afraid we need your project file to run the code, so to see what the problem is. Could you please send your project to technical support by the instruction in this page? Please refer to this post in your email.
Penn |
Vidyashankara |
Posted - 01/18/2012 : 12:04:47 PM I dont get any error messages... but when I run the code, it is supposed to pick out the peak positions of the various peaks and show it.
For instance, lets say you have this a number of traces with wavelength on the x-axis and intensity on the y axis. All the traces are gaussian-like curves. Assume the traces are the fluoresence spectra of a certain compound at different temperatures... the first trace is the peak you get from the compound at 10 C, next at 12.5 C, then at 15 C and so on...
the command "calc_peaks book1 msm 310 10 2.5" should list the peak positions and peak intensities for the multiple curves... msm is the method you use for peak picking, 310 is the wavelength that the software use to start integrating from, is the temperature at first trace was measured and 2.5 is the temperature increment at which the first trace was measured.
When I do that in Origin 7.0, it lists all the peak position and the graphs, in origin 8, i just get blank sheets and graphs with no data in it...
|
Penn |
Posted - 01/15/2012 : 9:49:41 PM Hi,
I have created the same files and copy the same code to the corresponding file in Origin 8. However, all the codes can be compiled successfully. Then what is the wrong when you try the code? Compile error? Or error happens when running the code? Please provide the error message. You can open the Code Builder to see the error message.
Penn |
Vidyashankara |
Posted - 01/13/2012 : 3:22:18 PM I can find all the functions in there... i think some function thats supported in origin 7 is not supported in origin 8 :S |
Vidyashankara |
Posted - 01/13/2012 : 3:18:55 PM Sorry for the trouble. When I had origin 7.0, I used to compile the below three files and it used to work flawlessly. I am sure all the functions are in here..
Fluoro_analysis.cpp
#include <origin.h>
void ans_batch(string wks, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
ans_diff(wks, minWavelength);
calc_all("S2", minWavelength, tempbegin, tempinc);
calc_all("S4", minWavelength, tempbegin, tempinc);
}
void int_batch(string wks, string buffer, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diff(wks, buffer, minWavelength);
calc_all("S1", minWavelength, tempbegin, tempinc);
calc_all("S2", minWavelength, tempbegin, tempinc);
calc_all("S3", minWavelength, tempbegin, tempinc);
calc_all("S4", minWavelength, tempbegin, tempinc);
}
void ans_batchn(string wks, int samples, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
ans_diffn(wks, samples, minWavelength);
switch(samples) {
case 4:
calc_all("S4", minWavelength, tempbegin, tempinc);
case 2:
calc_all("S2", minWavelength, tempbegin, tempinc);
}
}
void int_batchn(string wks, string buffer, int samples, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diffn(wks, buffer, samples, minWavelength);
switch(samples) {
case 4:
calc_all("S4", minWavelength, tempbegin, tempinc);
case 3:
calc_all("S3", minWavelength, tempbegin, tempinc);
case 2:
calc_all("S2", minWavelength, tempbegin, tempinc);
case 1:
calc_all("S1", minWavelength, tempbegin, tempinc);
}
}
void calc_all(string wks, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
calc_peaks(wks, "max", minWavelength, tempbegin, tempinc);
calc_peaks(wks, "msm", minWavelength, tempbegin, tempinc);
calc_peaks(wks, "dpoly", minWavelength, tempbegin, tempinc);
}
/*
void ans_batch(string wks) {
ans_process(wks, "max");
ans_process(wks, "msm");
ans_process(wks, "dpoly");
}
*/
/*
void int_batch(string wks, string buffer) {
int_process(wks, buffer, "msm");
int_process(wks, buffer, "max");
int_process(wks, buffer, "dpoly");
}
*/
void ans_process(string strWks, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
ans_diff(strWks, minWavelength);
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
calc_peaks("S4", type, minWavelength, tempbegin, tempinc);
}
void ans_processn(string strWks, int samples, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
ans_diffn(strWks, samples, minWavelength);
switch(samples) {
case 4:
calc_peaks("S4", type, minWavelength, tempbegin, tempinc);
case 2:
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
}
}
void int_process(string strWksProtein, string strWksBuffer, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diff(strWksProtein, strWksBuffer, minWavelength);
calc_peaks("S1", type, minWavelength, tempbegin, tempinc);
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
calc_peaks("S3", type, minWavelength, tempbegin, tempinc);
calc_peaks("S4", type, minWavelength, tempbegin, tempinc);
}
void int_processn(string strWksProtein, string strWksBuffer, int samples, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diffn(strWksProtein, strWksBuffer, samples, minWavelength);
switch(samples) {
case 4:
calc_peaks("S4", type, minWavelength, tempbegin, tempinc);
case 3:
calc_peaks("S3", type, minWavelength, tempbegin, tempinc);
case 2:
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
case 1:
calc_peaks("S1", type, minWavelength, tempbegin, tempinc);
}
}
void int_process1(string strWksProtein, string strWksBuffer, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diff1(strWksProtein, strWksBuffer, minWavelength);
calc_peaks("S1", type, minWavelength, tempbegin, tempinc);
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
calc_peaks("S3", type, minWavelength, tempbegin, tempinc);
calc_peaks("S4", type, minWavelength, tempbegin, tempinc);
}
void int_process2(string strWksProtein, string strWksBuffer, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diff2(strWksProtein, strWksBuffer, minWavelength);
calc_peaks("S1", type, minWavelength, tempbegin, tempinc);
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
}
void int_process3(string strWksProtein, string strWksBuffer, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diff3(strWksProtein, strWksBuffer, minWavelength);
calc_peaks("S1", type, minWavelength, tempbegin, tempinc);
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
calc_peaks("S3", type, minWavelength, tempbegin, tempinc);
}
void int_process_grp(string strWksProtein, string strWksBuffer, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
int_diff_grouped(strWksProtein, strWksBuffer, minWavelength);
calc_peaks("S1", type, minWavelength, tempbegin, tempinc);
calc_peaks("S2", type, minWavelength, tempbegin, tempinc);
calc_peaks("S3", type, minWavelength, tempbegin, tempinc);
calc_peaks("S4", type, minWavelength, tempbegin, tempinc);
}
void ans_diff(string strWks, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWks);
Worksheet diff_data2, diff_data4;
diff_data2.Create();
diff_data4.Create();
diff_data2.GetPage().Rename("S2");
diff_data4.GetPage().Rename("S4");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data2.SetCell(k, 0, wavelength);
diff_data4.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=8) {
int index = floor(i/8) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+3)) - atof(raw_data.TCell(j, i+1));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+7)) - atof(raw_data.TCell(j, i+5));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
}
}
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void ans_diffn(string strWks, int samples, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWks);
Worksheet diff_data2, diff_data4;
switch(samples) {
case 4:
//Worksheet diff_data4;
diff_data4.Create();
diff_data4.GetPage().Rename("S4");
case 2:
//Worksheet diff_data2:
diff_data2.Create();
diff_data2.GetPage().Rename("S2");
}
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
switch(samples) {
case 4:
diff_data4.SetCell(k, 0, wavelength);
case 2:
diff_data2.SetCell(k, 0, wavelength);
}
}
}
int offset = 2*samples;
for(int i = 0; i < raw_data.GetNumCols(); i+=offset) {
int index = floor(i/offset) + 1;
printf("Processing trace set %d...\n", index);
switch(samples) {
case 4:
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
case 2:
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
switch(samples) {
case 4:
diff = atof(raw_data.TCell(j, i+7)) - atof(raw_data.TCell(j, i+5));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
case 2:
diff = atof(raw_data.TCell(j, i+3)) - atof(raw_data.TCell(j, i+1));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
}
}
}
}
void ans_diff3(string strWks, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWks);
Worksheet diff_data2, diff_data3 ,diff_data4;
diff_data2.Create();
diff_data3.Create();
diff_data4.Create();
diff_data2.GetPage().Rename("S2");
diff_data3.GetPage().Rename("S3");
diff_data4.GetPage().Rename("S4");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data2.SetCell(k, 0, wavelength);
diff_data3.SetCell(k, 0, wavelength);
diff_data4.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=8) {
int index = floor(i/8) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+3)) - atof(raw_data.TCell(j, i+1));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+5)) - atof(raw_data.TCell(j, i+1));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+7)) - atof(raw_data.TCell(j, i+1));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
}
}
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void int_diff1(string strWksProtein, string strWksBuffer, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data1, diff_data2, diff_data3, diff_data4;
diff_data1.Create();
diff_data2.Create();
diff_data3.Create();
diff_data4.Create();
diff_data1.GetPage().Rename("S1");
diff_data2.GetPage().Rename("S2");
diff_data3.GetPage().Rename("S3");
diff_data4.GetPage().Rename("S4");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data1.SetCell(k, 0, wavelength);
diff_data2.SetCell(k, 0, wavelength);
diff_data3.SetCell(k, 0, wavelength);
diff_data4.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=8) {
int index = floor(i/8) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
diff = atof(raw_data.TCell(j, i+3)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+5)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+7)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
}
}
//calc_peaks(diff_data1.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data3.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void int_diff(string strWksProtein, string strWksBuffer, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data1, diff_data2, diff_data3, diff_data4;
diff_data1.Create();
diff_data2.Create();
diff_data3.Create();
diff_data4.Create();
diff_data1.GetPage().Rename("S1");
diff_data2.GetPage().Rename("S2");
diff_data3.GetPage().Rename("S3");
diff_data4.GetPage().Rename("S4");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data1.SetCell(k, 0, wavelength);
diff_data2.SetCell(k, 0, wavelength);
diff_data3.SetCell(k, 0, wavelength);
diff_data4.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=8) {
int index = floor(i/8) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
diff = atof(raw_data.TCell(j, i+3)) - atof(buffer.TCell(j, 3));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+5)) - atof(buffer.TCell(j, 5));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+7)) - atof(buffer.TCell(j, 7));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
}
}
//calc_peaks(diff_data1.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data3.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void total_diff(string strWksProtein, string strWksBuffer) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data1;
diff_data1.Create();
diff_data1.GetPage().Rename("S1");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data1.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=8) {
int index = floor(i/8) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, i+1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
}
}
}
void int_diffn(string strWksProtein, string strWksBuffer, int samples, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data1, diff_data2, diff_data3, diff_data4;
/*
diff_data1.Create();
diff_data2.Create();
diff_data3.Create();
diff_data4.Create();
diff_data1.GetPage().Rename("S1");
diff_data2.GetPage().Rename("S2");
diff_data3.GetPage().Rename("S3");
diff_data4.GetPage().Rename("S4");
*/
switch(samples) {
case 4:
diff_data4.Create();
diff_data4.GetPage().Rename("S4");
case 3:
diff_data3.Create();
diff_data3.GetPage().Rename("S3");
case 2:
diff_data2.Create();
diff_data2.GetPage().Rename("S2");
case 1:
diff_data1.Create();
diff_data1.GetPage().Rename("S1");
}
/*
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data1.SetCell(k, 0, wavelength);
diff_data2.SetCell(k, 0, wavelength);
diff_data3.SetCell(k, 0, wavelength);
diff_data4.SetCell(k, 0, wavelength);
}
}
*/
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
switch(samples) {
case 4:
diff_data4.SetCell(k, 0, wavelength);
case 3:
diff_data3.SetCell(k, 0, wavelength);
case 2:
diff_data2.SetCell(k, 0, wavelength);
case 1:
diff_data1.SetCell(k, 0, wavelength);
}
}
}
int offset = 2*samples;
for(int i = 0; i < raw_data.GetNumCols(); i+=offset) {
int index = floor(i/offset) + 1;
printf("Processing trace set %d...\n", index);
switch(samples) {
case 4:
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
case 3:
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
case 2:
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
case 1:
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
switch(samples) {
case 4:
diff = atof(raw_data.TCell(j, i+7)) - atof(buffer.TCell(j, 7));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
case 3:
diff = atof(raw_data.TCell(j, i+5)) - atof(buffer.TCell(j, 5));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
case 2:
diff = atof(raw_data.TCell(j, i+3)) - atof(buffer.TCell(j, 3));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
case 1:
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
}
}
}
/*
for(int i = 0; i < raw_data.GetNumCols(); i+=8) {
int index = floor(i/8) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
diff = atof(raw_data.TCell(j, i+3)) - atof(buffer.TCell(j, 3));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+5)) - atof(buffer.TCell(j, 5));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+7)) - atof(buffer.TCell(j, 7));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
}
}
*/
//calc_peaks(diff_data1.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data3.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void int_diff2(string strWksProtein, string strWksBuffer, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data1, diff_data2;
diff_data1.Create();
diff_data2.Create();
diff_data1.GetPage().Rename("S1");
diff_data2.GetPage().Rename("S2");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data1.SetCell(k, 0, wavelength);
diff_data2.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=4) {
int index = floor(i/4) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
diff = atof(raw_data.TCell(j, i+3)) - atof(buffer.TCell(j, 3));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
}
}
//calc_peaks(diff_data1.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data3.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void int_diff3(string strWksProtein, string strWksBuffer, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data1, diff_data2, diff_data3;
diff_data1.Create();
diff_data2.Create();
diff_data3.Create();
diff_data1.GetPage().Rename("S1");
diff_data2.GetPage().Rename("S2");
diff_data3.GetPage().Rename("S3");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data1.SetCell(k, 0, wavelength);
diff_data2.SetCell(k, 0, wavelength);
diff_data3.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=6) {
int index = floor(i/6) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
diff = atof(raw_data.TCell(j, i+3)) - atof(buffer.TCell(j, 3));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+5)) - atof(buffer.TCell(j, 5));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
}
}
//calc_peaks(diff_data1.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data3.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void int_diff_grouped(string strWksProtein, string strWksBuffer, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data1, diff_data2, diff_data3, diff_data4;
diff_data1.Create();
diff_data2.Create();
diff_data3.Create();
diff_data4.Create();
diff_data1.GetPage().Rename("S1");
diff_data2.GetPage().Rename("S2");
diff_data3.GetPage().Rename("S3");
diff_data4.GetPage().Rename("S4");
int data_size = raw_data.GetNumCols() / 4;
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data1.SetCell(k, 0, wavelength);
diff_data2.SetCell(k, 0, wavelength);
diff_data3.SetCell(k, 0, wavelength);
diff_data4.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols()/4; i+=2) {
int index = floor(i/2) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data1.Columns(index).IsValid()) {
diff_data1.AddCol();
}
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
if(diff) {
diff_data1.SetCell(j, index, diff);
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, 1)), diff);
}
diff = atof(raw_data.TCell(j, i+1+data_size)) - atof(buffer.TCell(j, 3));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+1+(2*data_size))) - atof(buffer.TCell(j, 5));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+1+(3*data_size))) - atof(buffer.TCell(j, 7));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
}
}
//calc_peaks(diff_data1.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data3.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void subtract(string strWksProtein, string strWksBuffer, float minWavelength = 0, float maxWavelength = 600) {
Worksheet raw_data(strWksProtein);
Worksheet buffer(strWksBuffer);
Worksheet diff_data;//, diff_data2, diff_data3, diff_data4;
diff_data.Create();
//diff_data2.Create();
//diff_data3.Create();
//diff_data4.Create();
diff_data.GetPage().Rename("sub");
//diff_data2.GetPage().Rename("S2");
//diff_data3.GetPage().Rename("S3");
//diff_data4.GetPage().Rename("S4");
for(int k = 0; k < raw_data.GetNumRows(); k++) {
double wavelength = atof(raw_data.TCell(k, 0));
if(wavelength) {
diff_data.SetCell(k, 0, wavelength);
//diff_data2.SetCell(k, 0, wavelength);
//diff_data3.SetCell(k, 0, wavelength);
//diff_data4.SetCell(k, 0, wavelength);
}
}
for(int i = 0; i < raw_data.GetNumCols(); i+=2) {
int index = floor(i/2) + 1;
printf("Processing trace set %d...\n", index);
if(!diff_data.Columns(index).IsValid()) {
diff_data.AddCol();
}
/*
if(!diff_data2.Columns(index).IsValid()) {
diff_data2.AddCol();
}
if(!diff_data3.Columns(index).IsValid()) {
diff_data3.AddCol();
}
if(!diff_data4.Columns(index).IsValid()) {
diff_data4.AddCol();
}
*/
for(int j = 0; j < raw_data.GetNumRows(); j++) {
double diff;
diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, i+1));
//diff = atof(raw_data.TCell(j, i+1)) - atof(buffer.TCell(j, 1));
//printf("%f - %f = %f\n", atof(raw_data.TCell(j, i+1)), atof(buffer.TCell(j, i+1)), diff);
if(diff) {
diff_data.SetCell(j, index, diff);
}
/*diff = atof(raw_data.TCell(j, i+3)) - atof(buffer.TCell(j, 3));
if(diff) {
diff_data2.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+5)) - atof(buffer.TCell(j, 5));
if(diff) {
diff_data3.SetCell(j, index, diff);
}
diff = atof(raw_data.TCell(j, i+7)) - atof(buffer.TCell(j, 7));
if(diff) {
diff_data4.SetCell(j, index, diff);
}
*/
}
}
//calc_peaks(diff_data1.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data2.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data3.GetPage().GetName(), type, minWavelength);
//calc_peaks(diff_data4.GetPage().GetName(), type, minWavelength);
}
void plot_cols(string strWks) {
Worksheet data(strWks);
printf("Plotting %s:\n", strWks);
for(int i = 1; i < data.GetNumCols(); i++) {
string ColName = data.Columns(i).GetName();
printf("Plotting column %s...\n", ColName);
GraphPage graph;
graph.Create("line", CREATE_VISIBLE);
graph.Rename(strWks+".plot"+ColName);
GraphLayer layer = graph.Layers(0);
Curve cv(strWks+"_"+ColName);
//printf("smoothing...\n");
//smooth(cv);
layer.AddPlot(cv);
layer.Rescale();
//double[9] coeff;
//fitpoly(cv, 9, &coeff);
//printf("coefficients: \n%f\n", coeff);
//printf("Plotting trace set %d...\n", index);
//string s2name = diff_data2.GetPage().GetName()+"_"+diff_data2.Columns(index).GetName();
//string s4name = diff_data4.GetPage().GetName()+"_"+diff_data4.Columns(index).GetName();
//printf("%s\n%s\n", s2name, s4name);
/*
GraphPage graph2;
graph2.Create("line", CREATE_VISIBLE);
graph2.Rename("trace"+index+"sample2");
GraphPage graph4;
graph4.Create("line", CREATE_VISIBLE);
graph4.Rename("trace"+index+"sample4");
GraphLayer layer2 = graph2.Layers(0);
GraphLayer layer4 = graph4.Layers(0);
Curve cv2(diff_data2.GetPage().GetName()+"_"+diff_data2.Columns(index).GetName());
Curve cv4(diff_data4.GetPage().GetName()+"_"+diff_data4.Columns(index).GetName());
smooth(cv2);
smooth(cv4);
layer2.AddPlot(cv2);
layer4.AddPlot(cv4);
layer2.Rescale();
layer4.Rescale();
*/
}
}
void calc_peaks(string strWks, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
clean_null_rows(strWks);
Worksheet data(strWks);
Worksheet wdata;
Worksheet idata;
wdata.Create();
wdata.GetPage().Rename(strWks+type+"W");
wdata.AddCol();
idata.Create();
idata.GetPage().Rename(strWks+type+"I");
printf("Calculating peaks for %s...\n", strWks);
for(int i = 1; i < data.GetNumCols(); i++) {
/*
float Min_y = atof(data.TCell(0, i));
float Min_x = atof(data.TCell(0, 0));
float Max_y = 0;
float Max_x = 0;
for(int j = 0; j < data.GetNumRows(); j++) {
double intensity = atof(data.TCell(j, i));
double wavelength = atof(data.TCell(j, 0));
if(intensity < Min_y) {
Min_y = intensity;
Min_x = wavelength;
}
if((intensity > Max_y) && (wavelength > Min_x) && (wavelength > minWavelength)) {
Max_y = intensity;
Max_x = wavelength;
}
}
*/
vector<double> peak(2);
if(type.Compare("max") == 0) {
peak = max_peak(data, i, minWavelength);
} else if (type.Compare("msm") == 0) {
peak = msm(data.GetPage().GetName(), i);
} else if (type.Compare("dpoly") == 0) {
peak = dpoly(data.GetPage().GetName(), i, minWavelength);
} else if (type.Compare("dsmooth") == 0) {
peak = dsmooth(data.GetPage().GetName(), i, minWavelength);
} else {
printf("Incorrect peak calculation type.\n");
return;
}
double Max_x = peak[0];
double Max_y = peak[1];
double hpw = half_peak_width(data.GetPage().GetName(), i, Max_y);
float temp = (i-1)*tempinc+tempbegin;
//float temp = (i-1)*2.5+10;
if((Max_x > 0) || (Max_y > 0)) {
wdata.SetCell(i-1, 0, temp);
idata.SetCell(i-1, 0, temp);
wdata.SetCell(i-1, 1, Max_x);
wdata.SetCell(i-1, 2, hpw);
idata.SetCell(i-1, 1, Max_y);
}
}
plot_cols(wdata.GetPage().GetName());
plot_cols(idata.GetPage().GetName());
}
void calc_peak(string strWks, int i, string type, float minWavelength = 0, float tempbegin = 12, float tempinc = 3) {
clean_null_rows(strWks);
Worksheet data(strWks);
//Worksheet wdata;
//Worksheet idata;
//wdata.Create();
//wdata.GetPage().Rename(strWks+type+"W");
//idata.Create();
//idata.GetPage().Rename(strWks+type+"I");
printf("Calculating peaks for %s...\n", strWks);
/*
float Min_y = atof(data.TCell(0, i));
float Min_x = atof(data.TCell(0, 0));
float Max_y = 0;
float Max_x = 0;
for(int j = 0; j < data.GetNumRows(); j++) {
double intensity = atof(data.TCell(j, i));
double wavelength = atof(data.TCell(j, 0));
if(intensity < Min_y) {
Min_y = intensity;
Min_x = wavelength;
}
if((intensity > Max_y) && (wavelength > Min_x) && (wavelength > minWavelength)) {
Max_y = intensity;
Max_x = wavelength;
}
}
*/
vector<double> peak(2);
if(type.Compare("max") == 0) {
peak = max_peak(data, i, minWavelength);
} else if (type.Compare("msm") == 0) {
peak = msm(data.GetPage().GetName(), i);
} else if (type.Compare("dpoly") == 0) {
peak = dpoly(data.GetPage().GetName(), i, minWavelength, 1);
} else if (type.Compare("dsmooth") == 0) {
peak = dsmooth(data.GetPage().GetName(), i, minWavelength, 1);
} else {
printf("Incorrect peak calculation type.\n");
return;
}
double Max_x = peak[0];
double Max_y = peak[1];
/*
float temp = (i-1)*tempinc+tempbegin;
//float temp = (i-1)*2.5+10;
if((Max_x > 0) && (Max_y > 0)) {
wdata.SetCell(i-1, 0, temp);
idata.SetCell(i-1, 0, temp);
wdata.SetCell(i-1, 1, Max_x);
idata.SetCell(i-1, 1, Max_y);
}
*/
//plot_cols(wdata.GetPage().GetName());
//plot_cols(idata.GetPage().GetName());
}
double half_peak_width(string strWks, int col, double height) {
//printf("Calculating half peak width for height %f...\n", height);
Worksheet data(strWks);
double target_height = height;
double width = 0.0;
int height_index = 0;
double height_diff = height;
int peak_index = 0;
//find index of point closest to desired height
//printf("Finding height match...");
for(int row = 0; row < data.GetNumRows(); row++) {
double intensity = atof(data.TCell(row, col));
//printf("\nExamining %f...", intensity);
double temp = abs(target_height - intensity);
if(temp < height_diff) {
height = intensity;
height_diff = temp;
peak_index = row;
//printf("chose height %f at index %f.", height, row);
}
}
//get half height
double half_height = height / 2;
//find wavelength where half height occurs
//beign by finding closest half height point to left of peak
double left_wavelength = 0.0;
double left_half_diff = half_height;
for(row = peak_index; row >= 0; row--) {
double intensity = atof(data.TCell(row, col));
double temp = abs(half_height - intensity);
if(temp < left_half_diff) {
//half_height = intensity;
left_half_diff = temp;
left_wavelength = atof(data.TCell(row, 0));
}
}
//next find closest half height point to right of peak
double right_wavelength = 0.0;
double right_half_diff = half_height;
for(row = peak_index; row < data.GetNumRows(); row++) {
double intensity = atof(data.TCell(row, col));
double temp = abs(half_height - intensity);
if(temp < right_half_diff) {
//half_height = intensity;
right_half_diff = temp;
right_wavelength = atof(data.TCell(row, 0));
}
}
width = right_wavelength - left_wavelength;
//printf("Width at half peak height %f...H/L/R/W: %f/%f/%f/%f\n", target_height, half_height, left_wavelength, right_wavelength, width);
printf("Half peak width: %f\n", width);
return width;
}
vector<double> max_peak(Worksheet data, int col, float minWavelength = 0) {
int i = col;
float Min_y = atof(data.TCell(0, i));
float Min_x = atof(data.TCell(0, 0));
float Max_y = 0;
float Max_x = 0;
printf("Calculating max peak for %s, column %d...", data.GetPage().GetName(), col);
for(int j = 0; j < data.GetNumRows(); j++) {
double intensity = atof(data.TCell(j, i));
double wavelength = atof(data.TCell(j, 0));
if(intensity < Min_y) {
Min_y = intensity;
Min_x = wavelength;
}
if((intensity > Max_y) && (wavelength > Min_x) && (wavelength > minWavelength)) {
Max_y = intensity;
Max_x = wavelength;
}
}
printf("%f,%f\n", Max_x, Max_y);
vector<double> peak(2);
peak[0] = Max_x;
peak[1] = Max_y;
return peak;
}
vector<double> msm(string strWks, int col) {
Worksheet data(strWks);
/*
Worksheet msmdata;
msmdata.Create();
//msmdata.GetPage().Rename(strWks+"MSM"+col);
msmdata.GetPage().Rename("temp");
msmdata.AddCol();
msmdata.AddCol();
msmdata.AddCol();
*/
double msm_w = 0;
double msm_i = 0;
int rows = data.GetNumRows();
vector<double> msm1(rows);
vector<double> msm2(rows);
//double[rows] msm3;
printf("Calculating mean spectral mass for %s, column %d...", strWks, col);
for(int j = 0; j < rows; j++) {
double intensity = atof(data.TCell(j, col));
double wavelength = atof(data.TCell(j, 0));
//msmdata.SetCell(j, 0, wavelength);
//msmdata.SetCell(j, 1, intensity);
double temp = wavelength*intensity;
if(j > 0) {
//temp += atof(msmdata.TCell(j-1, 2));
temp += msm1[j-1];
}
//msmdata.SetCell(j, 2, temp);
msm1[j] = temp;
temp = intensity;
if(j > 0) {
//temp += atof(msmdata.TCell(j-1, 3));
temp += msm2[j-1];
}
//msmdata.SetCell(j, 3, temp);
msm2[j] = temp;
/*
if(j == data.GetNumRows()-1) {
//msm_w = atof(msmdata.TCell(j, 2)) / atof(msmdata.TCell(j, 3));
msm_w = msm1[j] / msm2[j];
//msmdata.SetCell(j, 4, msm_w);
msm3[j] = msm_w;
}
*/
}
msm_w = msm1[rows-1] / msm2[rows-1];
for(j = 0; j < data.GetNumRows(); j++) {
double wavelength = atof(data.TCell(j, 0));
if(abs(wavelength - msm_w) < .5) {
msm_i = atof(data.TCell(j, col));
}
}
printf("%f,%f\n", msm_w, msm_i);
vector<double> peak(2);
peak[0] = msm_w;
peak[1] = msm_i;
//msmdata.Destroy();7
return peak;
}
vector<double> dpoly(string wks, int col, float minWavelength = 0, int order = 7, int writedata = 0) {
clean_null_rows(wks);
vector<double> poly_w;
vector<double> poly_i;
printf("Calculating poly-deriv peak for %s, column %d...", wks, col);
matrix<double> poly = get_smooth(wks, col, writedata);//get_poly(wks, col, order, writedata);
poly.GetRow(poly_w, 0);
poly.GetRow(poly_i, 1);
Curve cvPoly(poly_w, poly_i);
double dpoly_w = get_deriv(cvPoly, minWavelength, writedata);
double dpoly_i = Curve_yfromX(&cvPoly, dpoly_w);
printf("%f,%f\n", dpoly_w, dpoly_i);
vector<double> peak(2);
peak[0] = dpoly_w;
peak[1] = dpoly_i;
return peak;
}
matrix<double> get_poly(string wks, int col, int order, int writedata = 0) {
Worksheet data(wks);
string ColName = data.Columns(col).GetName();
Curve cv(wks+"_"+ColName);
double coeff[10];
fitpoly(cv, order, &coeff);
//printf("coeffs:\n%d, %d, %d\n%d, %d, %d\n%d, %d, %d\n%d\n",
// coeff[0], coeff[1], coeff[2], coeff[3], coeff[4], coeff[5], coeff[6], coeff[7], coeff, coeff[9]);
/*
printf("Plotting column %s...\n", ColName);
GraphPage graph;
graph.Create("line", CREATE_VISIBLE);
graph.Rename(wks+".plot"+ColName);
GraphLayer layer = graph.Layers(0);
layer.AddPlot(cv);
layer.Rescale();
*/
matrix<double> polydata(2, data.GetNumRows());
for(int i = 0; i < data.GetNumRows(); i++)
{
double wavelength = atof(data.TCell(i, 0));
polydata[0][i] = wavelength;
polydata[1][i] = poly_y(wavelength, &coeff, order);
}
if(writedata) {
Worksheet polywks;
polywks.Create();
for(i = 0; i < data.GetNumRows(); i++) {
polywks.SetCell(i, 0, polydata[0][i]);
polywks.SetCell(i, 1, polydata[1][i]);
}
}
return polydata;
}
vector<double> dsmooth(string wks, int col, float minWavelength = 0, int writedata = 0) {
clean_null_rows(wks);
vector<double> poly_w;
vector<double> poly_i;
printf("Calculating smooth-deriv peak for %s, column %d...", wks, col);
matrix<double> poly = get_smooth(wks, col, writedata);//get_poly(wks, col, order, writedata);
poly.GetRow(poly_w, 0);
poly.GetRow(poly_i, 1);
Curve cvPoly(poly_w, poly_i);
double dpoly_w = get_deriv(cvPoly, minWavelength, writedata);
double dpoly_i = Curve_yfromX(&cvPoly, dpoly_w);
printf("%f,%f\n", dpoly_w, dpoly_i);
vector<double> peak(2);
peak[0] = dpoly_w;
peak[1] = dpoly_i;
return peak;
}
matrix<double> get_smooth(string wks, int col, int writedata = 0) {
Worksheet data(wks);
string ColName = data.Columns(col).GetName();
Curve cv(wks+"_"+ColName);
//double coeff[10];
//fitpoly(cv, order, &coeff);
smooth(cv, 0, 5, 5, 2);
//printf("coeffs:\n%d, %d, %d\n%d, %d, %d\n%d, %d, %d\n%d\n",
// coeff[0], coeff[1], coeff[2], coeff[3], coeff[4], coeff[5], coeff[6], coeff[7], coeff, coeff[9]);
/*
printf("Plotting column %s...\n", ColName);
GraphPage graph;
graph.Create("line", CREATE_VISIBLE);
graph.Rename(wks+".plot"+ColName);
GraphLayer layer = graph.Layers(0);
layer.AddPlot(cv);
layer.Rescale();
*/
matrix<double> polydata(2, data.GetNumRows());
for(int i = 0; i < data.GetNumRows(); i++)
{
double wavelength = atof(data.TCell(i, 0));
polydata[0][i] = wavelength;
polydata[1][i] = Curve_yfromX(&cv, wavelength);//poly_y(wavelength, &coeff, order);
}
if(writedata) {
Worksheet polywks;
polywks.Create();
for(i = 0; i < data.GetNumRows(); i++) {
polywks.SetCell(i, 0, polydata[0][i]);
polywks.SetCell(i, 1, polydata[1][i]);
}
}
return polydata;
}
double get_deriv(Curve cvData, float minWavelength = 0, int writedata = 0) {
/*
Worksheet data(wks);
string ColName = data.Columns(col).GetName();
Curve cvData(wks+"_"+ColName);
*/
Curve cvResult(cvData);
//cvResult.Create(cvData);
Curve_derivative(&cvData, &cvResult, 1);
/*
GraphPage graph;
graph.Create("line", CREATE_VISIBLE);
graph.Rename(wks+".plot"+ColName);
GraphLayer layer = graph.Layers(0);
layer.AddPlot(cvResult);
layer.Rescale();
*/
matrix<double> derivdata(2, cvResult.GetSize());
for(int i = 0; i < cvResult.GetSize(); i++)
{
derivdata[0][i] = Curve_x(&cvResult, i);
derivdata[1][i] = Curve_y(&cvResult, i);
}
if(writedata) {
Worksheet derivwks;
derivwks.Create();
for(i = 0; i < cvResult.GetSize(); i++) {
//double wavelength = atof(derivdata.TCell(i, 0));
derivwks.SetCell(i, 0, derivdata[0][i]);
derivwks.SetCell(i, 1, derivdata[1][i]);
}
}
//double wv = y_intercept(cvResult);
//double wv = y_intercept(derivdata.GetPage().GetName());
//double wv = Curve_xfromY(&cvResult, 0);
double wv = y_intercept(derivdata, minWavelength);
//printf("%f\n", wv);
return wv;
//return xatymin(cvResult);
}
double y_intercept(Curve cv) {
double cur_w, cur_i;
double prev_w, prev_i;
double min_w, min_i;
prev_w = Curve_x(&cv, 0);
prev_i = Curve_yfromX(&cv, prev_w);
min_w = prev_w;
min_i = prev_i;
//prev_i = Curve_y(&cv, 0);
//double result = Curve_xfromY(&cv, 0);
for(int i = 1; i < cv.GetSize(); i++) {
cur_w = Curve_x(&cv, i);//wavelength;
cur_i = Curve_yfromX(&cv, cur_w);//intensity;
//cur_i = Curve_y(&cv, i);
//printf("cur %f %f | prev %f %f\n", cur_w, cur_i, prev_w, prev_i);
if((prev_i >= 0) && (cur_i <= 0)) {
//double result = (cur_w) + (cur_i /(prev_i - cur_i));
double result = cur_w;
return result;
}
prev_w = cur_w;
prev_i = cur_i;
}
/*
for(i = 1; i < cv.GetSize(); i++) {
cur_w = Curve_x(&cv, i);//wavelength;
cur_i = Curve_yfromX(&cv, cur_w);//intensity;
if(abs(cur_i) < abs(min_i)) {
min_w = cur_w;
min_i = cur_i;
}
}
*/
//return min_w;
return -1;
}
double y_intercept(string wks) {
Worksheet data(wks);
double cur_w, cur_i;
double prev_w, prev_i;
//prev_w = Curve_x(&cv, 0);
//prev_i = Curve_yfromX(&cv, prev_w);
//prev_i = Curve_y(&cv, 0);
prev_w = atof(data.TCell(0,0));
prev_i = atof(data.TCell(0,1));
for(int i = 1; i < data.GetNumRows(); i++) {
//cur_w = Curve_x(&cv, i);//wavelength;
//cur_i = Curve_yfromX(&cv, cur_w);//intensity;
//cur_i = Curve_y(&cv, i);
cur_w = atof(data.TCell(i, 0));
cur_i = atof(data.TCell(i, 1));
//printf("cur %f %f | prev %f %f\n", cur_w, cur_i, prev_w, prev_i);
if((prev_i >= 0) && (cur_i <= 0)) {
//double result = (cur_w) + (cur_i /(prev_i - cur_i));
double result = cur_w;
return result;
}
prev_w = cur_w;
prev_i = cur_i;
}
return -1;
}
double y_intercept(matrix<double> data, float minWavelength = 0) {
//Worksheet data(wks);
double cur_w, cur_i;
double prev_w, prev_i;
//prev_w = Curve_x(&cv, 0);
//prev_i = Curve_yfromX(&cv, prev_w);
//prev_i = Curve_y(&cv, 0);
prev_w = data[0][0];
prev_i = data[1][0];
double max_i = -1;
double max_result = -1;
for(int i = 1; i < data.GetNumCols(); i++) {
//cur_w = Curve_x(&cv, i);//wavelength;
//cur_i = Curve_yfromX(&cv, cur_w);//intensity;
//cur_i = Curve_y(&cv, i);
cur_w = data[0][i];//atof(data.TCell(i, 0));
cur_i = data[1][i];//atof(data.TCell(i, 1));
//printf("cur %f %f | prev %f %f\n", cur_w, cur_i, prev_w, prev_i);
if((prev_i >= 0) && (cur_i <= 0) && (cur_w > minWavelength)) {
//double result = (cur_w) + (cur_i /(prev_i - cur_i));
double result = prev_w + ((prev_i / (prev_i - cur_i)) * (cur_w - prev_w));
//double result = cur_w;
//printf("%f %f\n", prev_w, cur_w);
return result;
/*
if(prev_i > max_i) {
max_result = result;
max_i = prev_i;
}
*/
}
prev_w = cur_w;
prev_i = cur_i;
}
return max_result;
}
double poly_y(double x, double *coeff, int order) {
double y = coeff[0];
for(int i = 1; i <= order; i++) {
y += coeff[i]*pow(x, i);
}
/*
y = coeff[0] +
coeff[1]*pow(x, 1) +
coeff[2]*pow(x, 2) +
coeff[3]*pow(x, 3) +
coeff[4]*pow(x, 4) +
coeff[5]*pow(x, 5) +
coeff[6]*pow(x, 6) +
coeff[7]*pow(x, 7) +
coeff*pow(x, 8) +
coeff[9]*pow(x, 9);
*/
return y;
}
void clean_null_rows(string wks) {
//return;
Worksheet data(wks);
for(int i = 0; i < data.GetNumRows(); i++) {
if(!atof(data.TCell(i, 0))) {
//printf("null row %d\n", i);
data.DeleteRow(i);
i--;
}
}
}
intercept.cpp
#include <origin.h>
void find_int(string wks) {
Worksheet data(wks);
for(int j = 0; j < data.GetNumRows(); j++) {
float xval = atof(data.TCell(j, 0));
float yval = atof(data.TCell(j, 1));
if(j == data.GetNumRows()-1) {
break;
}
float next_x = atof(data.TCell(j+1, 0));
float next_y = atof(data.TCell(j+1, 1));
//printf("(x,y) = (%f,%f)\n", xval, yval);
if((yval > 0 && next_y < 0) || (yval < 0 && next_y > 0)) {
printf("Intercept found!\n");
printf("--- y=%f; next y=%f\n", yval, next_y);
printf("--- x=%f; next x=%f\n", xval, next_x);
double x_result = xval + ((yval / (yval - next_y)) * (next_x - xval));
printf("--- Calculated x value: %f\n", x_result);
}
}
}
uv_analysis.cpp
#include <origin.h>
void uv_process(string strWks, float peakrange) {
plot_all(strWks);
//string peaks = transpose_peaks(peakrange);
transpose_peaks(peakrange);
//check_peaks(peaks, peak_range);
//make_doubles(peaks);
}
void plot_all(string strWks) {
Worksheet wks(strWks);
foreach(Column cc in wks.Columns) {
if(cc.GetType() == OKDATAOBJ_DESIGNATION_Y) {
string strFullColName;
//wks.GetName(strFullColName);
strFullColName = strWks+"_"+cc.GetName();
printf("Plotting column %s...\n", strFullColName);
plot(strFullColName);
}
}
/*
int i = 0;
while(wks.Columns(i).isValid()) {
Column C = wks.Columns(i);
if(C.GetType() == OKDATAOBJ_DESIGNATION_Y)
{
plot(C.GetName());
}
i++;
}
*/
}
void plot(string strData) {
GraphPage graph;
graph.Create("line", CREATE_VISIBLE);
GraphLayer layer = graph.Layers(0);
Curve cv(strData);
layer.AddPlot(cv);
layer.Rescale();
LT_set_var("PickPeak!Positive", 0);
LT_set_var("PickPeak!Negative", 1);
LT_set_var("PickPeak!Width.v1", 1);
LT_set_var("PickPeak!Height.v1", 0.00001);
LT_set_var("PickPeak!MinHeight.v1", 0.00001);
LT_set_var("PickPeak!ShowCenter", 1);
LT_set_var("PickPeak!ShowLabel", 1);
LT_execute("run.section(PICKPEAK.OGS, Find.OnClick)");
//LT_set_var("cv", &cv);
//LT_execute("cv.pickPeaks.rectWidth = 1");
//LT_execute("cv.pickPeaks.rectHeight = 0.00001");
//LT_execute("cv.pickPeaks.minHeight = 0.00001");
//LT_execute("cv.pickPeaks()");
//peaks(cv, 1, 0.00001);
//cv.pickPeaks.rectWidth = 1;
//cv.pickPeaks.rectHeight = 0.00001;
//cv.pickPeaks.minHeight = 0.00001;
//cv.pickPeaks();
}
void transpose_peaks(float peakrange) {
Worksheet peaks;
peaks.Create("Peak_Summary");
int index = 0;
foreach(WorksheetPage wp in Project.WorksheetPages) {
string strName;
wp.GetName(strName);
if(strName.Find("Peaks") > -1) {
printf("transposing %s...\n", strName);
int lower = 0;
int upper = 0;
Worksheet wks(strName);
if(wks.IsValid()) {
wks.GetRange(lower, upper);
//printf("%d %d\n", lower, upper);
peaks.SetCell(index, 0, index*2.5+10);
for(int i = 0; i <= upper; i++) {
//string val1 = wks.TCell(i,4);
//string val2 = peaks.TCell(index-1, i);
if(peaks.Columns(i+1).IsValid()) {
peaks.SetCell(index, i+1, wks.TCell(i,4))
} else {
peaks.AddCol();
peaks.SetCell(index, i+1, wks.TCell(i,4))
}
//printf("%s\n", wks.TCell(i,4));
}
} else {
printf("Worksheet not valid.\n");
}
index++;
}
}
string strWks;
strWks = peaks.GetPage().GetName();
check_peaks(strWks, peakrange);
//return strWks;
}
void check_peaks(string strWks, float peakrange) {
int col = 0;
int lower = 0;
int upper = 0;
Worksheet peaks(strWks);
peaks.GetRange(lower, upper);
for(int i = lower; i <= upper; i++) {
for(int j = 1; j < peaks.GetNumCols(); j++) {
double curval = atof(peaks.TCell(i, j));
double nextval = atof(peaks.TCell(i, j+1));
if(abs(curval - nextval) < peakrange) {
for(int k = j; k < peaks.GetNumCols(); k++) {
peaks.SetCell(i, k, peaks.TCell(i, k+1));
}
}
}
}
foreach(Column C in peaks.Columns) {
if(C.GetType() == OKDATAOBJ_DESIGNATION_Y) {
double minval = atof(peaks.TCell(0, col));
//double avgval = atof(peaks.TCell(0, col));
printf("checking peak col: %s\n", C.GetName());
for(int i = lower; i <=upper; i++) {
double curval = atof(peaks.TCell(i, col));
//avgval = (avgval+curval)/2;
//TODO: CULL OUT DUPLICATE PEAKS
if(((curval < minval) && (curval > 0)) || (minval == 0)) {
minval = curval;
}
//printf("curval: %f\n", curval);
}
printf("minimum value: %f\n", minval);
//printf("average value: %f\n", avgval);
for(i = lower; i <=upper; i++) {
double curval = atof(peaks.TCell(i, col));
string repcell = "";
if(abs(curval - minval) > peakrange) {
for(int j = col; j < peaks.GetNumCols(); j++) {
string curcell = peaks.TCell(i, j);
//if(abs(atof(curcell) - atof(repcell)) < 2.75) {
// j--;
// printf("Repeat peak at cell %d %d\n", i, j);
//}
peaks.SetCell(i, j, repcell);
repcell = curcell;
if((!peaks.Columns(j+1).IsValid()) && (!repcell.IsEmpty())) {
peaks.AddCol();
}
}
}
}
}
col++;
}
make_doubles(strWks);
}
void make_doubles(string strWks) {
int lower = 0;
int upper = 0;
Worksheet peaks(strWks);
printf("cleaning %s...\n", strWks);
peaks.GetRange(lower, upper);
for(int i = lower; i <= upper; i++) {
for(int j = 0; j < peaks.GetNumCols(); j++) {
if(atof(peaks.TCell(i,j))) {
peaks.SetCell(i, j, atof(peaks.TCell(i, j)));
}
}
}
}
|
Penn |
Posted - 01/10/2012 : 8:37:57 PM Hi Vidyashankara,
I have tried this code in Origin 8, and it should work if you can provide the following functions: clean_null_rows, max_peak, msm, dpoly, dsmooth, half_peak_width, plot_cols. All these functions are not provided by Origin, but defined by the user, maybe the guy in your lab you mentioned.
Penn |
|
|