|
| 1 | +import re |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from pypdf import PdfReader |
| 5 | +from PyQt6.QtWidgets import ( |
| 6 | + QFileDialog, |
| 7 | + QHBoxLayout, |
| 8 | + QLabel, |
| 9 | + QLineEdit, |
| 10 | + QPlainTextEdit, |
| 11 | + QPushButton, |
| 12 | + QTextEdit, |
| 13 | + QVBoxLayout, |
| 14 | + QWidget, |
| 15 | +) |
| 16 | + |
| 17 | +DNA_NEEDLES = [ |
| 18 | + [ |
| 19 | + 'Bighead Carp', |
| 20 | + 'AACTTAAATAAACAGATTATTCCACTAACAATTGATTCTCAAATTTATTACTGAATTATTAACTAAAATCTAACTCAAGTATATTATTAAAGTAAGAGACCACCTACTTATTTATATTAAGGTATTATATTCATGATAAGATCAAGGACAATAACAGTGGGGGTGGCGCAAAATGAACTATTACTTGCATCTGGTTTGGAATCTCACGGACATGGCTACAAAATTCCACCCCCGTTACATTATAACTGGCATATGGTTAAATGATGTGAGTACATACTCCTCATTAACCCCACATGCCGAGCATTCTTTTAT', |
| 21 | + ], |
| 22 | + [ |
| 23 | + 'Silver Carp', |
| 24 | + 'CCTGAGAAAAGAGTTGTTCCACTATAATTGGTTCTCAAATATTTCCTTGAAATATTAACTTCTATTTAATTTAACTATATTAATGTAGTAAGAAACCACCTACTGGTTTATATTAAGGTATTCTATTCATGATAAGATCAGGGACAATAATCGTGGGGGTGGCGCAGAATGAACTATTACTTGCATTTGGC', |
| 25 | + ], |
| 26 | + [ |
| 27 | + 'Grass Carp', |
| 28 | + 'GAGTTTCTGACTTCTACCCCCTTCTTTCCTCCTACTATTAGCCTCTTCTGGTGTTGAGGCCGGAGCTGGAACAGGGTGAACAG', |
| 29 | + ], |
| 30 | + [ |
| 31 | + 'Black Carp', |
| 32 | + 'ACACCACGTTCTTTGACCCAGCAGGCGGAGGAGACCCAATCCTATATCAACACCTGTTCTGATTCTTCGGCCACCCAGAAGTTTACATTCTTATTTTACCCGGGTTTGGGATCATTTCAC', |
| 33 | + ], |
| 34 | +] |
| 35 | + |
| 36 | + |
| 37 | +class DNA(QWidget): |
| 38 | + def __init__(self) -> None: |
| 39 | + super().__init__() |
| 40 | + |
| 41 | + self.root_layout = QVBoxLayout() |
| 42 | + self.base_layout = QHBoxLayout() |
| 43 | + |
| 44 | + self.input_layout = QVBoxLayout() |
| 45 | + self.auto_output_layout = QVBoxLayout() |
| 46 | + self.manual_output_layout = QVBoxLayout() |
| 47 | + |
| 48 | + file_picker_layout = QHBoxLayout() |
| 49 | + |
| 50 | + self.sample = QPlainTextEdit() |
| 51 | + |
| 52 | + file_browse = QPushButton('Browse') |
| 53 | + file_browse.clicked.connect(self.open_file_dialog) |
| 54 | + self.filename = QLineEdit() |
| 55 | + |
| 56 | + show_button = QPushButton('Search Database', None) |
| 57 | + |
| 58 | + show_button.clicked.connect(self.display_result) |
| 59 | + |
| 60 | + self.check_text = QTextEdit() |
| 61 | + |
| 62 | + self.input_layout.addWidget(QLabel('Select PDF File: ')) |
| 63 | + file_picker_layout.addWidget(self.filename) |
| 64 | + file_picker_layout.addWidget(file_browse) |
| 65 | + self.input_layout.addLayout(file_picker_layout) |
| 66 | + |
| 67 | + self.input_layout.addWidget(QLabel('Or enter sample directly: ')) |
| 68 | + self.input_layout.addWidget(self.sample) |
| 69 | + |
| 70 | + self.input_layout.addWidget(show_button) |
| 71 | + |
| 72 | + self.base_layout.addLayout(self.input_layout) |
| 73 | + |
| 74 | + self.base_layout.addLayout(self.auto_output_layout) |
| 75 | + self.base_layout.addLayout(self.manual_output_layout) |
| 76 | + self.root_layout.addLayout(self.base_layout) |
| 77 | + self.root_layout.addWidget(QLabel('Check DNA Samples:')) |
| 78 | + self.root_layout.addWidget(self.check_text) |
| 79 | + |
| 80 | + self.manual_result: QLabel | None = None |
| 81 | + self.auto_results: list[QLabel] = [] |
| 82 | + |
| 83 | + self.setLayout(self.root_layout) |
| 84 | + |
| 85 | + def open_file_dialog(self) -> None: |
| 86 | + filename, _ = QFileDialog.getOpenFileName( |
| 87 | + caption='Select a File', filter='PDF files (*.pdf)' |
| 88 | + ) |
| 89 | + if filename: |
| 90 | + path = Path(filename) |
| 91 | + self.text_file = str(path) |
| 92 | + self.filename.setText(self.text_file) |
| 93 | + |
| 94 | + def find_samples(self, pdf_file: str) -> list[str]: |
| 95 | + reader = PdfReader(pdf_file) |
| 96 | + samples = [] |
| 97 | + self.check_text.setText('') |
| 98 | + for page in reader.pages: |
| 99 | + text = page.extract_text() |
| 100 | + last_index = 0 |
| 101 | + while text.find('Unknown Sample', last_index) != -1: |
| 102 | + current_index = text.find( |
| 103 | + 'Unknown Sample', last_index |
| 104 | + ) # Roughly identify location of next sample |
| 105 | + sample_index = text.find('\n', current_index) + 1 # Find start of sample |
| 106 | + sample_index_end = text.find(' \n', sample_index) # Find end of sample |
| 107 | + sample = text[sample_index:sample_index_end].replace('\n', '') |
| 108 | + samples.append(sample) |
| 109 | + self.check_sample(sample, len(samples)) |
| 110 | + last_index = sample_index_end |
| 111 | + return samples |
| 112 | + |
| 113 | + def check_sample(self, sample: str, sample_num: int) -> None: |
| 114 | + # Check if sample has characters not corresponding to a DNA base |
| 115 | + if sample.replace('A', '').replace('C', '').replace('G', '').replace('T', '') != '': |
| 116 | + self.check_text.setText( |
| 117 | + 'Sample ' |
| 118 | + + str(sample_num) |
| 119 | + + ' has characters other than ACGT\n\n' |
| 120 | + + self.check_text.toPlainText() |
| 121 | + ) |
| 122 | + |
| 123 | + # Display all samples for manual checking |
| 124 | + self.check_text.setText( |
| 125 | + self.check_text.toPlainText() + ('Sample ' + str(sample_num) + ': ' + sample) + '\n\n' |
| 126 | + ) |
| 127 | + |
| 128 | + def display_result(self) -> None: |
| 129 | + for result in self.auto_results: |
| 130 | + self.auto_output_layout.removeWidget(result) |
| 131 | + self.manual_output_layout.removeWidget(self.manual_result) |
| 132 | + |
| 133 | + if self.filename.text(): |
| 134 | + self.auto_results = [] |
| 135 | + samples = self.find_samples(self.filename.text()) |
| 136 | + for i, sample in enumerate(samples): |
| 137 | + self.auto_results.append(QLabel(str(i + 1) + ': ' + self.search(sample))) |
| 138 | + if self.sample.toPlainText(): |
| 139 | + sample = self.sample.toPlainText() |
| 140 | + sample = re.sub('[^ACGT]', '', sample) # Remove every character except ACGT |
| 141 | + self.manual_result = QLabel('Manual: ' + self.search(sample)) |
| 142 | + |
| 143 | + for result in self.auto_results: |
| 144 | + self.auto_output_layout.addWidget(result) |
| 145 | + self.manual_output_layout.addWidget(self.manual_result) |
| 146 | + |
| 147 | + def search(self, sample: str) -> str: |
| 148 | + for name, substr in DNA_NEEDLES: |
| 149 | + if sample.find(substr) != -1: |
| 150 | + return name |
| 151 | + |
| 152 | + return 'No match' |
0 commit comments