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 "ExportSequencesTask.h"
23 
24 #include <QFile>
25 #include <QScopedPointer>
26 
27 #include <U2Core/AppContext.h>
28 #include <U2Core/DNAAlphabet.h>
29 #include <U2Core/DNASequenceObject.h>
30 #include <U2Core/DocumentSelection.h>
31 #include <U2Core/GUrlUtils.h>
32 #include <U2Core/IOAdapterUtils.h>
33 #include <U2Core/MSAUtils.h>
34 #include <U2Core/MultiTask.h>
35 #include <U2Core/ProjectModel.h>
36 #include <U2Core/SaveDocumentTask.h>
37 #include <U2Core/U2ObjectDbi.h>
38 #include <U2Core/U2SequenceUtils.h>
39 
40 #include <U2Formats/DocumentFormatUtils.h>
41 
42 #include <U2Gui/ObjectViewModel.h>
43 
44 namespace U2 {
45 
ExportSequencesTask(const MultipleSequenceAlignment & msa,const QSet<qint64> & rowIds,bool trimGaps,bool addToProjectFlag,const QString & dirUrl,const DocumentFormatId & format,const QString & extension,const QString & customFileName)46 ExportSequencesTask::ExportSequencesTask(const MultipleSequenceAlignment &msa, const QSet<qint64> &rowIds, bool trimGaps, bool addToProjectFlag, const QString &dirUrl, const DocumentFormatId &format, const QString &extension, const QString &customFileName)
47     : Task(tr("Export selected sequences from alignment"), TaskFlags_NR_FOSE_COSC),
48       addToProjectFlag(addToProjectFlag),
49       dirUrl(dirUrl),
50       format(format),
51       extension(extension),
52       customFileName(customFileName) {
53     sequences = MSAUtils::convertMsaToSequenceList(msa, stateInfo, trimGaps, rowIds);
54 }
55 
prepare()56 void ExportSequencesTask::prepare() {
57     QList<Task *> tasks;
58     QSet<QString> existingFilenames;
59     for (const DNASequence &sequence : qAsConst(sequences)) {
60         QString filename = GUrlUtils::fixFileName(customFileName.isEmpty() ? sequence.getName() : customFileName);
61         QString filePath = GUrlUtils::prepareFileLocation(dirUrl + "/" + filename + "." + extension, stateInfo);
62         CHECK_OP(stateInfo, );
63 
64         filePath = GUrlUtils::rollFileName(filePath, "_", existingFilenames);
65         existingFilenames.insert(filePath);
66         GUrl url(filePath);
67         IOAdapterFactory *iof = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(IOAdapterUtils::url2io(url));
68         DocumentFormat *df = AppContext::getDocumentFormatRegistry()->getFormatById(format);
69         SAFE_POINT(df != nullptr, "Cant get DocuemtFormat by given DocumentFormatId", );
70 
71         QScopedPointer<Document> doc(df->createNewLoadedDocument(iof, filePath, stateInfo));
72         CHECK_OP(stateInfo, );
73 
74         QVariantMap hints;
75         hints.insert(DocumentFormat::DBI_FOLDER_HINT, U2ObjectDbi::ROOT_FOLDER);
76         U2EntityRef ref = U2SequenceUtils::import(stateInfo, doc->getDbiRef(), U2ObjectDbi::ROOT_FOLDER, sequence, sequence.alphabet->getId());
77         CHECK_OP(stateInfo, );
78 
79         auto sequenceObject = new U2SequenceObject(sequence.getName(), ref);
80         doc->addObject(sequenceObject);
81 
82         Document *takenDoc = doc.take();
83         auto saveTask = new SaveDocumentTask(takenDoc, takenDoc->getIOAdapterFactory(), takenDoc->getURL());
84         saveTask->addFlag(addToProjectFlag ? SaveDoc_OpenAfter : SaveDoc_DestroyAfter);
85         addSubTask(saveTask);
86     }
87 }
88 
89 }  // namespace U2
90