Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.
Open
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
5 changes: 5 additions & 0 deletions mzio-py/src/fasta/entries/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Contains wrappers for FASTA headers.
///

pub mod plain;
pub mod uniprot;
65 changes: 65 additions & 0 deletions mzio-py/src/fasta/entries/plain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 3rd party imports
use pyo3::prelude::*;
use mzio::fasta::entry::Entry as BaseEntry;
use mzio::fasta::headers::{
Header,
plain::Plain as BasePlain
};

/// Wrapper for the rust implementation entry
///
#[pyclass]
pub struct Plain {
base_entry: BaseEntry<BasePlain>
}

#[pymethods]
impl Plain {
/// Python constructor
///
/// # Arguments
///
#[new]
fn new(header: String, sequence: String) -> Self {
Self {
base_entry: BaseEntry::new(
BasePlain::new(&header),
sequence
)
}
}

/// Returns the header
///
#[getter]
pub fn header(&self) -> PyResult<&str> {
Ok(&self.base_entry.get_header().get_header())
}

/// Returns the amino acid sequence
///
#[getter]
pub fn sequence(&self) -> PyResult<&str> {
Ok(&self.base_entry.get_sequence())
}
}


impl From<BaseEntry<BasePlain>> for Plain {
/// Convert entry from the Rust implementation to the python wrapper.
///
/// # Arguments
///
/// * `base_entry` - Plain from rust implementation
fn from(base_entry: BaseEntry<BasePlain>) -> Self {
Self {
base_entry
}
}
}

impl<'a> Into<&'a BaseEntry<BasePlain>> for &'a Plain {
fn into(self) -> &'a BaseEntry<BasePlain> {
&self.base_entry
}
}
49 changes: 21 additions & 28 deletions mzio-py/src/fasta/entry.rs → mzio-py/src/fasta/entries/uniprot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@ use std::collections::HashMap;
// 3rd party imports
use pyo3::prelude::*;
use mzio::fasta::entry::Entry as BaseEntry;
use mzio::fasta::headers::{
Header,
uniprot::UniProt as BaseUniProt
};


