chapmanb / synbio (http://bcbio.wordpress.com/)

Python Synthetic Biology libraries

Clone this repository (size: 8.4 MB): HTTPS / SSH
$ hg clone http://bitbucket.org/chapmanb/synbio/
commit 2: 775d9e9a5c4d
parent 1: d62271144b61
branch: default
tags: tip
Fix setup to reflect actual modules. Add in SQLAlchemy database models.
cha...@sobchak.mgh.harvard.edu
10 months ago
r2:775d9e9a5c4d 635 loc 24.0 KB embed / history / annotate / raw /
  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
"""Analysis of designed oligos for potential problems.

This modules contain classes that take simple OligoRegion-like objects (have
attributes full_seq, start, end) and analyze them for problem issues, which
need to be detected and designed around.
"""
import re
import copy

from Bio.Seq import Seq
from Bio.Data import IUPACData
from Bio.Alphabet.IUPAC import unambiguous_dna

from SynBio.Primers.Check import FiveHybFinder, ThreeHybFinder
from Oligo import _WorkOligoManager

# analysis constants
HAIRPIN = "End hairpin"

class SplitterAndAnalyzer:
    """Encompass an existing splitter with an oligo analyzer.

    This makes use of existing OligoSplitters and Analyzers by combining the
    functionality into a single class that does the splitting and
    post-analysis and adjustment.
    """
    def __init__(self, splitter, analyzer):
        self.splitter = splitter
        # create a unique copy of the analyzer -- since we may have multiple
        # splitters using a single analyzer
        # XXX This is a hack, revealing that this is probably not designed
        # right.
        self.analyzer = copy.deepcopy(analyzer)
        self.analyzer.set_oligo_min_max(self.splitter.min_size,
                self.splitter.max_size)

    def set_melt_calc(self, melt_calc):
        self.splitter.set_melt_calc(melt_calc)
        self.analyzer.set_melt_info(melt_calc, self.splitter._melt_thresh)

    def split(self, segment_name, sequence, region_start, region_end,
            index_position, num_pieces):
        """Hijack a splitter "split" function to provide post-analysis.
        """
        designed_oligos = self.splitter.split(segment_name, sequence)

        if self.analyzer:
            self.analyzer.analyze_oligos(segment_name,
                    designed_oligos, sequence)

        #for oligo in designed_oligos:
        #    print oligo
        #    print oligo.str_analyses()

        return designed_oligos

class _OligoProblems:
    """Hold problems with an oligo and provide functionality for fixing them.
    """
    def __init__(self, oligo, hairpin_issues):
        self.oligo = oligo
        self.hairpin_issues = hairpin_issues

    def __str__(self):
        out = ""
        for hairpin in self.hairpin_issues:
            out += "\t%s\n" % (str(hairpin))

        return out

    def extends_secondary_structure(self, five_primer, three_primer,
            num_nts = 2):
        """Determine if the given primers will extend oligo secondary structure.

        Using all hairpin problems, this examines whether the addition of
        either the five or three primer primer will increase existing
        secondary structure.
        """
        five_seq = five_primer.get_internal_five_region()
        three_seq = three_primer.get_internal_three_region()
        five_check = five_seq[-num_nts:]
        three_check = three_seq[:num_nts]
        assert len(five_check) == num_nts and len(three_check) == num_nts
        for hairpin in self.hairpin_issues:
            if hairpin.will_extend_five(five_check):
                return 1
            if hairpin.will_extend_three(three_check):
                return 1

        # if we got here, we don't have secondary structure issues
        return 0

class OligoRepairError(Exception):
    """Error raised when an oligo could not be repaired.

    This is raised when we cannot adjust an oligo to account for
    cross-hybridization problems
    """
    pass

class _SingleOligoRepair:
    """Try and fix issues with a single oligo.

    This expand an oligo, trying to work around issues with an oligo end by
    burying them in the oligo.
    """
    def __init__(self, min_nts, max_nts, min_gcs, max_expand, max_shrink,
            loose_flex, desperate_flex):
        self.strict_five_analyzer = FiveHybFinder(min_nts, max_nts, min_gcs)
        self.strict_three_analyzer = ThreeHybFinder(min_nts, max_nts, min_gcs)

        loose_five_analyzer = FiveHybFinder(min_nts + loose_flex,
           max_nts + loose_flex, min_gcs)
        loose_three_analyzer = ThreeHybFinder(min_nts + loose_flex,
           max_nts + loose_flex, min_gcs)

        desperate_five_analyzer = FiveHybFinder(min_nts + desperate_flex,
           max_nts + desperate_flex, min_gcs)
        desperate_three_analyzer = ThreeHybFinder(min_nts + desperate_flex,
           max_nts + desperate_flex, min_gcs)

        self.five_analyzers = [self.strict_five_analyzer, loose_five_analyzer,
                desperate_five_analyzer]
        self.three_analyzers = [self.strict_three_analyzer, loose_three_analyzer,
                desperate_three_analyzer]

        self.max_expand = max_expand
        self.max_shrink = max_shrink
        
        self.debug = False
    
    def set_oligo_min_max(self, min_size, max_size):
        """Set the range of sizes for oligos.
        """
        self.min_oligo_size = min_size
        self.max_oligo_size = max_size
    
    def check_oligo(self, name, oligo, full_sequence):
        """Check an oligo to look for cross hybridization not in the sequence.
        """
        fix_oligo = oligo.copy()
        test_oligo = fix_oligo.get_seq()
        five_hybs = self.strict_five_analyzer.check_oligo(test_oligo,
                full_sequence)
        # fix 5' issues given that we are not dealing with the 5'-most oligo:
        # we can't fix that one
        if five_hybs and not fix_oligo.at_five_end():
            if self.debug:
                print "Five"
                for hyb in five_hybs:
                    print hyb
            cur_index = 0
            while 1:
                analyzer = self.five_analyzers[cur_index]
                try:
                    fix_oligo = self._fix_five(name, fix_oligo, full_sequence,
                            analyzer)
                    break
                except OligoRepairError, msg:
                    pass

                cur_index += 1
                if cur_index >= len(self.five_analyzers):
                    raise OligoRepairError(msg)

        test_oligo = fix_oligo.get_seq()
        three_hybs = self.strict_three_analyzer.check_oligo(test_oligo,
                full_sequence)
        # fix 3' issues given that we are not dealing with the 3'-most oligo
        if three_hybs and not fix_oligo.at_three_end():
            if self.debug:
                print "Three"
                for hyb in three_hybs:
                    print hyb

            cur_index = 0
            while 1:
                analyzer = self.three_analyzers[cur_index]
                try:
                    fix_oligo = self._fix_three(name, fix_oligo, full_sequence,
                            analyzer)
                    break
                except OligoRepairError, msg:
                    pass

                cur_index += 1
                if cur_index >= len(self.three_analyzers):
                    raise OligoRepairError("3 error: %s" % msg)

        oligo.update(fix_oligo)
    
    def _fix_three(self, name, oligo, full_seq, analyzer):
        """Fix an issue with the 3' end of an oligo.

        If the 3' end of the oligo cross-reacts, try to fix the problem
        by adjusting the end of the oligo.
        """
        if self.debug:
            print "3", oligo
        hybs = []
        for expand in range(self.max_expand):
            test_oligo = oligo.copy()
            test_oligo.adjust_three(expand + 1)
            if self.debug:
                print "Expand", test_oligo
            if test_oligo.bad_size(self.min_oligo_size,
                    self.max_oligo_size) == 0:
                hybs = analyzer.check_oligo(test_oligo.get_seq(),
                        full_seq)
                if len(hybs) == 0:
                    return test_oligo
                elif self.debug:
                    for hyb in hybs:
                        print hyb

        for shrink in range(self.max_shrink):
            test_oligo = oligo.copy()
            test_oligo.adjust_three(-(shrink + 1))
            if self.debug:
                print "Shrink", test_oligo
            if test_oligo.bad_size(self.min_oligo_size,
                    self.max_oligo_size) == 0:
                hybs = analyzer.check_oligo(test_oligo.get_seq(),
                        full_seq)
                if len(hybs) == 0:
                    return test_oligo
                elif self.debug:
                    for hyb in hybs:
                        print hyb
        
        raise OligoRepairError("Could not adjust to fix issue: %s\n>%s\n%s" %
                ("\n".join([str(x) for x in hybs]), name, full_seq))

    def _fix_five(self, name, oligo, full_seq, analyzer):
        """Fix an issue with the 5' end of an oligo.

        If the 5' end of the oligo cross-reacts, try to fix the problem
        by adjusting the end of the oligo.
        """
        hybs = []
        if self.debug:
            print "5", oligo
        for expand in range(self.max_expand):
            test_oligo = oligo.copy()
            test_oligo.adjust_five(expand + 1)
            if self.debug:
                print "Expand", test_oligo
            if not test_oligo.bad_size(self.min_oligo_size,
                    self.max_oligo_size):
                hybs = analyzer.check_oligo(test_oligo.get_seq(),
                        full_seq)
                if len(hybs) == 0:
                    return test_oligo
                elif self.debug:
                    for hyb in hybs:
                        print hyb

        for shrink in range(self.max_shrink):
            test_oligo = oligo.copy()
            test_oligo.adjust_five(-(shrink + 1))
            if self.debug:
                print "Shrink", test_oligo
            if not test_oligo.bad_size(self.min_oligo_size,
                    self.max_oligo_size):
                hybs = analyzer.check_oligo(test_oligo.get_seq(),
                        full_seq)
                if len(hybs) == 0:
                    return test_oligo
                elif self.debug:
                    for hyb in hybs:
                        print hyb

        raise OligoRepairError("Could not adjust to fix issue: %s\n>%s\n%s" %
                ("\n".join([str(x) for x in hybs]), name, full_seq))

class CrossOligoRepair:
    def __init__(self, min_nts, max_nts, min_gcs, max_expand = 8, max_shrink =
            2, loose_flex = 1, desperate_flex = 2, adjust_amount = 6):
        self.single_repair = _SingleOligoRepair(min_nts, max_nts, min_gcs,
                max_expand, max_shrink, loose_flex, desperate_flex)

        self.adjust_amount = adjust_amount

    def set_oligo_min_max(self, min_size, max_size):
        """Set the range of sizes for oligos.
        """
        self.single_repair.set_oligo_min_max(min_size, max_size)
        self.min_oligo_size = min_size
        self.max_oligo_size = max_size

    def set_melt_info(self, melt_calc, melt_thresh):
        self.melt_calc = melt_calc
        self.melt_thresh = melt_thresh

    def check_oligos(self, name, oligos, full_sequence):
        """Check all oligos and repair cross hybridization problems.
        """
        oligo_manager = _WorkOligoManager(oligos, self.min_oligo_size,
                self.max_oligo_size, self.melt_calc, self.melt_thresh)
        while 1:
            try:
                for index, oligo in enumerate(oligo_manager.get_cur_oligos()):
                    self.single_repair.check_oligo(name, oligo, full_sequence)
                break
            # if we get an error, we need to adjust and try again
            except OligoRepairError, msg:
                num_adjust = 0
                while 1:
                    # four different adjustments to try and fix issues
                    # 1. shrink the previous oligo (5' end change)
                    if num_adjust == 0:
                        target, change = oligo_manager.adjust_oligo_and_prev(
                                index, -self.adjust_amount)
                    # 2. expand the previous oligo (5' end change)
                    elif num_adjust == 1:
                        target, change = oligo_manager.adjust_oligo_and_prev(
                                index, self.adjust_amount)
                    # 3. shrink the next oligo (3' end change)
                    elif num_adjust == 2:
                        target, change = oligo_manager.adjust_oligo_and_next(
                                index, -self.adjust_amount)
                    # 4. expand the next oligo (3' end change)
                    elif num_adjust == 3:
                        target, change = oligo_manager.adjust_oligo_and_next(
                                index, self.adjust_amount)
                    # give up and raise an error that we can't fix the problem
                    else:
                        raise OligoRepairError(
                        "Can't fix oligo %s %s: %s; %s" %
                                (name, index, oligo, msg))
                    
                    is_good = self._check_change_oligos(name, target, change,
                            full_sequence, oligo_manager)
                    if is_good:
                        break
                    else:
                        num_adjust += 1

    def _check_change_oligos(self, name, target, change, full_sequence,
            oligo_manager):
        """Finish up an adjustment, checking oligos for lingering issues.

        This checks if a change worked to fix problems, returning True if so
        or False if not. If a change did work, then the oligos in the manager
        are changed to the good changes.
        """
        if target is None:
            assert change is None
            return False

        try:
            self.single_repair.check_oligo(name, target.raw_oligo(),
                    full_sequence)
            self.single_repair.check_oligo(name, change.raw_oligo(),
                    full_sequence)
        except OligoRepairError:
            return False

        # if we got here everything is good
        oligo_manager.replace_oligo(target)
        oligo_manager.replace_oligo(change)
        return True

class OligoAnalyzer:
    """Analyze designed oligos, looking for issues which need changes.

    This looks at designed oligos for problematic 3' or 5' ends, internal
    hairpins, and other issues.
    """
    def __init__(self, single_analyses = [], multi_analyses = []):
        self.single_analyses = single_analyses
        self.multi_analyses = multi_analyses

    def set_oligo_min_max(self, min_size, max_size):
        """Set maximum and minimum oligo sizes, presumedly from splitter info
        """
        for analysis in self.single_analyses:
            analysis.set_oligo_min_max(min_size, max_size)
        for analysis in self.multi_analyses:
            analysis.set_oligo_min_max(min_size, max_size)

    def set_melt_info(self, melt_calc, melt_thresh):
        """Set information on melting temperatures.
        """
        for analysis in self.single_analyses:
            analysis.set_melt_info(melt_calc, melt_thresh)
        for analysis in self.multi_analyses:
            analysis.set_melt_info(melt_calc, melt_thresh)

    def analyze_oligos(self, name, oligos, full_sequence):
        """Analyze a list of OligoRegions for potential problems.
        """
        self._single_analysis(self.single_analyses, name, oligos, full_sequence)
        self._multi_analysis(self.multi_analyses, name, oligos, full_sequence)

    def _multi_analysis(self, analyses, name, oligos, full_sequence):
        """Perform analyses that look at all oligos at once.
        """
        for analysis in analyses:
            analysis.check_oligos(name, oligos, full_sequence)

    def _single_analysis(self, analyses, name, oligos, full_sequence):
        """Do analyses that require looking at oligos one at a time.
        """
        for oligo in oligos:
            for analysis in analyses:
                analysis.check_oligo(name, oligo, full_sequence)

class _HairpinProblem:
    """Hold information on a hairpin region detected in a sequence.
    """
    def __init__(self, sequence, a_start, a_end, b_start, b_end):
        """Define two regions on a sequence with a hairpin issue.
        """
        self.seq = sequence
        self.a_region = (a_start, a_end)
        self.b_region = (b_start, b_end)

    def __str__(self):
        out = "Hairpin : %s,%s (%s) vs. %s,%s (%s)" % (
                self.a_region[0], self.a_region[1],
                self.seq[self.a_region[0]:self.a_region[1]],
                self.b_region[0], self.b_region[1],
                self.seq[self.b_region[0]:self.b_region[1]])
        return out

    def __hash__(self):
        return hash((self.a_region, self.b_region))

    def __cmp__(self, other):
        """Determine if two detected hairpins are identical.
        """
        return cmp((self.a_region, self.b_region),
                   (other.a_region, other.b_region))

    def expand(self):
        """Expand the initially found hairpin match if possible.
        """
        self.expand_five()
        self.expand_three()

    def expand_five(self):
        """Expand this issue as far as possible in the 5' direction

        The 5' expansion is relative to the a_region, so 3' expanding for the
        b_region.
        """
        cur_a_start, cur_a_end = self.a_region
        cur_b_start, cur_b_end = self.b_region
        # print "5'", cur_a_start, cur_a_end, cur_b_start, cur_b_end
        expand = 0
        while 1:
            expand += 1
            new_a_test = cur_a_start - expand
            # subtract by 1 for end sequences since the end coordinate is
            # accessed differently than the range
            # >>> b = "012345"
            # >>> b[1:4]
            # '123'
            # >>> b[4]
            # '4'
            new_b_test = cur_b_end + expand - 1

            if new_a_test < 0 or new_b_test >= len(self.seq):
                break

            if not self._hairpin_matches(new_a_test, new_b_test):
                break

        # adjust by the expanded amount
        # last expansion caused the problem, so we retract by one
        expand -= 1
        new_a_start = cur_a_start - expand
        new_b_end = cur_b_end + expand
        self.a_region = (new_a_start, cur_a_end)
        self.b_region = (cur_b_start, new_b_end)

    def _hairpin_matches(self, first_index, second_index):
        """Decide if the two bases match (are complements of each other).
        """
        first_base = self.seq[first_index]
        second_base = self.seq[second_index]
        #print first_index, first_base, second_index, second_base
        if IUPACData.ambiguous_dna_complement[first_base] == second_base:
            return 1
        else:
            return 0

    def expand_three(self):
        """Expand this hairpin as far as possible in the 3' direction.
        
        The 3' expansion is relative to the a_region, so 5' expanding for the
        b_region.
        """
        cur_a_start, cur_a_end = self.a_region
        cur_b_start, cur_b_end = self.b_region
        #print "3'", cur_a_start, cur_a_end, cur_b_start, cur_b_end
        expand = 0
        while 1:
            expand += 1
            new_a_test = cur_a_end + expand - 1
            new_b_test = cur_b_start - expand

            if new_b_test < new_a_test:
                break

            if not self._hairpin_matches(new_a_test, new_b_test):
                break

        # adjust by the expanded amount
        # last expansion caused the problem, so we retract by one
        expand -= 1
        new_a_end = cur_a_end + expand
        new_b_start = cur_b_start - expand
        self.a_region = (cur_a_start, new_a_end)
        self.b_region = (new_b_start, cur_b_end)

    def will_extend_five(self, new_nts):
        """Determine if the added 5' nucleotides will increase the hairpin.

        These nucleotides are assumed to be added at the 5' end of the 5' most
        end of the hairpin, and thus the hairpin must be at the 5' end of the
        oligo.
        """
        # if we are on the 5' end
        if self.a_region[0] == 0:
            other_start = self.b_region[1]
            other_end = self.b_region[1] + len(new_nts)
            # check if we are off the 3' end of the oligo
            if other_end > len(self.seq):
                return 0
            else:
                test_nts = self.seq[other_start:other_end]
                rc_test_nts = Seq(test_nts,
                        unambiguous_dna).reverse_complement().data
                assert len(rc_test_nts) == len(new_nts), (rc_test_nts, new_nts,
                        self.seq, self.a_region, self.b_region)
                if rc_test_nts == new_nts:
                    return 1

        # if we got here, we can't extend
        return 0
    
    def will_extend_three(self, new_nts):
        """Determine if the added 3' nucleotides will increase the hairpin.

        These nucleotides are assumed to be added at the 3' end of the 3' most
        end of the hairpin, and thus the hairpin must be at the 3' end of the
        oligo. So, this checks the opposite side of will_extend_five
        """
        # if we are on the 3' end
        if self.b_region[1] == len(self.seq):
            other_start = self.a_region[0] - len(new_nts)
            other_end = self.a_region[0]
            # check if we are off the 5' end of the oligo
            if other_start < 0:
                return 0
            else:
                test_nts = self.seq[other_start:other_end]
                rc_test_nts = Seq(test_nts,
                        unambiguous_dna).reverse_complement().data
                assert len(rc_test_nts) == len(new_nts), (rc_test_nts, new_nts)
                if rc_test_nts == new_nts:
                    return 1
            
        # if we got here, we can't extend
        return 0

class HairpinAnalysis:
    """Search for hairpin regions within designed oligos.

    This looks for a exact hairpins (defined as the presence of a sequence and
    its reverse complement in the same oligo), and returns them.
    """
    def __init__(self, min_hairpin):
        self.name = HAIRPIN
        self.min_hairpin = min_hairpin

    def set_oligo_min_max(self, min_size, max_size):
        """We don't need this information.
        """
        pass
    
    def set_melt_info(self, melt_calc, melt_thresh):
        """We don't need this information.
        """
        pass

    def check(self, sequence):
        """Check a sequence returning hairpin problem regions.
        """
        all_seed_hairpins = self._find_seed_hairpins(sequence)
        expanded_hairpins = []
        for hairpin in all_seed_hairpins:
            hairpin.expand()
            expanded_hairpins.append(hairpin)
        # remove any duplicates using sets
        final_hairpins = list(set(expanded_hairpins))

        return _OligoProblems(sequence, final_hairpins)

    def _find_seed_hairpins(self, oligo):
        """Find any hairpin regions which meet the minimum size requirements.

        This is a very simple window iterator which walks across the sequence
        and searches for the reverse complement of the given sequence. If
        found, a seed oligo is created.
        """
        hairpins = []
        for index in range(len(oligo) - self.min_hairpin):
            cur_start = index
            cur_end = index + self.min_hairpin
            cur_test = oligo[cur_start:cur_end]
            rc_search = Seq(cur_test, unambiguous_dna).reverse_complement().data
            pat = re.compile(rc_search)
            matches = pat.finditer(oligo)
            for match in matches:
                match_start, match_end = match.span()
                if match_start not in range(cur_start, cur_end) and \
                   match_end not in range(cur_start, cur_end):
                    # set the ends as the 5' most end of the hairpin first
                    #print cur_start, cur_end, match_start, match_end
                    if cur_start < match_start:
                        assert cur_end <= match_start
                        hairpins.append(_HairpinProblem(oligo, cur_start,
                            cur_end, match_start, match_end))
                    else:
                        assert cur_start >= match_end
                        hairpins.append(_HairpinProblem(oligo, match_start,
                            match_end, cur_start, cur_end))

        # remove any duplicates using sets
        unique_hairpins = list(set(hairpins))
                   
        return unique_hairpins