-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster_011.py
1462 lines (899 loc) · 41.7 KB
/
cluster_011.py
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
import numpy as np
from numpy import linalg as LA
from scipy.spatial import distance
import mpi4py
import pandas as pd
import re
import glob
import math
from enum import Enum
from scipy import optimize
##-----create an enum
class resin_sequence(Enum):
# define
O_C = 1 # O-C bod
pN_B = 2 #primary nitrogen-benzene bond
sN_B = 3 #secondary nitrogen-benzene bond
eC_sN = 4 #epoxide carbon -reacted carbon bond
eC_tN = 5 #epoxide secondary amine
resin_key = 6
resin_rev = 7 #reverse of resin key
N_N = 8
C_N = 11
C_rC = 12
#C_tN = 12
B_N = 13
#B_tN = 14
sN_eC = 15
tN_eC = 16
# BFDGE_OC = [18, 12] #CNT-O-epoxide
# BFDGE_CsN = [12, 14, 15]#CNT-
# BFDGE_CtN = [12, 14, 16]
# BFDGE_sNpN = [15, 4, 4, 4, 9]
class BFDGE():
def __init__(self):
self.R = [12, 2, 2, 3, 4, 4, 4, 4, 2, 4, 4, 4, 4, 3, 2, 2, 12] #end to end sequence of atoms
#notes:
#overlap between end atom at the end and the atom at the beginning (because sequece[1:])
##define all pathways between twoo nodes, i.e. epoxide C and benzene atom
R1 = self.R #functionalized resin molecule not bonded to any
R2 = self.R[:-1] + [14, 15, 4] #functionalized epoxy to primary nitrogen
R3 = self.R[:-1] + [14, 16, 4]
#assume that the connecting benezene is the connecting node
R4 = [15, 14] + self.R[1:]
R5 = [15, 14] + self.R[1:-1] + [14, 15, 4]
R6 = [15, 14] + self.R[1:-1] + [14, 16, 4]
#
R7 = [16, 14] + self.R[1:]
R8 = [16, 14] + self.R[1:-1] + [14, 15, 4]
R9 = [16, 14] + self.R[1:-1] + [14, 16, 4]
#tertiary connection
#The "S" resins are the R resins in reverse
R10 = self.R[1:]
R11 = self.R[1:-1] + [14, 15]
R12 = self.R[1:-1] + [14, 16]
self.sequences = {}
self.sequences[resin_sequence.O_C] = [[18, 2]]
self.sequences[resin_sequence.pN_B] = [[9, 4]]
self.sequences[resin_sequence.sN_B] = [[15, 4]]
self.sequences[resin_sequence.eC_sN] = [(self.R[:-1]) + [14, 15], (self.R[1:-1]) + [14, 15]]
self.sequences[resin_sequence.eC_tN] = [(self.R[:-1]) + [14, 16], (self.R[1:-1]) + [14, 15]]
self.sequences[resin_sequence.resin_key] = [R1, R2, R3, R4, R5, R6, R7, R8, R9]
###second degree chains
self.sequences[resin_sequence.C_N] = [[12, 14, 15], [12, 14, 16]]
self.sequences[resin_sequence.C_rC] = [R10, R11, R12]
#self.sequences[resin_sequence.C_tN] = [[12, 14, 16]]
self.sequences[resin_sequence.B_N] = [[4, 4, 4, 9], [4, 4, 4, 15], [4, 4, 4, 16]]
self.sequences[resin_sequence.N_N] = [[9, 4, 4, 4, 9], [15, 4, 4, 4, 9], [16, 4, 4, 4, 9],
[9, 4, 4, 4, 15], [15, 4, 4, 4, 15], [16, 4, 4, 4, 15],
[9, 4, 4, 4, 16], [15, 4, 4, 4, 16], [16, 4, 4, 4, 16]]
#self.sequences[resin_sequence.B_N] = [[4, 4, 4, 9], [4, 4, 4, 15], [4, 4, 4, 16], [4, 4, 4, 4, 9], [4, 4, 4, 4, 15], [4, 4, 4, 4, 16]]
#self.sequences[resin_sequence.B_tN] = [[4, 4, 4, 16]]
#define the reverse N-epoxy chain
self.sequences[resin_sequence.sN_eC] = ((self.R[:-1]) + [14, 15])[::-1]
self.sequences[resin_sequence.tN_eC] = ((self.R[:-1]) + [14, 15])[::-1]
class cluster_files():
def __init__(self, list_of_files):
self.list = list_of_files
self.rho_list = []
for i in range(0, len(list_of_files)):
file_temp = sorted(glob.glob(list_of_files[i]), key=key_func)
for j in range(0, len(file_temp)):
rho_cl = self.read_files(file_temp[j])
self.rho_list.append(rho_cl)
print (self.rho_list)
def read_files(self, filename, cnt_type=22):
df = pd.read_table(filename, delim_whitespace=True, header=None, skiprows=3)
type = ((df.iloc[:, 3].str.split("_", expand=True)).as_matrix()[:, -1]).astype(int)
particle_id = (df.iloc[:, 4]).as_matrix()
cluster_id = df.iloc[:, -1].as_matrix()
#index out CNT type
idx_cnt= np.where(type==cnt_type)[0]
unique_cnt_cluster = np.unique(cluster_id[idx_cnt]) #find the unique clusters in
if len(unique_cnt_cluster) == 1:
cluster_choose = unique_cnt_cluster[0]
particle_idx = np.where(cluster_id==cluster_choose)[0]
particle_pick = particle_id[particle_idx]
rho_cl = float(len(particle_pick))/float(len(particle_id))
else:
rho_cl = 0.0
return rho_cl
#---------
class MD_model():
def __init__(self, MD_file, f_value, chain_file, classname=BFDGE, extract_feature_bool=True):
#extract_feature_bool -> indicates if we want to extract feautures
self.feature_bool = extract_feature_bool
##MD file -> MD file sin question
#f_value -> force value
self.classname = classname
self.file_name = MD_file
self.f_value = f_value
self.chain_file = chain_file
#find center:
#this class identifies each MD model, and for each model, identify all the clusters
#seq_to_pick = self.load_resin(self, class_name=BFDGE, seq_type=O_C)
d_thr, self.id_fun, id_cnt, self.fun_type, self.df_xyz, self.df_bond = self.detect_fun_atoms()
print "troubleshoot: ", d_thr, self.id_fun, id_cnt, self.fun_type
print "id_cnt: ", id_cnt
#get the center of the CNT circle
self.center, self.R = get_circle_center(df=self.df_xyz)
print "self.center: ", self.center
print "self.id_fun: ", self.id_fun
if len(self.id_fun) > 0:
if self.id_fun[0] != -1:
global_var = global_features(fun_id=self.id_fun, cnt_id=id_cnt, center=self.center, df=self.df_xyz)
#find_chain_particles(sequence=[12, 2, 2, 3, 4, 4, 4, 4, 2, 4, 4, 4, 4, 3, 2, 2, 12], init_id=3770, df_xyz=self.df_xyz, df_bond=self.df_bond)
###this block loads the chains
if self.id_fun[0] != -1:
self.execute_chain()
#if ('force_wpattern' in kwargs):
#force_wpattern = kwargs['force_wpattern']
#self.MD_file_list, self.f_file_list = read_MD_files(list_of_files=MD_wpattern, forcefile=force_wpattern)
#else:
#self.MD_file_list, _ = read_MD_files(list_of_files=MD_wpattern)
def execute_chain(self, max_degree=10, fun_max=5):
#this function takes in the inital atom id as input and generates a staggered list containing the atom ids
#this should be done for each new funtionalized atom
#max_degree -> maximum degree of removal from the cnt atom
#fun_max = maximum number of
#initialize lists
next_atoms = []
next_atom_types = []
stag_list = []
#start initializing a hierarchy of lists
#level_0 initializaation
X0_list = [] #high level node list
A0_list = [] #high level Adjacency matrix list
self.fun_node = self.id_fun
#total dimensions -> [C, N, O]
#Adjacency matrix
if self.feature_bool:
self.fun_mat = np.zeros((fun_max, 3))
for idx, fun_id in enumerate(self.id_fun):
#D = benzene_density_count(df=self.df_xyz, node_id=self.id_fun[idx])
print idx, self.id_fun[idx]
self.set_fun_feature(idx=idx)
print "---new fun----------------"
#level 1 list initialization
X1_list = [] # high level node list
A1_list = [] # high level Adjacency matrix list
thread_list = []
node_list = []
node_type_list = []
node_seq = []
node_seq_type = []
for n in range(0, max_degree):
print "-------------------------------------New degree of removal---------------------"
# level 2 list initialization
X2_list = [] # high level node list
A2_list = [] # high level Adjacency matrix list
if n == 0:
init_id = [fun_id]
next_atoms = []
next_atom_types = []
else:
init_id = next_atoms
init_types = next_atom_types
#reset next_atoms to get rid of old next_atoms
next_atoms = []
next_atom_types = []
print "init_id; ", init_id
for i in range(0, len(init_id)):
print "!!!!!!!!!New atom id !!!!!!!!!!!!!!"
#print init_id
print "init id: ", init_id[i]
atom_seq_1 = atom_seq_2 = type_seq_1 = type_seq_2 = -1
# first load the sequece that you'd like to investigate
# need to define
init_pick = init_id[i] #select which among the init id
if n==0:
fun_bool = True
fun_val = self.fun_type[idx] ##the type of atom in question
end_type = -1
else:
fun_bool = False
fun_val =-1
end_type = init_types[i]
#now load the resin molecule and traverse through the chain
#print "n, end_type: ", n, end_type
#print "fun bool, fun_val: ", fun_bool, fun_val
orig_atom, epoxy_bool = trace_origin(init_atom=init_pick, node_list=node_list,
node_type_list=node_seq_type)
print "orig atom: ", orig_atom
print "epoxy bool: ", epoxy_bool
seq_1, seq_2, num_pathway = self.load_resin_molecule(fun_bool=fun_bool,
fun_type=fun_val, end_type=end_type, epoxy_bool=epoxy_bool)
#print "Seq 1: ", seq_1
#print "Seq 2: ", seq_2, fun_bool, fun_val
#print seq_1, seq_2, num_pathway
if seq_1 == -1:
continue
atom_seq_1, type_seq_1, other_id, chain_length = self.track_atoms(sequence_atoms=seq_1, init_atom=init_pick)
#print "atom seq 1 (before): ", atom_seq_1
if not atom_seq_1:
atom_seq_1, type_seq_1, _, chain_length = self.track_atoms(sequence_atoms=seq_1, init_atom=init_pick,
other_id=other_id)
#print "atom seq 1 (after): ", atom_seq_1
if atom_seq_1 == -1 or len(atom_seq_1) < 1:
break
end_type_old = end_type
end_atom, end_type = atom_seq_1[-1], type_seq_1[-1]
###add this line to keep track of paths
thread_list, og_list = thread_check(new_T=atom_seq_1, T_list=thread_list)
print "thread list 1: ", thread_list
#og_list -> the orignal lst before concatenation
#appending for X2_list
node_list.append([init_pick, end_atom])
node_type_list.append([end_type_old, end_type])
node_seq.append(atom_seq_1)
node_seq_type.append(type_seq_1)
if len(node_list) and node_list not in X2_list> 0:
X2_list.append(node_list)
##check if the list is empty. if it is empty, it indicates that the
#print "next atoms 1: ", next_atoms
if len(next_atoms) > 0:
next_atoms = next_atoms + [end_atom]
next_atom_types = next_atom_types + [next_atom_types]
else:
next_atoms = [end_atom]
next_atom_types = [end_type]
#print "nxt atoms 2: ", next_atoms
#print "next atom types 2: ", next_atom_types
#print "atom seq 1: ", atom_seq_1
if num_pathway == 2:
#if we're branching off due to a tertiary amine, let's fetch off the taken_C
#taken_C = atom_seq_1[1]
type_check = type_seq_1[1]
if type_check == 14:
taken_C = atom_seq_1[1]
atom_seq_2, type_seq_2, _, chain_length = self.track_atoms(sequence_atoms=seq_2,
init_atom=init_pick, taken_C=taken_C)
else:
taken_C = find_taken_C(init_atom=init_pick, node_seq_list=node_seq, type_seq_1=type_seq_1)
print "taken C: ", taken_C
if taken_C == -1:
atom_seq_2, type_seq_2, _, chain_length = self.track_atoms(sequence_atoms=seq_2,
init_atom=init_pick)
else:
atom_seq_2, type_seq_2, _, chain_length = self.track_atoms(sequence_atoms=seq_2,
init_atom=init_pick, taken_C=taken_C)
thread_list, _ = thread_check(new_T=atom_seq_2, T_list=thread_list, og_list=og_list,
create_thread=True)
end_atom_2, end_type_2 = atom_seq_2[-1], type_seq_2[-1]
next_atoms = next_atoms + [end_atom_2]
next_atom_types = next_atom_types + [end_type_2]
#print "atom_Seq_2: ", atom_seq_2
print "thread list 2: ", thread_list
node_list.append([init_pick, end_atom_2])
node_type_list.append([end_type_old, end_type_2])
node_seq.append(atom_seq_2)
node_seq_type.append(type_seq_2)
#if len(node_list_2) and node_list_2 not in X2_list> 0:
#X2_list.append(node_list_2)
print "nxt atoms 3: ", next_atoms
print "next atom types 3: ", next_atom_types
print "node seq: ", node_seq
print "node seq type: ", node_seq_type
#load_resin_molecule(self, fun_bool, fun_type, end_type)
stag_list.append(atom_seq_1)
###append to all_list:
try:
all_list = all_list + atom_seq_1
all_type = all_type + type_seq_1
except:
all_list = atom_seq_1
all_type = type_seq_1
if num_pathway == 2:
all_list = all_list + atom_seq_2
all_type = all_type + type_seq_2
print "atom seq 1: ", atom_seq_1
print "type seq 1: ", type_seq_1
print "atom seq 2: ", atom_seq_2
print "type seq 2: ", type_seq_2
print "next atoms: ", next_atoms
print "next atom types: ", next_atom_types
print "fun id: ", fun_id
print "node list: ", node_list
print "node type list: ", node_type_list
print "node seq: ", node_seq
print "node seq type: ", node_seq_type
if len(X2_list) > 0: X1_list.append(X2_list)
X0_list.append(X1_list)
#print "next_atoms: ", next_atoms
#print "next_atom_types: ", next_atom_types
print "all_list: ", np.unique(all_list).tolist()
#print "all type: ", all_type
print "all length ", len(np.unique(all_list).tolist())
#print "fun id: ", self.id_fun
#print "X0 list: ", X0_list
return None
def detect_fun_atoms(self, type_choose=22, thresh=1.8, d_node=10.0):
df_xyz, df_bond = self.read_bonds() #df_xyz -> coordinate system, df_bond -> topology of bonds
header_list = ['id', 'molecule-id', 'type', 'x', 'y', 'z', 'nx', 'ny', 'nz']
df_xyz.columns = header_list
header_list = ['bond_id', 'bond_type', 'particle_1', 'particle_2']
df_bond.columns = header_list
df_cnt = df_xyz.loc[(df_xyz['type'] == type_choose)]
df_other = df_xyz.loc[~(df_xyz['type'] == type_choose)]
id_cnt = df_cnt.loc[:, ['id']].as_matrix().flatten()
id_fun = df_other.loc[:, ['id']].as_matrix().flatten()
type_fun = df_other.loc[:, ['type']].as_matrix().flatten()
df_1 = df_cnt.loc[:, ['x', 'y', 'z']]
df_2 = df_other.loc[:, ['x', 'y', 'z']]
D, _, _ = compute_distance(df_1.as_matrix(), df_2.as_matrix())
d_thr = D[D < thresh]
if len(d_thr)>0:
idx = np.where(D < thresh)
id_fun = id_fun[idx[0]]
id_cnt = id_cnt[idx[1]]
type_choose = type_fun[idx[0]]
else:
id_fun = -1
id_cnt = -1
type_choose = -1
####now that we've selected the
d_node = D[D < d_node]
#print id_fun
#this block to ensure that the extracted fun atoms is unique
id_fun, unique_idx = np.unique(id_fun, return_index=True)
print id_fun
if len(id_fun) > 1:
type_choose = type_choose[unique_idx]
id_cnt = id_cnt[unique_idx]
#correct based on simulations
if id_fun[0]!=-1:
id_fun, id_cnt, type_choose = check_fun_atoms(fun_in=id_fun, cnt_in=id_cnt, df_xyz=df_xyz, type_in=type_choose)
return d_thr, id_fun, id_cnt, type_choose, df_xyz, df_bond
def read_bonds(self):
with open(self.chain_file) as f:
text = f.readlines()
count = 0
skip_count = 0
vel_count = 0
bond_count = 0
for line in text:
count += 1
if line.startswith('Atoms'):
skip_count = count
if line.startswith('Velocities'):
vel_count = count - 3
if line.startswith('Bonds'):
bond_count = count
print "troubleshoot", skip_count, vel_count, bond_count
df_xyz = pd.read_table(self.chain_file, delim_whitespace=True, header=None, skiprows=skip_count, nrows=(vel_count - skip_count))
df_bond = pd.read_table(self.chain_file, delim_whitespace=True, header=None, skiprows=bond_count)
return df_xyz, df_bond
def load_resin_molecule(self, fun_bool, fun_type, end_type, **kwargs):
##extract out kwargs
if 'epoxy_bool' in kwargs:
epoxy_bool = kwargs['epoxy_bool']
#first unpack opt_list
resin = self.classname()
#fun_bool is a boolean to indicate whether the node is a functionalized atom
#fun type -> [0, 1, 2, 3] depending upon the functioanlized atom
#if fun_bool is False and the node is internal, then we need to find the end atom to start the next chain
num_pathway = 1 ##num_pathway -> the pathways that a can follow a given end-point
seq_type = -1
seq_type_2 = -1
if fun_bool == True:
num_pathway = 1
if fun_type == 12:
seq_type = resin_sequence.resin_key
elif fun_type == 9:
seq_type = resin_sequence.pN_B
elif fun_type == 15:
num_pathway = 2
seq_type = resin_sequence.sN_B
seq_type_2 = resin_sequence.resin_key
elif fun_type == 18:
seq_type = resin_sequence.O_C
else:
seq_type = -1
else:
#based on the end type we need to select a chain molecule
if end_type == 4: #end with a benzene molecule
num_pathway = 1
seq_type = resin_sequence.B_N #link benzene to nitrogen
elif end_type == 15:
num_pathway = 1
if epoxy_bool == False:
##check if the starting atom is benzene or not
seq_type = resin_sequence.resin_key
else:
seq_type = resin_sequence.N_N
elif end_type == 16:
num_pathway = 2
if epoxy_bool == False:
seq_type = resin_sequence.resin_key
seq_type_2 = resin_sequence.resin_key
else:
seq_type = resin_sequence.N_N
seq_type_2 = resin_sequence.resin_key
elif end_type == 2: #in case of two
num_pathway =2
seq_type = resin_sequence.C_N
seq_type_2 = resin_sequence.C_rC
else:
seq_type = -1
seq_type_2 = -1
#print "seq_type: ", seq_type
#print "seq_type 2: ", seq_type_2
if seq_type != -1:
seq_to_pick = resin.sequences[seq_type]
else:
seq_to_pick = -1
if seq_type_2 != -1:
seq_to_pick2 = resin.sequences[seq_type_2]
else:
seq_to_pick2 = -1
##this function takes in the class name and outputs a sequence
#seq_type -> O_C,
return seq_to_pick, seq_to_pick2, num_pathway
def track_atoms(self, init_atom, sequence_atoms, **kwargs):
###define a function to track along the resin molecule
#track the sequence of atoms
seq_out = type_out = []
#track if there are multiple options:
for j in range(len(sequence_atoms)):
seq_to_track = sequence_atoms[j]
if ('other_id' in kwargs):
other_id_in = kwargs['other_id']
seq_tmp, type_tmp, other_id, chain_length = find_chain_particles(sequence=seq_to_track, init_id=init_atom,
df_xyz=self.df_xyz, df_bond=self.df_bond, other_id=other_id_in) #change this
elif ('taken_C' in kwargs):
taken_C = kwargs['taken_C']
seq_tmp, type_tmp, other_id, chain_length = find_chain_particles(sequence=seq_to_track,
init_id=init_atom,
df_xyz=self.df_xyz,
df_bond=self.df_bond, taken_C=taken_C) # change this
else:
seq_tmp, type_tmp, other_id, chain_length = find_chain_particles(sequence=seq_to_track, init_id=init_atom,
df_xyz=self.df_xyz,
df_bond=self.df_bond) # change this
#if the final sequence ends with -1, then we know we have the right sequence
if seq_tmp[-1] != -1:
seq_out = seq_tmp
type_out = type_tmp
break
#def traverse_chain(fun_array, resin_type==1):
print "other id: ", other_id
return seq_out, type_out, other_id, chain_length
def set_fun_feature(self, idx):
if self.feature_bool:
if self.fun_type[idx] == 12:
self.fun_mat[idx, 0] = 1
elif self.fun_type[idx] == 9 or self.fun_type[idx] == 15:
self.fun_mat[idx, 1] = 1
elif self.fun_type[idx] == 18:
self.fun_mat[idx, 2] = 1
else:
self.fun_mat[idx, :] = 0
return None
def fun_adjacency(self, max_fun=5, theta_bool=True, z_bool=True):
num_fun = len(self.id_fun) #total number of functionalized atoms
#need the coordinates of all the
if z_bool == True:
A_z = np.zeros((5, 5))
return None
##------
#globalfiles
def read_MD_files(list_of_files, **kwargs):
#list of files -> [XXXX.00*, YYY.00*]
#need to convert wildcard pattern to a single list
targets = np.empty((0,)) #targets only needed if forcefile is in **kwargs
total_files = []
chain_files = []
for i in range(0, len(list_of_files)):
# print list_of_files[i]
file_temp = sorted(glob.glob(list_of_files[i]), key=key_func)
total_files = total_files + file_temp
###if the forcefile is in kwargs then tie the pullout force with the given model
if ('forcefile' in kwargs):
##forcefile has the same
forcefile = kwargs['forcefile']
raw_y = get_raw_targets(forcefile[i])
target_conf = configure_targets(file_list=file_temp, targets=raw_y)
targets = np.append(targets, target_conf)
if ('chainfile' in kwargs):
chainfile = kwargs['chainfile']
file_new = sorted(glob.glob(chainfile[i]), key=key_func)
chain_files = chain_files + file_new
return total_files, targets, chain_files
##-----utility fun--------
def key_func(x):
nondigits= re.compile("\D")
return int(nondigits.sub("", x))
#def split
def get_raw_targets(filename):
df = pd.read_csv(filename, sep=',', header=None)
y_out = df.as_matrix()[:, 1]
return y_out
###--------------
def configure_targets(file_list, targets):
#need to tally the file_list with the targets
target_final = np.zeros((len(file_list), ))
id_f = np.zeros_like(target_final)
for i in range(0, len(file_list)):
file_num = (file_list[i]).rsplit('.')[-1]
id_f[i] = int(file_num)
id_f = id_f.astype(int)
target_final = targets[id_f]
# print "targets: ", target_final[32]
return target_final
def compute_distance(cnt_array, cnt_other_array):
dist_list = []
sort_dist_list = []
idx_list = []
N = 5
points = cnt_array
for j in range(0, len(cnt_other_array)):
single_point = cnt_other_array[j, :]
dist = np.sum(((points - single_point)**2), axis=1)
dist = np.sqrt(dist)
sort_dist = np.sort(dist)
sort_dist = sort_dist[:N]
idx_sort = np.argsort(dist)
idx_sort = idx_sort[:N]
dist_list.append(dist)
sort_dist_list.append(sort_dist)
idx_list.append(idx_sort)
return np.asarray(dist_list), sort_dist_list, idx_list
def find_chain_particles(sequence, init_id, df_xyz, df_bond, symmetry_bool = False, **kwargs):
id_mat = df_xyz.loc[:, ['id']].as_matrix()
type_mat = df_xyz.loc[:, ['type']].as_matrix()
bond_mat = df_bond.loc[:, ['particle_1', 'particle_2'] ].as_matrix()
#This function takes in two argiments: sequence -> list of types for the sequence
#and init_id -> the particle id of the first particle
#Symmetry_bool -> indicates wether
chain_id = [init_id]
chain_type = [sequence[0]]
count = 1
other_id = -1
#Init troubleshoot
#idx_p = np.where(id_mat == 4131)
#type_p = type_mat[idx_p[0]].flatten()
#print "type p: ", type_p
chain_length = 0
#print "troubleshoot: ", init_id
for type_choose in sequence[1:]:
#type -> the type of atom
#find where the atom sequence matches
idx_all = np.where(bond_mat == init_id) #find all bonding where init_id is involved
#bond_choose
#extract out the rows
bond_select = bond_mat[idx_all[0]]
other_particle = np.sort(bond_select[bond_select!=init_id]) #find the other bonded particle
# print ""
#print "bond_select: ", bond_select
#print "other_particle: ", other_particle
#print "id_mat: ", id_mat
#Indexing out the "other particle"
idx_tmp = index_mat(id_mat.flatten(), other_particle.flatten())
#idx_tmp = np.where(id_mat == other_particle)
#print "idx_temp troubleshoot: ", idx_tmp
#type_bond = type_mat[(idx_tmp[0])].flatten() ####this is the confusing part
type_bond = type_mat[idx_tmp].flatten()
#print "type_bond: ", type_bond
#print "type bond 2: ", type_bond_second
idx_tmp = np.where(type_bond == type_choose)
#print "idx_temp: ", idx_tmp
#print "count: ", count
###this block of code ensures that when tracking benezene atoms
if len(idx_tmp[0]) > 1 and count < len(sequence) and type_choose == 4:
#print "benzene block: "
particle_tmp = other_particle[idx_tmp]
#print "particle tmp: ", particle_tmp
idx_to_del = np.where(np.isin(particle_tmp, np.asarray(chain_id)))
particle_tmp = np.delete(particle_tmp, idx_to_del)
#print "particle tmp: ", particle_tmp
#new_atom_id = particle_tmp[0] ### cehck this for benzene
##testing out the code for benzene
#if
if symmetry_bool == False and len(particle_tmp) > 1:
new_atom_id, other_id = detect_particle_benzene(df_xyz=df_xyz, particle_1=particle_tmp[0],
particle_2=particle_tmp[1])
###conisdering the case where we know that our previous work hasn;t worked and we need to try
if ('other_id' in kwargs):
other_id_in = kwargs['other_id']
#print other_id_in, other_id
if other_id == other_id_in:
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
new_atom_id = other_id_in
#print "other block: "
#print other_id_in
#print new_atom_id
else:
new_atom_id = particle_tmp[0]
#once the previous a
elif len(idx_tmp[0]) > 0:
new_atom_id = other_particle[idx_tmp][0]
if type_choose == 14 and 'taken_C' in kwargs:
taken_C = kwargs['taken_C']
#print "other_particle: ", other_particle[idx_tmp]
#print "taken_C: ", taken_C
chosen_particle = other_particle[idx_tmp]
new_atom_id = chosen_particle[chosen_particle != taken_C][0]
#print "new atom id: ", new_atom_id
else:
new_atom_id = -1
#renew init_id
chain_id.append(new_atom_id)
chain_type.append(type_choose)
if new_atom_id == -1:
break
else:
###find the bond distance
#print "computing bond distance: "
df_1 = df_xyz.loc[df_xyz['id']==init_id]
f_1 = df_1.loc[:, ['x', 'y', 'z']].as_matrix()
df_2 = df_xyz.loc[df_xyz['id'] == new_atom_id]
f_2 = df_2.loc[:, ['x', 'y', 'z']].as_matrix()
d_1, _, _ = compute_distance(f_1, f_2)
chain_length += d_1
#print d_1
init_id = new_atom_id
count += 1
#indx out from the main id matrix where new_atom id
idx_tmp2 = np.where(id_mat == new_atom_id)
#print "check: ", type_mat[idx_tmp2]