/// Wrapper for the rust implementation entry
///
#[pyclass]
pub struct Entry {
base_entry: BaseEntry
pub struct UniProt {
base_entry: BaseEntry<BaseUniProt>
}

#[pymethods]
impl Entry {
impl UniProt {
/// Python constructor
///
/// # Arguments
///
/// * `database` - The FASTA database
/// * `accession` - Entry accession
/// * `entry_name` - Entry name
/// * `protein_name` - Protein name
/// * `keyword_attributes` - Additional keyword attributes, e.g. OX=381666
/// * `sequence` - Amino acid sequence
///
#[new]
fn new(database: String, accession: String, entry_name: String, protein_name: String,
keyword_attributes: HashMap<String, String>, sequence: String) -> Self {
fn new(header: String, sequence: String) -> Self {
Self {
base_entry: BaseEntry::new(
database,
accession,
entry_name,
protein_name,
keyword_attributes,
BaseUniProt::new(&header),
sequence
)
}
Expand All @@ -44,28 +37,28 @@ impl Entry {
///
#[getter]
pub fn database(&self) -> PyResult<&str> {
Ok(&self.base_entry.get_database())
Ok(&self.base_entry.get_header().get_database())
}

/// Returns the accession
///
#[getter]
pub fn accession(&self) -> PyResult<&str> {
Ok(&self.base_entry.get_accession())
Ok(&self.base_entry.get_header().get_accession())
}

/// Entry name
/// UniProt name
///
#[getter]
pub fn entry_name(&self) -> PyResult<&str> {
Ok(&self.base_entry.get_entry_name())
Ok(&self.base_entry.get_header().get_entry_name())
}

/// Returns the protein name
///
#[getter]
pub fn protein_name(&self) -> PyResult<&str> {
Ok(&self.base_entry.get_protein_name())
Ok(&self.base_entry.get_header().get_protein_name())
}

/// Returns additional keyword attributes, e.g
Expand All @@ -76,7 +69,7 @@ impl Entry {
#[getter]
pub fn keyword_attributes(&self) -> PyResult<HashMap<String, String>> {
// TODO: avoid clone?
Ok(self.base_entry.get_keyword_attributes().clone())
Ok(self.base_entry.get_header().get_keyword_attributes().clone())
}

/// Returns the amino acid sequence
Expand All @@ -88,21 +81,21 @@ impl Entry {
}


impl From<BaseEntry> for Entry {
impl From<BaseEntry<BaseUniProt>> for UniProt {
/// Convert entry from the Rust implementation to the python wrapper.
///
/// # Arguments
///
/// * `base_entry` - Entry from rust implementation
fn from(base_entry: BaseEntry) -> Self {
/// * `base_entry` - UniProt from rust implementation
fn from(base_entry: BaseEntry<BaseUniProt>) -> Self {
Self {
base_entry
}
}
}

impl<'a> Into<&'a BaseEntry> for &'a Entry {
fn into(self) -> &'a BaseEntry {
impl<'a> Into<&'a BaseEntry<BaseUniProt>> for &'a UniProt {
fn into(self) -> &'a BaseEntry<BaseUniProt> {
&self.base_entry
}
}
9 changes: 6 additions & 3 deletions mzio-py/src/fasta/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod entry;
pub mod reader;
pub mod writer;
/// Contains python wrappers for FASTA I/O.
///

pub mod entries;
pub mod readers;
pub mod writers;
37 changes: 0 additions & 37 deletions mzio-py/src/fasta/reader.rs

This file was deleted.

5 changes: 5 additions & 0 deletions mzio-py/src/fasta/readers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Contains wrappers for FASTA headers.
///

pub mod plain;
pub mod uniprot;
40 changes: 40 additions & 0 deletions mzio-py/src/fasta/readers/plain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// std imports
use std::path::PathBuf;

// 3rd party modules
use pyo3::prelude::*;
use anyhow::{Result};
use mzio::fasta::headers::plain::Plain as BasePlain;
use mzio::fasta::reader::Reader as BaseReader;

// internal imports
use crate::fasta::entries::plain::Plain;


/// A FASTA reader which returns the header in plain text.
///
#[pyclass]
pub struct PlainReader {
base_reader: BaseReader<BasePlain>
}

#[pymethods]
impl PlainReader {
#[new]
fn new(fasta_file_path: PathBuf, buffer_size: usize) -> Result<Self> {
Ok(Self {
base_reader: BaseReader::<BasePlain>::new(&fasta_file_path, buffer_size)?
})
}

fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<Plain> {
match slf.base_reader.next() {
Some(base_entry) => Some(Plain::from(base_entry)),
None => None
}
}
}
40 changes: 40 additions & 0 deletions mzio-py/src/fasta/readers/uniprot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// std imports
use std::path::PathBuf;

// 3rd party modules
use pyo3::prelude::*;
use anyhow::{Result};
use mzio::fasta::headers::uniprot::UniProt as BaseUniProt;
use mzio::fasta::reader::Reader as BaseReader;

// internal imports
use crate::fasta::entries::uniprot::UniProt;


/// A FASTA reader which parses the UniProt formatted header format.
///
#[pyclass]
pub struct UniProtReader {
base_reader: BaseReader<BaseUniProt>
}

#[pymethods]
impl UniProtReader {
#[new]
fn new(fasta_file_path: PathBuf, buffer_size: usize) -> Result<Self> {
Ok(Self {
base_reader: BaseReader::<BaseUniProt>::new(&fasta_file_path, buffer_size)?
})
}

fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<UniProt> {
match slf.base_reader.next() {
Some(base_entry) => Some(UniProt::from(base_entry)),
None => None
}
}
}
5 changes: 5 additions & 0 deletions mzio-py/src/fasta/writers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// Contains FASTA writers.
///

pub mod plain;
pub mod uniprot;
21 changes: 10 additions & 11 deletions mzio-py/src/fasta/writer.rs → mzio-py/src/fasta/writers/plain.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ use std::path::PathBuf;
// 3rd party modules
use anyhow::Result;
use pyo3::prelude::*;
use mzio::fasta::headers::plain::Plain as BasePlain;
use mzio::fasta::writer::Writer as BaseWriter;

// internal imports
use crate::fasta::entry::Entry;
use crate::fasta::entries::plain::Plain;


/// A FASTA writer which writes the header in plain text.
///
#[pyclass]
pub struct Writer {
base_writer: BaseWriter
pub struct PlainWriter {
base_writer: BaseWriter<BasePlain>
}

#[pymethods]
impl Writer {
impl PlainWriter {
/// Creates a new Writer
///
/// # Arguments
Expand All @@ -25,20 +28,16 @@ impl Writer {
///
#[new]
pub fn new(fasta_file_path: PathBuf) -> PyResult<Self> {
match BaseWriter::new(&fasta_file_path) {
Ok(base_writer) => Ok(Self{base_writer}),
Err(err) => Err(err.into())
}
Ok(Self { base_writer: BaseWriter::<BasePlain>::new(&fasta_file_path)? })
}

pub fn write_entry(&mut self, entry: &Entry, sort_keyword_attributes: bool) -> Result<usize> {
match self.base_writer.write_entry(entry.into(), sort_keyword_attributes) {
pub fn write_entry(&mut self, entry: &Plain) -> Result<usize> {
match self.base_writer.write_entry(entry.into()) {
Ok(written_bytes) => Ok(written_bytes),
Err(err) => Err(err)
}
}


pub fn flush(&mut self) -> Result<()> {
match self.base_writer.flush() {
Ok(_) => Ok(()),
Expand Down
Loading