Skip to content

Instantly share code, notes, and snippets.

@bluedragon1221
Created October 22, 2023 20:31
Show Gist options
  • Save bluedragon1221/2ace1ba00d256f5e049477ca19bd9a56 to your computer and use it in GitHub Desktop.
Save bluedragon1221/2ace1ba00d256f5e049477ca19bd9a56 to your computer and use it in GitHub Desktop.
super over-engineered library for creating DNA strands
class Base:
def __init__(self, base):
assert base in ["A", "T", "G", "C"], "Base must be A, T, G, or C"
self.__base: str = base
def compliment(self) -> "Base":
sup = {
"G": "C",
"C": "G",
"A": "T",
"T": "A",
}
return Base(sup.get(self.__base))
def destructure(self) -> str:
return self.__base
@staticmethod
def verify_base(base1: "Base", base2: "Base") -> bool:
return base1.compliment().destructure() == base2.destructure()
class Strand:
def __init__(self, strand: list[Base]):
self.__strand = strand
def compliment(self) -> "Strand":
return Strand([i.compliment() for i in self.__strand])
def destructure(self) -> list[str]:
return [i.destructure() for i in self.__strand]
@classmethod
def from_strs(cls, lst: list[str]):
return cls([Base(i) for i in lst])
def __iter__(self):
yield from self.__strand
@staticmethod
def verify_strand(strand1: "Strand", strand2: "Strand") -> bool:
tf = [Base.verify_base(i, j) for i, j in zip(strand1, strand2)]
return all(tf)
if __name__ == "__main__":
strand1 = Strand([Base(i) for i in ["A", "A", "T", "C", "G", "G"]])
strand2 = Strand.from_strs(["A", "A", "T", "C", "G", "G"])
complimentary_strand = strand1.compliment()
tf = Strand.verify_strand(strand1, complimentary_strand)
print(tf)
print(strand1.compliment().destructure())
print(strand2.compliment().destructure())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment