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 "DocumentFormatRegistryImpl.h"
23 
24 #include <U2Core/AppContext.h>
25 #include <U2Core/DbiDocumentFormat.h>
26 #include <U2Core/RawDataUdrSchema.h>
27 #include <U2Core/U2OpStatusUtils.h>
28 #include <U2Core/U2SafePoints.h>
29 
30 #include <U2Formats/ABIFormat.h>
31 #include <U2Formats/ASNFormat.h>
32 #include <U2Formats/AceFormat.h>
33 #include <U2Formats/AceImporter.h>
34 #include <U2Formats/AprFormat.h>
35 #include <U2Formats/AprImporter.h>
36 #include <U2Formats/BedFormat.h>
37 #include <U2Formats/ClustalWAlnFormat.h>
38 #include <U2Formats/DatabaseConnectionFormat.h>
39 #include <U2Formats/DifferentialFormat.h>
40 #include <U2Formats/EMBLPlainTextFormat.h>
41 #include <U2Formats/FastaFormat.h>
42 #include <U2Formats/FastqFormat.h>
43 #include <U2Formats/FpkmTrackingFormat.h>
44 #include <U2Formats/GFFFormat.h>
45 #include <U2Formats/GTFFormat.h>
46 #include <U2Formats/GenbankPlainTextFormat.h>
47 #include <U2Formats/MSFFormat.h>
48 #include <U2Formats/MegaFormat.h>
49 #include <U2Formats/MysqlDbi.h>
50 #include <U2Formats/NEXUSFormat.h>
51 #include <U2Formats/NewickFormat.h>
52 #include <U2Formats/PDBFormat.h>
53 #include <U2Formats/PDWFormat.h>
54 #include <U2Formats/PhylipFormat.h>
55 #include <U2Formats/PlainTextFormat.h>
56 #include <U2Formats/RawDNASequenceFormat.h>
57 #include <U2Formats/SAMFormat.h>
58 #include <U2Formats/SCFFormat.h>
59 #include <U2Formats/SQLiteDbi.h>
60 #include <U2Formats/SimpleSNPVariationFormat.h>
61 #include <U2Formats/StockholmFormat.h>
62 #include <U2Formats/SwissProtPlainTextFormat.h>
63 #include <U2Formats/VCF4VariationFormat.h>
64 #include <U2Formats/VectorNtiSequenceFormat.h>
65 
66 namespace U2 {
67 
~DocumentFormatRegistryImpl()68 DocumentFormatRegistryImpl::~DocumentFormatRegistryImpl() {
69     qDeleteAll(formats);
70 }
71 
registerFormat(DocumentFormat * f)72 bool DocumentFormatRegistryImpl::registerFormat(DocumentFormat *f) {
73     SAFE_POINT(getFormatById(f->getFormatId()) == nullptr, "Existing format", false);
74     formats.push_back(f);
75     emit si_documentFormatRegistered(f);
76     if (f->getFormatDescription().isEmpty()) {
77         coreLog.trace("Warn! Format has no description: " + f->getFormatName());
78     }
79     return true;
80 }
81 
getRegisteredFormats() const82 QList<DocumentFormatId> DocumentFormatRegistryImpl::getRegisteredFormats() const {
83     QList<DocumentFormatId> ids;
84     foreach (DocumentFormat *df, formats) {
85         ids.append(df->getFormatId());
86     }
87     return ids;
88 }
89 
selectFormatByFileExtension(const QString & fileExt) const90 DocumentFormat *DocumentFormatRegistryImpl::selectFormatByFileExtension(const QString &fileExt) const {
91     foreach (DocumentFormat *df, formats) {
92         if (df->getSupportedDocumentFileExtensions().contains(fileExt)) {
93             return df;
94         }
95     }
96     return nullptr;
97 }
98 
selectFormats(const DocumentFormatConstraints & c) const99 QList<DocumentFormatId> DocumentFormatRegistryImpl::selectFormats(const DocumentFormatConstraints &c) const {
100     QList<DocumentFormatId> ids;
101     foreach (DocumentFormat *df, formats) {
102         if (df->checkConstraints(c)) {
103             ids.append(df->getFormatId());
104         }
105     }
106     return ids;
107 }
108 
unregisterFormat(DocumentFormat * f)109 bool DocumentFormatRegistryImpl::unregisterFormat(DocumentFormat *f) {
110     int n = formats.removeAll(f);
111     bool res = n > 0;
112     if (res) {
113         emit si_documentFormatUnregistered(f);
114     }
115     return res;
116 }
117 
getFormatById(DocumentFormatId id) const118 DocumentFormat *DocumentFormatRegistryImpl::getFormatById(DocumentFormatId id) const {
119     foreach (DocumentFormat *f, formats) {
120         if (BaseDocumentFormats::equal(f->getFormatId(), id)) {
121             return f;
122         }
123     }
124     return nullptr;
125 }
126 
init()127 void DocumentFormatRegistryImpl::init() {
128     U2OpStatusImpl os;
129     RawDataUdrSchema::init(os);
130     SAFE_POINT_OP(os, );
131 
132     PlainTextFormat *text = new PlainTextFormat(this);
133     registerFormat(text);
134 
135     FastaFormat *fasta = new FastaFormat(this);
136     registerFormat(fasta);
137 
138     GenbankPlainTextFormat *gb = new GenbankPlainTextFormat(this);
139     registerFormat(gb);
140 
141     EMBLPlainTextFormat *em = new EMBLPlainTextFormat(this);
142     registerFormat(em);
143 
144     SwissProtPlainTextFormat *sp = new SwissProtPlainTextFormat(this);
145     registerFormat(sp);
146 
147     ABIFormat *abi = new ABIFormat(this);
148     registerFormat(abi);
149 
150     SCFFormat *scf = new SCFFormat(this);
151     registerFormat(scf);
152 
153     RawDNASequenceFormat *rsf = new RawDNASequenceFormat(this);
154     registerFormat(rsf);
155 
156     ClustalWAlnFormat *aln = new ClustalWAlnFormat(this);
157     registerFormat(aln);
158 
159     StockholmFormat *stf = new StockholmFormat(this);
160     registerFormat(stf);
161 
162     NewickFormat *nwf = new NewickFormat(this);
163     registerFormat(nwf);
164 
165     PDBFormat *pdb = new PDBFormat(this);
166     registerFormat(pdb);
167 
168     FastqFormat *ftq = new FastqFormat(this);
169     registerFormat(ftq);
170 
171     ASNFormat *asn = new ASNFormat(this);
172     registerFormat(asn);
173 
174     MSFFormat *msf = new MSFFormat(this);
175     registerFormat(msf);
176 
177     BedFormat *bed = new BedFormat(this);
178     registerFormat(bed);
179 
180     GFFFormat *gff = new GFFFormat(this);
181     registerFormat(gff);
182 
183     GTFFormat *gtf = new GTFFormat(this);
184     registerFormat(gtf);
185 
186     FpkmTrackingFormat *fpkmTr = new FpkmTrackingFormat(this);
187     registerFormat(fpkmTr);
188 
189     NEXUSFormat *nexus = new NEXUSFormat(this);
190     registerFormat(nexus);
191 
192     SAMFormat *sam = new SAMFormat(this);
193     registerFormat(sam);
194 
195     MegaFormat *meg = new MegaFormat(this);
196     registerFormat(meg);
197 
198     ACEFormat *aceFormat = new ACEFormat(this);
199     registerFormat(aceFormat);
200 
201     AprFormat *apr = new AprFormat(this);
202     registerFormat(apr);
203 
204     AceImporter *aceImporter = new AceImporter();
205     importSupport.addDocumentImporter(aceImporter);
206 
207     AprImporter *aprImporter = new AprImporter();
208     importSupport.addDocumentImporter(aprImporter);
209 
210     PDWFormat *pdw = new PDWFormat(this);
211     registerFormat(pdw);
212 
213     SimpleSNPVariationFormat *snp = new SimpleSNPVariationFormat(this);
214     registerFormat(snp);
215 
216     VCF4VariationFormat *vcf4 = new VCF4VariationFormat(this);
217     registerFormat(vcf4);
218 
219     DifferentialFormat *diff = new DifferentialFormat(this);
220     registerFormat(diff);
221 
222     PhylipInterleavedFormat *phIn = new PhylipInterleavedFormat(this);
223     registerFormat(phIn);
224 
225     PhylipSequentialFormat *phSeq = new PhylipSequentialFormat(this);
226     registerFormat(phSeq);
227 
228     registerFormat(new DatabaseConnectionFormat(this));
229     registerFormat(new VectorNtiSequenceFormat(this));
230 
231     AppContext::getDbiRegistry()->registerDbiFactory(new SQLiteDbiFactory());
232     AppContext::getDbiRegistry()->registerDbiFactory(new MysqlDbiFactory());
233 
234     DocumentFormatFlags flags(DocumentFormatFlag_SupportWriting | DocumentFormatFlag_CannotBeCompressed);
235     DbiDocumentFormat *sdbi = new DbiDocumentFormat(SQLiteDbiFactory::ID, BaseDocumentFormats::UGENEDB, tr("UGENE Database"), QStringList() << "ugenedb", flags);
236     registerFormat(sdbi);
237 }
238 
239 }  // namespace U2
240