|
| 1 | +import random |
| 2 | +import tkinter as tk |
| 3 | +from tkinter import filedialog |
| 4 | + |
| 5 | +def generate_random_dna(length, gc_content): |
| 6 | + """Generate a random DNA sequence of the specified length and GC content.""" |
| 7 | + |
| 8 | + # Calculate the number of GC and AT base pairs required based on the GC content |
| 9 | + gc_count = int(length * gc_content) |
| 10 | + at_count = length - gc_count |
| 11 | + |
| 12 | + # Generate the random sequence of GC and AT base pairs |
| 13 | + seq = "" |
| 14 | + for i in range(gc_count): |
| 15 | + seq += random.choice("GC") |
| 16 | + for i in range(at_count): |
| 17 | + seq += random.choice("AT") |
| 18 | + |
| 19 | + # Shuffle the sequence to make it truly random |
| 20 | + seq_list = list(seq) |
| 21 | + random.shuffle(seq_list) |
| 22 | + seq = "".join(seq_list) |
| 23 | + |
| 24 | + return seq |
| 25 | + |
| 26 | +def save_sequence_to_file(seq, filename): |
| 27 | + """Save the DNA sequence to a file.""" |
| 28 | + |
| 29 | + with open(filename, "w") as f: |
| 30 | + f.write(seq) |
| 31 | + |
| 32 | +def generate_sequence(): |
| 33 | + """Generate a random DNA sequence and save it to a file.""" |
| 34 | + |
| 35 | + # Get the desired length and GC content from the user |
| 36 | + length = int(length_entry.get()) |
| 37 | + gc_content = float(gc_content_entry.get()) |
| 38 | + |
| 39 | + # Generate the random DNA sequence |
| 40 | + seq = generate_random_dna(length, gc_content) |
| 41 | + |
| 42 | + # Get the filename to save the sequence to |
| 43 | + filename = filedialog.asksaveasfilename(defaultextension=".txt") |
| 44 | + |
| 45 | + # Save the sequence to the file |
| 46 | + save_sequence_to_file(seq, filename) |
| 47 | + |
| 48 | + # Show a message box indicating that the sequence was saved |
| 49 | + message = f"DNA sequence of length {length} and GC content {gc_content} saved to {filename}." |
| 50 | + tk.messagebox.showinfo("Sequence generated", message) |
| 51 | + |
| 52 | +# Create the main window |
| 53 | +root = tk.Tk() |
| 54 | +root.title("Random DNA Sequence Generator") |
| 55 | + |
| 56 | +# Create the length label and entry box |
| 57 | +length_label = tk.Label(root, text="Enter length:") |
| 58 | +length_label.pack() |
| 59 | +length_entry = tk.Entry(root) |
| 60 | +length_entry.pack() |
| 61 | + |
| 62 | +# Create the GC content label and entry box |
| 63 | +gc_content_label = tk.Label(root, text="Enter GC content (between 0 and 1):") |
| 64 | +gc_content_label.pack() |
| 65 | +gc_content_entry = tk.Entry(root) |
| 66 | +gc_content_entry.pack() |
| 67 | + |
| 68 | +# Create the generate button |
| 69 | +generate_button = tk.Button(root, text="Generate Sequence", command=generate_sequence) |
| 70 | +generate_button.pack() |
| 71 | + |
| 72 | +# Start the main event loop |
| 73 | +root.mainloop() |
0 commit comments