-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRefNtSequenceModel.java
More file actions
575 lines (480 loc) · 13.2 KB
/
RefNtSequenceModel.java
File metadata and controls
575 lines (480 loc) · 13.2 KB
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*
* Copyright (c) 2014 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.sequenceanalysis;
import htsjdk.samtools.util.StringUtil;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.DbSchema;
import org.labkey.api.data.DbSchemaType;
import org.labkey.api.data.Table;
import org.labkey.api.data.TableInfo;
import org.labkey.api.data.TableSelector;
import org.labkey.api.data.Transient;
import org.labkey.api.exp.api.DataType;
import org.labkey.api.exp.api.ExpData;
import org.labkey.api.exp.api.ExperimentService;
import org.labkey.api.files.FileContentService;
import org.labkey.api.security.Crypt;
import org.labkey.api.security.User;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.MemTracker;
import org.labkey.api.util.logging.LogHelper;
import org.labkey.api.writer.PrintWriters;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.Writer;
import java.util.Arrays;
import java.util.Date;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
* Represents a record in sequenceanalysis.ref_nt_sequences. Contains methods to assist with accessing/saving the sequence data,
* which is stored in a gzipped text file on the filesystem.
*/
public class RefNtSequenceModel implements Serializable
{
private static final Logger _log = LogHelper.getLogger(RefNtSequenceModel.class, "Messages related to Reference NT Sequences");
public static String BASE_DIRNAME = ".sequences";
private int _rowid;
private String _name;
//deprecated
private String _sequence;
private Long _sequenceFile;
private Long _jobId;
private String _category;
private String _subset;
private String _locus;
private String _lineage;
private String _mol_type;
private String _genbank;
private String _refSeqId;
private String _ipd_accession;
private String _species;
private String _geographic_origin;
private String _status;
private String _aliases;
private String _comments;
private Integer _seqLength;
private Date _datedisabled;
private String _disabledby;
private String _container;
private Integer _createdby;
private Date _created;
private Integer _modifiedby;
private Date _modified;
private byte[] _sequenceBytes = null;
public RefNtSequenceModel()
{
MemTracker.getInstance().put(this);
}
public static RefNtSequenceModel getForRowId(int rowId)
{
return new TableSelector(DbSchema.get("sequenceanalysis", DbSchemaType.Module).getTable("ref_nt_sequences")).getObject(rowId, RefNtSequenceModel.class);
}
public int getRowid()
{
return _rowid;
}
public void setRowid(int rowid)
{
_rowid = rowid;
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
@Nullable @Transient
public InputStream getSequenceInputStream() throws IOException
{
if (_sequenceFile != null)
{
ExpData d = ExperimentService.get().getExpData(_sequenceFile);
if (d == null || !d.getFile().exists())
{
_log.error("unable to find sequence file for Id: " + getRowid());
return null;
}
return new GZIPInputStream(new FileInputStream(d.getFile()));
}
return null;
}
public boolean hasSequenceFile()
{
if (_sequenceFile != null)
{
ExpData d = ExperimentService.get().getExpData(_sequenceFile);
return d != null && d.getFile().exists();
}
return false;
}
public String getLegacySequence()
{
return _sequence;
}
@Transient
public String getSequence()
{
//resolve from cached file
if (_sequenceBytes == null && _sequenceFile != null)
{
//note: this will cache result
getSequenceBases();
}
return _sequenceBytes == null ? null : StringUtil.bytesToString(_sequenceBytes);
}
@Deprecated
public void setSequence(String sequence)
{
_sequence = sequence;
}
public Long getSequenceFile()
{
return _sequenceFile;
}
public void setSequenceFile(Long sequenceFile)
{
_sequenceFile = sequenceFile;
}
public String getCategory()
{
return _category;
}
public void setCategory(String category)
{
_category = category;
}
public String getSubset()
{
return _subset;
}
public void setSubset(String subset)
{
_subset = subset;
}
public String getLocus()
{
return _locus;
}
public void setLocus(String locus)
{
_locus = locus;
}
public String getLineage()
{
return _lineage;
}
public void setLineage(String lineage)
{
_lineage = lineage;
}
public String getMol_type()
{
return _mol_type;
}
public void setMol_type(String mol_type)
{
_mol_type = mol_type;
}
public String getGenbank()
{
return _genbank;
}
public void setGenbank(String genbank)
{
_genbank = genbank;
}
public String getRefSeqId()
{
return _refSeqId;
}
public void setRefSeqId(String refSeqId)
{
_refSeqId = refSeqId;
}
public String getIpd_accession()
{
return _ipd_accession;
}
public void setIpd_accession(String ipd_accession)
{
_ipd_accession = ipd_accession;
}
public String getSpecies()
{
return _species;
}
public void setSpecies(String species)
{
_species = species;
}
public String getGeographic_origin()
{
return _geographic_origin;
}
public void setGeographic_origin(String geographic_origin)
{
_geographic_origin = geographic_origin;
}
public String getStatus()
{
return _status;
}
public void setStatus(String status)
{
_status = status;
}
public String getAliases()
{
return _aliases;
}
public void setAliases(String aliases)
{
_aliases = aliases;
}
public String getComments()
{
return _comments;
}
public void setComments(String comments)
{
_comments = comments;
}
public String getContainer()
{
return _container;
}
public void setContainer(String container)
{
_container = container;
}
public Integer getCreatedby()
{
return _createdby;
}
public void setCreatedby(Integer createdby)
{
_createdby = createdby;
}
public Date getCreated()
{
return _created;
}
public void setCreated(Date created)
{
_created = created;
}
public Integer getModifiedby()
{
return _modifiedby;
}
public void setModifiedby(Integer modifiedby)
{
_modifiedby = modifiedby;
}
public Date getModified()
{
return _modified;
}
public void setModified(Date modified)
{
_modified = modified;
}
public Long getJobId()
{
return _jobId;
}
public void setJobId(Long jobId)
{
_jobId = jobId;
}
public Date getDatedisabled()
{
return _datedisabled;
}
public void setDatedisabled(Date datedisabled)
{
_datedisabled = datedisabled;
}
public String getDisabledby()
{
return _disabledby;
}
public void setDisabledby(String disabledby)
{
_disabledby = disabledby;
}
@Transient
public byte[] getSequenceBases()
{
if (_sequenceBytes == null)
{
try (InputStream is = getSequenceInputStream())
{
if (is != null)
{
_sequenceBytes = IOUtils.toByteArray(is);
}
}
catch (IOException e)
{
_log.error("Unable to read sequence for sequence Id: " + getRowid() + ", " + e.getMessage());
}
}
return _sequenceBytes;
}
public void createFileForSequence(User u, String sequence, @Nullable File outDir) throws IOException
{
File output = getExpectedSequenceFile();
if (output.exists())
{
output.delete();
}
try (PrintWriter writer = PrintWriters.getPrintWriter(new GZIPOutputStream(new FileOutputStream(output))))
{
writer.write(sequence);
}
Container c = getLabKeyContainer();
ExpData d = ExperimentService.get().createData(c, new DataType("Sequence Data"));
d.setName(output.getName());
d.setDataFileURI(output.toURI());
d.save(u);
setSeqLength(sequence.length());
setSequenceFile(d.getRowId());
TableInfo ti = DbSchema.get("sequenceanalysis", DbSchemaType.Module).getTable("ref_nt_sequences");
Table.update(u, ti, this, _rowid);
}
public File getExpectedSequenceFile() throws IllegalArgumentException
{
return FileUtil.appendName(getHashedDir(true), _rowid + ".txt.gz");
}
private Container getLabKeyContainer()
{
Container c = ContainerManager.getForId(_container);
if (c == null)
{
throw new IllegalArgumentException("Unable to find container: " + _container);
}
return c;
}
private File getBaseSequenceDir() throws IllegalArgumentException
{
Container c = getLabKeyContainer();
FileContentService fileService = FileContentService.get();
File root = fileService == null ? null : fileService.getFileRoot(c, FileContentService.ContentType.files);
if (root == null)
{
throw new IllegalArgumentException("File root not defined for container: " + c.getPath());
}
return FileUtil.appendName(root, BASE_DIRNAME);
}
public void writeSequence(Writer writer, int lineLength, Integer start, Integer end) throws IOException
{
byte[] seq = getSequenceBases();
if (start != null || end != null)
{
if (start == null)
{
start = 0;
}
//always keep start 0-based
else if (start != 0)
{
start--;
}
if (end == null)
{
end = seq.length;
}
seq = Arrays.copyOfRange(seq, start, end);
}
if (seq != null)
{
int len = seq.length;
int count = 0;
for (int i = 0; i < len; i++)
{
if (count == lineLength)
{
writer.write('\n');
count = 0;
}
writer.write(seq[i]);
count++;
}
//always terminate w/ a newline
if (lineLength != -1)
writer.write('\n');
}
else
{
_log.error("no sequence found for refId: " + getRowid());
}
}
public void clearCachedSequence()
{
_sequenceBytes = null;
}
public Integer getSeqLength()
{
return _seqLength;
}
public void setSeqLength(Integer seqLength)
{
_seqLength = seqLength;
}
@Nullable
public File getOffsetsFile()
{
if (getSequenceFile() == null)
{
return null;
}
ExpData d = ExperimentService.get().getExpData(_sequenceFile);
if (d == null || d.getFile() == null)
{
return null;
}
return FileUtil.appendName(d.getFile().getParentFile(), getRowid() + "_offsets.txt");
}
private File getHashedDir(boolean create)
{
File baseDir = getBaseSequenceDir();
String digest = Crypt.MD5.digest(String.valueOf(getRowid()));
baseDir = FileUtil.appendName(baseDir, digest.substring(0,4));
baseDir = FileUtil.appendName(baseDir, digest.substring(4,8));
baseDir = FileUtil.appendName(baseDir, digest.substring(8,12));
baseDir = FileUtil.appendName(baseDir, digest.substring(12,20));
baseDir = FileUtil.appendName(baseDir, digest.substring(20,28));
baseDir = FileUtil.appendName(baseDir, digest.substring(28,32));
if (create)
{
baseDir.mkdirs();
}
return baseDir;
}
}