public float CalculateDaviesBouldinIndex(List <List <string> > clusters) { //ClusterOutput clustOut; List <string> refStructues = new List <string>(clusters.Count); int [] dist; float [] avr = new float[clusters.Count]; float measure = 0; for (int i = 0; i < clusters.Count; i++) { float sum = 0; if (clusters[i].Count == 0) { continue; } //clustOut=jury.JuryOpt(clusters[i]); string refStr = dMeasure.GetReferenceStructure(clusters[i]); dist = dMeasure.GetDistance(refStr, clusters[i]); foreach (var item in dist) { sum += item; } avr[i] = sum / dist.Length; refStructues.Add(refStr); } for (int i = 0; i < refStructues.Count; i++) { float max = 0; for (int j = 0; j < refStructues.Count; j++) { int cDist; float v; if (i == j) { continue; } cDist = dMeasure.GetDistance(refStructues[i], refStructues[j]); v = ((float)(avr[i] + avr[j])) / cDist; if (v > max) { max = v; } } measure += max; } return(measure / refStructues.Count); }
/*private void CheckRefDistances() * { * HClusterNode current; * hierarchicalCluster dendrog = new hierarchicalCluster(dMeasure); * float dist; * Stack <HClusterNode> localSt=new Stack<HClusterNode>(); * st.Clear(); * st.Push(root); * current = root; * while (st.Count != 0) * { * current=st.Pop(); * st.Push(current.joined[0]); * st.Push(current.joined[1]); * * localSt.Push(current); * } * while(localSt.Count!=0) * { * float dist2; * * current=localSt.Pop(); * dist = dMeasure.GetDistance(current.setStruct[0], current.joined[0].setStruct[0]); * dist2 = dMeasure.GetDistance(current.setStruct[0], current.joined[1].setStruct[0]); * //current.levelDist = (dist > dist2) ? dist : dist2; * //current.levelDist = (dist + current.joined[0].levelDist + dist2 + current.joined[1].levelDist) / 2; * current.levelDist = dMeasure.GetDistance(current.joined[0].setStruct[0], current.joined[1].setStruct[0]); * * } * }*/ private HClusterNode JoinNodes(List <HClusterNode> nodes) { HClusterNode node = new HClusterNode(); node.joined = new List <HClusterNode>(); node.setStruct = new List <string>(); foreach (var item in nodes) { node.joined.Add(item); foreach (var itemN in item.setStruct) { node.setStruct.Add(itemN); } } List <string> refList = null; if (node.joined != null) { refList = new List <string>(); foreach (var item in node.joined) { refList.Add(item.refStructure); } } string refStr = dMeasure.GetReferenceStructure(node.setStruct, refList); node.refStructure = refStr; for (int i = 0; i < node.setStruct.Count; i++) { if (refStr == node.setStruct[i]) { refStr = node.setStruct[0]; node.setStruct[0] = node.setStruct[i]; node.setStruct[i] = refStr; break; } } return(node); }
public ClusterOutput HierarchicalClustering(List <string> structures) { List <List <HClusterNode> > level = new List <List <HClusterNode> >(); List <HClusterNode> levelNodes = new List <HClusterNode>(); List <HClusterNode> rowNodes = new List <HClusterNode>(); ClusterOutput outCl = new ClusterOutput(); int levelCount = 0; bool end = false; HClusterNode node; if (structures.Count <= 1) { outCl.hNode = new HClusterNode(); outCl.hNode.setStruct = structures; outCl.hNode.refStructure = structures[0]; outCl.hNode.levelDist = 0; outCl.hNode.joined = null; return(outCl); } progressRead = 1; dMeasure.CalcDistMatrix(structures); for (int i = 0; i < structures.Count; i++) { node = new HClusterNode(); node.refStructure = structures[i]; node.joined = null; node.setStruct.Add(structures[i]); node.levelNum = levelCount; node.levelDist = dMeasure.maxSimilarity; node.realDist = dMeasure.GetRealValue(node.levelDist); levelNodes.Add(node); } maxV = levelNodes.Count + 1; level.Add(levelNodes); while (!end) { levelNodes = new List <HClusterNode>(); List <List <HClusterNode> > rowList = LevelMinimalDist(level[level.Count - 1]); if (rowList.Count > 0) { foreach (var item in rowList) { node = new HClusterNode(); node.joined = item; node.levelDist = min; node.realDist = dMeasure.GetRealValue(min); node.levelNum = level.Count; for (int m = 0; m < item.Count; m++) { node.setStruct.AddRange(item[m].setStruct); item[m].fNode = true; } node.refStructure = dMeasure.GetReferenceStructure(node.setStruct); List <string> refList = new List <string>(); foreach (var itemJoined in node.joined) { refList.Add(itemJoined.refStructure); } node.refStructure = null; if (mustRefStructure != null) { foreach (var itemRef in refList) { if (itemRef == mustRefStructure) { node.refStructure = mustRefStructure; } } } if (node.refStructure == null) { node.refStructure = dMeasure.GetReferenceStructure(node.setStruct, refList); } levelNodes.Add(node); } } if (levelNodes.Count > 0) { level.Add(levelNodes); for (int i = 0; i < level[level.Count - 2].Count; i++) { if (!level[level.Count - 2][i].fNode) { level[level.Count - 1].Add(level[level.Count - 2][i]); } } currentV = maxV - levelNodes.Count; } if (level[level.Count - 1].Count == 1) { end = true; } } outCl.hNode = level[level.Count - 1][0]; outCl.hNode.levelNum = 0; //At the end level num must be set properly Queue <HClusterNode> qq = new Queue <HClusterNode>(); HClusterNode h; for (int i = 0; i < level.Count; i++) { for (int j = 0; j < level[i].Count; j++) { level[i][j].fNode = true; } } for (int i = 0; i < level.Count; i++) { for (int j = 0; j < level[i].Count; j++) { if (level[i][j].fNode) { level[i][j].levelDist = Math.Abs(level[i][j].levelDist - dMeasure.maxSimilarity); level[i][j].realDist = dMeasure.GetRealValue(level[i][j].levelDist); level[i][j].fNode = false; } } } qq.Enqueue(level[level.Count - 1][0]); while (qq.Count != 0) { h = qq.Dequeue(); if (h.joined != null) { foreach (var item in h.joined) { item.levelNum = h.levelNum + 1; qq.Enqueue(item); } } } outCl.hNode.dirName = dirName; outCl.clusters = null; outCl.juryLike = null; currentV = maxV; outCl.runParameters = hierOpt.GetVitalParameters(); return(outCl); }
string CLusterRepresent(DistanceMeasure distance, List<string> targets) { return distance.GetReferenceStructure(targets); }
public void CalcStat() { List<string> fileNames = new List<string>(); List<string> refStructures = new List<string>(); DistanceMeasures measure = distanceControl1.distDef; DistanceMeasure distTemp = null; double distRes=0; maxV = selected.Count; distList.Clear(); try { tableRes = new DataTable(); tableRes.Columns.Add("Cluster Size", typeof(int)); tableRes.Columns.Add("Reference structure", typeof(string)); tableRes.Columns.Add("Distance", typeof(double)); tableRes.Columns.Add("Hidden1", typeof(int)); tableRes.Columns.Add("Hidden2", typeof(string)); if (checkBoxSable.Checked) { tableRes.Columns.Add("Dist to Sable", typeof(double)); maxV *= 2; } if (checkBox1.Checked) { for (int i = 0; i < selected.Count; i++) maxV += selected[i].Count; } fileNames.Add(textBox1.Text); jury1D jury = new jury1D(); if (distanceControl1.reference) jury.PrepareJury(dirName, alignFile, distanceControl1.referenceProfile); refStructures.Clear(); selected.Sort(delegate(List<string> first, List<string> second) { return first.Count.CompareTo(second.Count); }); selected.Reverse(); // dataGridView1.Rows.Add(selected.Count); for (int i = 0; i < selected.Count; i++) { string refD = selected[i][0]; ClusterOutput juryO = null; if (distanceControl1.reference) { if (selected[i].Count > 5) { juryO = jury.JuryOptWeights(selected[i]); if (juryO == null) continue; refD = juryO.juryLike[0].Key; if (!fileNames.Contains(dirName + Path.DirectorySeparatorChar + juryO.juryLike[0].Key)) { fileNames.Add(dirName + Path.DirectorySeparatorChar + juryO.juryLike[0].Key); refStructures.Add(dirName + Path.DirectorySeparatorChar + refD); } } else if (!fileNames.Contains(dirName + Path.DirectorySeparatorChar + selected[i][0])) { fileNames.Add(dirName + Path.DirectorySeparatorChar + selected[i][0]); refStructures.Add(dirName + Path.DirectorySeparatorChar + selected[i][0]); } } else { dist = PrepareDistanceMeasure(selected[i], measure, dirName); distTemp = dist; refD = dist.GetReferenceStructure(selected[i]); fileNames.Add(dirName + Path.DirectorySeparatorChar + refD); refStructures.Add(dirName + Path.DirectorySeparatorChar + refD); } if (fileNames.Count == 2) { switch (measure) { case DistanceMeasures.HAMMING: dist = new JuryDistance(fileNames, alignFile, false, distanceControl1.profileName); break; case DistanceMeasures.MAXSUB: dist = new MaxSub(fileNames, null, false); break; case DistanceMeasures.RMSD: dist = new Rmsd(fileNames, null, false, distanceControl1.CAtoms); break; } dist.InitMeasure(); distRes = Convert.ToDouble(String.Format("{0:0.00}", dist.GetDistance(native, refD) / 100.0)); } fileNames.RemoveAt(fileNames.Count - 1); if (checkBoxSable.Checked && refStructures.Count > 0) tableRes.Rows.Add(selected[i].Count, refD, distRes, i, dirName, 0.0); else tableRes.Rows.Add(selected[i].Count, refD, distRes, i, dirName); currentV++; } if (checkBoxSable.Checked && refStructures.Count > 0) { Dictionary<string, double> res = null;// SableDist(refStructures); for (int i = 0; i < refStructures.Count; i++) { if (res.ContainsKey(Path.GetFileName(refStructures[i]))) { DataRow dr = tableDist.Rows[i]; dr[5] = Convert.ToDouble(String.Format("{0:0.00}", res[Path.GetFileName(refStructures[i])])); } } } if (checkBox1.Checked) { CalculateDistToAll(); } } catch(Exception ex) { exc = ex; } }
public void CalcStat() { List <string> fileNames = new List <string>(); List <string> refStructures = new List <string>(); DistanceMeasures measure = distanceControl1.distDef; DistanceMeasure distTemp = null; double distRes = 0; maxV = selected.Count; distList.Clear(); try { tableRes = new DataTable(); tableRes.Columns.Add("Cluster Size", typeof(int)); tableRes.Columns.Add("Reference structure", typeof(string)); tableRes.Columns.Add("Distance", typeof(double)); tableRes.Columns.Add("Hidden1", typeof(int)); tableRes.Columns.Add("Hidden2", typeof(string)); if (checkBoxSable.Checked) { tableRes.Columns.Add("Dist to Sable", typeof(double)); maxV *= 2; } if (checkBox1.Checked) { for (int i = 0; i < selected.Count; i++) { maxV += selected[i].Count; } } fileNames.Add(textBox1.Text); jury1D jury = new jury1D(); if (distanceControl1.reference) { jury.PrepareJury(dirName, alignFile, distanceControl1.referenceProfile); } refStructures.Clear(); selected.Sort(delegate(List <string> first, List <string> second) { return(first.Count.CompareTo(second.Count)); }); selected.Reverse(); // dataGridView1.Rows.Add(selected.Count); for (int i = 0; i < selected.Count; i++) { string refD = selected[i][0]; ClusterOutput juryO = null; if (distanceControl1.reference) { if (selected[i].Count > 5) { juryO = jury.JuryOptWeights(selected[i]); if (juryO == null) { continue; } refD = juryO.juryLike[0].Key; if (!fileNames.Contains(dirName + Path.DirectorySeparatorChar + juryO.juryLike[0].Key)) { fileNames.Add(dirName + Path.DirectorySeparatorChar + juryO.juryLike[0].Key); refStructures.Add(dirName + Path.DirectorySeparatorChar + refD); } } else if (!fileNames.Contains(dirName + Path.DirectorySeparatorChar + selected[i][0])) { fileNames.Add(dirName + Path.DirectorySeparatorChar + selected[i][0]); refStructures.Add(dirName + Path.DirectorySeparatorChar + selected[i][0]); } } else { dist = PrepareDistanceMeasure(selected[i], measure, dirName); distTemp = dist; refD = dist.GetReferenceStructure(selected[i]); fileNames.Add(dirName + Path.DirectorySeparatorChar + refD); refStructures.Add(dirName + Path.DirectorySeparatorChar + refD); } if (fileNames.Count == 2) { switch (measure) { case DistanceMeasures.HAMMING: dist = new JuryDistance(fileNames, alignFile, false, distanceControl1.profileName); break; case DistanceMeasures.MAXSUB: dist = new MaxSub(fileNames, null, false); break; case DistanceMeasures.RMSD: dist = new Rmsd(fileNames, null, false, distanceControl1.CAtoms); break; } dist.InitMeasure(); distRes = Convert.ToDouble(String.Format("{0:0.00}", dist.GetDistance(native, refD) / 100.0)); } fileNames.RemoveAt(fileNames.Count - 1); if (checkBoxSable.Checked && refStructures.Count > 0) { tableRes.Rows.Add(selected[i].Count, refD, distRes, i, dirName, 0.0); } else { tableRes.Rows.Add(selected[i].Count, refD, distRes, i, dirName); } currentV++; } if (checkBoxSable.Checked && refStructures.Count > 0) { Dictionary <string, double> res = null;// SableDist(refStructures); for (int i = 0; i < refStructures.Count; i++) { if (res.ContainsKey(Path.GetFileName(refStructures[i]))) { DataRow dr = tableDist.Rows[i]; dr[5] = Convert.ToDouble(String.Format("{0:0.00}", res[Path.GetFileName(refStructures[i])])); } } } if (checkBox1.Checked) { CalculateDistToAll(); } } catch (Exception ex) { exc = ex; } }
string CLusterRepresent(DistanceMeasures distance,string dirName, List<string> targets,string alignFile, string profileName) { DistanceMeasure dist; List<string> fileNames=new List<string>(); foreach (var item in targets) fileNames.Add(dirName + Path.DirectorySeparatorChar + item); dist = PrepareDistance(distance, fileNames, alignFile, profileName); return dist.GetReferenceStructure(targets); }