Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CTX-4665: Altered SequenceDataset and added multithreading to bwa and samtools #59

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions coretex/bioinformatics/sequence_alignment/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pathlib import Path

import subprocess
import logging
import os

from ..utils import command, logProcessOutput, CommandException
from ...coretex import CustomDataset
Expand Down Expand Up @@ -61,7 +61,14 @@ def indexCommand(bwaPath: Path, sequencePath: Path, prefix: Path) -> None:
])


def alignCommand(bwaPath: Path, prefix: Path, sequencePath: Path, outputPath: Path) -> None:
def alignCommand(
bwaPath: Path,
prefix: Path,
sequencePath: Path,
outputPath: Path,
multithreading: bool = True
) -> None:

"""
This function acts as a wrapper for the mem command of BWA
(Burrows-Wheeler Aligner). It perfoms alignment of a given sequence read
Expand Down Expand Up @@ -92,15 +99,29 @@ def alignCommand(bwaPath: Path, prefix: Path, sequencePath: Path, outputPath: Pa

args = [
str(bwaPath.absolute()), "mem",
"-o", str(outputPath.absolute()),
"-o", str(outputPath.absolute())
]

if multithreading:
threads = os.cpu_count()
if threads is not None:
args.extend(["-t", str(threads)])

args.extend([
str(prefix.absolute()),
str(sequencePath.absolute())
]
])

command(args, True)


def sam2bamCommand(samtoolsPath: Path, samPath: Path, outputPath: Path) -> None:
def sam2bamCommand(
samtoolsPath: Path,
samPath: Path,
outputPath: Path,
multithreading: bool = True
) -> None:

"""
This function uses the CLI tool "samtools" to convert SAM files into their binary
version, BAM.
Expand All @@ -125,13 +146,23 @@ def sam2bamCommand(samtoolsPath: Path, samPath: Path, outputPath: Path) -> None:
Link to samtools: http://htslib.org/
"""

command([
args = [
str(samtoolsPath.absolute()), "view",
"-b", "-S", "-o",
str(outputPath.absolute()),
"-b", "-S"
]

if multithreading:
threads = os.cpu_count()
if threads is not None:
args.extend(["--threads", str(threads - 1)])

args.extend([
"-o", str(outputPath.absolute()),
str(samPath.absolute())
])

command(args)


def extractData(samtoolsPath: Path, file: Path) -> Tuple[List[int], List[int], List[int]]:
"""
Expand Down
3 changes: 1 addition & 2 deletions coretex/coretex/dataset/network_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ def createDataset(
) -> Optional[Self]:

"""
Creates a new dataset with the provided name, type
and samples (if present, samples are not required)
Creates a new dataset with the provided name and type

Parameters
----------
Expand Down
60 changes: 58 additions & 2 deletions coretex/coretex/dataset/sequence_dataset/sequence_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@
# 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 Dict
from typing import Dict, Optional, Any, Union
from typing_extensions import Self
from pathlib import Path

import logging

from .base import BaseSequenceDataset
from ..network_dataset import NetworkDataset
from ..custom_dataset import CustomDataset
from ...sample import SequenceSample, CustomSample
from ....codable import KeyDescriptor

Expand Down Expand Up @@ -50,6 +55,58 @@ def onDecode(self) -> None:
if sample.id != self.metadata.id
]

@classmethod
def createSequenceDataset(
cls,
name: str,
spaceId: int,
metadataPath: Union[Path, str],
meta: Optional[Dict[str, Any]] = None
) -> Optional[Self]:

"""
Creates a new sequence dataset with the provided name and metadata

Parameters
----------
name : str
dataset name
spaceId : int
space for which the dataset will be created
metadataPath : Union[Path, str]
path the zipped metadata file

Returns
-------
The created sequence dataset object or None if creation failed

Example
-------
>>> from coretex import SequenceDataset
\b
>>> dummyDataset = SequenceDataset.createSequenceDataset("dummyDataset", 123, pathToMetadata)
>>> if dummyDataset is not None:
print("Dataset created successfully")
"""

if isinstance(metadataPath, str):
metadataPath = Path(metadataPath)

dataset = CustomDataset.createDataset(name, spaceId, meta)
if dataset is None:
return None

if CustomSample.createCustomSample(
"_metadata",
dataset.id,
metadataPath
) is None:

logging.getLogger("coretexpylib").warning(">> [Coretex] Failed to create _metadata sample")
return None

return cls.fetchById(dataset.id)

def download(self, ignoreCache: bool = False) -> None:
super().download(ignoreCache)

Expand Down Expand Up @@ -80,4 +137,3 @@ def isPairedEnd(self) -> bool:
return False

raise ValueError(">> [Coretex] Dataset contains a mix of paired-end and single-end sequences. It should contain either one or the other")

dule1322 marked this conversation as resolved.
Show resolved Hide resolved