~ubuntu-branches/debian/jessie/ugene/jessie

« back to all changes in this revision

Viewing changes to src/plugins/dna_export/src/DNAExportTask.cpp

  • Committer: Package Import Robot
  • Author(s): Steffen Moeller
  • Date: 2011-11-02 13:29:07 UTC
  • mfrom: (1.2.1) (3.1.11 natty)
  • Revision ID: package-import@ubuntu.com-20111102132907-o34gwnt0uj5g6hen
Tags: 1.9.8+repack-1
* First release to Debian
  - added README.Debian
  - increased policy version to 3.9.2
  - added URLs for version control system
* Added debug package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*****************************************************************
2
 
* Unipro UGENE - Integrated Bioinformatics Suite
3
 
* Copyright (C) 2008 Unipro, Russia (http://ugene.unipro.ru)
4
 
* All Rights Reserved
5
 
6
 
*     This source code is distributed under the terms of the
7
 
*     GNU General Public License. See the files COPYING and LICENSE
8
 
*     for details.
9
 
*****************************************************************/
10
 
 
11
 
#include "DNAExportTask.h"
12
 
 
13
 
#include <core_api/DNAAlphabet.h>
14
 
#include <core_api/DocumentModel.h>
15
 
#include <core_api/DocumentFormats.h>
16
 
#include <core_api/IOAdapter.h>
17
 
#include <core_api/AppContext.h>
18
 
#include <core_api/DNATranslation.h>
19
 
 
20
 
#include <util_text/TextUtils.h>
21
 
 
22
 
#include <gobjects/DNASequenceObject.h>
23
 
#include <gobjects/MAlignmentObject.h>
24
 
#include <memory>
25
 
 
26
 
namespace GB2 {
27
 
 
28
 
DNAExportSequenceTask::DNAExportSequenceTask(const DNAExportTaskSettings& s) 
29
 
: Task("", TaskFlag_DeleteWhenFinished), config(s)
30
 
{
31
 
    setTaskName(tr("Export sequence to '%1'").arg(QFileInfo(s.fileName).fileName()));
32
 
    setVerboseLogMode(true);
33
 
 
34
 
    assert(config.names.size()!=0);
35
 
    assert(config.names.size() == config.sequences.size());
36
 
    assert(config.names.size() == config.alphabets.size());
37
 
}
38
 
 
39
 
void DNAExportSequenceTask::run() {
40
 
    DocumentFormatRegistry* r = AppContext::getDocumentFormatRegistry();
41
 
    DocumentFormat* f = r->getFormatById(BaseDocumentFormats::PLAIN_FASTA);
42
 
    IOAdapterFactory* iof = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::url2io(config.fileName));
43
 
    std::auto_ptr<Document> doc(f->createNewDocument(iof, config.fileName));
44
 
    
45
 
    //prepare complement if needed
46
 
    QStringList             namesPre;
47
 
    QList<DNAAlphabet*>     alphabetsPre; 
48
 
    QList<QByteArray>       sequencesPre;
49
 
    QList<DNATranslation*>  aminoTTPre;
50
 
 
51
 
    for (int i=0; i < config.names.size(); i++) {
52
 
        if (config.strand == TriState_Yes || config.strand == TriState_Unknown) {
53
 
            namesPre.append(config.names[i]);
54
 
            alphabetsPre.append(config.alphabets[i]);
55
 
            sequencesPre.append(config.sequences[i]);
56
 
            aminoTTPre.append(config.aminoTranslations[i]);
57
 
        } 
58
 
        if (config.strand == TriState_No || config.strand == TriState_Unknown) { 
59
 
            DNATranslation* complTT = config.complTranslations[i];
60
 
            QByteArray seq;
61
 
            if (complTT == NULL) {
62
 
                stateInfo.error =tr("compl_tt_not_found");
63
 
                return;
64
 
            }
65
 
 
66
 
            char* data = config.sequences[i].data();
67
 
            int len = config.sequences[i].length();
68
 
            complTT->translate(data, len);
69
 
            TextUtils::reverse(data, len);
70
 
            namesPre.append(config.names[i] + "|rev-compl"); //TODO: check if unique!
71
 
            alphabetsPre.append(config.alphabets[i]);
72
 
            sequencesPre.append(config.sequences[i]);
73
 
            aminoTTPre.append(config.aminoTranslations[i]);
74
 
            config.sequences[i].clear(); //release resources
75
 
        }
76
 
    }
77
 
 
78
 
    //translate data if needed
79
 
    QStringList             names;
80
 
    QList<DNAAlphabet*>     alphabets; 
81
 
    QList<QByteArray>       sequences;
82
 
    for (int i=0; i < namesPre.size(); i++) {
83
 
        DNATranslation* aminoTT = aminoTTPre[i];
84
 
        if (aminoTT != NULL) {
85
 
            for (int j=0, nStrands = config.allAminoStrands ? 3 : 1; j < nStrands; j++) {
86
 
                QByteArray seq = sequencesPre[i];
87
 
                int len = (seq.length() - j)/3;
88
 
                QByteArray res(len, '\0');
89
 
                if (res.isNull() && len != 0) {
90
 
                    stateInfo.error =tr("memory_overflow_error");
91
 
                    return;
92
 
                }
93
 
                assert(aminoTT->isThree2One());
94
 
                aminoTT->translate(seq.constData() + j, seq.length() - j, res.data(), res.length());
95
 
 
96
 
                sequences.append(res);
97
 
                names.append(namesPre[i] + QString("|transl") + (nStrands == 1 ? QString("") : " " + QString::number(j))); //TODO: check if unique!
98
 
                alphabets.append(aminoTT->getDstAlphabet());
99
 
            }
100
 
        } else {
101
 
            names.append(namesPre[i]);
102
 
            alphabets.append(alphabetsPre[i]);
103
 
            sequences.append(sequencesPre[i]);
104
 
        }
105
 
        sequencesPre[i].clear(); //release resources
106
 
    }
107
 
 
108
 
    //process merge property and construct the object
109
 
    int n = names.size();
110
 
    assert(n > 0);
111
 
    if (config.merge) {
112
 
        int size = 0;
113
 
        for (int i=0; i<n; i++) {
114
 
            const QByteArray& seq = sequences[i];
115
 
            size += seq.size();
116
 
            if (i != n-1) {
117
 
                size+=config.mergeGap;
118
 
            }
119
 
        }
120
 
        QByteArray mergedSequence;
121
 
        mergedSequence.reserve(size);
122
 
        DNAAlphabet* al = alphabets[0];
123
 
        const QString& name = names[0];
124
 
        QByteArray gapSequence(config.mergeGap, al->getDefaultSymbol());
125
 
        for (int i=0; i < n; i++) {
126
 
            mergedSequence+=sequences[i];
127
 
            sequences[i].clear(); //release resource
128
 
            if (i != n-1) {
129
 
                mergedSequence+=gapSequence;
130
 
            }
131
 
        }
132
 
        doc->addObject(new DNASequenceObject(mergedSequence, al, name));
133
 
    } else {
134
 
        for (int i=0; i < names.size(); i++) {
135
 
            DNAAlphabet* al = alphabets[i];
136
 
            const QString& name = names[i];
137
 
            const QByteArray& seq = sequences[i];
138
 
            doc->addObject(new DNASequenceObject(seq, al, name));
139
 
        }
140
 
    }
141
 
    //store the document
142
 
    f->storeDocument(doc.get(), stateInfo);
143
 
}
144
 
 
145
 
 
146
 
DNAExportAlignmentTask::DNAExportAlignmentTask(const MAlignment& _ma, const QString& _fileName)
147
 
: Task("", TaskFlag_DeleteWhenFinished), ma(_ma), fileName(_fileName)
148
 
{
149
 
    setTaskName(tr("Export alignment to '%1'").arg(QFileInfo(fileName).fileName()));
150
 
    setVerboseLogMode(true);
151
 
 
152
 
    assert(!ma.isEmpty());
153
 
}
154
 
 
155
 
void DNAExportAlignmentTask::run() {
156
 
    DocumentFormatRegistry* r = AppContext::getDocumentFormatRegistry();
157
 
    DocumentFormat* f = r->getFormatById(BaseDocumentFormats::CLUSTAL_ALN);
158
 
    IOAdapterFactory* iof = AppContext::getIOAdapterRegistry()->getIOAdapterFactoryById(BaseIOAdapters::url2io(fileName));
159
 
    std::auto_ptr<Document> doc(f->createNewDocument(iof, fileName));
160
 
    ma.normalizeModel();
161
 
    doc->addObject(new MAlignmentObject(ma, "Multiple Alignment"));
162
 
    f->storeDocument(doc.get(), stateInfo);
163
 
}
164
 
 
165
 
}//namespace
166