-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CTX-4247: Created a bioinformatics folder, moved the qiime2 folder th…
…ere and migrated shared code from templates related to sequence alignment to sequence_alignment
- Loading branch information
Vuk Manojlovic
committed
Jul 14, 2023
1 parent
9415689
commit 23423e3
Showing
4 changed files
with
179 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Copyright (C) 2023 Coretex LLC | ||
|
||
# This file is part of Coretex.ai | ||
|
||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
|
||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
|
||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
from typing import List, Tuple | ||
from pathlib import Path | ||
|
||
import subprocess | ||
import logging | ||
|
||
from ...coretex import CustomDataset | ||
from ..utils import logProcessOutput | ||
|
||
|
||
def command(args: List[str], ignoreOutput: bool = False) -> None: | ||
process = subprocess.Popen( | ||
args, | ||
shell = False, | ||
cwd = Path(__file__).parent, | ||
stdout = subprocess.PIPE, | ||
stderr = subprocess.PIPE | ||
) | ||
|
||
while process.poll() is None: | ||
stdout = process.stdout.readline() | ||
stderr = process.stderr.readline() | ||
|
||
if len(stdout) > 0 and not ignoreOutput: | ||
logProcessOutput(stdout, logging.INFO) | ||
|
||
if len(stderr) > 0 and not ignoreOutput: | ||
if process.returncode == 0: | ||
logProcessOutput(stderr, logging.INFO) | ||
else: | ||
logProcessOutput(stderr, logging.CRITICAL) | ||
|
||
if process.returncode != 0: | ||
raise RuntimeError(f">> [Coretex] Falied to execute command. Returncode: {process.returncode}") | ||
|
||
|
||
def indexCommand(bwaPath: Path, sequencePath: Path, prefix: Path) -> None: | ||
command([ | ||
str(bwaPath.absolute()), "index", | ||
"-p", str(prefix.absolute()), | ||
str(sequencePath.absolute()) | ||
]) | ||
|
||
|
||
def alignCommand(bwaPath: Path, prefix: Path, sequencePath: Path, outputPath: Path) -> None: | ||
args = [ | ||
str(bwaPath.absolute()), "mem", | ||
"-o", str(outputPath.absolute()), | ||
str(prefix.absolute()), | ||
str(sequencePath.absolute()) | ||
] | ||
|
||
command(args, True) | ||
|
||
|
||
def sam2bamCommand(samtoolsPath: Path, samPath: Path, outputPath: Path) -> None: | ||
command([ | ||
str(samtoolsPath.absolute()), "view", | ||
"-b", "-S", "-o", | ||
str(outputPath.absolute()), | ||
str(samPath.absolute()) | ||
]) | ||
|
||
|
||
def extractData(samtoolsPath: Path, file: Path) -> Tuple[List[int], List[int], List[int]]: | ||
scores: list[int] = [] | ||
positions: list[int] = [] | ||
sequenceLengths: list[int] = [] | ||
|
||
args = [ | ||
str(samtoolsPath.absolute()), | ||
"view", "-F", "256", "-F", "2048", "-F", "512", | ||
str(file.absolute()) | ||
] | ||
|
||
process = subprocess.Popen( | ||
args, | ||
shell = False, | ||
cwd = Path(__file__).parent, | ||
stdout = subprocess.PIPE | ||
) | ||
|
||
for line in process.stdout: | ||
fields = line.decode("UTF-8").strip().split("\t") | ||
scores.append(int(fields[4])) | ||
positions.append(int(fields[3])) | ||
sequenceLengths.append(len(fields[9])) | ||
|
||
return scores, positions, sequenceLengths | ||
|
||
|
||
def chmodX(file: Path) -> None: | ||
command(["chmod", "+x", str(file.absolute())]) | ||
|
||
|
||
def loadFa(dataset: CustomDataset) -> List[Path]: | ||
inputFiles: list[Path] = [] | ||
|
||
for sample in dataset.samples: | ||
sample.unzip() | ||
|
||
for file in Path(sample.path).iterdir(): | ||
if file.suffix.startswith(".fa"): | ||
inputFiles.append(file) | ||
|
||
if len(inputFiles) == 0: | ||
raise ValueError(">> [Coretex] No sequence reads found") | ||
|
||
return inputFiles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright (C) 2023 Coretex LLC | ||
|
||
# This file is part of Coretex.ai | ||
|
||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as | ||
# published by the Free Software Foundation, either version 3 of the | ||
# License, or (at your option) any later version. | ||
|
||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
|
||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
import logging | ||
|
||
|
||
def logProcessOutput(output: bytes, severity: int) -> None: | ||
decoded = output.decode("UTF-8") | ||
|
||
for line in decoded.split("\n"): | ||
# skip empty lines | ||
if line.strip() == "": | ||
continue | ||
|
||
# ignoring type for now, has to be fixed in coretexpylib | ||
logging.getLogger("coretexpylib").log(severity, line) |