1 /**
2 * UGENE - Integrated Bioinformatics Tools.
3 * Copyright (C) 2008-2021 UniPro <ugene@unipro.ru>
4 * http://ugene.net
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22 #include "StreamSequenceWriter.h"
23
24 #include <U2Core/AppContext.h>
25 #include <U2Core/DocumentModel.h>
26 #include <U2Core/IOAdapter.h>
27 #include <U2Core/U2OpStatusUtils.h>
28 #include <U2Core/U2SafePoints.h>
29
30 #include <U2Formats/FastaFormat.h>
31
32 namespace U2 {
33
StreamShortReadsWriter(const GUrl & url,const QString & refName,int refLength)34 StreamShortReadsWriter::StreamShortReadsWriter(const GUrl &url, const QString &refName, int refLength)
35 : numSeqWritten(0), refSeqLength(refLength) {
36 refSeqName = QString(refName).replace(QRegExp("\\s|\\t"), "_").toLatin1();
37
38 IOAdapterFactory *iof = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::LOCAL_FILE);
39 io = iof->createIOAdapter();
40 bool res = io->open(url, IOAdapterMode_Write);
41 assert(res == true);
42 Q_UNUSED(res);
43 }
44
writeNextAlignedRead(int offset,const DNASequence & seq)45 bool StreamShortReadsWriter::writeNextAlignedRead(int offset, const DNASequence &seq) {
46 bool writeOk = format.storeAlignedRead(offset, seq, io, refSeqName, refSeqLength, numSeqWritten == 0);
47 if (writeOk) {
48 ++numSeqWritten;
49 return true;
50 }
51
52 return false;
53 }
54
close()55 void StreamShortReadsWriter::close() {
56 io->close();
57 }
58
~StreamShortReadsWriter()59 StreamShortReadsWriter::~StreamShortReadsWriter() {
60 delete io;
61 }
62
StreamShortReadWriter()63 StreamShortReadWriter::StreamShortReadWriter() {
64 IOAdapterFactory *iof = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::LOCAL_FILE);
65 io = iof->createIOAdapter();
66 }
67
~StreamShortReadWriter()68 StreamShortReadWriter::~StreamShortReadWriter() {
69 close();
70 delete io;
71 }
72
init(const GUrl & url)73 bool StreamShortReadWriter::init(const GUrl &url) {
74 outputPath = url;
75 bool res = io->open(url, IOAdapterMode_Write);
76 return res;
77 }
78
writeNextSequence(const DNASequence & seq)79 bool StreamShortReadWriter::writeNextSequence(const DNASequence &seq) {
80 U2OpStatus2Log os;
81 FastaFormat::storeSequence(seq, io, os);
82
83 return !os.hasError();
84 }
85
writeNextSequence(const U2SequenceObject * seq)86 bool StreamShortReadWriter::writeNextSequence(const U2SequenceObject *seq) {
87 U2OpStatus2Log os;
88 FastaFormat::storeSequence(seq, io, os);
89
90 return !os.hasError();
91 }
92
close()93 void StreamShortReadWriter::close() {
94 CHECK(io->isOpen(), );
95 io->close();
96 }
97
StreamGzippedShortReadWriter()98 StreamGzippedShortReadWriter::StreamGzippedShortReadWriter()
99 : StreamShortReadWriter() {
100 delete io;
101 io = nullptr;
102
103 IOAdapterFactory *iof = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::GZIPPED_LOCAL_FILE);
104 io = iof->createIOAdapter();
105 }
106
107 } // namespace U2
108