-
Notifications
You must be signed in to change notification settings - Fork 1
/
Module1.bas
434 lines (303 loc) · 13 KB
/
Module1.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
'***************************************
'************** MODULE 1 *************
'***************************************
'
' This module contains functions (return something) and subroutines (do something on the sheet)
'
' - 6 functions: FindMatchingValues, SortArray, IsStringInArray, Count_Libraries, ProtocolStatus, SCProtocolStatus
' - 3 formatting subroutines: Warning, Fatal, ClearFormatting
' - 4 subroutines: ExperimentStatus, AnnotationStatus, BiologicalStatus, RNASeqTags
'
'***************************************
Public Sub Warning(cell As range)
' Fill the cell in yellow
cell.Interior.Color = RGB(255, 255, 155)
' Fill the first col in yellow
Cells(cell.row, 1).Interior.Color = RGB(255, 255, 155)
' Find header and put it in first column
Dim header As String
header = Cells(1, cell.Column).Value
If InStr(1, Cells(cell.row, 1).Value, header, vbTextCompare) = 0 Then
Cells(cell.row, 1).Value = Cells(cell.row, 1).Value + header + ", "
End If
End Sub
Public Sub Fatal(cell As range)
' Fill the cell in red
cell.Interior.Color = RGB(255, 0, 0)
' Change text to red
cell.Font.Color = RGB(255, 255, 255)
' Fill the first col in red
Cells(cell.row, 1).Interior.Color = RGB(255, 255, 155)
' Find header and put it in first column
Dim header As String
header = Cells(1, cell.Column).Value
If InStr(1, Cells(cell.row, 1).Value, header, vbTextCompare) = 0 Then
Cells(cell.row, 1).Value = Cells(cell.row, 1).Value + header + ", "
End If
End Sub
Public Sub ClearFormatting(cell As range)
' Remove color from cell
cell.Interior.Color = xlNone
' Find header and remove it from first column
Dim header As String
header = Cells(1, cell.Column).Value
Cells(cell.row, 1).Value = Replace(Cells(cell.row, 1).Value, header + ", ", "")
' If cell in first col is empty, remove its color
If Cells(cell.row, 1).Value = "" Then
Cells(cell.row, 1).Interior.Color = xlNone
End If
End Sub
Public Sub ExperimentStatus(cell As range)
Dim lastRow As Long
Dim arr As Variant
' Find the last row of possible terms
lastRow = Worksheets("settings").Cells(Rows.count, 1).End(xlUp).row
arr = Application.Transpose(Worksheets("settings").range("A2:A" & lastRow).Value)
If IsStringInArray(cell.Value, arr) Then
cell.Validation.Delete
Else
' Fill the cell with dropdown
With cell.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(arr, ",")
.InCellDropdown = True
.ShowInput = True
.ShowError = False
End With
End If
End Sub
Public Sub AnnotationStatus(cell As range)
Dim lastRow As Long
Dim arr As Variant
' Find the last row of possible terms
lastRow = Worksheets("settings").Cells(Rows.count, 2).End(xlUp).row
arr = Application.Transpose(Worksheets("settings").range("B2:B" & lastRow).Value)
If IsStringInArray(cell.Value, arr) Then
ClearFormatting cell
cell.Validation.Delete
Else
Warning cell
' Fill the cell with dropdown
With cell.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(arr, ",")
.InCellDropdown = True
.ShowInput = True
.ShowError = False
End With
End If
End Sub
Public Sub BiologicalStatus(cell As range)
Dim lastRow As Long
Dim arr As Variant
' Find the last row of possible terms
lastRow = Worksheets("settings").Cells(Rows.count, 3).End(xlUp).row
arr = Application.Transpose(Worksheets("settings").range("C2:C" & lastRow).Value)
If IsStringInArray(cell.Value, arr) Then
ClearFormatting cell
cell.Validation.Delete
Else
Warning cell
' Fill the cell with dropdown
With cell.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(arr, ",")
.InCellDropdown = True
.ShowInput = True
.ShowError = False
End With
End If
End Sub
Public Sub RNAseqTags(cell As range)
Dim lastRow As Long
Dim arr As Variant
' Find the last row of possible terms
lastRow = Worksheets("settings").Cells(Rows.count, 4).End(xlUp).row
arr = Application.Transpose(Worksheets("settings").range("D2:D" & lastRow).Value)
If IsStringInArray(cell.Value, arr) Then
ClearFormatting cell
cell.Validation.Delete
Else
Warning cell
' Fill the cell with dropdown
With cell.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=Join(arr, ",")
.InCellDropdown = True
.ShowInput = True
.ShowError = False
End With
End If
End Sub
Function ProtocolStatus(protocol As String, protocolType As String, RNASelection As String, dbsheet As Worksheet) As Variant
Dim dbLastRow As Long
Dim rowCount As Long
Dim searchResults As Variant
Dim checkedProtocols() As Variant
Dim protocol_values() As Variant
Dim type_values() As Variant
Dim RNASel_values() As Variant
Dim i As Long
' Get the last row number in the "protocols-db" sheet
dbLastRow = dbsheet.Cells(dbsheet.Rows.count, "A").End(xlUp).row
' Fill the data arrays with the 3 columns of interest
protocol_values = Application.Transpose(dbsheet.range("A2:A" & dbLastRow))
type_values = Application.Transpose(dbsheet.range("D2:D" & dbLastRow))
RNASel_values = Application.Transpose(dbsheet.range("C2:C" & dbLastRow))
' Loop through each row in dbsheet and check the conditions
rowCount = 0
ReDim searchResults(1 To 3, 0 To 0)
For i = 1 To UBound(protocol_values)
If InStr(1, protocol_values(i), protocol, vbTextCompare) > 0 _
And (Not IsStringInArray(protocol, checkedProtocols)) _
And InStr(1, type_values(i), protocolType, vbTextCompare) > 0 _
And InStr(1, RNASel_values(i), RNASelection, vbTextCompare) > 0 Then
' Add the matching values from columns A and B to the array
rowCount = rowCount + 1
ReDim Preserve checkedProtocols(1 To rowCount)
checkedProtocols(rowCount) = protocol_values(i)
ReDim Preserve searchResults(1 To 3, 1 To rowCount)
searchResults(1, rowCount) = protocol_values(i)
searchResults(2, rowCount) = type_values(i)
searchResults(3, rowCount) = RNASel_values(i)
End If
Next i
ProtocolStatus = searchResults
End Function
Function SCProtocolStatus(protocol As String, protocolType As String, dbsheet As Worksheet) As Variant
Dim dbLastRow As Long
Dim rowCount As Long
Dim searchResults As Variant
Dim checkedProtocols() As Variant
Dim protocol_values() As Variant
Dim type_values() As Variant
Dim i As Long
' Get the last row number in the "protocols-db" sheet
dbLastRow = dbsheet.Cells(dbsheet.Rows.count, "A").End(xlUp).row
' Fill the data arrays with the 2 columns of interest
protocol_values = Application.Transpose(dbsheet.range("A2:A" & dbLastRow))
type_values = Application.Transpose(dbsheet.range("B2:B" & dbLastRow))
' Loop through each row in dbsheet and check the conditions
rowCount = 0
ReDim searchResults(1 To 2, 0 To 0)
For i = 1 To UBound(protocol_values)
If InStr(1, protocol_values(i), protocol, vbTextCompare) > 0 _
And (Not IsStringInArray(protocol, checkedProtocols)) _
And InStr(1, type_values(i), protocolType, vbTextCompare) > 0 Then
' Add the matching values from columns A and B to the array
rowCount = rowCount + 1
ReDim Preserve checkedProtocols(1 To rowCount)
checkedProtocols(rowCount) = protocol_values(i)
ReDim Preserve searchResults(1 To 2, 1 To rowCount)
searchResults(1, rowCount) = protocol_values(i)
searchResults(2, rowCount) = type_values(i)
End If
Next i
SCProtocolStatus = searchResults
End Function
Function SortArray(arr As Variant, ByVal sortRowIndex As Long) As Variant
Dim numCols As Long
Dim numRows As Long
Dim i As Long, j As Long, k As Long
Dim temp As String
' Get the number of columns/rows in the array
numCols = UBound(arr, 2)
numRows = UBound(arr, 1)
' Perform the sorting using Bubble Sort
For i = 1 To numCols - 1
For j = 1 To numCols - 1
If Len(arr(sortRowIndex, j)) > Len(arr(sortRowIndex, j + 1)) Then
' Swap columns directly (for all rows)
For k = 1 To numRows
temp = arr(k, j)
arr(k, j) = arr(k, j + 1)
arr(k, j + 1) = temp
Next k
End If
Next j
Next i
If UBound(arr, 2) > 15 Then
ReDim Preserve arr(1 To 2, 1 To 15)
End If
SortArray = arr
End Function
Function FindMatchingValues(Id As String, Term As String, Species As String, RefSheet As Worksheet) As Variant
Dim lastRowRefSheet As Long
Dim rowCount As Long
Dim matchingValuesArray As Variant
Dim ID_col() As Variant
Dim Term_col() As Variant
Dim Species_col() As Variant
Dim Sym_col() As Variant
Dim i As Long
' Get the last row number in the "organ-db" sheet
lastRowRefSheet = RefSheet.Cells(RefSheet.Rows.count, "A").End(xlUp).row
' Fill the data arrays with the 3 columns of interest
ID_col = Application.Transpose(RefSheet.range("A2:A" & lastRowRefSheet))
Term_col = Application.Transpose(RefSheet.range("B2:B" & lastRowRefSheet))
Species_col = Application.Transpose(RefSheet.range("C2:C" & lastRowRefSheet))
Sym_col = Application.Transpose(RefSheet.range("E2:E" & lastRowRefSheet))
' Loop through each row in RefSheet and check the conditions
rowCount = 0
ReDim matchingValuesArray(1 To 2, 0 To 0)
For i = 1 To UBound(ID_col)
If InStr(1, ID_col(i), Id, vbTextCompare) > 0 _
And (InStr(1, Term_col(i), Term, vbTextCompare) > 0 Or InStr(1, Sym_col(i), Term, vbTextCompare) > 0) _
And InStr(1, Species_col(i), Species, vbTextCompare) > 0 Then
' Add the matching values from columns A and B to the array
rowCount = rowCount + 1
ReDim Preserve matchingValuesArray(1 To 2, 1 To rowCount)
matchingValuesArray(1, rowCount) = ID_col(i)
matchingValuesArray(2, rowCount) = Term_col(i)
End If
Next i
FindMatchingValues = matchingValuesArray
End Function
Function IsStringInArray(searchString As String, searchArray As Variant) As Boolean
' Check if array is empty
If Len(Join(searchArray, "")) = 0 Then
IsStringInArray = False
Exit Function
End If
' If not empty, proceed to search
Dim ii As Long
For ii = LBound(searchArray) To UBound(searchArray)
If searchArray(ii) = searchString Then
IsStringInArray = True
Exit Function
End If
Next ii
' If not found, element is not in array
IsStringInArray = False
End Function
Function Count_Libraries(expID As String, expSheet As Worksheet, libSheet As Worksheet) As Long
' Declare variables
Dim counter As Long
Dim exp_expID_col As String
Dim lib_expID_col As String
Dim lib_libID_col As String
Dim lib_lastrow As Long
Dim exp_lastrow As Long
' Get values of interest (columns and last rows)
exp_expID_col = Split(expSheet.Cells(1, Application.Match("#experimentId", expSheet.Rows(1), 0)).address, "$")(1)
lib_expID_col = Split(libSheet.Cells(1, Application.Match("experimentId", libSheet.Rows(1), 0)).address, "$")(1)
lib_libID_col = Split(libSheet.Cells(1, Application.Match("#libraryId", libSheet.Rows(1), 0)).address, "$")(1)
lib_lastrow = libSheet.Cells(libSheet.Rows.count, lib_libID_col).End(xlUp).row
exp_lastrow = expSheet.Cells(expSheet.Rows.count, exp_expID_col).End(xlUp).row
' Declare 2 data arrays
Dim libIDs() As Variant
Dim expIDs() As Variant
' Save the data range in 2 arrays (library ID and experiment ID of library sheet)
libIDs = Application.Transpose(libSheet.range(lib_libID_col & "2:" & lib_libID_col & lib_lastrow).Value)
expIDs = Application.Transpose(libSheet.range(lib_expID_col & "2:" & lib_expID_col & lib_lastrow).Value)
' Initialize the counter
counter = 0
' Loop through the 2D array and check the conditions
For rowCounter = LBound(libIDs) To UBound(libIDs)
If (Not libIDs(rowCounter) Like "[#]*") And (expIDs(rowCounter) = expID) Then
' Both conditions are met, so increment the counter
counter = counter + 1
End If
Next rowCounter
Count_Libraries = counter
End Function