2
* UGENE - Integrated Bioinformatics Tools.
3
* Copyright (C) 2008-2011 UniPro <ugene@unipro.ru>
4
* http://ugene.unipro.ru
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.
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.
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,
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>
28
#include "ExtractAnnotatedRegionTask.h"
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)
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();
44
void ExtractAnnotatedRegionTask::prepareTranslations() {
45
//TODO move these logic somewhere upstairs
46
bool aminoSeq = inputSeq.alphabet->isAmino();
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();
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();
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);
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);
90
resParts = SequenceUtils::translateRegions(resParts, aminoT, inputAnn->isJoin());
91
resLocation = SequenceUtils::toJoinedRegions(resParts);
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());