Maintenance - Mise à jour mensuelle Lundi 1 Juin 2020 entre 7h00 et 9h00

sample.py 3.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#
# Copyright (C) 2009 INRA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

Penom Nom's avatar
Penom Nom committed
18 19
from ng6.exceptions import UnsavedRunError
from ng6.t3MySQLdb import t3MySQLdb
20 21 22

class Sample(object):
    
23
    AVAILABLE_TYPES = ["pe", "se", "ose", "ope", "mp","10X"]
Penom Nom's avatar
Penom Nom committed
24
    
25
    def __init__(self, sample_id, reads1, reads2 = None, readsi = None, name = None, description = None, type = None, 
Penom Nom's avatar
Penom Nom committed
26
                 insert_size = None, species = None, nb_sequences = None, full_size = None, id = None ):
Penom Nom's avatar
Penom Nom committed
27
        self.sample_id = sample_id
28 29 30
        self.name = name
        self.description = description
        self.reads1 = reads1
Penom Nom's avatar
Penom Nom committed
31
        self.reads2 = reads2
32
        self.readsi = readsi
Penom Nom's avatar
Penom Nom committed
33
        self.insert_size = insert_size
Penom Nom's avatar
Penom Nom committed
34 35
        self.nb_sequences = nb_sequences
        self.full_size = full_size
Penom Nom's avatar
Penom Nom committed
36 37
        self.species = species
        self.type = type
Penom Nom's avatar
Penom Nom committed
38
        self.id = id
Penom Nom's avatar
Penom Nom committed
39
        
40 41
        if isinstance(reads1, str) :
            self.reads1 = [reads1]
Penom Nom's avatar
Penom Nom committed
42
        
43 44
        if isinstance(reads2, str) :
            self.reads2 = [reads2]
45
            
46 47
        if isinstance(readsi, str) :
            self.readsi = [readsi]
Penom Nom's avatar
Penom Nom committed
48 49 50 51 52 53 54
        
        if self.type is None:
            if self.reads2 :
                self.type = self.AVAILABLE_TYPES[0]
            else :
                self.type = self.AVAILABLE_TYPES[1]
        
Penom Nom's avatar
Penom Nom committed
55 56
        if self.nb_sequences and isinstance(self.nb_sequences, str) :
            self.nb_sequences = int(self.nb_sequences)
Penom Nom's avatar
Penom Nom committed
57
        
58 59 60 61 62 63 64 65 66 67
        self.metadata = {}
        
    def add_metadata(self, key, value):
        self.metadata[key] = value
    
    def get_metadata(self, key):
        if self.has_metadata(key):
            return self.metadata[key]

    def has_metadata(self, key):
Penom Nom's avatar
Penom Nom committed
68
        return key in self.metadata
69
    
Penom Nom's avatar
Penom Nom committed
70 71 72 73 74
    def get_all_reads(self):
        allreads = self.reads1
        if self.reads2 :
            allreads += self.reads2
        return allreads
Penom Nom's avatar
Penom Nom committed
75

Penom Nom's avatar
Penom Nom committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    def set_nb_sequences(self, nb_seqs):
        """
        Sets the number of sequences to nb_seqs
          @param nb_seqs : the sample number of sequences
        """
        if self.id != None:
            t3mysql = t3MySQLdb()
            t3mysql.update_sample_info(self.id, nb_sequences=nb_seqs)
            self.nb_sequences = nb_seqs
        else :
            raise UnsavedRunError()
    
    def set_full_size(self, full_size):
        """
        Sets the number of nucleotides in all sequences
          @param full_size : the number of nucleotides
        """
        if self.id != None :
            t3mysql = t3MySQLdb()
            t3mysql.update_sample_info(self.id, full_seq_size=full_size)
            self.full_size = full_size
        else :
Penom Nom's avatar
Penom Nom committed
98 99 100
            raise UnsavedRunError()
    
    def __str__(self, *args, **kwargs):
101
        return "sid={sid}; name={name}; desc={desc}; r1={r1}; r2={r2}; i={i}; insize={insize}; nbs={nbs}; fsize={fsize}; spec={spec}; t={t}".format(
102 103 104 105 106
        sid = self.sample_id or '',
        name = self.name or '',
        desc = self.description or '',
        r1 = self.reads1 or [],
        r2 = self.reads2 or [],
107
        i = self.readsi or [],
108 109 110 111 112
        insize = self.insert_size or '',
        nbs = self.nb_sequences or '',
        fsize = self.full_size or '',
        spec = self.species or '',
        t = self.type  or '')