-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrandGroupMaker.cs
More file actions
1411 lines (1244 loc) · 63.9 KB
/
Copy pathStrandGroupMaker.cs
File metadata and controls
1411 lines (1244 loc) · 63.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using betaBarrelProgram.AtomParser;
using betaBarrelProgram.BarrelStructures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Numerics;
namespace betaBarrelProgram
{
public class StrandGroupMaker //Rik
{
public string PdbName { get; set; }
public List<GroupOfStrands> GroupOfGroup { get; set; }
public List<Strand> BarrelStrands { get; set; }
//public Chain myChain;
public StrandGroupMaker(Protein newProt, List<Strand> AllStrands)
{
this.PdbName = newProt.PdbName;
this.GroupOfGroup = new List<GroupOfStrands>();
this.BarrelStrands = new List<Strand>();
var UnassignedStrandSet = new List<Strand>(); //Create a set of unsorted strand with strand size greater than 3
foreach (var strands in AllStrands)
{
if (strands.Residues.Count > 2)
{
UnassignedStrandSet.Add(strands);
}
}
var GroupedStrandSet = new GroupOfStrands();
var FirstStrand = new OneStrand(UnassignedStrandSet[0]); // Selecting the first strand from the unsorted list of strands.
UnassignedStrandSet.RemoveAt(0); //Remove the first strand from the unsorted list.
GroupedStrandSet.StrandSet.Add(FirstStrand);
do //Do this method till there are no more neighbors
{
var TotalNumberOfStrands = UnassignedStrandSet.Count;
if ((UnassignedStrandSet.Count != 0) || (GroupedStrandSet.StrandSet.Count != 0))
{
StrandSorter(UnassignedStrandSet, GroupedStrandSet.StrandSet); //Assign to different groups
if (TotalNumberOfStrands == (UnassignedStrandSet.Count/*-1*/))
{
Coordinate centroidOfGroup = Centroid(GroupedStrandSet); //Calculate the centroid of the group
GroupedStrandSet.Centroid = centroidOfGroup; //Add the centroid of the group
this.GroupOfGroup.Add(GroupedStrandSet); //Add the group of strands to the group of group.
FirstStrand = new OneStrand(UnassignedStrandSet[0]); // Selecting the first strand from the unsorted list of strands.
UnassignedStrandSet.RemoveAt(0); //Remove the first strand from the unsorted list.
GroupedStrandSet = new GroupOfStrands(); //Creat a new group
GroupedStrandSet.StrandSet.Add(FirstStrand); //Add the next first strand from the unassigned to the new group.
}
}
if ((UnassignedStrandSet.Count == 0) && (GroupedStrandSet.StrandSet.Count != 0)) //for the last strand set stuck in the GroupedStrandSet
{
Coordinate centroidOfGroup = Centroid(GroupedStrandSet); //Calculate the centroid of the group
GroupedStrandSet.Centroid = centroidOfGroup; //Add the centroid of the group
this.GroupOfGroup.Add(GroupedStrandSet); //Add the group of strands to the group of group.
GroupedStrandSet = new GroupOfStrands(); //Creat a new group
}
} while ((UnassignedStrandSet.Count != 0) || (GroupedStrandSet.StrandSet.Count != 0));
SharedFunctions.writePymolScriptForStrandGroups(GroupOfGroup, Global.OUTPUT_DIR, Global.DB_DIR, PdbName);
#region Hardcoding the PDBs that fails to work on it's own
var PDBList = new List<string>() { "1O8V", "1IFC", "4UU3", "1GL4", "2HLV", "2YNK", "4GEY",
"5GAQ", "6RB9", "3W9T", "5WC3",
"6RHV", "2R73", "2RD7", "5AZO", "3ANZ",
"4P1X", "5JZT",
"1QWD", "3S26", "3HPE", "1R0U", "2JOZ", "4XMC", "2OOJ",
"3KZA", "4R0B"};//#Hard Coding# Manually select strands
bool ManualSelect = PDBList.Contains(PdbName);
if (ManualSelect)
{
HardCode(GroupOfGroup);
}
#endregion
foreach (var group in this.GroupOfGroup)
{
if ((group.StrandSet.Count >= 8) || (PdbName == "1VKY") || (PdbName == "1XQB"))
{
if (!ManualSelect)
{
group.Cylinder = GetAxisCylinder(group);
NeighboringStrand(group);
CheckIfBarrel(group);
}
if (group.IsBarrel)
{
rearrangeBarrel(group);
AssignDirection(group);
BarrelAxis(group);
LogStrandData(group);
foreach (var strand in group.StrandSet)
{
BarrelStrands.Add(strand.StrandInTheGroup);
}
SharedFunctions.writePymolScriptForBarrelStrands(this.BarrelStrands, Global.OUTPUT_DIR, Global.DB_DIR, PdbName);
//SharedFunctions.WritePymolScriptForInOutStrands(this.BarrelStrands, Global.OUTPUT_DIR, Global.DB_DIR, PdbName, CCentroid, NCentroid);
break;
}
}
}
//Console.WriteLine("");
}
private void HardCode(List<GroupOfStrands> groupOfGroup)
{
var pdbs = new Dictionary<string, int>() { { "1O8V", 0 }, { "1IFC", 0 }, { "4UU3", 0 }, { "4GEY", 0 }, { "2HLV", 0 }, { "2R73", 0 },
{ "2RD7", 0 }, { "1QWD", 0 }, { "3S26", 0 }, { "3HPE", 0 }, { "1R0U", 0 }, { "2JOZ", 0 },
{ "4XMC", 0 }, { "2OOJ", 0 }, { "3KZA", 0 }, { "4R0B", 0 },
{ "2YNK", 1 }, { "5GAQ", 1 }, { "1GL4", 2 }, { "6RB9", 3 }, { "5JZT", 3 }, { "3W9T", 3 }
};//PDB name and the group number.
if (new List<string>() { "2HLV", "2R73", "3S26", "3KZA", "4R0B" }.Contains(this.PdbName))
{
groupOfGroup[pdbs[this.PdbName]].StrandSet.RemoveAt(8);//Removing problem strand
}
if (this.PdbName == "2RD7")
{
groupOfGroup[pdbs[this.PdbName]].StrandSet.RemoveAt(8);//Removing problem strand
groupOfGroup[pdbs[this.PdbName]].StrandSet.RemoveAt(8);//Removing problem strand
}
if (this.PdbName == "1QWD")
{
groupOfGroup[pdbs[this.PdbName]].StrandSet.RemoveAt(0);//Removing problem strand
groupOfGroup[pdbs[this.PdbName]].StrandSet.RemoveAt(0);//Removing problem strand
}
if (this.PdbName == "4XMC")
{
groupOfGroup[pdbs[this.PdbName]].StrandSet.RemoveAt(5);//Removing problem strand
}
if (new List<string>(pdbs.Keys).Contains(this.PdbName))
{
Console.WriteLine($"Manually changing 'IsBarrel' {this.PdbName}");
var group = groupOfGroup[pdbs[this.PdbName]];
group.IsBarrel = true;
}
else
{
Console.WriteLine($"Manually didn't change anything for {this.PdbName}");
}
}
public void AssignDirection(GroupOfStrands group)
{
for (int i = 0; i < group.StrandSet.Count; i++)
{
if (i == 0)
{
group.StrandSet[0].DirectionAssigned = true;
}
else
{
var strandOne = group.StrandSet[i - 1];
var strandTwo = group.StrandSet[i];
if (strandOne.DirectionAssigned = !strandTwo.DirectionAssigned)//check if either of the strands don't have a direction assigned
{
if (strandOne.DirectionAssigned == true) //Assign direction to the strand which doesn't have a direction yet
{
CheckIfParallel(strandTwo, strandOne);
strandTwo.DirectionAssigned = true;
}
else
{
CheckIfParallel(strandOne, strandTwo);
strandOne.DirectionAssigned = true;
}
}
}
}
}
private void BarrelAxis(GroupOfStrands group)
{
#region Selecting the ellipse based on which side of the plane the end residues are.
List<Vector3> myCEllipse = new List<Vector3>();
List<Vector3> myNEllipse = new List<Vector3>();
Vector3 Ccentroid = new Vector3();
Vector3 Ncentroid = new Vector3();
int CCtr = 0;
int NCtr = 0;
int TCtr = 0;
int ECtr = 0;
foreach (var strand in group.StrandSet)
{
TCtr++;
var strandStart = strand.StrandInTheGroup;
var StartAtom = strandStart.Residues.Single(s => s.ResNum == strandStart.ResNumStart).BackboneCoords["CA"];
var EndAtom = strandStart.Residues.Single(s => s.ResNum == strandStart.ResNumEnd).BackboneCoords["CA"];
if (strand.DirectionAssigned)
{
if (!strand.IsAntiParallel)
{
myCEllipse.Add(StartAtom);
Ccentroid += StartAtom;
CCtr++;
myNEllipse.Add(EndAtom);
Ncentroid += EndAtom;
NCtr++;
}
else
{
myCEllipse.Add(EndAtom);
Ccentroid += EndAtom;
CCtr++;
myNEllipse.Add(StartAtom);
Ncentroid += StartAtom;
NCtr++;
}
}
else
{
ECtr++;
Console.WriteLine($"Direction not assigned for Strand {strand.StrandInTheGroup.betaStrandNum}!!");
}
}
#endregion
#region Defining the Ccentroid, Ncentroid and axis
Ccentroid /= CCtr;
Ncentroid /= NCtr;
var Axis = Ncentroid - Ccentroid;
//Console.WriteLine($"Ccentroid: {Ccentroid.X}, {Ccentroid.Y}, {Ccentroid.Z} and Ncentroid: {Ncentroid.X}, {Ncentroid.Y}, {Ncentroid.Z}");
group.Ccentroid = Ccentroid;
group.Ncentroid = Ncentroid;
group.Axis = Axis;
group.CellipseCoords = myCEllipse;
group.NellipseCoords = myNEllipse;
#endregion
//foreach (var coordinate in myCEllipse)
//{
// Console.WriteLine($"pseudoatom myCEllipse, pos=[{coordinate.X}, {coordinate.Y}, {coordinate.Z}]" +
// $"\nshow spheres, myCEllipse" +
// $"\ncolor red, myCEllipse " +
// $"");
//}
//foreach (var coordinate in myNEllipse)
//{
// Console.WriteLine($"pseudoatom myNEllipse, pos=[{coordinate.X}, {coordinate.Y}, {coordinate.Z}]" +
// $"\nshow spheres, myNEllipse" +
// $"\ncolor blue, myNEllipse " +
// $"");
//}
#region Calculate InOut
//List<Strand> strandList = new List<Strand>();
//foreach (var strand in group.StrandSet)
//{
// strandList.Add(strand.StrandInTheGroup);
//}
////SharedFunctions.setInOut(strandList, Global.OUTPUT_DIR, this.PdbName, Axis, Ccentroid, Ncentroid);
////RYAN//SharedFunctions.setInOutMin(strandList, Global.OUTPUT_DIR, this.PdbName, Ccentroid, Ncentroid);
//SharedFunctions.WritePymolScriptForInOutStrands(strandList, Global.OUTPUT_DIR, Global.DB_DIR, PdbName, Ccentroid, Ncentroid);
////SharedFunctions.setInOutMin(strandList, Global.OUTPUT_DIR, this.PdbName, Ccentroid, Ncentroid);
#endregion
}
private void rearrangeBarrel(GroupOfStrands group)
{
Vector3 centroid = new Vector3((float)group.Centroid.X, (float)group.Centroid.Y, (float)group.Centroid.Z);
List<Strand> strands = new List<Strand>();
Vector3 firstCentroid = centroidCalculate(group.StrandSet[0].StrandInTheGroup);
Vector3 reference = firstCentroid - centroid;
foreach (var strand in group.StrandSet)
{
Vector3 strandCentroid = centroidCalculate(strand.StrandInTheGroup);
Vector3 strandDirection = strandCentroid - centroid;
strand.StrandInTheGroup.angle = SharedFunctions.AngleBetween(reference, strandDirection);
strands.Add(strand.StrandInTheGroup);
}
Strand selectStrand = strands.Where(strand => (strand.angle > 60 && strand.angle < 130)).First();
Vector3 selectStrandDirection = centroidCalculate(selectStrand) - centroid;
Vector3 refCrossP = Vector3.Cross(reference, selectStrandDirection);
var ctr = 0;
foreach (var strand in group.StrandSet)
{
Vector3 strandCentroid = centroidCalculate(strand.StrandInTheGroup);
Vector3 strandDirection = strandCentroid - centroid;
Vector3 CrossP = Vector3.Cross(reference, strandDirection);
var refAngle = SharedFunctions.AngleBetween(CrossP, refCrossP);
if (refAngle > 90)
{
strand.StrandInTheGroup.angle = 360 - strand.StrandInTheGroup.angle;
}
//Console.WriteLine($"pseudoatom strand{ctr}, pos=[{strandCentroid.X}, {strandCentroid.Y}, {strandCentroid.Z}]" +
// $"\nshow spheres, strand{ctr}" +
// $"\nlabel strand{ctr}, \" {strand.StrandInTheGroup.angle:000.##}\"" +
// $"");
//ctr++;
}
// Console.WriteLine($"pseudoatom centroid, pos=[{centroid.X}, {centroid.Y}, {centroid.Z}]" +
//$"\nshow spheres, centroid");
group.StrandSet.Sort((a, b) => a.StrandInTheGroup.angle.CompareTo(b.StrandInTheGroup.angle));
//foreach (var item in strands)
//{
// Console.WriteLine(item.angle);
//}
int i = 0;
foreach (var strand in group.StrandSet)
{
//Console.WriteLine($"{item.betaStrandNum} angle is {item.angle}");
strand.StrandInTheGroup.StrandNum = i;
i++;
}
Vector3 centroidCalculate(Strand Strand)
{
var caList = Strand.Select(residue => residue.BackboneCoords["CA"]).ToList();
Vector3 strandCentroid = new Vector3
{
X = caList.Sum(ca => ca.X) / caList.Count,
Y = caList.Sum(ca => ca.Y) / caList.Count,
Z = caList.Sum(ca => ca.Z) / caList.Count
};
return strandCentroid;
}
//return strands;
}
private void CheckIfBarrel(GroupOfStrands group)
{
int NeighborCount = 0;
foreach (var oneStrand in group.StrandSet)
{
NeighborCount += oneStrand.LeftNeighbors.Count;
NeighborCount += oneStrand.RightNeighbors.Count;
}
if (NeighborCount == (2 * group.StrandSet.Count)) //Closed beta barrels should have 2n neighbors compared to 2n-2 for open beta sheet
{
group.IsBarrel = true;
}
if ((NeighborCount == ((2 * group.StrandSet.Count) - 2)) && (group.StrandSet.Count > 4))
{
{
foreach (var oneStrand in group.StrandSet)
{
if (oneStrand.LeftNeighbors.Count == 0 || oneStrand.RightNeighbors.Count == 0)
{
OneStrand strand1 = oneStrand;
foreach (var oneStrand2 in group.StrandSet)
{
if ((oneStrand2 != strand1) && (oneStrand2.LeftNeighbors.Count == 0 || oneStrand2.RightNeighbors.Count == 0))
{
OneStrand strand2 = oneStrand2;
group.IsBarrel = CheckIfNear(strand1, strand2);
break;
}
}
break;
}
}
}
}
}
private bool CheckIfNear(OneStrand strand1, OneStrand strand2)
{
bool flag = false;
foreach (var Res1 in strand1.StrandInTheGroup.Residues)
{
foreach (var Res2 in strand2.StrandInTheGroup.Residues)
{
var distance = Res1.BackboneCoords["CA"] - Res2.BackboneCoords["CA"];
if (distance.Length() < 11)
{
flag = true;
}
}
}
return flag;
}
private void StrandSorter(List<Strand> UnassignedStrandSet, List<OneStrand> GroupedStrandSet)
{
for (int i = 0; i < UnassignedStrandSet.Count; i++)
{
for (int j = 0; j < GroupedStrandSet.Count; j++)
{
var SortedStrand = GroupedStrandSet[j];
int Hbond = 0;
if (Hbond < 1)
{
for (int r = 0; r < UnassignedStrandSet[i].Residues.Count; r++)
{
Res Residue1 = UnassignedStrandSet[i].Residues[r];
//Console.WriteLine($"Residue {r} in unassigned strand {i}, total residues in strand: {UnassignedStrandSet[i].Residues.Count}");
for (int p = 0; p < GroupedStrandSet[j].StrandInTheGroup.Residues.Count; p++)
{
Res Residue2 = GroupedStrandSet[j].StrandInTheGroup.Residues[p];
//Console.WriteLine($"Residue {p} in sorted strand {j}, total resideue in strand: {GroupedStrandSet[j].StrandInTheGroup.Residues.Count}");
if (CheckHBond(Residue1, Residue2, UnassignedStrandSet[i], GroupedStrandSet[j].StrandInTheGroup)) //Pass the two residue to the function CheckHBond to see if they form Hbond.
{
Hbond++;
if (Hbond >= 1)
{
break;
}
}
}
if (Hbond >= 1)
{
break;
}
}
}
if (Hbond >= 1)
{
//Console.WriteLine($"\n\nUnassigned count = {UnassignedStrandSet.Count}");
//SortedStrand.AllNeighboringStrands.Add(UnassignedStrandSet[i]); //Add the neighboring strand as the neighbor of the sorted strand.
var TempStrand = new OneStrand(UnassignedStrandSet[i]); //Create a new groupedstrand with the neighbor strand.
GroupedStrandSet.Add(TempStrand); //Add the neighboring grouped strand to the group.
UnassignedStrandSet.RemoveAt(i); //Remove that strand from the unassigned.
i = 0;
j = 0; //Reset counters as the the count has modified.
if ((UnassignedStrandSet.Count == 0))
{
break;
}
//if (UnassignedStrandSet.Count <= i)
//{
// i = -1;
//}
//if (GroupedStrandSet.Count <= j)
//{
// j = -1;
//}
#region CodeTest
//Console.WriteLine($"i = {i}, j = {j}");
//Console.WriteLine($" tempstrand = {TempStrand.StrandInTheGroup.ResNumStart}");
//Console.WriteLine($" unsortedStrand = {UnsortedStrand.ResNumStart}");
//Console.WriteLine($" unassigned at i = {UnassignedStrandSet[i].ResNumStart}");
//Console.WriteLine("\nUnassignedStrandSet\n");
//for (int a = 0; a < UnassignedStrandSet.Count; a++)
//{
// Console.WriteLine($" Pos{a} : {UnassignedStrandSet[a].ResNumStart}");
//}
//Console.WriteLine("\nGroupedStrandSet\n");
//for (int a = 0; a < GroupedStrandSet.Count; a++)
//{
// Console.WriteLine($" Pos{a} : {GroupedStrandSet[a].StrandInTheGroup.ResNumStart}");
//}
#endregion
}
}
if (UnassignedStrandSet.Count == 0)
{
break;
}
}
}
public void NeighboringStrand(GroupOfStrands groupOfStrands)
{
Coordinate centroidOfGroup = groupOfStrands.Centroid;
int k = 0;
groupOfStrands.StrandSet[0].DirectionAssigned = true; //Assigning the direction of first strand as parrallel
for (int i = 0; i < groupOfStrands.StrandSet.Count; i++) //Iterate over two strand and check if they are neigbors based on Hbonds
{
OneStrand strandOne = groupOfStrands.StrandSet[i];
strandOne.LeftNeighbors = new List<Strand>();
strandOne.RightNeighbors = new List<Strand>();
for (int j = 0; j < groupOfStrands.StrandSet.Count; j++)
{
OneStrand strandTwo = groupOfStrands.StrandSet[j];
k++;
int Hbond = 0;
var resList1 = new List<Res>();
var resList2 = new List<Res>();
//Console.WriteLine($"StrandOne: {strandOne.StrandInTheGroup.betaStrandNum}, strandTwo: {strandTwo.StrandInTheGroup.betaStrandNum}");
if (strandOne != strandTwo)
{
for (int p = 0; p < strandOne.StrandInTheGroup.Residues.Count; p++)
{
Res ResOne = strandOne.StrandInTheGroup.Residues[p];
for (int r = 0; r < strandTwo.StrandInTheGroup.Residues.Count; r++)
{
Res ResTwo = strandTwo.StrandInTheGroup.Residues[r];
//Console.WriteLine($"i:{i}, j:{j},p:{p},r:{r}");
if (CheckHBond(ResOne, ResTwo, strandOne.StrandInTheGroup, strandTwo.StrandInTheGroup))
{
Hbond++;
if (resList1.Count == 0)
{
resList1.Add(ResOne);
}
else if (resList1.Any(item => item.SeqID != ResOne.SeqID)) //make sure the residue is already not in the list
{
if (((resList1.Min(item => (item.BackboneCoords["CA"] - ResOne.BackboneCoords["CA"]).Length())) < 10)) //The residue is not too far away from residues in the list
{
resList1.Add(ResOne);
}
else
{
var dist = (resList1.Min(item => (item.BackboneCoords["CA"] - ResOne.BackboneCoords["CA"]).Length()));
//Console.WriteLine($"ResOne Distance: {dist}");
}
}
if (resList2.Count == 0)
{
resList2.Add(ResTwo);
}
else if (resList2.Any(item => item.SeqID != ResTwo.SeqID))//make sure the residue is already not in the list
{
if (((resList2.Min(item => (item.BackboneCoords["CA"] - ResTwo.BackboneCoords["CA"]).Length())) < 10)) //The residue is not too far away from residues in the list
{
resList2.Add(ResTwo);
}
else
{
var dist = (resList2.Min(item => (item.BackboneCoords["CA"] - ResTwo.BackboneCoords["CA"]).Length()));
//Console.WriteLine($"ResTwo Distance: {dist}");
}
}
}
//if (Hbond > 0)
//{
// break;
//}
}
//if (Hbond > 0)
//{
// break;
//}
}
}
if (Hbond != 0) //if the the strands are neighbors then check if the second strand is left or right of the first one.
{
//using (StreamWriter file = new StreamWriter(fileLocation, append: true))
//{
// string newLine = this.PdbName + "\t" + strandOne.StrandInTheGroup.betaStrandNum + "\t" + strandTwo.StrandInTheGroup.betaStrandNum + "\t" + Hbond; // "FirstAtom" + "\t" + "LastAtom" + "\t" + "StrandTwoAtom";
// file.Write(newLine);
//}
if (strandOne.StrandInTheGroup.betaStrandNum == 7)
{
//Console.ReadKey();
}
//Double detTest = CheckLeftOrRightStrandResList(strandOne.StrandInTheGroup, strandTwo.StrandInTheGroup, resList1, resList2, centroidOfGroup);
Double determinant = 0;
if (!groupOfStrands.Cylinder.Collision)
{
determinant = CheckLeftOrRightCylinder(resList1, resList2, groupOfStrands); //Use the method to determine left or right
}
else
{
determinant = CheckLeftOrRightResList(resList1, resList2, centroidOfGroup); //Use the method to determine left or right
}
if (determinant < 0) //If determinant is -ve then the strand is on the left.
{
strandOne.LeftNeighbors.Add(strandTwo.StrandInTheGroup);
}
else
{
strandOne.RightNeighbors.Add(strandTwo.StrandInTheGroup);
}
//if (strandOne.DirectionAssigned = !strandTwo.DirectionAssigned)//check if either of the strands don't have a direction assigned
//{
// if (strandOne.DirectionAssigned == true) //Assign direction to the strand which doesn't have a direction yet
// {
// CheckIfParallel(strandTwo, strandOne);
// strandTwo.DirectionAssigned = true;
// }
// else
// {
// CheckIfParallel(strandOne, strandTwo);
// strandOne.DirectionAssigned = true;
// }
//} //12/18/2020 - Removing strand assignment here. will implement it elsewhere.
}
}
//Console.WriteLine($"Strand One Left Count: {strandOne.LeftNeighbors.Count}, Strand One Right Count: {strandOne.RightNeighbors.Count} ");
if ((strandOne.LeftNeighbors.Count > 1) || (strandOne.RightNeighbors.Count > 1))
{
int ResCutOff = 2; //Ignore neighboring strands with 2 or less residues.
//check if any of the neighboring strand is less than 4 Residues
if (strandOne.LeftNeighbors.Count > 1)
{
foreach (var item in strandOne.LeftNeighbors)
{
if (item.NumOfRes <= ResCutOff)
{
groupOfStrands.StrandSet.Remove(groupOfStrands.StrandSet.Single(s => s.StrandInTheGroup.betaStrandNum == item.betaStrandNum));
strandOne.LeftNeighbors.Remove(item);
i = -1;
break;
}
}
}
if (strandOne.RightNeighbors.Count > 1)
{
foreach (var item in strandOne.RightNeighbors)
{
if (item.NumOfRes <= ResCutOff)
{
groupOfStrands.StrandSet.Remove(groupOfStrands.StrandSet.Single(s => s.StrandInTheGroup.betaStrandNum == item.betaStrandNum));
strandOne.RightNeighbors.Remove(item);
i = -1;
break;
}
}
}
if ((strandOne.LeftNeighbors.Count > 1) || (strandOne.RightNeighbors.Count > 1))
{
#region Uncomment this region and add breakpoint to see check strand selection
var ListOfStrand = new List<Strand>();
foreach (var oneStrand in groupOfStrands.StrandSet)
{
ListOfStrand.Add(oneStrand.StrandInTheGroup);
}
//SharedFunctions.writePymolScriptForBarrelStrands(ListOfStrand, Global.OUTPUT_DIR, Global.DB_DIR, PdbName);
#endregion
Concatenate(groupOfStrands);
i = -1;
}
}
}
}
private void CheckIfParallel(OneStrand strand1, OneStrand strand2)//first strand needs a direction assigned
{
var resiOne = strand1.StrandInTheGroup.Residues;
var resiTwo = strand2.StrandInTheGroup.Residues;
var resFirstStrand1 = resiOne[0].BackboneCoords["CA"];
var resFirstStrand2 = resiTwo[0].BackboneCoords["CA"];
var resLastStrand1 = resiOne[resiOne.Count - 1].BackboneCoords["CA"];
var resLastStrand2 = resiTwo[resiTwo.Count - 1].BackboneCoords["CA"];
var vectorStrand1 = resFirstStrand1 - resLastStrand1;
var vectorStrand2 = resFirstStrand2 - resLastStrand2;
var angleBetween = SharedFunctions.AngleBetween(vectorStrand1, vectorStrand2);
//Console.WriteLine(angleBetween);
if ((strand2.IsAntiParallel == false && angleBetween >= 90) || (strand2.IsAntiParallel == true && angleBetween < 90))
{
strand1.IsAntiParallel = true;
}
}
// Calculate the centroid of a group of Strands based on the CA of each residue.
private Coordinate Centroid(GroupOfStrands groupOfStrands)
{
// !!!ATENTION!!! MAybe change this later to read
int i = 0;
double x = 0;
double y = 0;
double z = 0;
foreach (OneStrand strand in groupOfStrands.StrandSet)
{
foreach (Res residue in strand.StrandInTheGroup.Residues)
{
foreach (Atom atom in residue)
{
if (atom.AtomName == "CA")
{
i++;
x = x + atom.Coords.X;
y = y + atom.Coords.Y;
z = z + atom.Coords.Z;
}
}
}
}
Coordinate centroid = new Coordinate((x / i), (y / i), (z / i));
return centroid;
}
private void Concatenate(GroupOfStrands groupOfStrands)
{
var StrandSet = groupOfStrands.StrandSet;
for (int i = 0; i < StrandSet.Count; i++)
{
if (StrandSet[i].LeftNeighbors.Count > 1) //For any strand that has more than 1 neighbor.
{
//moveNeighbor(StrandSet[i].LeftNeighbors);
joinNeighbors(StrandSet[i].LeftNeighbors);
if (StrandSet[i].LeftNeighbors.Count < 2 && StrandSet[i].RightNeighbors.Count < 2)
{
break;
}
}
if (StrandSet[i].RightNeighbors.Count > 1) //For any strand that has more than 1 neighbor.
{
//moveNeighbor(StrandSet[i].RightNeighbors);
joinNeighbors(StrandSet[i].RightNeighbors);
if (StrandSet[i].LeftNeighbors.Count < 2 && StrandSet[i].RightNeighbors.Count < 2)
{
break;
}
}
//8/11/2020 - if there are more than 2 neighbors, this fails and causes an infinite loop
//void moveNeighbor(List<Strand> Neighbors)
//{
// for (int j = (Neighbors.Count - 1); j == 1; j--)
// {
// var StrandNum1 = Neighbors[j].betaStrandNum;
// var StrandNum2 = Neighbors[j - 1].betaStrandNum;
// var Strand1 = StrandSet.Single(s => s.StrandInTheGroup.betaStrandNum == StrandNum1).StrandInTheGroup;
// var Strand2 = StrandSet.Single(s => s.StrandInTheGroup.betaStrandNum == StrandNum2).StrandInTheGroup;
// Strand1.Residues.AddRange(Strand2.Residues);
// StartEnd(Strand1, Strand2);
// //Strand1.ResNumEnd = Strand2.ResNumEnd;
// Strand1.NumOfRes = Strand1.Residues.Count;
// StrandSet.Remove(StrandSet.Single(s => s.StrandInTheGroup.betaStrandNum == StrandNum2));
// }
//}
//8/11/2020 - Adding this new method to deal with more than 2 neighbors
void joinNeighbors(List<Strand> Neighbors)
{
do
{
Strand firstStrand = findfarthestNeighbor(Neighbors);
Strand nearestStrand = findNearestNeighbor(firstStrand, Neighbors);
firstStrand.Residues.AddRange(nearestStrand.Residues);
StartEnd(firstStrand, nearestStrand);
//firstStrand.Residues.Sort((x, y) => x.SeqID.CompareTo(y.SeqID)); //8/23/20 - Removed, as it should be sort by physical position. //This sorts residue by sequence ID
var firstRes = firstStrand.Residues.Single(s => s.ResNum == firstStrand.ResNumStart);
firstStrand.Residues.Sort((x, y) =>
{
var d1 = (firstRes.BackboneCoords["CA"] - x.BackboneCoords["CA"]).Length();
var d2 = (firstRes.BackboneCoords["CA"] - y.BackboneCoords["CA"]).Length();
//Console.WriteLine($"d1 = {d1}, d2= {d2}");
return d1.CompareTo(d2);
});//sort residues based on there distance from the first strand
StrandSet.Single(s => s.StrandInTheGroup.betaStrandNum == firstStrand.betaStrandNum).Concatenated = true;
StrandSet.Remove(StrandSet.Single(s => s.StrandInTheGroup.betaStrandNum == nearestStrand.betaStrandNum));
Neighbors.Remove(Neighbors.Single(s => s.betaStrandNum == nearestStrand.betaStrandNum));
} while (Neighbors.Count > 1);
}
//method to find the strand farthest from it's neighbor
Strand findfarthestNeighbor(List<Strand> neighbors)
{
double maxOfMaxLength = 0;
Strand maxStrand = neighbors[0];
foreach (var strand1 in neighbors)
{
foreach (var strand2 in neighbors)
{
if (strand1 != strand2)
{
var listOfDist = CompareStrands(strand1, strand2);
var maxLength = listOfDist.Max(r => r.Length());
if (maxLength > maxOfMaxLength)//change the strand and longest length if that is found
{
maxOfMaxLength = maxLength;
maxStrand = strand1;
}
}
}
}
return maxStrand;
}
//method to find the nearest strand to the first strand
Strand findNearestNeighbor(Strand firstStrand, List<Strand> neighbors)
{
double minOfMinLength = 0;
Strand minStrand = neighbors[neighbors.Count - 1];
foreach (var strand in neighbors)
{
if (strand != firstStrand)
{
var listOfDist = CompareStrands(firstStrand, strand);
var minLength = listOfDist.Min(r => r.Length());
if (minLength > minOfMinLength)//change the strand and longest length if that is found
{
minOfMinLength = minLength;
minStrand = strand;
}
}
}
return minStrand;
}
//method to get the list of distance between the first and last residue of two strands
List<Vector3> CompareStrands(Strand strand1, Strand strand2)
{
var PointOneA = strand1.Residues.Single(s => s.ResNum == strand1.ResNumStart).BackboneCoords["CA"];
var PointOneB = strand1.Residues.Single(s => s.ResNum == strand1.ResNumEnd).BackboneCoords["CA"];
var PointTwoA = strand2.Residues.Single(s => s.ResNum == strand2.ResNumStart).BackboneCoords["CA"];
var PointTwoB = strand2.Residues.Single(s => s.ResNum == strand2.ResNumEnd).BackboneCoords["CA"];
var AA = PointOneA - PointTwoA;
var AB = PointOneA - PointTwoB;
var BA = PointOneB - PointTwoA;
var BB = PointOneB - PointTwoB;
var listOfDist = new List<Vector3> { AA, AB, BA, BB };
return listOfDist;
}
void StartEnd(Strand strand1, Strand strand2)
{
var listOfDist = CompareStrands(strand1, strand2);
double maxLength = listOfDist.Max(r => r.Length());
//Console.WriteLine($"Start: {strand1.Residues.Single(s => s.ResNum == strand1.ResNumStart).SeqID} & End: {strand1.Residues.Single(s => s.ResNum == strand1.ResNumEnd).SeqID}");
if (listOfDist[0].Length() == maxLength)
{
strand1.ResNumEnd = strand2.ResNumStart;
}
else if (listOfDist[1].Length() == maxLength)
{
strand1.ResNumEnd = strand2.ResNumEnd;
}
else if (listOfDist[2].Length() == maxLength)
{
strand1.ResNumStart = strand2.ResNumStart;
}
else if (listOfDist[3].Length() == maxLength)
{
strand1.ResNumStart = strand2.ResNumEnd;
}
//Console.WriteLine($"Start: {strand1.Residues.Single(s => s.ResNum == strand1.ResNumStart).SeqID} & End: {strand1.Residues.Single(s => s.ResNum == strand1.ResNumEnd).SeqID}");
}
}
}
private double CheckLeftOrRightResList(List<Res> ResList1, List<Res> ResList2, Coordinate centroid)
{
Vector3 FirstAtom = new Vector3(); //A
Vector3 LastAtom = new Vector3(); //B
Vector3 Centroid = new Vector3((float)centroid.X, (float)centroid.Y, (float)centroid.Z); //C
Vector3 StrandTwoAtom = new Vector3(); //X
//Console.WriteLine($"ResList1 Count : {ResList1.Count}, ResList Count2 : {ResList2.Count}");
////Display the two list
//Console.WriteLine("ResList1:");
//foreach (var res in ResList1)
//{
// Console.Write($"{res.SeqID} ");
//}
//Console.WriteLine("");
//Console.WriteLine("ResList2:");
//foreach (var res in ResList2)
//{
// Console.Write($"{res.SeqID} ");
//}
//Console.WriteLine("");
var medianResList1 = (ResList1.Count) / 2;
var medianResList2 = (ResList2.Count) / 2;
if (ResList1.Count >= 3)
{
FirstAtom = ResList1[medianResList1 - 1].BackboneCoords["CA"];
LastAtom = ResList1[medianResList1 + 1].BackboneCoords["CA"];
StrandTwoAtom = ResList2[medianResList2].BackboneCoords["CA"];
//string fileLocation = Global.OUTPUT_DIR + "NeighboringStrandLog.txt";
//using (StreamWriter file = new StreamWriter(fileLocation, append: true))
//{
// string newLine = "\t" + ResList1[medianResList1 - 1].SeqID + "\t" + ResList1[medianResList1 + 1].SeqID + "\t" + ResList2[medianResList2].SeqID;
// file.Write(newLine);
//}
}
else
{
if (ResList1.Count == 2)
{
FirstAtom = ResList1[0].BackboneCoords["CA"];
LastAtom = ResList1[1].BackboneCoords["CA"];
StrandTwoAtom = ResList2[0].BackboneCoords["CA"];
//string fileLocation = Global.OUTPUT_DIR + "NeighboringStrandLog.txt";
//using (StreamWriter file = new StreamWriter(fileLocation, append: true))
//{
// string newLine = "\t" + ResList1[0].SeqID + "\t" + ResList1[1].SeqID + "\t" + ResList2[0].SeqID;
// file.Write(newLine);
//}
}
else
{
FirstAtom = ResList1[0].BackboneCoords["N"];
LastAtom = ResList1[0].BackboneCoords["C"];
StrandTwoAtom = ResList2[0].BackboneCoords["CA"];
//string fileLocation = Global.OUTPUT_DIR + "NeighboringStrandLog.txt";
//using (StreamWriter file = new StreamWriter(fileLocation, append: true))
//{
// string newLine = "\t" + ResList1[0].SeqID + "\t" + ResList1[0].SeqID + "\t" + ResList2[0].SeqID;
// file.Write(newLine);
//}
}
}
var determinantPoint = Determinant(FirstAtom, LastAtom, StrandTwoAtom, Centroid);
//Console.WriteLine(determinantPoint);
return determinantPoint;
}
//Method to return a positive or negative value based on which side of the plane the MEDIAN strand residue is at.
private double CheckLeftOrRightCylinder(List<Res> ResList1, List<Res> ResList2, GroupOfStrands group)
{
Vector3 FirstAtom, LastAtom, StrandTwoAtom, Reference;
var medianResList1 = (ResList1.Count) / 2;
var medianResList2 = (ResList2.Count) / 2;
if (ResList1.Count >= 3) //When there are more than three atoms, use spaced out CA atoms.
{
FirstAtom = ResList1[medianResList1 - 1].BackboneCoords["CA"];
LastAtom = ResList1[medianResList1 + 1].BackboneCoords["CA"];
StrandTwoAtom = ResList2[medianResList2].BackboneCoords["CA"];
}
else
{
if (ResList1.Count == 2) //When there are only two atoms, use those two CA atoms.
{
FirstAtom = ResList1[0].BackboneCoords["CA"];
LastAtom = ResList1[1].BackboneCoords["CA"];
StrandTwoAtom = ResList2[0].BackboneCoords["CA"];
}
else //When there is only one atom, use the N and C atoms.
{
FirstAtom = ResList1[0].BackboneCoords["N"];
LastAtom = ResList1[0].BackboneCoords["C"];
StrandTwoAtom = ResList2[0].BackboneCoords["CA"];
}
}
//Calculate nearest reference point on the cylinder axis
Cylinder cylinder = group.Cylinder;
Vector3 p1 = cylinder.pointOne;
Vector3 p2 = cylinder.pointTwo;
Vector3 q = FirstAtom;
Vector3 u = p2 - p1;
Vector3 pq = q - p1;
Vector3 w2 = pq - Vector3.Multiply(u, Vector3.Dot(pq, u) / u.LengthSquared());
Reference = q - w2;
//Output a file with list of Pseudoatom with reference
//SharedFunctions.writePymolScriptReference(group, FirstAtom, LastAtom, StrandTwoAtom, Reference, Global.OUTPUT_DIR, Global.DB_DIR, PdbName);
var determinantPoint = Determinant(FirstAtom, LastAtom, StrandTwoAtom, Reference);
//Console.WriteLine(determinantPoint);
return determinantPoint;