1 | package nl.tno.metagenomics |
---|
2 | |
---|
3 | /** |
---|
4 | * Represents a samples that is used in an assay. |
---|
5 | * |
---|
6 | * @author Robert Horlings (robert@isdat.nl) |
---|
7 | * |
---|
8 | */ |
---|
9 | class AssaySample { |
---|
10 | // To be computed in run time |
---|
11 | private long _numSequences = -1; |
---|
12 | private float _averageQuality = -1.0; |
---|
13 | |
---|
14 | Integer numUniqueSequences // Number of unique sequences / OTUs. Is only available after preprocessing |
---|
15 | |
---|
16 | String oligoNumber // Oligonumber used to identify the sample |
---|
17 | String tagSequence // Tag originally used to identify the sample |
---|
18 | |
---|
19 | static belongsTo = [ assay: Assay, sample: Sample ] |
---|
20 | static hasMany = [ sequenceData: SequenceData ] |
---|
21 | |
---|
22 | static constraints = { |
---|
23 | numUniqueSequences(nullable: true) |
---|
24 | oligoNumber(nullable: true) |
---|
25 | tagSequence(nullable: true) |
---|
26 | } |
---|
27 | |
---|
28 | static mapping = { |
---|
29 | columns { |
---|
30 | numSequences index:'numsequences_idx' |
---|
31 | } |
---|
32 | } |
---|
33 | |
---|
34 | /** |
---|
35 | * Returns the number of files in the system, belonging to this |
---|
36 | * assay-sample combination. |
---|
37 | * |
---|
38 | * @return |
---|
39 | */ |
---|
40 | public int numFiles() { |
---|
41 | if( !sequenceData ) |
---|
42 | return 0 |
---|
43 | |
---|
44 | int numFiles = 0; |
---|
45 | sequenceData.each { numFiles += it.numFiles() } |
---|
46 | |
---|
47 | return numFiles; |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Returns the number of sequence files in the system, belonging to this |
---|
52 | * assay-sample combination. |
---|
53 | * |
---|
54 | * @return |
---|
55 | */ |
---|
56 | public int numSequenceFiles() { |
---|
57 | if( !sequenceData ) |
---|
58 | return 0 |
---|
59 | |
---|
60 | int numFiles = 0; |
---|
61 | sequenceData.each { |
---|
62 | if( it.sequenceFile ) |
---|
63 | numFiles++ |
---|
64 | } |
---|
65 | |
---|
66 | return numFiles; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Returns the number of quality files in the system, belonging to this |
---|
71 | * assay-sample combination. |
---|
72 | * |
---|
73 | * @return |
---|
74 | */ |
---|
75 | public int numQualityFiles() { |
---|
76 | if( !sequenceData ) |
---|
77 | return 0 |
---|
78 | |
---|
79 | int numFiles = 0; |
---|
80 | sequenceData.each { |
---|
81 | if( it.qualityFile ) |
---|
82 | numFiles++ |
---|
83 | } |
---|
84 | |
---|
85 | return numFiles; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Returns the number of sequences in the files on the system, belonging to this |
---|
90 | * assay-sample combination. |
---|
91 | * |
---|
92 | * @return |
---|
93 | */ |
---|
94 | public long numSequences() { |
---|
95 | if( _numSequences > -1 ) |
---|
96 | return _numSequences; |
---|
97 | |
---|
98 | if( !sequenceData ) |
---|
99 | return 0 |
---|
100 | |
---|
101 | long numSequences = 0; |
---|
102 | sequenceData.each { numSequences += it.numSequences } |
---|
103 | |
---|
104 | // Save as cache |
---|
105 | _numSequences = numSequences; |
---|
106 | |
---|
107 | return numSequences; |
---|
108 | } |
---|
109 | |
---|
110 | /** |
---|
111 | * Returns the average quality of the sequences in the files on the system, |
---|
112 | * belonging to this assay-sample combination. |
---|
113 | * |
---|
114 | * @return |
---|
115 | */ |
---|
116 | public float averageQuality() { |
---|
117 | if( _averageQuality > -1 ) |
---|
118 | return _averageQuality; |
---|
119 | |
---|
120 | if( !sequenceData ) |
---|
121 | return 0.0 |
---|
122 | |
---|
123 | int numSequences = 0; |
---|
124 | float averageQuality = 0.0; |
---|
125 | |
---|
126 | sequenceData.each { |
---|
127 | numSequences += it.numSequences |
---|
128 | averageQuality = averageQuality + ( it.averageQuality - averageQuality ) / numSequences * it.numSequences; |
---|
129 | } |
---|
130 | |
---|
131 | // Save as cache |
---|
132 | _averageQuality = averageQuality; |
---|
133 | |
---|
134 | return averageQuality; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * Reset the statistics to their default value, in order to ensure that the values are recomputed next time. |
---|
139 | */ |
---|
140 | public void resetStats() { |
---|
141 | _numSequences = -1; |
---|
142 | _averageQuality = -1; |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Check whether this assay-sample combination contains information that should be saved in trash on a delete |
---|
147 | * @return |
---|
148 | */ |
---|
149 | public boolean containsData() { |
---|
150 | return tagSequence || oligoNumber || numFiles() > 0; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * Move information that should be kept on delete to another assaySample object. |
---|
155 | * |
---|
156 | * N.B. The sequencedata objects are really moved, so removed from the original object! |
---|
157 | * |
---|
158 | * @param otherAssaySample Object to move |
---|
159 | */ |
---|
160 | public void moveValuableDataTo( AssaySample otherAssaySample ) { |
---|
161 | // Copy properties |
---|
162 | otherAssaySample.tagSequence = tagSequence; |
---|
163 | otherAssaySample.oligoNumber = oligoNumber; |
---|
164 | |
---|
165 | // Move attached data |
---|
166 | def dataList = sequenceData?.toList() |
---|
167 | def otherAssay = otherAssaySample.assay; |
---|
168 | |
---|
169 | if( dataList && dataList.size() > 0 ) { |
---|
170 | for( def j = dataList.size() - 1; j >= 0; j-- ) { |
---|
171 | // Check whether the run of this sequenceData object is also connected to the assay |
---|
172 | def run = dataList[j].run; |
---|
173 | |
---|
174 | if( !otherAssay.runs || !otherAssay.runs.contains( run ) ) { |
---|
175 | otherAssay.addToRuns( run ); |
---|
176 | } |
---|
177 | |
---|
178 | // Copy data |
---|
179 | dataList[j].sample = otherAssaySample; |
---|
180 | this.removeFromSequenceData( dataList[j] ); |
---|
181 | otherAssaySample.addToSequenceData( dataList[j] ); |
---|
182 | dataList[j].save(); |
---|
183 | } |
---|
184 | } |
---|
185 | } |
---|
186 | } |
---|