File size: 23,335 Bytes
9f39a5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
.. codeauthor:: Zander Harteveld <zandermilanh@gmail.com>

.. affiliation::
    Laboratory of Protein Design and Immunoengineering <lpdi.epfl.ch>
    Bruno Correia <bruno.correia@epfl.ch>

.. function:: builders
"""

# Standard Libraries
import re
import copy
import itertools
from pdb import set_trace

# External Libraries
import numpy as np
import pandas as pd
import networkx as nx
from transforms3d.euler import euler2mat, mat2euler

# This Library
from .virtual.VirtualStructure import VirtualStructure
from .virtual.VirtualBeta import VirtualBeta
from .virtual.VirtualHelix import VirtualHelix
from .form.FakeForm import FakeForm
from .form.Form import Form, Constraint, ConstraintSet, Loops
from .form.SecondaryStructure import SecondaryStructure
#from topogocurvy.utils import rotation_matrix_from_vectors, superimpose


__all__ = [
    'build_forms', 'prepare_forms',
    'build_generic',
    'build_similar', 'build_flat'
    ]



def build_forms(form, check_forms=False, connectivity=None, links=None, link_distance_add=2., verbose=0):
    """
    Build sample and build from forms.
    """
    _DEF_Z_DISTANCE = 11.
    _DEF_X_DISTANCE = {"H": 11.,
                       "E": 5.}

    _LINK_DISTANCE  = (np.sqrt(2 * (_DEF_Z_DISTANCE * _DEF_Z_DISTANCE))) + link_distance_add

    _CONNECTIVITY   = [c[:3] for c in connectivity.split('.')] if connectivity else None # topology string
    _LINKER_LENGTH  = [int(l) if l != 'x' else l for l in links.split('.')] if links else None # ['x', 8, 8, 4, 'x', 2] -- x for unknown length

    data = _prepare_form_string(form, connectivity=_CONNECTIVITY, links=_LINKER_LENGTH)
    layer_sets = []
    for lyr in sorted(data["layers"]):
        layer_sets.append( data["layers"][lyr] )

    layers = []
    shapes = []
    for x in range(len(layer_sets)):
        layers.append([])
        width = 0
        for y in range(len(layer_sets[x])):
            ss   = layer_sets[x][y]
            if "shift_y" not in ss: ss["shift_y"] = 0.0
            if "shift_z" not in ss: ss["shift_z"] = _DEF_Z_DISTANCE * x
            else: ss["shift_z"] += (_DEF_Z_DISTANCE * x)
            if "shift_x" not in ss:
                xdist = _DEF_X_DISTANCE["E"] if ss["type"] == "E" else _DEF_X_DISTANCE["H"]
                xdist = 0.0 if y == 0 else xdist
                ss["shift_x"] = width + xdist
            else:
                ss["shift_x"] += width
            if "tilt_x" not in ss: ss["tilt_x"] = 0.0
            if "tilt_y" not in ss: ss["tilt_y"] = 0.0
            if "tilt_z" not in ss: ss["tilt_z"] = 0.0
            if "length" not in ss:
                if ss["type"] == "H": ss["length"] = 10
                else: ss["length"] = 5
            secstr = SecondaryStructure(ss)
            vs = _prepare(ss)
            #if secstr.ref is not None:
            #    ref = None
            #    for mtf in data["motifs"]:
            #        if mtf["id"] == secstr.get_ref_motif():
            #            for z in mtf["segments"]:
            #                if z["id"] == secstr.get_ref_segment():
            #                    ref = z
            #                    break
            #    vs.atoms = ref["coordinates"]
            #else:
            #    vs.tilt_y_degrees(ss["tilt_y"])
            #    vs.tilt_degrees(ss["tilt_x"], 0, ss["tilt_z"])
            vs.tilt_y_degrees(ss["tilt_y"])
            vs.tilt_degrees(ss["tilt_x"], 0, ss["tilt_z"])
            #xdist = _DEF_X_DISTANCE["E"] if vs.get_type() == "E" else _DEF_X_DISTANCE["H"]
            vs.shift(ss["shift_x"], ss["shift_y"], ss["shift_z"])
            width = vs.centre[0]
            secstr.add_structure(vs)
            layers[-1].append(secstr)
            shapes.append(vs)

    if _CONNECTIVITY:
        if verbose == 0: print("Building connectivity {}".format(_CONNECTIVITY))
        data.setdefault("forms", [])
        okforms = []
        forms = _create_forms_by_specification(layers, _LINK_DISTANCE, _CONNECTIVITY, verbose)
        if verbose == 0: print("forms created:", str(len(forms)))
    else:
        forms = _create_forms(layers, _LINK_DISTANCE, _CONNECTIVITY, verbose)
        if verbose == 0: print("forms created:", str(len(forms)))

    # GRAPHIC REPRESENTATIONS
    #vs = VisualForms(okforms if options.hurry else forms)
    #vs.make_svg(data)

    okforms = []
    for _, f in enumerate(forms):
        f.evaluate()
        if check_forms is True:
            if f.do: okforms.append(f)
        else: okforms.append(f)
        if _ > 0 and _ % 100 == 0 and verbose == 0:
            print("{0} out of {1} evaluated ({2} ok)".format(_, len(forms), len(okforms)))
    if verbose == 0: print("{0} evaluated ({1} ok)".format(len(forms), len(okforms)))
    data['forms']  = okforms
    return data


def prepare_forms(data, directions=None, two_way=True, action='memory'):
    """
    Creates the Forms from the container.
    Only use the two_way=False if your topology is symmetric, i.e. if the lengths of the
    secondary structure elements are the same.
    """
    sizes = {}
    for lyr in sorted(data["layers"].keys()):
        for i, sse in enumerate(data['layers'][lyr]):
            if i == 0:
                size = 0
                continue
            if sse['type'] == 'E':
                size += 5.
            else:
                size += 11.
        sizes[lyr] = size

    shifts = {}
    for i in range(len(sizes.keys()) - 1):
        a = sizes[sorted(sizes.keys())[i]]
        b = sizes[sorted(sizes.keys())[i + 1]]
        if a > b:
            shifts[sorted(sizes.keys())[i]] = 0.
            shifts[sorted(sizes.keys())[i + 1]] = (a - b) / 2
        else:
            shifts[sorted(sizes.keys())[i]] = (b - a) / 2
            shifts[sorted(sizes.keys())[i + 1]] = 0.
    
    structures = {}
    for l in sorted(data["layers"]):
        for xi, x in enumerate(data["layers"][l]):
            structures[x["id"]] = _prepare(x)
            structures[x["id"]].name = x["id"]
            structures[x["id"]].remove_movement_memory()
            structures[x["id"]].create_val_sequence()
            structures[x["id"]].tilt_y_degrees(x["tilt_y"])
            structures[x["id"]].tilt_degrees(x["tilt_x"], 0, x["tilt_z"])
            
            #if xi == 0.:
            xshift = shifts[l]
            structures[x["id"]].shift(x["shift_x"] + xshift, x["shift_y"], x["shift_z"])
            
            #else:
            #    structures[x["id"]].shift(x["shift_x"], x["shift_y"], x["shift_z"])

    sketches = {
        'description': [],
        'direction': [],
        'naive': [],
    }
    custom_directions = False
    # forward + direction
    for x in data['forms']:
        #if x.do:
        if not directions:
            custom_directions = True
            directions = [0 if i%2==0 else 1 for i in range(len(x.id.split("_")))]
            print('+ directions are {}'.format(directions))
        directions = [1 if dr == 0 else 0 for dr in directions] # flip for good convention
        sslist  = []
        refsegs = {}
        for cy, y in enumerate(x.id.split("_")):
            sslist.append(copy.deepcopy(structures[y]))
            #print('self up', sslist[-1].up_is_1())
            #if sslist[-1].up_is_1() != x.turn[cy]:
            if directions[cy] == 1:
                sslist[-1].invert_direction()
            #if sslist[-1].up_is_1() != x.turn[cy]:
            #if sslist[-1].ref is not None:
            #    refsegs[sslist[-1].ref] = sslist[-1].atoms
        if "links" not in data.keys(): #data["config"]["l_linkers"] = None
            f = Form(x.id, sslist, None)
        else:
            f = Form(x.id, sslist, data["links"])
        f.prepare_coords()
        #order = []
        #for mtf in data["motifs"]:
        #    for sgm in mtf["segments"]:
        #        order.append(mtf["id"] + "." + sgm["id"])
        #f.set_order(order)
        #f.make_loops()
        #f.make_constraints()

        if action == 'write':
            wdir = os.path.join('form_sketch', x.id)
            os.makedirs(wdir, exist_ok=True)
            seqF, ssF, pdbF, loopF, cnstF = _name_files(wdir)
            #with open(seqF, "w") as fd: fd.write(f.to_sequence())
            #with open(ssF, "w") as fd: fd.write(f.to_psipred_ss())
            with open(pdbF, "w") as fd: fd.write(f.to_pdb())
            #with open(loopF, "w") as fd: fd.write(str(f.loops))
            #with open(cnstF, "w") as fd: fd.write(str(f.const))
        frame = f.to_frame()
        sketches['description'].append( '.'.join([str(s) for s in x.sslist]) )
        sketches['direction'].append( '+' )
        sketches['naive'].append(  f.to_frame() )
        forward_directions = directions
    
    if two_way == True:
        # forward - direction
        for x in data['forms']:
            sslist = []
            refsegs = {}
            for cy, y in enumerate(x.id.split("_")):
                if custom_directions == True:
                    directions = [1 if i%2==0 else 0 for i in range(len(x.id.split("_")))]
                    print('- directions are {}'.format(directions))
                    custom_directions = False
                directions = [1 if dr == 0 else 0 for dr in directions]
                sslist.append(copy.deepcopy(structures[y]))
                if directions[cy] == 1:
                    sslist[-1].invert_direction()

            if "links" not in data.keys():
                f = Form(x.id, sslist, None)
            else:
                f = Form(x.id, sslist, data["links"])
            f.prepare_coords()
            #order = []
            #f.set_order(order)

            if action == 'write':
                wdir = os.path.join('form_sketch', x.id)
                os.makedirs(wdir, exist_ok=True)
                seqF, ssF, pdbF, loopF, cnstF = _name_files(wdir)
                with open(pdbF, "w") as fd: fd.write(f.to_pdb())

            sketches['description'].append( '.'.join([str(s) for s in x.sslist]) )
            sketches['direction'].append( '-' )
            sketches['naive'].append(  f.to_frame() )

            # reset directions
            #if forward_directions[-1] == 1: # we have to turn once
            #    for x in data['forms']:
            #        sslist = []
            #        refsegs = {}
            #        for cy, y in enumerate(x.id.split("_")):
            #            if custom_directions == True:
            #                directions = [1 if i%2==0 else 0 for i in range(len(x.id.split("_")))]
            #            sslist.append(copy.deepcopy(structures[y]))
            #            if directions[cy] == 1:
            #                sslist[-1].invert_direction()
            #        break
    return pd.DataFrame( sketches )


def _name_files(wdir):
    return (os.path.join(wdir, "sequence.fa"),
            os.path.join(wdir, "structure.ss2"),
            os.path.join(wdir, "sketch.pdb"),
            os.path.join(wdir, "design.loops"),
            os.path.join(wdir, "constraints.cst"))


def build_generic(topology, shift_noise = None, tilt_noise = None):
    """
    This is a mini-topobuilder - solely essentials to build sketches. Max 4 layers.

    :param string topology:   Topology string in sequence defining layer, element, ss type, direction, length.
    :param float shift_noise: List of dicts with noise values per ss element to translate.
    :param float tilt_noise:  List of dicts with noise values per ss element to tilt

    :return: :class:`~pandas.DataFrame` - a sketch.
    """
     # Global defaults
    _DEF_Z_DISTANCE = {"A": 0., "B": 11., "C": 22., "D": 33.}
    _DEF_X_DISTANCE = {"H": 11., "E": 5.}
    _CONNECTIVITY   = None
    _LINKER_LENGTH  = None

    # Local defaults
    start_atomcount = 0
    start_residuecount = 0

    # Collections
    collection = []

    # Precalculations
    tpg = topology.split('.')

    info = {}
    for lyr in list("ABCD"):
        size = [_DEF_X_DISTANCE[n[2]] * (int(n[1]) - 1) for n in tpg if n[0] == lyr]
        if size != []:
            info[lyr] = max( size )

    for tpgi in tpg:
        tpg_layer  = tpgi[0]
        tpg_number = int( tpgi[1] ) - 1
        tpg_type   = tpgi[2]
        tpg_direc  = tpgi[3]
        tpg_length = int( tpgi[4:] )

        # build
        if tpg_type == "E":
            vs = VirtualBeta(tpg_length, [0., 0., 0.],
                             start_atomcount=start_atomcount,
                             start_residuecount=start_residuecount,
                             chain = 'A')
        if tpg_type == "H":
            vs = VirtualHelix(tpg_length, [0., 0., 0.],
                              start_atomcount=start_atomcount,
                              start_residuecount=start_residuecount,
                              chain = 'A')

        start_atomcount    += (tpg_length * 4)
        start_residuecount += (tpg_length * 1)

        try:
            if info["A"] != info[tpg_layer] and tpg_layer != "A":
                recentering = (info["A"] / 2.) - (info[tpg_layer] / 2.)
            else:
                recentering = 0.
        except:
            recentering = 0.

        try:
            if tpg_layer == 'A':
                centering = 0.
            else:
                center = np.mean(info['A'])
                centering = (center + recentering) - np.mean(info[tpg_layer])
        except:
            centering = 0.

        # Shift
        vs.shift(x=_DEF_X_DISTANCE[tpg_type]*tpg_number + recentering,
                 y=0.,
                 z=_DEF_Z_DISTANCE[tpg_layer])

        # Invert
        if tpg_direc == '-':
            vs.invert_direction()

        # Add noise
        if shift_noise:
            vs.shift(x=float(np.random.randint(-1,1)), y=float(np.random.randint(-7,7)), z=0.)

        if tilt_noise:
            vs.tilt_degrees(x_angle=float(np.random.randint(-20,20)), y_angle=0., z_angle=0.)

        vs = vs.get_frame()
        vs = vs.assign(layer=[tpg_layer]*len(vs), element=[tpg_number]*len(vs))
        collection.append(vs)

    collection = pd.concat( collection )

    # centering by layers
    #if len(collection[collection.layer == 'A'] > len(collection[collection.layer == 'B']:
    #if not collection[collection.layer == 'B'].empty:
        #center1 = collection[collection.layer == 'A'][['x']].mean().values
        #center2 = collection[collection.layer == 'B'][['x']].mean().values
        #diff = center1 - center2
        #collection[collection.layer == 'B'].loc[:, 'x'] -= diff[0]
        #collection[collection.layer == 'B'].loc[:, 'y'] += diff[1]
        #collection[collection.layer == 'B'].loc[:, 'z'] += diff[2]

    #return pd.concat( collection )
    return collection


def build_similar(pdbframe, shift_noise = None, tilt_noise = None):
    """
    This is a mini-topobuilder - solely essentials to build sketches. Max 4 layers.

    :param string topology:   Topology string in sequence defining layer, element, ss type, direction, length.
    :param float shift_noise: List of dicts with noise values per ss element to translate.
    :param float tilt_noise:  List of dicts with noise values per ss element to tilt

    :return: :class:`~pandas.DataFrame` - a sketch.
    """
    # Local defaults
    start_atomcount = 0
    start_residuecount = 0

    # Collections
    collection = []

    # Precalculations
    pdb_ca = pdbframe[pdbframe.atomtype == 'CA']
    dssp_str = ''.join(pdb_ca.dssp3.values)
    combs = ["".join(g) for k, g in itertools.groupby(dssp_str)]
    combs = [[c[0], len(c)] for c in combs]

    prev, post = 0, 0
    for i, comb in enumerate(combs):
        post += comb[1]
        piece = pdb_ca.iloc[prev:post]
        prev += comb[1]

        tpg_type   = comb[0]
        tpg_length = int( comb[1] )

        # build
        if tpg_type == "E":
            vs = VirtualBeta(tpg_length, [0., 0., 0.],
                             start_atomcount=start_atomcount,
                             start_residuecount=start_residuecount,
                             chain = 'A')
        elif tpg_type == "H":
            vs = VirtualHelix(tpg_length, [0., 0., 0.],
                              start_atomcount=start_atomcount,
                              start_residuecount=start_residuecount,
                              chain = 'A')
        else:
            continue

        start_atomcount    += (tpg_length * 4)
        start_residuecount += (tpg_length * 1)


        vs = vs.get_frame()
        vs, rms = superimpose(piece, vs, ca_only=True)

        # Add noise
        if tilt_noise:
            xyz = vs[['x', 'y', 'z']].values
            vs = vs.drop(columns=['x', 'y', 'z'])

            Rx = euler2mat(float(np.random.randint(-5,5)), 0, 0, "sxyz")
            Ry = euler2mat(0, float(np.random.randint(-5,5)), 0, "sxyz")
            Rz = euler2mat(0, 0, float(np.random.randint(-5,5)), "sxyz")
            R  = np.dot(Rz, np.dot(Rx, Ry))

            xyz_cen = xyz - np.mean(xyz, axis=0)
            xyz_rot = xyz_cen.dot(R)
            xyz_new = xyz_rot + np.mean(xyz, axis=0)

            vs = pd.concat([vs, pd.DataFrame(xyz_new, columns=['x', 'y', 'z'])], axis=1)

        # Add noise
        #if shift_noise:
        #    vs['x'] += float(np.random.randint(-2,2))
        #    vs['y'] += float(np.random.randint(-2,2))
        #    vs['z'] += float(np.random.randint(-2,2))

        #if tilt_noise:
        #    vs.tilt_degrees(x_angle=float(np.random.randint(-20,20)), y_angle=0., z_angle=0.)

        collection.append(vs)

    collection = pd.concat( collection )
    return collection


def build_flat(pdbframe, shift_noise = None, tilt_noise = None):
    """
    This is a mini-topobuilder - solely essentials to build sketches. Max 4 layers.

    :param string topology:   Topology string in sequence defining layer, element, ss type, direction, length.
    :param float shift_noise: List of dicts with noise values per ss element to translate.
    :param float tilt_noise:  List of dicts with noise values per ss element to tilt

    :return: :class:`~pandas.DataFrame` - a sketch.
    """
    # Local defaults
    start_atomcount = 0
    start_residuecount = 0

    # Collections
    collection = []

    # Precalculations
    pdb_ca = pdbframe[pdbframe.atomtype == 'CA']
    dssp_str = ''.join(pdb_ca.dssp3.values)
    combs = [''.join(g) for k, g in itertools.groupby(dssp_str)]
    combs = [[c[0], len(c)] for c in combs]

    prev, post = 0, 0
    for i, comb in enumerate(combs):
        post += comb[1]
        if i == 0:
            piece_first = pdb_ca.iloc[prev:post]
        piece = pdb_ca.iloc[prev:post]
        com_piece = piece[['x', 'y', 'z']].mean().values
        prev += comb[1]

        tpg_type   = comb[0]
        tpg_length = int( comb[1] )

        # build
        if tpg_type == "E":
            vs = VirtualBeta(tpg_length, [0., 0., 0.],
                             start_atomcount=start_atomcount,
                             start_residuecount=start_residuecount,
                             chain = 'A')
        elif tpg_type == "H":
            vs = VirtualHelix(tpg_length, [0., 0., 0.],
                              start_atomcount=start_atomcount,
                              start_residuecount=start_residuecount,
                              chain = 'A')
        else:
            continue

        start_atomcount    += (tpg_length * 4)
        start_residuecount += (tpg_length * 1)

        vs = vs.get_frame()
        vs, rms = superimpose(piece_first, vs, ca_only=True)

        # Add noise
        #if shift_noise:
        #    vs.shift(x=float(np.random.randint(-1,1)), y=float(np.random.randint(-7,7)), z=0.)

        #if tilt_noise:
        #    vs.tilt_degrees(x_angle=float(np.random.randint(-20,20)), y_angle=0., z_angle=0.)

        collection.append(vs)

    collection = pd.concat( collection )
    return collection


def _prepare(sse):
    """
    """
    if sse["type"] == "E":
        vs = VirtualBeta(sse["length"], [0., 0., 0.])
    if sse["type"] == "H":
        vs = VirtualHelix(sse["length"], [0., 0., 0.])
    return vs


def _prepare_form_string(form, connectivity=None, links=None):
    formstr = form.split('.')

    data = {'layers': {}}
    layers = set([l[0] for l in formstr])

    for lr in layers:
        data['layers'][lr] = []
        for sse in formstr:
            if sse.startswith(lr):
                dl = {}
                dl['id']   = sse
                dl['type'] = sse[2]

                if len(sse) > 3:
                    if '+' in sse or '-' in sse:
                         dl['length'] = int(sse.replace('-', '+').split('+')[-1])
                    else:
                         dl['length'] = int(sse[3:])
                else:
                    if sse[2] == 'H': dl['length'] = 10
                    else: dl['length'] = 5

                data['layers'][lr].append(dl)
    if connectivity:
        data['connectivity'] = connectivity
    if links:
        data['links'] = links
    return data


def _create_graph( layers, distance, connectivity ):
    G = nx.Graph()
    for lyr1 in range(len(layers)):
        for lyr2 in range(len(layers)):
            if abs(lyr1 - lyr2) <= 1:  # Only consecutive layers
                for col1 in range(len(layers[lyr1])):
                    for col2 in range(len(layers[lyr2])):
                        if abs(col1 - col2) <= 1:  # Only consecutive columns
                            G.add_edge(layers[lyr1][col1],
                                       layers[lyr2][col2], object=SecondaryStructure)
    for lyr1 in layers:
        for lyr2 in layers:
            for sse1 in lyr1:
                for sse2 in lyr2:
                    if sse1 != sse2 and sse1.twoD_distance(sse2) <= distance:
                        G.add_edge(sse1, sse2)

    return G


def _search_paths(G, n1, n2, verbose):
    forms = []
    for path in nx.all_simple_paths(G, n1, n2):
        if len(path) == nx.number_of_nodes(G):
            f = FakeForm(copy.deepcopy(path))
            forms.append(f)
            path.reverse()
            f = FakeForm(copy.deepcopy(path))
            forms.append(f)
    if verbose == 0: print(n1.desc, "->", n2.desc, "  ", len(forms), "folds obtained")
    return forms


def _create_forms( layers, distance, connectivity, verbose ):
    G = _create_graph(layers, distance, connectivity)
    forms = []

    for _1, n1 in enumerate(G.nodes()):
        for _2, n2 in enumerate(G.nodes()):
            if _1 < _2:
                forms.extend(_search_paths(G, n1, n2, verbose))
    return forms


def _create_forms_by_specification( layers, distance, connectivity, verbose ):
    connect = {}
    for i,lyr in enumerate( layers ):
        l = map(str, lyr)
        for j,sse in enumerate(l):
            insertion_ind = connectivity.index(sse[:3])
            connect[insertion_ind] = [i, j]
    #connect = list(connect.values())
    connect = [connect[k] for k in sorted(connect)]

    path = []
    for i in range(len(connect)):
        lyr1, col1 = connect[i][0], connect[i][1]
        path.append(layers[lyr1][col1])
    forms = []
    if len(connectivity)==2:
        n1 = path[0]
        n2 = path[-1]
        forms.extend(_search_paths(G, n1, n2, verbose))
    else:
        f = FakeForm(copy.deepcopy(path))
        forms.append(f)
        path.reverse()
        f = FakeForm(copy.deepcopy(path))
        forms.append(f)
    return forms