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 "PrepareInputFastaFilesTask.h"
23 
24 #include <QFileInfo>
25 
26 #include <U2Core/BaseDocumentFormats.h>
27 #include <U2Core/CopyFileTask.h>
28 #include <U2Core/DocumentUtils.h>
29 #include <U2Core/U2SafePoints.h>
30 
31 #include <U2Formats/ConvertFileTask.h>
32 
33 namespace U2 {
34 
PrepareInputFastaFilesTask(const QStringList & inputFiles,const QString & tempDir)35 PrepareInputFastaFilesTask::PrepareInputFastaFilesTask(const QStringList &inputFiles, const QString &tempDir)
36     : Task(tr("Prepare input FASTA files"), TaskFlags_NR_FOSE_COSC),
37       inputFiles(inputFiles),
38       tempDir(tempDir) {
39 }
40 
getFastaFiles() const41 QStringList PrepareInputFastaFilesTask::getFastaFiles() const {
42     return fastaFiles;
43 }
44 
getTempFiles() const45 QStringList PrepareInputFastaFilesTask::getTempFiles() const {
46     return tempFiles;
47 }
48 
prepare()49 void PrepareInputFastaFilesTask::prepare() {
50     foreach (const QString &filePath, inputFiles) {
51         const QString formatId = getBestFormatId(filePath);
52         CHECK_CONTINUE(!formatId.isEmpty());
53 
54         if (formatId != BaseDocumentFormats::FASTA) {
55             const QString targetFilePath = tempDir + "/" + getFixedFileName(filePath);
56             DefaultConvertFileTask *convertTask = new DefaultConvertFileTask(filePath, formatId, targetFilePath, BaseDocumentFormats::FASTA, tempDir);
57             addSubTask(convertTask);
58         } else if (!isFilePathAcceptable(filePath)) {
59             CopyFileTask *copyTask = new CopyFileTask(filePath, tempDir + "/" + getFixedFileName(filePath));
60             addSubTask(copyTask);
61         } else {
62             fastaFiles << filePath;
63         }
64     }
65 }
66 
onSubTaskFinished(Task * subTask)67 QList<Task *> PrepareInputFastaFilesTask::onSubTaskFinished(Task *subTask) {
68     QList<Task *> newSubTasks;
69     CHECK_OP(stateInfo, newSubTasks);
70 
71     DefaultConvertFileTask *convertTask = qobject_cast<DefaultConvertFileTask *>(subTask);
72     if (nullptr != convertTask) {
73         fastaFiles << convertTask->getResult();
74         tempFiles << convertTask->getResult();
75     }
76 
77     CopyFileTask *copyTask = qobject_cast<CopyFileTask *>(subTask);
78     if (nullptr != copyTask) {
79         fastaFiles << copyTask->getTargetFilePath();
80         tempFiles << copyTask->getTargetFilePath();
81     }
82 
83     return newSubTasks;
84 }
85 
getBestFormatId(const QString & filePath)86 QString PrepareInputFastaFilesTask::getBestFormatId(const QString &filePath) {
87     QList<FormatDetectionResult> formats = DocumentUtils::detectFormat(filePath);
88     if (formats.isEmpty()) {
89         stateInfo.addWarning(tr("File '%1' was skipped. Cannot detect the file format.").arg(filePath));
90         return "";
91     }
92     SAFE_POINT_EXT(nullptr != formats.first().format, setError("An incorrect format found. An importer?"), "");
93     return formats.first().format->getFormatId();
94 }
95 
isFilePathAcceptable(const QString & filePath) const96 bool PrepareInputFastaFilesTask::isFilePathAcceptable(const QString &filePath) const {
97     return !filePath.contains(" ");
98 }
99 
getFixedFileName(const QString & filePath) const100 QString PrepareInputFastaFilesTask::getFixedFileName(const QString &filePath) const {
101     QFileInfo fileInfo(filePath);
102     return fileInfo.fileName().replace(" ", "_");
103 }
104 
105 }    // namespace U2
106