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
#[macro_use]extern crate serde_derive;
extern crate serde;

mod themes;
use finiteelement::geometry::{Point};
use finiteelement::Float;
use std::collections::{HashMap, HashSet};
use std::f64::consts::PI;
use themes::themes;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Points<F: Float> {
    pub steps: Vec<Vec<Point<F>>>,
    pub strands: Vec<Strand>,
    pub pairs: Vec<Option<usize>>,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Strand {
    pub strand: Vec<usize>,
    pub color: u32,
}

pub const LEN_STACK: f64 = 5. / 100.;
pub const LEN_H_BOUND: f64 = 10. / 100.;
pub const ANGLE_AOB: f64 = 11. * PI / 9.;
pub const ANGLE_AOC2: f64 = 2. * PI / 10.5;
pub const CADNANO_HELIX_SIZE: f64 = LEN_H_BOUND * 2.2;
pub const AUTO_COLOR: u32 = 0xABCDABCD;

pub const M13_7249: &'static str = include_str!("m13");
pub const M13_594: &'static str = include_str!("m13_594");

#[derive(Copy, Clone)]
pub struct DNAConst {
    len_stack: f64,
    len_h_bound: f64,
    nb_base_per_turn: f64,
    angle_groove: f64,
}

impl DNAConst {
    pub fn default() -> Self {
        DNAConst {
            len_stack: LEN_STACK,
            len_h_bound: LEN_H_BOUND,
            nb_base_per_turn: 10.5,
            angle_groove: ANGLE_AOB
        }
    }

    pub fn angle_aob(&self) -> f64 {
        self.angle_groove
    }

    pub fn angle_aoc2(&self) -> f64 {
        2. * PI / self.nb_base_per_turn
    }

    pub fn set_bpp(&mut self, bpp: f64) {
        self.nb_base_per_turn = bpp
    }

    pub fn set_groove(&mut self, groove: f64) {
        self.angle_groove = groove
    }

    fn angle_boc2(&self) -> f64 { self.angle_aob() - self.angle_aoc2() }

    fn angle_aod2(&self) -> f64 { self.angle_aob() + self.angle_aoc2() }
    pub fn ab(&self) -> f64 {
        (2. - 2. * self.angle_aob().cos()).powf(0.5) * self.len_h_bound
    }
    pub fn bc2(&self) -> f64 { 
        (2. - 2. * self.angle_boc2().cos()).powf(0.5) * self.len_h_bound
    }
    pub fn bc(&self) -> f64 {
        (self.bc2() * self.bc2() + self.len_stack * self.len_stack).powf(0.5)
    }
    pub fn ad2(&self) -> f64 {
        (2. - 2. * self.angle_aod2().cos()).powf(0.5) * self.len_h_bound
    }
    pub fn ad(&self) -> f64 {
        (self.ad2() * self.ad2() + self.len_stack * self.len_stack).powf(0.5)
    }
    pub fn ac2(&self) -> f64 {
        (2. - 2. * self.angle_aoc2().cos()).powf(0.5) * self.len_h_bound
    }
    pub fn ac(&self) -> f64 {
        (self.ac2() * self.ac2() + self.len_stack * self.len_stack).powf(0.5)
    }

}



#[derive(Serialize, Deserialize)]
pub struct Helix {
    origin: Point<f64>,

    // aka angle on x axis
    roll: f64,

    //aka angle on y axis
    yaw: f64,

    //aka angle on z axis
    pitch: f64,

}

impl Helix {
    // Angle on the circle
    fn theta(&self, n: isize, b: bool, cst: &DNAConst) -> f64 {
        let shift = if b { cst.angle_aob() } else { 0. };
        n as f64 * cst.angle_aoc2() + shift + self.roll
    }

    fn ry(point: Point<f64>, theta: f64) -> Point<f64> {
        Point {
            x: point.x * theta.cos() + point.z * theta.sin(),
            y: point.y,
            z: point.x * -theta.sin() + point.z * theta.cos(),
        }
    }

    fn rz(point: Point<f64>, theta: f64) -> Point<f64> {
        Point {
            x: point.x * theta.cos() + point.y * -theta.sin(),
            y: point.x * theta.sin() + point.y * theta.cos(),
            z: point.z,
        }
    }

    // Position in space
    pub fn space_pos(&self, n: isize, b: bool, cst: &DNAConst) -> Point<f64> {
        let theta = self.theta(n, b, cst);
        let mut ret = Point {
            x: n as f64 * cst.len_stack,
            y: -theta.cos() * cst.len_h_bound,
            z: -theta.sin() * cst.len_h_bound,
        };
        ret = Helix::ry(ret, self.yaw);
        ret = Helix::rz(ret, self.pitch);
        ret.x += self.origin.x;
        ret.y += self.origin.y;
        ret.z += self.origin.z;
        ret
    }

    pub fn from_grid(h: isize, v: isize) -> Helix {
        Helix {
            origin: Point {
                x: 0.,
                y: h as f64 * CADNANO_HELIX_SIZE,
                z: v as f64 * CADNANO_HELIX_SIZE,
            },
            roll: 0.,
            pitch: 0.,
            yaw: 0.,
        }
    }
}


pub struct RandomBases{}

impl Iterator for RandomBases {
    type Item = char;
    fn next(&mut self) -> Option<Self::Item> {
        use rand::Rng;
        let mut rng = rand::thread_rng();
        let between = rand::distributions::Uniform::from(0..4);
        Some(match rng.sample(&between) {
            0 => 'A',
            1 => 'C',
            2 => 'G',
            _ => 'T',
        })
    }
}

impl RandomBases {
    pub fn new() -> Self {
        RandomBases {}
    }
}

pub struct DefaultFilling {}

impl Iterator for DefaultFilling {
    type Item = char;
    fn next(&mut self) -> Option<Self::Item> {
        Some('*')
    }
}

#[derive(Serialize, Deserialize)]
pub struct Nucleotide {
    pub id: usize,
    pub helix_num: usize,
    pub pos_on_helix: isize,
    pub anti_sens: bool,
    pub strand: usize,
    pub pos_on_strand: usize,
    pub on_jump: bool,
    pub base: char,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Result<T> {
    pub result: T,
    pub stderr: String,
}

pub struct Nanostructure {
    pub helices: Vec<Helix>,

    //Maps (helix num, n, b) to a nucleotide identifier
    pub nucl_id: HashMap<(usize, isize, bool), usize>,

    pub nucleotides: Vec<Nucleotide>,

    //Idexed by nucleotides identifier, posisiton of each nucleotide
    pub positions: Vec<Point<f64>>,

    //The list of covalent bonds between nucleotides
    pub strands: Vec<Strand>,

    //Maps a nulc_id to a strand_id
    nucl_to_strand: HashMap<usize, usize>,

    //The list of h-bonds between nucleotides
    h_bonds: HashSet<(usize, usize)>,

    scaffold: usize,

    colors_idx: usize,

    // maps a strand identifier to the name of a group
    strand_group: Vec<String>,

    pub cst: DNAConst
}

#[derive(Serialize, Deserialize)]
pub struct JSONNanostructure {
    pub helices: Vec<Helix>,

    //Maps (helix num, n, b) to a nucleotide identifier
    pub nucl_id: Vec<((usize, isize, bool), usize)>,

    pub nucleotides: Vec<Nucleotide>,

    //Idexed by nucleotides identifier, posisiton of each nucleotide
    pub positions: Vec<Point<f64>>,

    //The list of covalent bonds between nucleotides
    pub strands: Vec<Strand>,

    //Maps a nulc_id to a strand_id
    pub nucl_to_strand: Vec<(usize, usize)>,

    //The list of h-bonds between nucleotides
    pub h_bonds: Vec<(usize, usize)>,

    pub scaffold: usize,

    pub err: String,
}

#[derive(Serialize, Deserialize)]
pub struct Res {
    pub out: String,
    pub err: String,
}

impl JSONNanostructure {
    pub fn into_nanostructure(self) -> Nanostructure {
        Nanostructure {
            helices: self.helices,
            nucl_id: self.nucl_id.into_iter().collect(),
            nucleotides: self.nucleotides,
            positions: self.positions,
            strand_group: vec!["Default".to_string(); self.strands.len()],
            strands: self.strands,
            nucl_to_strand: self.nucl_to_strand.into_iter().collect(),
            h_bonds: self.h_bonds.into_iter().collect(),
            scaffold: self.scaffold,
            colors_idx: 0,
            cst: DNAConst::default()
        }
    }
}

impl Nanostructure {
    pub fn new() -> Self {
        Nanostructure {
            helices: Vec::new(),
            nucl_id: HashMap::new(),
            nucleotides: Vec::new(),
            positions: Vec::new(),
            strands: Vec::new(),
            nucl_to_strand: HashMap::new(),
            h_bonds: HashSet::new(),
            scaffold: 0,
            colors_idx: 0,
            strand_group: Vec::new(),
            cst: DNAConst::default()
        }
    }

    pub fn with_constant(cst: DNAConst) -> Self {
        Nanostructure {
            cst,
            .. Nanostructure::new()
        }
    }


    fn into_json(self) -> JSONNanostructure {
        JSONNanostructure {
            helices: self.helices,
            nucl_id: self.nucl_id.into_iter().collect(),
            nucleotides: self.nucleotides,
            positions: self.positions,
            strands: self.strands,
            nucl_to_strand: self.nucl_to_strand.into_iter().collect(),
            h_bonds: self.h_bonds.into_iter().collect(),
            scaffold: self.scaffold,
            err: String::new(),
        }
    }

    pub fn finish(mut self) {
        self.make_h_bonds();
        serde_json::to_writer(std::io::stdout(), &self.into_json()).unwrap()
    }

    pub fn finish_into<P: AsRef<std::path::Path>>(mut self, p: P) {
        self.make_h_bonds();
        if std::env::var("REMOTE").is_ok() {
            serde_json::to_writer(std::io::stdout(), &self.into_json()).unwrap()
        } else {
            serde_json::to_writer(std::fs::File::create(p).unwrap(), &self.into_json()).unwrap()
        }
    }

    pub fn add_grid_helix(&mut self, h: isize, v: isize) -> usize {
        self.helices.push(Helix::from_grid(h, v));
        self.helices.len() - 1
    }

    pub fn add_helix(&mut self, x: f64, y: f64, z: f64, roll: f64, pitch: f64, yaw: f64) -> usize {
        self.helices.push(Helix {
            origin: Point { x, y, z },
            roll,
            pitch,
            yaw,
        });
        self.helices.len() - 1
    }

    pub fn add_nucl(
        &mut self,
        helix_id: usize,
        n: isize,
        b: bool,
        strand: usize,
        pos: usize,
    ) -> usize {
        if self.nucl_id.contains_key(&(helix_id, n, b)) {
            self.nucl_id[&(helix_id, n, b)]
        } else {
            let new_nucl = self.helices[helix_id].space_pos(n, b, &self.cst);
            let nucl_id = self.positions.len();
            self.positions.push(new_nucl);
            self.nucleotides.push(Nucleotide {
                id: nucl_id,
                helix_num: helix_id,
                pos_on_helix: n,
                anti_sens: b,
                strand,
                pos_on_strand: pos,
                on_jump: false,
                base: '*',
            });
            self.nucl_id.insert((helix_id, n, b), nucl_id);
            nucl_id
        }
    }

    pub fn add_strand(&mut self, color: u32) -> usize {
        let strand_id = self.strands.len();
        self.strands.push(Strand {
            strand: Vec::new(),
            color: color,
        });
        self.strand_group.push("Default".to_string());
        strand_id
    }

    pub fn draw_strand(
        &mut self,
        helix_id: usize,
        anti_sens: bool,
        begin: isize,
        end: isize,
        color: u32,
    ) {
        let first = if begin < end { begin } else { end };
        let last = if begin > end { begin } else { end };
        for i in first..(last + 1) {
            if self.nucl_id.contains_key(&(helix_id, i, anti_sens)) {
                return;
            }
        }

        let color_strand = if color == AUTO_COLOR {
            let t = themes("".to_string());
            let c = t[self.colors_idx % t.len()];
            self.colors_idx += 1;
            c
        } else { color };


        let strand_id = self.add_strand(color_strand);

        let (mut i, j, step) = if anti_sens {
            (last, first, -1)
        } else {
            (first, last, 1)
        };
        loop {
            let pos = if anti_sens { last - i } else { i - first };
            let c = self.add_nucl(helix_id, i, anti_sens, strand_id, pos as usize);
            self.strands[strand_id].strand.push(c);
            self.nucl_to_strand.insert(c, strand_id);
            if i == j {
                break
            }
            i += step;
        }
    }

    pub fn get_nucl(&self, helix_id: usize, nucl: isize, antisens: bool) -> usize {
        *self.nucl_id.get(&(helix_id, nucl, antisens)).unwrap()
    }

    pub fn break_strand(&mut self, nucl_id: usize) {
        unsafe {
            let nucl = self
                .nucleotides
                .as_mut_ptr()
                .offset(nucl_id as isize)
                .as_mut()
                .unwrap();
            let new_strand = self.add_strand(0);
            let old_strand = self
                .strands
                .as_mut_ptr()
                .offset(nucl.strand as isize)
                .as_mut()
                .unwrap();
            let new_5 = nucl.pos_on_strand;
            for i in 0..(old_strand.strand.len() - new_5) {
                let n = old_strand.strand[i + new_5];
                self.strands[new_strand].strand.push(n);
                self.nucleotides[n].pos_on_strand = i;
                self.nucleotides[n].strand = new_strand;
            }
            old_strand.strand.resize(new_5, 0);
        }
    }

    pub fn make_jump(&mut self, origin_id: usize, end_id: usize) {
        let new_color = {
            let t = themes("".to_string());
            let c = t[self.colors_idx % t.len()];
            self.colors_idx += 1;
            c
        };
        unsafe {
            // origin and end stay valid because self.nucleotides is never moved
            let origin = self
                .nucleotides
                .as_mut_ptr()
                .offset(origin_id as isize)
                .as_mut()
                .unwrap();
            let end = self
                .nucleotides
                .as_mut_ptr()
                .offset(end_id as isize)
                .as_mut()
                .unwrap();
            //println!("origin {:?}, end {:?}", self.strands[origin.strand], self.strands[end.strand]);

            if origin.strand == end.strand {
                if origin.pos_on_strand < end.pos_on_strand {
                    let new_strand = self.add_strand(new_color);
                    let old_strand = self
                        .strands
                        .as_mut_ptr()
                        .offset(origin.strand as isize)
                        .as_mut()
                        .unwrap();
                    let end_3 = origin.pos_on_strand.max(end.pos_on_strand);
                    let end_5 = origin.pos_on_strand.min(end.pos_on_strand);

                    for nucl_id in &old_strand.strand[end_5 + 1..end_3] {
                        self.strands[new_strand].strand.push(*nucl_id);
                        let ref mut nucl = self.nucleotides[*nucl_id];
                        nucl.strand = new_strand;
                        nucl.pos_on_strand -= end_5 + 1;
                    }

                    let diff = end_3 - end_5 - 1;
                    let last_pos = old_strand.strand.len() - diff;
                    for i in (end_5 + 1)..last_pos {
                        old_strand.strand[i] = old_strand.strand[i + diff];
                        let nucl_id = old_strand.strand[i];
                        let ref mut nucl = self.nucleotides[nucl_id];
                        nucl.pos_on_strand = i;
                    }
                    old_strand.strand.resize(last_pos, 0);
                } else {
                    panic!("ATTEMPT TO CREATE A LOOP ! THIS IS NOT ACCEPTABLE");
                    //println!("same strand");
                }
                return;
            }


            if origin.pos_on_strand < self.strands[origin.strand].strand.len() {
                // We must create a new strand with the nucleotides remaining on the 3' end

                let new_strand_3_id = self.add_strand(new_color);

                //origin_strand and end_strand remain valid, self.strands will not move
                let origin_strand = self
                    .strands
                    .as_mut_ptr()
                    .offset(origin.strand as isize)
                    .as_mut()
                    .unwrap();
                for nucl_id in &origin_strand.strand[origin.pos_on_strand + 1..] {
                    self.strands[new_strand_3_id].strand.push(*nucl_id);
                    let ref mut nucl = self.nucleotides[*nucl_id];
                    nucl.strand = new_strand_3_id;
                    nucl.pos_on_strand -= origin.pos_on_strand + 1;
                }
            }

            if end.pos_on_strand > 0 {
                // We must create a new strand with the nucleotides remaining on the 5' end
                let new_strand_5_id = self.add_strand(new_color);
                let end_strand = self
                    .strands
                    .as_mut_ptr()
                    .offset(end.strand as isize)
                    .as_mut()
                    .unwrap();
                for nucl_id in &end_strand.strand[..end.pos_on_strand] {
                    self.strands[new_strand_5_id].strand.push(*nucl_id);
                    let ref mut nucl = self.nucleotides[*nucl_id];
                    nucl.strand = new_strand_5_id;
                    //no need to modify nucl.pos_on_strand
                }
            }

            // Concatenate origin and end
            let origin_strand = self
                .strands
                .as_mut_ptr()
                .offset(origin.strand as isize)
                .as_mut()
                .unwrap();
            let end_strand = self
                .strands
                .as_mut_ptr()
                .offset(end.strand as isize)
                .as_mut()
                .unwrap();
            origin_strand.strand.resize(origin.pos_on_strand + 1, 0);
            let mut pos = origin.pos_on_strand + 1;
            for nucl_id in &end_strand.strand[end.pos_on_strand..] {
                origin_strand.strand.push(*nucl_id);
                let ref mut nucl = self.nucleotides[*nucl_id];
                nucl.strand = origin.strand;
                nucl.pos_on_strand = pos;
                pos += 1;
            }
            end_strand.strand.resize(0, 0);
            //println!("origin {:?}, end {:?}", self.strands[origin.strand], self.strands[end.strand]);
        }
        /*
        origin.on_jump = true;
        end.on_jump = true;
        */
    }

    pub fn set_scafold(&mut self, nucl_id: usize, scaffold: &str) {
        let nucl = &self.nucleotides[nucl_id];
        self.scaffold = nucl.strand;
        /*let scafold_sequence = fs::read_to_string("scafold.txt")
            .expect("Could not find scafold.txt")
            .chars()
            .collect::<Vec<char>>();*/
        let scaf = &self.strands[self.scaffold].strand;
        //let nb_skip = len_scaff - scaf.len() ;
        //println!("{}", nb_skip);
        for (&s, c) in scaf.iter().rev().zip(scaffold.chars()) {
            //println!("{} {} {} {}", self.nucleotides[s].helix_num, self.nucleotides[s].pos_on_helix, self.nucleotides[s].anti_sens, c);
            self.nucleotides[s].base = c;
        }
    }

    pub fn set_color(&mut self, nucl_id: usize, color: u32) {
        let nucl = &self.nucleotides[nucl_id];
        self.strands[nucl.strand].color = color;
    }

    pub fn auto_color(&mut self, theme: &str) {
        let colors = themes(theme.to_string());
        let mut color = 0;
        for i in 0..self.strands.len() {
            if !self.strands[i].strand.is_empty() {
                self.strands[i].color = colors[color % colors.len()];
                color += 1;
            }
        }
    }

    pub fn sequences<I: Iterator<Item = char>>(&self, mut fillings: I) -> String {
        let mut seq_vec: Vec<String> = Vec::new();
        let mut idxs: Vec<usize> = (0..self.strands.len()).collect();
        idxs.sort_by(|i, j| self.strand_group[*i].cmp(&self.strand_group[*j]));
        for i in idxs {
            if i != self.scaffold && !self.strands[i].strand.is_empty() {
                seq_vec.push(format!("{}, {}",
                                     self.strand_sequence(&self.strands[i], &mut fillings),
                                     self.strand_group[i]))
            }
        }
        //seq_vec.sort();
        seq_vec.join("\n")
    }

    pub fn make_pairs(&self) -> Vec<Option<usize>> {
        let n = self.positions.len();
        let mut ret = vec![None; n];
        for (a, b) in self.h_bonds.iter() {
            ret[*a] = Some(*b);
            ret[*b] = Some(*a);
        }
        ret
    }

    pub fn set_group(&mut self, nucl_id:usize, group: &str) {
        let strand = self.nucleotides[nucl_id].strand;
        self.strand_group[strand] = group.to_string();
    }

    /*
    pub fn first_snapshot(&self) -> Points<f64> {
        Points {
            steps: vec![self.positions.clone()],
            strands: self.strands.clone(),
            pairs: self.make_pairs()
        }
    }
    */

    pub fn make_h_bonds(&mut self) {
        for ref nucl_a in &self.nucleotides {
            if nucl_a.anti_sens || nucl_a.on_jump {
                // the h_bonds are made by the nucleotides on the sens strand
                continue;
            }
            if let Some(id_b) = self.nucl_id.get(&(nucl_a.helix_num, nucl_a.pos_on_helix, true)) {
                let ref nucl_b = self.nucleotides[*id_b];
                if !nucl_b.on_jump {
                    // Create h-bond between nucl_a and nucl_b
                    self.h_bonds.insert((nucl_a.id, nucl_b.id));
                }
            }
        }
    }

    fn strand_sequence<I: Iterator<Item = char>>(&self, strand: &Strand, mut fillings: I) -> String {
        let mut seq = String::new();
        for nucl_id in strand.strand.iter().rev() {
            let nucl = &self.nucleotides[*nucl_id];
            let letter = if let Some(compl) =
                self.nucl_id
                .get(&(nucl.helix_num, nucl.pos_on_helix, !nucl.anti_sens))
            {
                base_compl(self.nucleotides[*compl].base)
            }
            else {
                fillings.next().unwrap()
            };
            seq.push(letter);
        }
        seq
    }

    /*
    #[cfg(not(target_arch = "wasm32"))]
    pub fn export_excel(&self) {
        use simple_excel_writer::*;
        let path = std::env::current_dir().unwrap().join("plates.xlsx");
        let mut wb = Workbook::create(path.to_str().unwrap());
        let mut sheet = wb.create_sheet("Nanostructure");

        wb.write_sheet(&mut sheet, |sw| {
            sw.append_row(row!["Position", "Name", "Sequence"])?;

            let mut cell = 0;
            for i in 0..self.strands.len() {
                if i != self.scaffold && !self.strands[i].strand.is_empty() {
                    let row = (i&7) as u8;
                    let column = (i >> 3) as u8;
                    sw.append_row(row![
                        format!("{}{}", ('A' as u8+row) as char, column),
                        format!("S{}", i,),
                        self.strand_sequence(&self.strands[i], RandomBases::new())
                    ])?
                }
            }
            Ok(())
        }).unwrap();
        wb.close().expect("close excel error!");
    }
    */


}


fn base_compl(base: char) -> char {
    match base {
        'A' => 'T',
        'T' => 'A',
        'C' => 'G',
        'G' => 'C',
        '*' => '*',
        _ => {
            panic!("unexpected character");
        }
    }
}
/*
 * Typical use:
 * 1) create Helices, with an origin, orientation and roll
 * 2) create strands
 * 3) create 'section' of double strand
 */