Source code for pyhml.models.hml

# coding: utf-8

from __future__ import absolute_import
from pyhml.models.reporting_center import ReportingCenter
from pyhml.models.sample import Sample
from .base_model_ import Model
from datetime import date, datetime
from typing import List, Dict
from ..util import deserialize_model
import pandas as pd
from Bio import SeqIO
from pandas import DataFrame


[docs]class HML(Model): """ NOTE: This class is auto generated by the swagger code generator program. Do not edit the class manually. """ def __init__(self, project_name: str=None, version: str=None, schema_location: str=None, reporting_center: ReportingCenter=None, sample: List[Sample]=None): """ HML - a model defined in Swagger :param project_name: The project_name of this HML. :type project_name: str :param version: The version of this HML. :type version: str :param schema_location: The schema_location of this HML. :type schema_location: str :param reporting_center: The reporting_center of this HML. :type reporting_center: ReportingCenter :param sample: The sample of this HML. :type sample: List[Sample] """ self.swagger_types = { 'project_name': str, 'version': str, 'schema_location': str, 'reporting_center': ReportingCenter, 'sample': List[Sample] } self.attribute_map = { 'project_name': 'project_name', 'version': 'version', 'schema_location': 'schemaLocation', 'reporting_center': 'reporting_center', 'sample': 'sample' } self._project_name = project_name self._version = version self._schema_location = schema_location self._reporting_center = reporting_center self._sample = sample
[docs] @classmethod def from_dict(cls, dikt) -> 'HML': """ Returns the dict as a model :param dikt: A dict. :type: dict :return: The HML of this HML. :rtype: HML """ return deserialize_model(dikt, cls)
@property def project_name(self) -> str: """ Gets the project_name of this HML. :return: The project_name of this HML. :rtype: str """ return self._project_name @project_name.setter def project_name(self, project_name: str): """ Sets the project_name of this HML. :param project_name: The project_name of this HML. :type project_name: str """ self._project_name = project_name @property def version(self) -> str: """ Gets the version of this HML. :return: The version of this HML. :rtype: str """ return self._version @version.setter def version(self, version: str): """ Sets the version of this HML. :param version: The version of this HML. :type version: str """ self._version = version @property def schema_location(self) -> str: """ Gets the schema_location of this HML. :return: The schema_location of this HML. :rtype: str """ return self._schema_location @schema_location.setter def schema_location(self, schema_location: str): """ Sets the schema_location of this HML. :param schema_location: The schema_location of this HML. :type schema_location: str """ self._schema_location = schema_location @property def reporting_center(self) -> ReportingCenter: """ Gets the reporting_center of this HML. :return: The reporting_center of this HML. :rtype: ReportingCenter """ return self._reporting_center @reporting_center.setter def reporting_center(self, reporting_center: ReportingCenter): """ Sets the reporting_center of this HML. :param reporting_center: The reporting_center of this HML. :type reporting_center: ReportingCenter """ self._reporting_center = reporting_center @property def sample(self) -> List[Sample]: """ Gets the sample of this HML. :return: The sample of this HML. :rtype: List[Sample] """ return self._sample @sample.setter def sample(self, sample: List[Sample]): """ Sets the sample of this HML. :param sample: The sample of this HML. :type sample: List[Sample] """ self._sample = sample
[docs] def toPandas(self) -> DataFrame: """ Returns all the HML data as a pandas DataFrame. Examples: >>> import pyhml >>> hmlparser = pyhml.HmlParser(verbose=True) >>> hml = hmlparser.parse(hml_file) >>> hml_df = hml.toPandas() :return: Pandas dataframe :rtype: DataFrame """ data = [] for sample in self.sample: for loc in sample.seq_records: for seqrc in sample.seq_records[loc]: seq = str(seqrc.seq) gl = seqrc.name db = seqrc.description row = [sample.id, loc, gl, db, seq] data.append(row) return pd.DataFrame(data, columns=['ID', 'Locus', 'glstring', 'dbversion', 'sequence'])
[docs] def tobiotype(self, outdir, dtype='fasta', by='file'): """ Converts an HML object to a BioPython data fromat Examples: >>> import pyhml >>> hmlparser = pyhml.HmlParser(verbose=True) >>> hml = hmlparser.parse(hml_file) >>> hml.tobiotype("output/directory",dtype='imgt', by='subject') :param outdir: The output directory :type outdir: str :param dtype: The BioPython output type :type version: str :param by: What to print out the HML file by :type by: str """ if by == 'subject': for sample in self.sample: sequence_records = [] for loc in sample.seq_records: for seqrc in sample.seq_records[loc]: sequence_records.append(seqrc) outfile = outdir + '/' + sample.id + '.' + dtype SeqIO.write(sequence_records, outfile, dtype) else: sequence_records = [] for sample in self.sample: for loc in sample.seq_records: for seqrc in sample.seq_records[loc]: sequence_records.append(seqrc) outfile = outdir + '/' + self.project_name + '.' + dtype SeqIO.write(sequence_records, outfile, dtype)