Edinburgh-Genome-Foundry / DnaChisel

:pencil2: A versatile DNA sequence optimizer
https://edinburgh-genome-foundry.github.io/DnaChisel/
MIT License
219 stars 40 forks source link

Error if location not specified #53

Closed veghp closed 3 years ago

veghp commented 3 years ago

Issue for bug mentioned in https://github.com/Edinburgh-Genome-Foundry/DnaChisel/issues/52#issuecomment-810384388. If no location given;

from dnachisel import DnaOptimizationProblem, EnforcePatternOccurence
seq="AAAAAAAAAA"

problem = DnaOptimizationProblem(
    sequence=seq,
    constraints=[
        EnforcePatternOccurence(pattern='CTG', occurences=2, strand=-1)
    ],
)

then:

AttributeError                            Traceback (most recent call last)
<ipython-input-219-82901a020845> in <module>
      5     sequence=seq,
      6     constraints=[
----> 7         EnforcePatternOccurence(pattern='CTG', occurences=2, strand=-1)
      8     ],
      9 )

/path/to/DnaChisel/dnachisel/builtin_specifications/EnforcePatternOccurence.py in __init__(self, pattern, occurences, location, strand, center, boost)
     69             if strand not in [-1, 0, 1]:
     70                 raise ValueError("unknown strand: %s" % strand)
---> 71             self.location.strand = strand
     72         self.strand = strand
     73         self.occurences = occurences

AttributeError: 'NoneType' object has no attribute 'strand'

because in https://github.com/Edinburgh-Genome-Foundry/DnaChisel/blob/9c72428ae822c1e5afba480232158e604c355699/dnachisel/builtin_specifications/EnforcePatternOccurence.py#L67 from_data() returns a None as location, and a value is assigned to its attribute in: https://github.com/Edinburgh-Genome-Foundry/DnaChisel/blob/9c72428ae822c1e5afba480232158e604c355699/dnachisel/builtin_specifications/EnforcePatternOccurence.py#L73 This issue also exists in other classes.

veghp commented 3 years ago

Also: missing explanation of the default option strand="from_location". The docstring example EnforcePatternOccurence('BsmBI_site', strand=1) does not work.

A proposed solution is in the issue53 branch: https://github.com/Edinburgh-Genome-Foundry/DnaChisel/blob/776d8cd381b12b05ba79d30562087c913220fb42/dnachisel/builtin_specifications/EnforcePatternOccurence.py#L65

I made handling of the 6 cases of initialisation more explicit: location=None or specified * 3 strand options: "from_location", "both", one of [-1, 0, 1], and carried over the intended strand with https://github.com/Edinburgh-Genome-Foundry/DnaChisel/blob/776d8cd381b12b05ba79d30562087c913220fb42/dnachisel/builtin_specifications/EnforcePatternOccurence.py#L82

If this approach proves correct, I'll add more tests, and implement the changes in AvoidPattern too.