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