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

« back to all changes in this revision

Viewing changes to src/corelibs/U2Core/src/tasks/ExtractAnnotatedRegionTask.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
 * UGENE - Integrated Bioinformatics Tools.
 
3
 * Copyright (C) 2008-2011 UniPro <ugene@unipro.ru>
 
4
 * http://ugene.unipro.ru
 
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 <U2Core/AppContext.h>
 
23
#include <U2Core/DNAAlphabet.h>
 
24
#include <U2Core/TextUtils.h>
 
25
#include <U2Core/AnnotationSettings.h>
 
26
#include <U2Core/SequenceUtils.h>
 
27
 
 
28
#include "ExtractAnnotatedRegionTask.h"
 
29
 
 
30
namespace U2{ 
 
31
 
 
32
ExtractAnnotatedRegionTask::ExtractAnnotatedRegionTask( const DNASequence & sequence_, SharedAnnotationData sd_, const ExtractAnnotatedRegionTaskSettings & cfg_ ) :
 
33
Task( tr("Extract annotated regions"), TaskFlag_None ), inputSeq(sequence_), inputAnn(sd_), cfg(cfg_), complT(0), aminoT(0)
 
34
{
 
35
}
 
36
 
 
37
void ExtractAnnotatedRegionTask::prepare() {
 
38
    prepareTranslations();
 
39
    resultedSeq.alphabet = aminoT ? aminoT->getDstAlphabet() : complT ? complT->getDstAlphabet() : inputSeq.alphabet;
 
40
   // resultedSeq.info = inputSeq.info;
 
41
    resultedSeq.info[DNAInfo::ID] = inputSeq.getName();
 
42
}
 
43
 
 
44
void ExtractAnnotatedRegionTask::prepareTranslations() {
 
45
    //TODO move these logic somewhere upstairs
 
46
    bool aminoSeq = inputSeq.alphabet->isAmino();
 
47
    if (aminoSeq) {
 
48
        return;
 
49
    }
 
50
    if (cfg.complement && inputAnn->getStrand().isCompementary()) {
 
51
        QList<DNATranslation*> compTTs = AppContext::getDNATranslationRegistry()->
 
52
            lookupTranslation( inputSeq.alphabet, DNATranslationType_NUCL_2_COMPLNUCL );
 
53
        if (!compTTs.isEmpty()) {
 
54
            complT = compTTs.first(); 
 
55
        }
 
56
    }
 
57
 
 
58
    if (cfg.translate) {
 
59
        DNATranslationType dnaTranslType = (inputSeq.alphabet->getType() == DNAAlphabet_NUCL) ? DNATranslationType_NUCL_2_AMINO : DNATranslationType_RAW_2_AMINO;
 
60
        QList<DNATranslation*> aminoTTs = AppContext::getDNATranslationRegistry()->lookupTranslation( inputSeq.alphabet, dnaTranslType );
 
61
        if( !aminoTTs.isEmpty() ) {
 
62
            aminoT = aminoTTs.first();
 
63
        } 
 
64
    }
 
65
}
 
66
 
 
67
void ExtractAnnotatedRegionTask::run() {
 
68
    QVector<U2Region> safeLocation = inputAnn->getRegions();
 
69
    U2Region::bound(0, inputSeq.length(), safeLocation);
 
70
    QList<QByteArray> resParts = SequenceUtils::extractRegions(inputSeq.constData(), safeLocation, complT);
 
71
    QVector<U2Region> resLocation = SequenceUtils::toJoinedRegions(resParts);
 
72
    if (aminoT == NULL) { // extension does not work for translated annotations
 
73
        if (cfg.extLeft > 0) {
 
74
            int annStart = safeLocation.first().startPos;
 
75
            int preStart = qMax(0,  annStart - cfg.extLeft);
 
76
            int preLen = annStart - preStart;
 
77
            QByteArray preSeq = inputSeq.seq.mid(preStart, preLen);
 
78
            resParts.prepend(preSeq);
 
79
            U2Region::shift(cfg.extLeft, resLocation);
 
80
        }
 
81
        if (cfg.extRight) {
 
82
            U2Region annRegion = U2Region::containingRegion(safeLocation);
 
83
            int annEnd = annRegion.endPos();
 
84
            int postEnd = qMin(inputSeq.length(), annEnd + cfg.extRight);
 
85
            int postLen = postEnd - annEnd;
 
86
            QByteArray postSeq = inputSeq.seq.mid(annEnd, postLen);
 
87
            resParts.append(postSeq);
 
88
        }
 
89
    } else {
 
90
        resParts = SequenceUtils::translateRegions(resParts, aminoT, inputAnn->isJoin());
 
91
        resLocation = SequenceUtils::toJoinedRegions(resParts);
 
92
    }
 
93
    resultedSeq.seq = resParts.size() == 1 ? resParts.first() : SequenceUtils::joinRegions(resParts);
 
94
    resultedAnn = inputAnn;
 
95
    resultedAnn->location->regions = resLocation;
 
96
    resultedAnn->setStrand(U2Strand::Direct);
 
97
    resultedAnn->setLocationOperator(inputAnn->getLocationOperator());
 
98
}
 
99
 
 
100
 
 
101
} // U2