File: LibraryWorker.cpp

package info (click to toggle)
ray 2.3.1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,008 kB
  • sloc: cpp: 49,973; sh: 339; makefile: 281; python: 168
file content (178 lines) | stat: -rw-r--r-- 6,447 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 	Ray
    Copyright (C) 2010, 2011, 2012 Sébastien Boisvert

	http://DeNovoAssembler.SourceForge.Net/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3 of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You have received a copy of the GNU General Public License
    along with this program (gpl-3.0.txt).  
	see <http://www.gnu.org/licenses/>

*/

#include "LibraryWorker.h"

#include <code/Mock/constants.h>
#include <code/SeedingData/SeedingData.h>

#include <RayPlatform/communication/VirtualCommunicator.h>
#include <RayPlatform/communication/Message.h>

#include <map>
#include <assert.h>
using namespace std;

bool LibraryWorker::isDone(){
	return m_done;
}

void LibraryWorker::constructor(WorkerHandle id,SeedingData*seedingData,VirtualCommunicator*virtualCommunicator,RingAllocator*outboxAllocator,Parameters*parameters,
StaticVector*inbox,StaticVector*outbox,	map<int,map<int,int> >*libraryDistances,int*detectedDistances,MyAllocator*allocator,
MessageTag RAY_MPI_TAG_GET_READ_MATE,
MessageTag RAY_MPI_TAG_REQUEST_VERTEX_READS
){

	this->RAY_MPI_TAG_GET_READ_MATE=RAY_MPI_TAG_GET_READ_MATE;
	this->RAY_MPI_TAG_REQUEST_VERTEX_READS=RAY_MPI_TAG_REQUEST_VERTEX_READS;

	m_done=false;
	m_parameters=parameters;
	m_SEEDING_i=id;
	m_seedingData=seedingData;
	m_virtualCommunicator=virtualCommunicator;
	m_outboxAllocator=outboxAllocator;
	m_EXTENSION_currentPosition=0;
	m_EXTENSION_reads_requested=false;
	m_EXTENSION_hasPairedReadRequested=false;
	m_EXTENSION_edgeIterator=0;
	m_inbox=inbox;
	m_outbox=outbox;
	m_libraryDistances=libraryDistances;
	m_detectedDistances=detectedDistances;

	m_allocator=allocator;
	m_database.constructor();
}

