57 addUsageLine(
"This function determines the location of high contrast features in a volume.");
58 addParamsLine(
" --vol <vol_file=\"\"> : File path to input volume.");
59 addParamsLine(
" [-o <output=\"coordinates3D.xmd\">] : File path to output coordinates file.");
60 addParamsLine(
" [--samplingRate <samplingRate=1>] : Sampling rate of the input tomogram (A/px).");
61 addParamsLine(
" [--fiducialSize <fiducialSize=100>] : Size of the fiducial markers in Angstroms (A).");
62 addParamsLine(
" [--boxSize <boxSize=32>] : Box size of the peaked fiducials.");
63 addParamsLine(
" [--numberSampSlices <numberSampSlices=10>] : Number of sampling slices to calculate the threshold value.");
64 addParamsLine(
" [--sdThr <sdThr=5>] : Number of STD away the mean to consider that a pixel has an outlier value.");
65 addParamsLine(
" [--numberOfCoordinatesThr <numberOfCoordinatesThr=10>] : Minimum number of points attracted to a coordinate.");
66 addParamsLine(
" [--mirrorCorrelationThr <mirrorCorrelationThr=0.1>] : Minimum correlation of a coordinate with its mirror.");
67 addParamsLine(
" [--mahalanobisDistanceThr <mahalanobisDistanceThr=2>] : Minimum Mahalanobis distance.");
68 addParamsLine(
" [--relaxedModeThr <mahalanobisDistanceThr=3>] : Number of remaining coordinates to disable a filter in case it removes all coordinates.");
75 std::cout <<
"Saving output coordinates... " << std::endl;
81 for(
size_t i = 0 ;
i < coordinates3D.size();
i++)
92 std::cout <<
"Output coordinates metadata saved at: " <<
fnOut << std::endl;
102 #ifdef VERBOSE_OUTPUT 103 std::cout <<
"Preprocessing volume..." << std::endl;
107 #ifdef VERBOSE_OUTPUT 108 std::cout <<
"Averaging volume..." << std::endl;
117 for (
int k = 2;
k < zSize-2;
k++)
136 for (
int i = 0;
i < ySize;
i++)
138 for (
int j = 0;
j < xSize;
j++)
149 #ifdef DEBUG_OUTPUT_FILES 150 size_t lastindex =
fnOut.find_last_of(
".");
151 std::string rawname =
fnOut.substr(0, lastindex);
152 std::string outputFileNameFilteredVolume;
153 outputFileNameFilteredVolume = rawname +
"_average.mrc";
155 V.
write(outputFileNameFilteredVolume);
158 #ifdef VERBOSE_OUTPUT 159 std::cout <<
"Volume averaging finished succesfully!" << std::endl;
167 #ifdef VERBOSE_OUTPUT 168 std::cout <<
"Applying bandpass filter to volume..." << std::endl;
177 double cutoffFreqHigh = freqHigh +
w;
178 double cutoffFreqLow = freqLow -
w;
181 normDim = (xSize>ySize) ? xSize : ySize;
186 #ifdef DEBUG_PREPROCESS 187 std::cout <<
"samplingRate " <<
samplingRate << std::endl;
188 std::cout <<
"fiducialSize " <<
fiducialSize << std::endl;
189 std::cout <<
"freqLow " << freqLow << std::endl;
190 std::cout <<
"freqHigh " << freqHigh << std::endl;
191 std::cout <<
"cutoffFreqLow " << cutoffFreqLow << std::endl;
192 std::cout <<
"cutoffFreqHigh " << cutoffFreqHigh << std::endl;
214 double u=
sqrt(uz2y2+ux*ux);
216 if(u > cutoffFreqHigh || u < cutoffFreqLow)
222 if(u >= freqHigh && u < cutoffFreqHigh)
227 if (u <= freqLow && u > cutoffFreqLow)
240 #ifdef DEBUG_OUTPUT_FILES 241 lastindex =
fnOut.find_last_of(
".");
242 rawname =
fnOut.substr(0, lastindex);
243 outputFileNameFilteredVolume;
244 outputFileNameFilteredVolume = rawname +
"_bandpass.mrc";
246 V.
write(outputFileNameFilteredVolume);
249 #ifdef VERBOSE_OUTPUT 250 std::cout <<
"Bandpass filter applied to volume succesfully!" << std::endl;
259 #ifdef VERBOSE_OUTPUT 260 std::cout <<
"Applying laplacian filter to volume..." << std::endl;
263 for (
int k = 1;
k < zSize-1;
k++)
268 for (
int i = 1;
i < ySize-1;
i++)
270 for (
int j = 1;
j < xSize-1;
j++)
281 #ifdef VERBOSE_OUTPUT 282 std::cout <<
"Laplacian filter applied to volume succesfully!" << std::endl;
287 for (
int k = 0;
k < zSize;
k++)
289 if(k<2 || k > zSize-2)
291 for (
int i = 0;
i < ySize;
i++)
293 for (
int j = 0;
j < xSize;
j++)
301 #ifdef DEBUG_OUTPUT_FILES 302 lastindex =
fnOut.find_last_of(
".");
303 rawname =
fnOut.substr(0, lastindex);
304 outputFileNameFilteredVolume;
305 outputFileNameFilteredVolume = rawname +
"_preprocess.mrc";
307 V.
write(outputFileNameFilteredVolume);
310 #ifdef VERBOSE_OUTPUT 311 std::cout <<
"Volume preprocessed succesfully!" << std::endl;
318 #ifdef VERBOSE_OUTPUT 319 std::cout <<
"Picking coordinates..." << std::endl;
322 size_t centralSlice = zSize/2;
327 std::cout <<
"Number of sampling slices: " <<
numberSampSlices << std::endl;
328 std::cout <<
"Sampling region from slice " << minSamplingSlice <<
" to " << maxSamplingSlice << std::endl;
336 for(
size_t k = minSamplingSlice;
k < maxSamplingSlice; ++
k)
338 for(
size_t j = 0;
j < ySize; ++
j)
340 for(
size_t i = 0;
i < xSize; ++
i)
349 double average = sum / Nelems;
350 double standardDeviation =
sqrt(sum2/Nelems - average*average);
352 double thresholdL = average-
sdThr*standardDeviation;
355 double thresholdU = average+
sdThr*standardDeviation;
356 std::cout <<
"ThresholdU value = " << thresholdU << std::endl;
357 std::cout <<
"ThresholdL value = " << thresholdL << std::endl;
363 for(
size_t k = 1;
k < zSize-1;
k++)
365 binaryCoordinatesMapSlice.
initZeros(ySize, xSize);
368 int numberOfPointsAddedBinaryMap = 0;
371 for(
size_t j = 0;
j < xSize;
j++)
373 for(
size_t i = 0;
i < ySize;
i++)
377 if (value < thresholdL)
382 numberOfPointsAddedBinaryMap += 1;
389 std::cout <<
"Number of points in the binary map: " << numberOfPointsAddedBinaryMap << std::endl;
393 std::cout <<
"Labeling slice " <<
k << std::endl;
396 int colour =
labelImage2D(binaryCoordinatesMapSlice, labelCoordiantesMapSlice, 8);
399 #ifdef DEBUG_OUTPUT_FILES 400 for (
size_t j = 0;
j < xSize;
j++)
402 for (
size_t i = 0;
i < ySize;
i++)
419 std::cout <<
"Colour: " << colour << std::endl;
422 std::vector<std::vector<int>> coordinatesPerLabelX (colour);
423 std::vector<std::vector<int>> coordinatesPerLabelY (colour);
425 for(
size_t i = 0;
i < ySize;
i++)
427 for(
size_t j = 0;
j < xSize; ++
j)
433 coordinatesPerLabelX[value-1].push_back(
j);
434 coordinatesPerLabelY[value-1].push_back(
i);
439 size_t numberOfCoordinatesPerValue;
442 for(
size_t value = 0; value < colour; value++)
444 numberOfCoordinatesPerValue = coordinatesPerLabelX[value].size();
449 for(
size_t coordinate=0; coordinate < coordinatesPerLabelX[value].size(); coordinate++)
451 xCoor += coordinatesPerLabelX[value][coordinate];
452 yCoor += coordinatesPerLabelY[value][coordinate];
455 double xCoorCM = xCoor/numberOfCoordinatesPerValue;
456 double yCoorCM = yCoor/numberOfCoordinatesPerValue;
458 bool keep =
filterLabeledRegions(coordinatesPerLabelX[value], coordinatesPerLabelY[value], xCoorCM, yCoorCM);
463 coordinates3D.push_back(point3D);
468 #ifdef VERBOSE_OUTPUT 469 std::cout <<
"Number of high contrast features found: " << coordinates3D.size() << std::endl;
472 #ifdef DEBUG_OUTPUT_FILES 473 size_t lastindex =
fnOut.find_last_of(
".");
474 std::string rawname =
fnOut.substr(0, lastindex);
475 std::string outputFileNameLabeledVolume;
476 outputFileNameLabeledVolume = rawname +
"_label.mrc";
478 V.
write(outputFileNameLabeledVolume);
485 std::vector<size_t> coordinatesInSlice;
486 std::vector<size_t> coordinatesInSlice_up;
487 std::vector<size_t> coordinatesInSlice_down;
489 std::vector<size_t> coord3DVotes_V(coordinates3D.size(), 0);
494 std::cout <<
"thrVottingDistance2 " << thrVottingDistance2 << std::endl;
497 size_t deletedIndexes;
507 std::cout <<
"--- ITERATION " << iteration << std::endl;
513 for (
int k = 0;
k < zSize;
k++)
520 else if (
k == (nSize-1))
522 coordinatesInSlice_up = coordinatesInSlice;
523 coordinatesInSlice = coordinatesInSlice_down;
527 coordinatesInSlice_up = coordinatesInSlice;
528 coordinatesInSlice = coordinatesInSlice_down;
532 for(
size_t i = 0;
i < coordinatesInSlice.size();
i++)
539 for (
size_t j = 0;
j < coordinatesInSlice_up.size();
j++)
542 float distance2 = (c.
x-cu.
x)*(c.
x-cu.
x)+(c.
y-cu.
y)*(c.
y-cu.
y);
544 if(distance2 < thrVottingDistance2)
546 coord3DVotes_V[coordinatesInSlice[
i]] += 1;
554 for (
size_t j = 0;
j < coordinatesInSlice_down.size();
j++)
557 float distance2 = (c.
x-cd.
x)*(c.
x-cd.
x)+(c.
y-cd.
y)*(c.
y-cd.
y);
559 if(distance2 < thrVottingDistance2)
561 coord3DVotes_V[coordinatesInSlice[
i]] += 1;
569 for (
size_t i = 0;
i < coord3DVotes_V.size();
i++)
571 if (coord3DVotes_V[
i] == 0)
574 std::cout <<
"Deleted coordinate " <<
i << std::endl;
577 coordinates3D.erase(coordinates3D.begin()+
i);
578 coord3DVotes_V.erase(coord3DVotes_V.begin()+
i);
585 std::cout <<
"DeletedIndexes: " << deletedIndexes << std::endl;
590 while(deletedIndexes > 0);
593 std::cout <<
"coord3DVotes_V.size() " << coord3DVotes_V.size() << std::endl;
594 std::cout <<
"coordinates3D.size() " << coordinates3D.size() << std::endl;
596 for (
size_t i = 0;
i < coord3DVotes_V.size();
i++)
598 std::cout << coord3DVotes_V[
i] <<
" ";
600 std::cout << std::endl;
605 std::vector<size_t> coord3DId_V(coordinates3D.size(), 0);
606 size_t currentId = 1;
611 for(
size_t i = 0;
i < coordinatesInSlice.size();
i++)
613 coord3DId_V[coordinatesInSlice[
i]] = currentId;
618 for (
int k = 1;
k < zSize;
k++)
620 coordinatesInSlice_up = coordinatesInSlice;
623 for(
size_t i = 0;
i < coordinatesInSlice.size();
i++)
627 double match =
false;
628 for (
size_t j = 0;
j < coordinatesInSlice_up.size();
j++)
631 float distance2 = (c.
x-cu.
x)*(c.
x-cu.
x)+(c.
y-cu.
y)*(c.
y-cu.
y);
633 if(distance2 < thrVottingDistance2)
635 coord3DId_V[coordinatesInSlice[
i]] = coord3DId_V[coordinatesInSlice_up[
j]];
643 coord3DId_V[coordinatesInSlice[
i]] = currentId;
651 std::cout <<
"coord3DId_V.size() " << coord3DId_V.size() << std::endl;
653 for (
size_t i = 0;
i < coord3DId_V.size();
i++)
655 std::cout << coord3DId_V[
i] <<
" ";
657 std::cout << std::endl;
661 #ifdef VERBOSE_OUTPUT 662 std::cout <<
"Number of clusters identified: " << (currentId-1) << std::endl;
666 std::vector<Point3D<double>> coordinates3D_avg;
668 for (
size_t id = 1;
id < currentId;
id++)
674 for (
int n = 0;
n < coord3DId_V.size();
n++)
676 if (coord3DId_V[
n] ==
id)
678 coord3D_avg.
x += coordinates3D[
n].x;
679 coord3D_avg.
y += coordinates3D[
n].y;
680 coord3D_avg.
z += coordinates3D[
n].z;
683 coordinates3D.erase(coordinates3D.begin()+
n);
684 coord3DId_V.erase(coord3DId_V.begin()+
n);
689 coord3D_avg.
x /= nCoords;
690 coord3D_avg.
y /= nCoords;
691 coord3D_avg.
z /= nCoords;
693 coordinates3D_avg.push_back(coord3D_avg);
696 coordinates3D = coordinates3D_avg;
698 #ifdef VERBOSE_OUTPUT 699 std::cout <<
"Number of coordinates obtained after clustering: " << coordinates3D.size() << std::endl;
700 std::cout <<
"Clustering of coordinates finished successfully!" << std::endl;
707 #ifdef VERBOSE_OUTPUT 708 std::cout <<
"Centering coordinates..." << std::endl;
711 size_t numberOfFeatures = coordinates3D.size();
721 int doubleBoxSize =
boxSize * 2;
723 for(
size_t n = 0;
n < numberOfFeatures;
n++)
725 #ifdef DEBUG_CENTER_COORDINATES 726 std::cout <<
"-------------------- coordinate " <<
n <<
" (" << coordinates3D[
n].x <<
", " << coordinates3D[
n].y <<
", " << coordinates3D[
n].z <<
")" << std::endl;
731 feature.
initZeros(2 * doubleBoxSize, 2 * doubleBoxSize, 2 * doubleBoxSize);
732 mirrorFeature.
initZeros(2 * doubleBoxSize, 2 * doubleBoxSize, 2 * doubleBoxSize);
734 coordHalfX = coordinates3D[
n].x -
boxSize;
735 coordHalfY = coordinates3D[
n].y -
boxSize;
736 coordHalfZ = coordinates3D[
n].z -
boxSize;
738 for(
int k = 0;
k < doubleBoxSize;
k++)
740 for(
int j = 0;
j < doubleBoxSize;
j++)
742 for(
int i = 0;
i < doubleBoxSize;
i++)
745 if ((coordHalfZ +
k) < 0 || (coordHalfZ +
k) > zSize ||
746 (coordHalfY +
i) < 0 || (coordHalfY +
i) > ySize ||
747 (coordHalfX +
j) < 0 || (coordHalfX +
j) > xSize)
770 #ifdef DEBUG_CENTER_COORDINATES 773 std::cout <<
"Feature dimensions (" <<
XSIZE(feature) <<
", " <<
YSIZE(feature) <<
", " <<
ZSIZE(feature) <<
")" << std::endl;
775 size_t lastindex =
fnOut.find_last_of(
".");
776 std::string rawname =
fnOut.substr(0, lastindex);
777 std::string outputFileNameSubtomo;
778 outputFileNameSubtomo = rawname +
"_" +
std::to_string(
n) +
"_feature.mrc";
779 subtomo.
write(outputFileNameSubtomo);
781 std::cout <<
"Mirror feature dimensions (" <<
XSIZE(mirrorFeature) <<
", " <<
YSIZE(mirrorFeature) <<
", " <<
ZSIZE(mirrorFeature) <<
")" << std::endl;
782 subtomo() = mirrorFeature;
783 outputFileNameSubtomo = rawname +
"_" +
std::to_string(
n) +
"_mirrorFeature.mrc";
784 subtomo.
write(outputFileNameSubtomo);
791 auto maximumCorrelation = MINDOUBLE;
792 double xDisplacement = 0;
793 double yDisplacement = 0;
794 double zDisplacement = 0;
800 if (value > maximumCorrelation)
802 maximumCorrelation = value;
809 #ifdef DEBUG_CENTER_COORDINATES 810 std::cout <<
"maximumCorrelation " << maximumCorrelation << std::endl;
811 std::cout <<
"xDisplacement " << ((int) xDisplacement - doubleBoxSize) / 2 << std::endl;
812 std::cout <<
"yDisplacement " << ((int) yDisplacement - doubleBoxSize) / 2 << std::endl;
813 std::cout <<
"zDisplacement " << ((int) zDisplacement - doubleBoxSize) / 2 << std::endl;
815 std::cout <<
"Correlation volume dimensions (" <<
XSIZE(correlationVolumeR) <<
", " <<
YSIZE(correlationVolumeR) <<
", " <<
ZSIZE(correlationVolumeR) <<
")" << std::endl;
820 double updatedCoordinateX = coordinates3D[
n].x + ((int) xDisplacement - doubleBoxSize) / 2;
821 double updatedCoordinateY = coordinates3D[
n].y + ((int) yDisplacement - doubleBoxSize) / 2;
822 double updatedCoordinateZ = coordinates3D[
n].z + ((int) zDisplacement - doubleBoxSize) / 2;
824 int deletedCoordinates = 0;
826 if (updatedCoordinateZ < 0 || updatedCoordinateZ > zSize ||
827 updatedCoordinateY < 0 || updatedCoordinateY > ySize ||
828 updatedCoordinateX < 0 || updatedCoordinateX > xSize)
830 coordinates3D.erase(coordinates3D.begin()+
n-deletedCoordinates);
831 deletedCoordinates++;
835 coordinates3D[
n].x = updatedCoordinateX;
836 coordinates3D[
n].y = updatedCoordinateY;
837 coordinates3D[
n].z = updatedCoordinateZ;
840 #ifdef DEBUG_CENTER_COORDINATES 844 centerFeature.
initZeros(doubleBoxSize, doubleBoxSize, doubleBoxSize);
846 coordHalfX = coordinates3D[
n].x -
boxSize;
847 coordHalfY = coordinates3D[
n].y -
boxSize;
848 coordHalfZ = coordinates3D[
n].z -
boxSize;
850 for(
int k = 0;
k < doubleBoxSize;
k++)
852 for(
int j = 0;
j < doubleBoxSize;
j++)
854 for(
int i = 0;
i < doubleBoxSize;
i++)
857 if ((coordHalfZ +
k) < 0 || (coordHalfZ +
k) > zSize ||
858 (coordHalfY +
i) < 0 || (coordHalfY +
i) > ySize ||
859 (coordHalfX +
j) < 0 || (coordHalfX +
j) > xSize)
874 std::cout <<
"Centered feature dimensions (" <<
XSIZE(centerFeature) <<
", " <<
YSIZE(centerFeature) <<
", " <<
ZSIZE(centerFeature) <<
")" << std::endl;
876 subtomo() = centerFeature;
877 outputFileNameSubtomo = rawname +
"_" +
std::to_string(
n) +
"_centerFeature.mrc";
878 subtomo.
write(outputFileNameSubtomo);
882 #ifdef DEBUG_CENTER_COORDINATES 883 std::cout <<
"3D coordinates after centering: " << std::endl;
885 for(
size_t n = 0;
n < numberOfFeatures;
n++)
887 std::cout <<
"Coordinate " <<
n <<
" (" << coordinates3D[
n].x <<
", " << coordinates3D[
n].y <<
", " << coordinates3D[
n].z <<
")" << std::endl;
892 #ifdef VERBOSE_OUTPUT 893 std::cout <<
"Centering of coordinates finished successfully!" << std::endl;
900 #ifdef VERBOSE_OUTPUT 901 std::cout <<
"Removing duplicated coordinates..." << std::endl;
905 size_t deletedCoordinates;
907 #ifdef DEBUG_REMOVE_DUPLICATES 909 std::cout <<
"maxDistance " << maxDistance << std::endl;
914 #ifdef DEBUG_REMOVE_DUPLICATES 916 std::cout <<
"------------------------STARTING ITERATION " << iteration<< std::endl;
917 std::cout <<
"coordinates3D.size() " << coordinates3D.size() << std::endl;
920 std::vector<Point3D<double>> newCoordinates3D;
921 size_t numberOfFeatures = coordinates3D.size();
922 std::vector<size_t> deleteCoordinatesVector(numberOfFeatures, 0);
924 for(
size_t i = 0;
i < numberOfFeatures;
i++)
926 for(
size_t j =
i+1;
j < numberOfFeatures;
j++)
928 double distance = (coordinates3D[
i].x - coordinates3D[
j].x) * (coordinates3D[
i].
x - coordinates3D[
j].
x) +
929 (coordinates3D[
i].y - coordinates3D[
j].y) * (coordinates3D[
i].
y - coordinates3D[
j].
y) +
930 (coordinates3D[
i].z - coordinates3D[
j].z) * (coordinates3D[
i].
z - coordinates3D[
j].
z);
932 if (distance < maxDistance && deleteCoordinatesVector[
i] == 0 && deleteCoordinatesVector[
j] == 0)
935 (coordinates3D[
i].y + coordinates3D[
j].y)/2,
936 (coordinates3D[
i].z + coordinates3D[
j].z)/2);
937 newCoordinates3D.push_back(p);
939 #ifdef DEBUG_REMOVE_DUPLICATES 940 std::cout <<
"distance match between coordinates " <<
i <<
" and " <<
j <<
": " <<
sqrt(distance) << std::endl;
941 std::cout <<
"Coordinate " <<
i <<
": (" << coordinates3D[
i].x <<
", " << coordinates3D[
i].y <<
", " << coordinates3D[
i].z <<
")" << std::endl;
942 std::cout <<
"Coordinate " <<
j <<
": (" << coordinates3D[
j].x <<
", " << coordinates3D[
j].y <<
", " << coordinates3D[
j].z <<
")" << std::endl;
943 std::cout <<
"Average coordinate: " << p.
x <<
", " << p.
y <<
", " << p.
z <<
")" << std::endl;
946 deleteCoordinatesVector[
i] = 1;
947 deleteCoordinatesVector[
j] = 1;
952 #ifdef DEBUG_REMOVE_DUPLICATES 953 std::cout <<
"coordinates3D.size() " << coordinates3D.size() << std::endl;
956 deletedCoordinates = 0;
957 for (
size_t i = 0;
i < deleteCoordinatesVector.size();
i++)
959 if (deleteCoordinatesVector[
i] == 1)
961 coordinates3D.erase(coordinates3D.begin()+
i-deletedCoordinates);
962 deletedCoordinates++;
966 #ifdef DEBUG_REMOVE_DUPLICATES 967 std::cout <<
"deletedCoordinates " << deletedCoordinates << std::endl;
968 std::cout <<
"coordinates3D.size() " << coordinates3D.size() << std::endl;
969 std::cout <<
"newCoordinates3D.size() " << newCoordinates3D.size() << std::endl;
972 for (
size_t i = 0;
i < newCoordinates3D.size();
i++)
974 coordinates3D.push_back(newCoordinates3D[
i]);
977 #ifdef DEBUG_REMOVE_DUPLICATES 978 std::cout <<
"coordinates3D.size() " << coordinates3D.size() << std::endl;
981 newCoordinates3D.clear();
983 while (deletedCoordinates>0);
985 #ifdef DEBUG_REMOVE_DUPLICATES 987 size_t numberOfFeatures = coordinates3D.size();
997 for(
size_t n = 0;
n < numberOfFeatures;
n++)
1001 coordHalfX = coordinates3D[
n].x - halfBoxSize;
1002 coordHalfY = coordinates3D[
n].y - halfBoxSize;
1003 coordHalfZ = coordinates3D[
n].z - halfBoxSize;
1012 if ((coordHalfZ +
k) < 0 || (coordHalfZ +
k) > zSize ||
1013 (coordHalfY +
i) < 0 || (coordHalfY +
i) > ySize ||
1014 (coordHalfX +
j) < 0 || (coordHalfX +
j) > xSize)
1030 subtomo() = feature;
1031 std::string outputFileNameSubtomo;
1032 size_t lastindex =
fnOut.find_last_of(
".");
1033 std::string rawname =
fnOut.substr(0, lastindex);
1034 outputFileNameSubtomo = rawname +
"_RD_" +
std::to_string(
n) +
"_feature.mrc";
1035 subtomo.
write(outputFileNameSubtomo);
1039 #ifdef DEBUG_REMOVE_DUPLICATES 1040 std::cout <<
"3D coordinates after removing duplicates: " << std::endl;
1042 for(
size_t n = 0;
n < numberOfFeatures;
n++)
1044 std::cout <<
"Coordinate " <<
n <<
" (" << coordinates3D[
n].x <<
", " << coordinates3D[
n].y <<
", " << coordinates3D[
n].z <<
")" << std::endl;
1048 #ifdef VERBOSE_OUTPUT 1049 std::cout <<
"Removing duplicated coordinates finished succesfully!" << std::endl;
1056 #ifdef VERBOSE_OUTPUT 1057 std::cout <<
"Filter coordinates by correlation..." << std::endl;
1061 size_t halfBoxSize =
boxSize / 2;
1066 double dotProductMirror = 0;
1072 std::vector<Point3D<double>> newCoordinates3D;
1074 int numberOfCoordinates = coordinates3D.size();
1077 for(
size_t n = 0;
n < numberOfCoordinates;
n++)
1089 coordHalfX = coordinates3D[
n].x - halfBoxSize;
1090 coordHalfY = coordinates3D[
n].y - halfBoxSize;
1091 coordHalfZ = coordinates3D[
n].z - halfBoxSize;
1094 if ((coordHalfZ +
k) < 0 || (coordHalfZ +
k) > zSize ||
1095 (coordHalfY +
i) < 0 || (coordHalfY +
i) > ySize ||
1096 (coordHalfX +
j) < 0 || (coordHalfX +
j) > xSize)
1122 #ifdef DEBUG_FILTER_COORDINATES 1125 std::cout <<
"Feature dimensions (" <<
XSIZE(feature) <<
", " <<
YSIZE(feature) <<
", " <<
ZSIZE(feature) <<
")" << std::endl;
1126 subtomo() = feature;
1127 size_t lastindex =
fnOut.find_last_of(
".");
1128 std::string rawname =
fnOut.substr(0, lastindex);
1129 std::string outputFileNameSubtomo;
1130 outputFileNameSubtomo = rawname +
"_" +
std::to_string(
n) +
"_FC_feature.mrc";
1131 subtomo.
write(outputFileNameSubtomo);
1133 std::cout <<
"Mirror feature dimensions (" <<
XSIZE(mirrorFeature) <<
", " <<
YSIZE(mirrorFeature) <<
", " <<
ZSIZE(mirrorFeature) <<
")" << std::endl;
1134 subtomo() = mirrorFeature;
1135 outputFileNameSubtomo = rawname +
"_" +
std::to_string(
n) +
"_FC_mirrorFeature.mrc";
1136 subtomo.
write(outputFileNameSubtomo);
1151 dotProductMirror /= boxSize * boxSize *
boxSize;
1153 #ifdef DEBUG_FILTER_COORDINATES 1154 std::cout <<
"-------------------- coordinate " <<
n <<
" (" << coordinates3D[
n].x <<
", " << coordinates3D[
n].y <<
", " << coordinates3D[
n].z <<
")" << std::endl;
1155 std::cout <<
"dot product mirror: " << dotProductMirror << std::endl;
1160 newCoordinates3D.push_back(coordinates3D[
n]);
1164 #ifdef DEBUG_FILTER_COORDINATES 1165 std::cout <<
"Coordinate " <<
n <<
" removed. Mirror correlation: " << dotProductMirror << std::endl;
1170 #ifdef DEBUG_FILTER_COORDINATES 1171 std::cout <<
"Number of corrdinates filtered by mirror correlation: " << (coordinates3D.size() - newCoordinates3D.size()) << std::endl;
1175 if (!newCoordinates3D.empty())
1177 numberOfCoordinates = newCoordinates3D.size();
1183 std::vector<MultidimArray<float>> setOfFeatures_RA(numberOfCoordinates);
1185 int numAvgSlices = (
boxSize*0.25);
1186 int halfNumberAvgSlices = numAvgSlices/2;
1189 #ifdef DEBUG_FILTER_COORDINATES 1190 std::cout <<
"Calculate radial averages " << std::endl;
1193 for(
size_t n = 0;
n < numberOfCoordinates;
n++)
1195 #ifdef DEBUG_FILTER_COORDINATES 1196 std::cout <<
"Calculating radial average of coordinate " <<
n << std::endl;
1202 for(
int k = 0;
k < numAvgSlices;
k++)
1208 coordHalfX = newCoordinates3D[
n].x - halfBoxSize;
1209 coordHalfY = newCoordinates3D[
n].y - halfBoxSize;
1210 coordHalfZ = newCoordinates3D[
n].z - halfNumberAvgSlices;
1213 if (!((coordHalfZ +
k) < 0 || (coordHalfZ +
k) > zSize ||
1214 (coordHalfY +
i) < 0 || (coordHalfY +
i) > ySize ||
1215 (coordHalfX +
j) < 0 || (coordHalfX +
j) > xSize))
1228 #ifdef DEBUG_FILTER_COORDINATES 1229 std::cout <<
"Feature_float dimensions: (" <<
XSIZE(feature_float) <<
", " <<
YSIZE(feature_float) <<
", " <<
ZSIZE(feature_float) <<
")" << std::endl;
1233 setOfFeatures_RA[
n] =feature_RA;
1236 #ifdef DEBUG_FILTER_COORDINATES 1237 std::cout <<
"Calculate mahalanobis distance " << std::endl;
1238 size_t prevNumberOfCoordinates = newCoordinates3D.size();
1243 #ifdef DEBUG_FILTER_COORDINATES 1244 for (
size_t n = 0;
n < setOfFeatures_RA.size();
n++)
1246 std::cout <<
"pcaAnalyzer.getZscore(" <<
n <<
") " << mahalanobisDistance_List[
n] << std::endl;
1252 for (
size_t n = 0;
n < numberOfCoordinates;
n++)
1254 #ifdef DEBUG_FILTER_COORDINATES 1260 #ifdef DEBUG_FILTER_COORDINATES 1261 std::cout <<
"Deleted coordinate due to mahalanobis distance " <<
n <<
" at: (" << p.
x <<
", " << p.
y <<
", " << p.
z <<
")" << std::endl;
1264 newCoordinates3D.erase(newCoordinates3D.begin()+
n);
1265 numberOfCoordinates--;
1272 #ifdef DEBUG_FILTER_COORDINATES 1273 std::cout <<
"Number of corrdinates filtered by mahalanobis distance correlation: " << (prevNumberOfCoordinates - newCoordinates3D.size()) << std::endl;
1282 coordinates3D.clear();
1283 coordinates3D = newCoordinates3D;
1288 coordinates3D.clear();
1289 coordinates3D = newCoordinates3D;
1293 numberOfCoordinates = coordinates3D.size();
1295 for (
size_t i = 0;
i < numberOfCoordinates;
i++)
1299 std::cout <<
"Analyzing coordinate " <<
i << std::endl;
1301 if ((p.
z < halfBoxSize) || (p.
z + halfBoxSize) > zSize ||
1302 (p.
y < halfBoxSize) || (p.
y + halfBoxSize) > ySize ||
1303 (p.
x < halfBoxSize) || (p.
x + halfBoxSize) > xSize)
1305 #ifdef DEBUG_FILTER_COORDINATES 1306 std::cout <<
"Deleted border coordinate " <<
i <<
" at: (" << p.
x <<
", " << p.
y <<
", " << p.
z <<
")" << std::endl;
1309 coordinates3D.erase(coordinates3D.begin()+
i);
1310 numberOfCoordinates--;
1315 #ifdef DEBUG_FILTER_COORDINATES 1316 std::cout <<
"3D coordinates after filtering: " << std::endl;
1318 for(
size_t n = 0;
n < coordinates3D.size();
n++)
1320 std::cout <<
"Coordinate " <<
n <<
" (" << coordinates3D[
n].x <<
", " << coordinates3D[
n].y <<
", " << coordinates3D[
n].z <<
")" << std::endl;
1324 #ifdef VERBOSE_OUTPUT 1325 std::cout <<
"Filtering coordinates by correlation finished succesfully!" << std::endl;
1335 using std::chrono::high_resolution_clock;
1336 using std::chrono::duration_cast;
1337 using std::chrono::duration;
1338 using std::chrono::milliseconds;
1340 auto t1 = high_resolution_clock::now();
1348 xSize =
XSIZE(inputTomo);
1349 ySize =
YSIZE(inputTomo);
1350 zSize =
ZSIZE(inputTomo);
1351 nSize =
NSIZE(inputTomo);
1354 std::cout <<
"------------------ Input tomogram dimensions:" << std::endl;
1355 std::cout <<
"x " << xSize << std::endl;
1356 std::cout <<
"y " << ySize << std::endl;
1357 std::cout <<
"z " << zSize << std::endl;
1358 std::cout <<
"n " << nSize << std::endl;
1364 std::cout <<
"------------------ Preprocessed tomogram dimensions:" << std::endl;
1365 std::cout <<
"x " << xSize << std::endl;
1366 std::cout <<
"y " << ySize << std::endl;
1367 std::cout <<
"z " << zSize << std::endl;
1368 std::cout <<
"n " << nSize << std::endl;
1387 auto t2 = high_resolution_clock::now();
1388 auto ms_int = duration_cast<milliseconds>(t2 - t1);
1390 std::cout <<
"Execution time: " << ms_int.count() << std::endl;
1391 std::cout <<
"Program executed succesfully!" <<
"ms" << std::endl;
1400 #ifdef VERBOSE_OUTPUT 1401 std::cout <<
"Generating side info..." << std::endl;
1406 #ifdef VERBOSE_OUTPUT 1407 std::cout <<
"Side info generated successfully!" << std::endl;
1414 #ifdef DEBUG_FILTERLABEL 1427 double maxSquareDistance = 0;
1430 #ifdef DEBUG_FILTERLABEL 1434 for(
size_t n = 0;
n < coordinatesPerLabelX.size();
n++)
1436 distance = (coordinatesPerLabelX[
n]-centroX)*(coordinatesPerLabelX[
n]-centroX)+(coordinatesPerLabelY[
n]-centroY)*(coordinatesPerLabelY[
n]-centroY);
1438 if(distance >= maxSquareDistance)
1440 #ifdef DEBUG_FILTERLABEL 1449 maxDistace =
sqrt(maxSquareDistance);
1452 double circumscribedArea =
PI * (maxDistace * maxDistace);
1453 double area = 0.0 + (double)coordinatesPerLabelX.size();
1456 ocupation = area / circumscribedArea;
1458 #ifdef DEBUG_FILTERLABEL 1459 std::cout <<
"debugN " << debugN << std::endl;
1460 std::cout <<
"x max distance " << coordinatesPerLabelX[debugN] << std::endl;
1461 std::cout <<
"y max distance " << coordinatesPerLabelY[debugN] << std::endl;
1462 std::cout <<
"centroX " << centroX << std::endl;
1463 std::cout <<
"centroY " << centroY << std::endl;
1464 std::cout <<
"area " << area << std::endl;
1465 std::cout <<
"circumscribedArea " << circumscribedArea << std::endl;
1466 std::cout <<
"maxDistace " << maxDistace << std::endl;
1467 std::cout <<
"ocupation " << ocupation << std::endl;
1470 if(ocupation < 0.75)
1472 #ifdef DEBUG_FILTERLABEL 1473 std::cout <<
"COORDINATE REMOVED AT " << centroX <<
" , " << centroY <<
" BECAUSE OF OCCUPATION"<< std::endl;
1484 std::vector<size_t> coordinatesInSlice;
1486 #ifdef DEBUG_COORDS_IN_SLICE 1487 std::cout <<
"Geting coordinates from slice " << slice << std::endl;
1490 for(
size_t n = 0;
n < coordinates3D.size();
n++)
1492 if(slice == coordinates3D[
n].
z)
1494 coordinatesInSlice.push_back(
n);
1498 #ifdef DEBUG_COORDS_IN_SLICE 1499 std::cout <<
"Number of coordinates found: " << coordinatesInSlice.size() << std::endl;
1502 return coordinatesInSlice;
1508 #ifdef DEBUG_RADIAL_AVERAGE 1509 std::cout <<
"Calculating radial average..." << std::endl;
1515 for(
int k=0;
k<numSlices;
k++)
1519 double ii =
i-(boxSize/2);
1524 double jj =
j-(boxSize/2);
1525 int f =
sqrt(i2 + jj*jj);
1536 #ifdef DEBUG_RADIAL_AVERAGE 1537 std::cout <<
"Radial summatory" << std::endl;
1540 std::cout << radialAverage[
i] <<
" ";
1543 std::cout << std::endl;
1545 std::cout <<
"Radial average" << std::endl;
1550 radialAverage[
i] /= counter[
i];
1552 #ifdef DEBUG_RADIAL_AVERAGE 1553 std::cout << radialAverage[
i] <<
" ";
1557 #ifdef DEBUG_RADIAL_AVERAGE 1558 std::cout << std::endl;
1559 std::cout <<
"Number of elements per radius" << std::endl;
1563 std::cout << counter[
i] <<
" ";
1566 std::cout << std::endl;
1573 #ifdef DEBUG_MAHALANOBIS_DISTANCE 1574 std::cout <<
"Calculating Mahalanobis distance..." << std::endl;
1579 for (
size_t n = 0;
n < setOfFeatures_RA.size();
n++)
1581 #ifdef DEBUG_MAHALANOBIS_DISTANCE 1582 std::cout <<
"Adding vector " <<
n << std::endl;
1591 #ifdef DEBUG_MAHALANOBIS_DISTANCE 1592 std::cout <<
"Zscore list of vertors" << std::endl;
1595 for (
size_t n = 0;
n < setOfFeatures_RA.size();
n++)
1597 #ifdef DEBUG_MAHALANOBIS_DISTANCE 1598 std::cout <<
"pcaAnalyzer.getZscore(" <<
n <<
") " << pcaAnalyzer.
getZscore(
n) << std::endl;
1601 mahalanobisDistance_List[
n] = pcaAnalyzer.
getZscore(
n);
void addVector(const MultidimArray< float > &_v)
Add vector.
void mahalanobisDistance(std::vector< MultidimArray< float >> &setOfFeatures_RA, MultidimArray< double > &mahalanobisDistance_List) const
double getDoubleParam(const char *param, int arg=0)
n The following was calculated during iteration
void getSlice(int k, MultidimArray< T1 > &M, char axis='Z', bool reverse=false, size_t n=0) const
FourierTransformer transformer
void sqrt(Image< double > &op)
#define DIRECT_A2D_ELEM(v, i, j)
void write(const FileName &name="", size_t select_img=ALL_IMAGES, bool isStack=false, int mode=WRITE_OVERWRITE, CastWriteMode castMode=CW_CAST, int _swapWrite=0)
void correlation_matrix(const MultidimArray< double > &m1, const MultidimArray< double > &m2, MultidimArray< double > &R, CorrelationAux &aux, bool center)
void preprocessVolume(MultidimArray< double > &inputTomo)
#define FOR_ALL_DIRECT_ELEMENTS_IN_ARRAY3D(V)
void removeDuplicatedCoordinates()
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void filterCoordinatesByCorrelation(MultidimArray< double > volFiltered)
#define DIRECT_A1D_ELEM(v, i)
void centerCoordinates(MultidimArray< double > volFiltered)
const char * getParam(const char *param, int arg=0)
void writeOutputCoordinates()
void evaluateZScore(int NPCA, int Niter, bool trained=false, const char *filename="temp.txt", int numdesc=0)
double mirrorCorrelationThr
int numberOfCoordinatesThr
void getHighContrastCoordinates(MultidimArray< double > &volFiltered)
void radialAverage(MultidimArray< float > &feature, MultidimArray< float > &radialAverage, size_t numSlices) const
#define DIRECT_MULTIDIM_ELEM(v, n)
#define DIRECT_A3D_ELEM(v, k, i, j)
__device__ float FFT_IDX2DIGFREQ(int idx, int size)
std::vector< size_t > getCoordinatesInSliceIndex(size_t slice)
TYPE distance(struct Point_T *p, struct Point_T *q)
int labelImage2D(const MultidimArray< double > &I, MultidimArray< double > &label, int neighbourhood)
bool filterLabeledRegions(std::vector< int > coordinatesPerLabelX, std::vector< int > coordinatesPerLabelY, double centroX, double centroY) const
#define DIRECT_ZYX_ELEM(v, k, i, j)
void learnPCABasis(size_t NPCA, size_t Niter)
Learn basis.
bool checkParam(const char *param)
int read(const FileName &name, DataMode datamode=DATA, size_t select_img=ALL_IMAGES, bool mapData=false, int mode=WRITE_READONLY)
std::string to_string(bond_type bondType)
void addUsageLine(const char *line, bool verbatim=false)
void initZeros(const MultidimArray< T1 > &op)
int getIntParam(const char *param, int arg=0)
void addParamsLine(const String &line)
void statisticsAdjust(U avgF, U stddevF)
double mahalanobisDistanceThr