-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemContext.cpp
More file actions
365 lines (315 loc) · 10.5 KB
/
Copy pathSystemContext.cpp
File metadata and controls
365 lines (315 loc) · 10.5 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/* Copyright (C) 2010 Ion Torrent Systems, Inc. All Rights Reserved */
#include <string>
#include <iostream>
#include <assert.h>
#include "SystemContext.h"
#include "dirent.h"
#include "HandleExpLog.h"
#include "IonErr.h"
using namespace std;
void SystemContext::DefaultSystemContext()
{
dat_source_directory = NULL;
wells_output_directory = NULL;
results_folder = NULL;
analysisLocation = "";
strcpy (runId, "");
sprintf (wellsFileName, "1.wells");
strcpy (tmpWellsFile, "");
LOCAL_WELLS_FILE = 1;
strcpy (wellsFilePath, "");
wellStatFile=NULL;
stackDumpFile=NULL;
//wellsFormat = "hdf5";
wellsFormat.assign("hdf5");
NO_SUBDIR = 0; // when set to true, no experiment subdirectory is created for output files.
explog_path = NULL;
}
//const char *SystemContext::GetResultsFolder()
char *SystemContext::GetResultsFolder()
{
return(results_folder);
}
void SystemContext::GenerateContext ()
{
if (!dat_source_directory)
{
dat_source_directory = (char *) malloc (2);
snprintf (dat_source_directory, 1, "."); // assume current directory if not provided as an argument
}
// Test for a valid data source directory
if (isDir (dat_source_directory) == false )
{
fprintf (stderr, "'%s' is not a directory. Exiting.\n", dat_source_directory);
exit (EXIT_FAILURE);
}
// standard output directory
if (!wells_output_directory)
{
if (results_folder) { free (results_folder); }
results_folder = (char*) malloc (3);
strcpy (results_folder, "./");
}
else // --output-dir specified, so wells_output_directory an input arg
{
if (NO_SUBDIR) // --no-subdir specified
{
// wells_output_directory is a directory spec.
// make fully qualified to avoid SetUpAnalysis munging
if (strlen (wells_output_directory) == 1)
assert (wells_output_directory[0] != '/'); // root not allowed
char *tmpPath = strdup (wells_output_directory);
char *real_path = realpath (dirname (tmpPath), NULL);
if (real_path == NULL){
std::string ss = tmpPath; // dirname overwrites tmpPath
ss = ss + ": directory not found";
ION_ASSERT ((real_path != NULL), ss.c_str());
}
char *tmpBase = strdup (wells_output_directory);
char *base_name = basename (tmpBase);
int strSz = strlen (real_path) + strlen (base_name) + 2;
if (results_folder) { free (results_folder); }
results_folder = (char *) malloc (sizeof (char) * strSz);
snprintf (results_folder,strSz,"%s/%s",real_path,base_name);
free (tmpPath);
free (tmpBase);
free (real_path);
}
else // put wells_output_directory+time_stamp in dat_source_directory
{
if (results_folder) { free (results_folder); }
results_folder = experimentDir (dat_source_directory, wells_output_directory);
}
}
}
SystemContext::~SystemContext()
{
if (results_folder){
free (results_folder);
results_folder = NULL;
}
if (wells_output_directory){
free (wells_output_directory);
wells_output_directory = NULL;
}
if (dat_source_directory)
free (dat_source_directory);
if (explog_path)
free (explog_path);
}
// utility function
void SystemContext::MakeSymbolicLinkToOldDirectory (char *results_folder)
{
// Create symbolic link to bfmask.bin and 1.wells in new subdirectory: links are for disc space usage reasons
char *oldpath = NULL;
int sz = strlen (wellsFilePath) + strlen (wellsFileName) + 2;
oldpath = (char *) malloc (sz);
snprintf (oldpath, sz, "%s/%s", wellsFilePath, wellsFileName);
char *fullPath = realpath (oldpath, NULL);
char *newpath = NULL;
sz = strlen (results_folder) + strlen (wellsFileName) + 2;
newpath = (char *) malloc (sz);
snprintf (newpath, sz, "%s/%s", results_folder, wellsFileName);
int ret = symlink (fullPath, newpath);
if (ret)
{
perror (oldpath);
}
free (oldpath);
free (newpath);
free (fullPath);
}
void SystemContext::MakeNewTmpWellsFile (char *results_folder)
{
if (wellsFilePath[0] == '\0')
{
if (LOCAL_WELLS_FILE)
{
char fTemplate[256] = { 0 };
//Utils:ClearStaleWellsFile() is sensitive to temp well filename format
sprintf (fTemplate, "/tmp/well_%d_XXXXXX", getpid());
int tmpFH = mkstemp (fTemplate);
if (tmpFH == 0)
exit (EXIT_FAILURE);
close (tmpFH);
strcpy (tmpWellsFile, fTemplate);
strcpy (wellsFilePath, "/tmp");
strcpy (wellsFileName, basename (fTemplate));
}
else
{
strcpy (wellsFilePath, results_folder);
}
}
printf("wells_file_path: %s\n", wellsFilePath);
printf("wells_file_name: %s\n", wellsFileName);
}
// fill the new directory with files needed for report generation
void SystemContext::CopyFilesForReportGeneration (char *results_folder, SeqListClass &my_keys)
{
//--- Copy files needed for report generation ---
//--- Copy bfmask.stats ---
int sz;
char *newpath = NULL;
char *oldpath = NULL;
sz = strlen (wellsFilePath) + strlen ("bfmask.stats") + 2;
oldpath = (char *) malloc (sz);
snprintf (oldpath, sz, "%s/%s", wellsFilePath, "bfmask.stats");
sz = strlen (results_folder) + strlen ("bfmask.stats") + 2;
newpath = (char *) malloc (sz);
snprintf (newpath, sz, "%s/%s", results_folder, "bfmask.stats");
fprintf (stderr, "%s\n%s\n", oldpath, newpath);
if( CopyFile (oldpath, newpath) ) ExitCode::UpdateExitCode(EXIT_FAILURE);
free (oldpath);
free (newpath);
//--- Copy avgNukeTrace_ATCG.txt and avgNukeTrace_TCAG.txt
//@TODO: Is this really compatible with 3 keys?
for (int q = 0; q < my_keys.numSeqListItems; q++)
{
char *filename;
filename = (char *) malloc (strlen ("avgNukeTrace_") + strlen (
my_keys.seqList[q].seq.c_str()) + 5);
sprintf (filename, "avgNukeTrace_%s.txt", my_keys.seqList[q].seq.c_str());
sz = strlen (wellsFilePath) + strlen (filename) + 2;
oldpath = (char *) malloc (sz);
snprintf (oldpath, sz, "%s/%s", wellsFilePath, filename);
sz = strlen (results_folder) + strlen (filename) + 2;
newpath = (char *) malloc (sz);
snprintf (newpath, sz, "%s/%s",results_folder, filename);
if( CopyFile (oldpath, newpath) ) ExitCode::UpdateExitCode(EXIT_FAILURE);
free (oldpath);
free (newpath);
free (filename);
}
}
//
// Create a name for the results of the analysis
// Use the raw data directory name. If it is not in standard format, use it in its entirety
// Raw dir names are R_YYYY_MM_DD_hh_mm_ss_XXX_description
// Results directory ("experiment" directory) will be 'description'_username_YY_MM_DD_seconds-in-day
//
char *SystemContext::experimentDir (char *rawdataDir, char *dirOut)
{
char *expDir = NULL;
char *timeStamp = NULL;
char *sPtr = NULL;
time_t now;
struct tm *tm = NULL;
// Strip a trailing slash
if (dirOut[strlen (dirOut)-1] == '/')
dirOut[strlen (dirOut)-1] = '\0';
// Another algorithm counts forward through the date portion 6 underscores
sPtr = rawdataDir;
for (int i = 0; i < 7; i++)
{
sPtr = strchr (sPtr, '_');
if (!sPtr)
{
sPtr = "analysis";
break;
}
sPtr++;
}
if (sPtr[strlen (sPtr)-1] == '/') // Strip a trailing slash too
sPtr[strlen (sPtr)-1] = '\0';
// Generate a timestamp string
time (&now);
tm = localtime (&now);
timeStamp = (char *) malloc (sizeof (char) * 18);
snprintf (timeStamp, 18, "_%d_%02d_%02d_%d",1900 + tm->tm_year, tm->tm_mon+1, tm->tm_mday, 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec);
int strSize = strlen (dirOut) + strlen (timeStamp) + strlen (sPtr) +2;
expDir = (char *) malloc (sizeof (char) * strSize);
if (expDir != NULL)
snprintf (expDir, strSize, "%s/%s%s", dirOut,sPtr,timeStamp);
free (timeStamp);
cout << "SystemContext::experimentDir... dat_source_directory=" << dat_source_directory << ", wells_output_directory=" << wells_output_directory << ", expDir=" << expDir << endl;
return (expDir);
}
void SystemContext::SetUpAnalysisLocation()
{
char *path = strdup(results_folder);
//get full path
char *real_path = realpath (path, NULL);
if (real_path == NULL) {
cout << "Couldn't set up real output path from " << path << ". Using working directory." << endl;
real_path = realpath("./", NULL);
}
analysisLocation = string(real_path);
// file locations
if (!analysisLocation.empty() && *analysisLocation.rbegin() != '/')
analysisLocation = analysisLocation+"/";
char *bName = basename(real_path);
ion_run_to_readname (runId, bName, strlen (bName)); // Create a run identifier from output results directory string
cout << "SystemContext::SetUpAnalysisLocation... experimentName=" << results_folder << endl;
cout << "SystemContext::SetUpAnalysisLocation... analysisLocation =" << analysisLocation << endl << endl;
cout << "SystemContext::SetUpAnalysisLocation... baseName =" << bName << endl;
cout << "SystemContext::SetUpAnalysisLocation... runId =" << runId << endl;
free(path);
free(real_path);
}
// make sure we have explog file if not set cmd-line
void SystemContext::FindExpLogPath()
{
if (!explog_path){
explog_path = MakeExpLogPathFromDatDir(dat_source_directory);
if (!explog_path)
{
fprintf (stderr, "Unable to find explog file. Exiting.\n");
exit (EXIT_FAILURE);
}
}
}
void SystemContext::CleanupTmpWellsFile ()
{
//Cleanup
//Copy wells file from temporary, local file to permanent; remove temp file
//Copy temp wells file moved to pre-cafie code.
if (LOCAL_WELLS_FILE)
{
unlink (tmpWellsFile);
}
}
void SystemContext::CopyTmpWellFileToPermanent ( char *results_folder)
{
// defaults moved here because never changed
static char *wellfileIndex = "1";
static char *wellfileExt = "wells";
if (LOCAL_WELLS_FILE)
{
char wellFileName[MAX_PATH_LENGTH];
sprintf (wellFileName, "%s/%s.%s", results_folder, wellfileIndex, wellfileExt);
if( CopyFile (tmpWellsFile, wellFileName) )
ExitCode::UpdateExitCode(EXIT_FAILURE);
}
}
/*
* Remove temporary 1.wells files leftover from previous Analysis
*/
void ClearStaleWellsFile (void)
{
DIR *dirfd;
struct dirent *dirent;
dirfd = opendir ("/tmp");
while ( (dirent = readdir (dirfd)) != NULL)
{
if (! (strstr (dirent->d_name, "well_")))
continue;
int pid;
DIR *tmpdir = NULL;
char name[MAX_PATH_LENGTH];
char trash[MAX_PATH_LENGTH];
sscanf (dirent->d_name,"well_%d_%s", &pid, trash);
sprintf (name, "/proc/%d", pid);
if ( (tmpdir = opendir (name)))
{
closedir (tmpdir);
}
else
{
char creamEntry[MAX_PATH_LENGTH];
sprintf (creamEntry, "/tmp/%s", dirent->d_name);
unlink (creamEntry);
}
}
closedir (dirfd);
}