void LibraryWorker::work(){
	if(m_done){
		return;
	}
	#ifdef CONFIG_ASSERT
	assert(m_SEEDING_i<m_seedingData->m_SEEDING_seeds.size());
	#endif
	if(m_EXTENSION_currentPosition==(int)m_seedingData->m_SEEDING_seeds[m_SEEDING_i].size()){
		while(m_database.size()>0){
			SplayNode<ReadHandle,LibraryElement>*node=m_database.getRoot();
			ReadHandle key=node->getKey();
			m_database.remove(key,true,m_allocator);
		}
		m_done=true;
	}else{
		if(!m_EXTENSION_reads_requested){
			m_EXTENSION_reads_requested=true;
			#ifdef CONFIG_ASSERT
			assert(m_seedingData!=NULL);
			assert(m_EXTENSION_currentPosition<(int)m_seedingData->m_SEEDING_seeds[m_SEEDING_i].size());
			#endif

			Kmer theKmerObject;
			m_seedingData->m_SEEDING_seeds[m_SEEDING_i].at(m_EXTENSION_currentPosition,&theKmerObject);
			Kmer*vertex=&theKmerObject;
		
			m_readFetcher.constructor(vertex,m_outboxAllocator,m_inbox,m_outbox,m_parameters,m_virtualCommunicator,
				m_SEEDING_i,RAY_MPI_TAG_REQUEST_VERTEX_READS);
			#ifdef CONFIG_ASSERT
			assert(!m_readFetcher.isDone());
			#endif
			m_EXTENSION_edgeIterator=0;// iterate over reads
			m_EXTENSION_hasPairedReadRequested=false;
		}else if(!m_readFetcher.isDone()){
			m_readFetcher.work();
		}else{
			if(m_EXTENSION_edgeIterator<(int)m_readFetcher.getResult()->size()){
				ReadAnnotation annotation=m_readFetcher.getResult()->at(m_EXTENSION_edgeIterator);
				int rightRead=annotation.getReadIndex();
				if(!m_EXTENSION_hasPairedReadRequested){
					MessageUnit*message=(MessageUnit*)(m_outboxAllocator)->allocate(1*sizeof(MessageUnit));
					message[0]=rightRead;
					#ifdef CONFIG_ASSERT
					assert(m_parameters!=NULL);
					if(!(annotation.getRank()<m_parameters->getSize())){
						cout<<"Error rank="<<annotation.getRank()<<" size="<<m_parameters->getSize()<<endl;
					}
					assert(annotation.getRank()<m_parameters->getSize());
					#endif
					Message aMessage(message,1,annotation.getRank(),RAY_MPI_TAG_GET_READ_MATE,m_parameters->getRank());
					m_virtualCommunicator->pushMessage(m_SEEDING_i,&aMessage);
					m_EXTENSION_hasPairedReadRequested=true;
				}else if(m_virtualCommunicator->isMessageProcessed(m_SEEDING_i)){
					vector<MessageUnit> buffer;
					m_virtualCommunicator->getMessageResponseElements(m_SEEDING_i,&buffer);
					#ifdef CONFIG_ASSERT
					assert((int)buffer.size()==4);
					#endif
	
					/** this is a sentinel value */
					/** data: readLength, rank, id, library */
					if((int)buffer[1] != MAX_NUMBER_OF_MPI_PROCESSES){
						int library=buffer[3];
						int readLength=buffer[0];
						bool isAutomatic=m_parameters->isAutomatic(library);
						if(isAutomatic){
							PathHandle uniqueReadIdentifier=getPathUniqueId(buffer[1],buffer[2]);
							SplayNode<ReadHandle,LibraryElement>*node=m_database.find(uniqueReadIdentifier.getValue(),false);
							if(node!=NULL){
								LibraryElement*element=node->getValue();
								int rightStrandPosition=annotation.getPositionOnStrand();
								Strand rightStrand=annotation.getStrand();
								Strand leftStrand=element->m_readStrand;
								int leftStrandPosition=element->m_strandPosition;
											
								if(( leftStrand=='F' && rightStrand=='R' )
								||(  leftStrand=='R' && rightStrand=='F' )){// make sure the orientation is OK
									int p1=element->m_readPosition;
									int p2=m_EXTENSION_currentPosition;
									int d=p2-p1+readLength+leftStrandPosition-rightStrandPosition;
									(*m_libraryDistances)[library][d]++;
									(*m_detectedDistances)++;
								}
							}
						}
					}
					m_EXTENSION_edgeIterator++;
					m_EXTENSION_hasPairedReadRequested=false;
				}
			}else{
				for(int i=0;i<(int)m_readFetcher.getResult()->size();i++){
					ReadHandle uniqueId=m_readFetcher.getResult()->at(i).getUniqueId();
					int position=m_EXTENSION_currentPosition;
					Strand strand=m_readFetcher.getResult()->at(i).getStrand();
					int strandPosition=m_readFetcher.getResult()->at(i).getPositionOnStrand();
					// read, position, strand
					bool flag;
					SplayNode<ReadHandle,LibraryElement>*node=m_database.insert(uniqueId,m_allocator,&flag);
					LibraryElement*element=node->getValue();
					element->m_readPosition=position;
					element->m_readStrand=strand;
					element->m_strandPosition=strandPosition;
				}
				m_EXTENSION_currentPosition++;
				m_EXTENSION_reads_requested=false;
			}
		}
	}
}

WorkerHandle LibraryWorker::getWorkerIdentifier(){
	return m_SEEDING_i;
}