-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathTFs.cpp
More file actions
175 lines (151 loc) · 4.1 KB
/
Copy pathTFs.cpp
File metadata and controls
175 lines (151 loc) · 4.1 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
/* Copyright (C) 2010 Ion Torrent Systems, Inc. All Rights Reserved */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h> // for PATH_MAX
#include <sys/types.h> // for S_ISREG()
#include <sys/stat.h> // for stat()
#include <unistd.h> // for getcwd()
#include "TFs.h"
#include "Utils.h"
#include "dbgmem.h"
TFs::TFs(const char *_flowOrder)
{
tfInfo = NULL;
numTFs = 0;
flowOrder = strdup(_flowOrder);
numFlowsPerCycle = strlen(flowOrder);
UpdateIonograms();
}
TFs::~TFs()
{
if (tfInfo != NULL)
free(tfInfo);
if (flowOrder)
free(flowOrder);
}
// Load - loads a TF config file
// expected format has fields comma-separated, and contains one entry per line as:
// TF name, key, sequence
bool TFs::LoadConfig(char *file)
{
char buf[1024];
FILE *fp = fopen(file, "r");
numTFs = 0;
if (fp) {
if (tfInfo != NULL)
free(tfInfo);
tfInfo = NULL;
while (fgets(buf, sizeof(buf), fp) != NULL) {
if ((buf[0] != '#') && (strlen(buf) > 10)) {
// scan in the entry
// read the name
char name[64];
char *comma = strchr(buf, ',');
if (comma) {
*comma = 0;
comma++;
strncpy(name, buf, sizeof(name));
name[sizeof(name)-1] = 0; // keeps people from entering crazy-long names that could crash us
// read the key
char key[64];
char *keyPtr = comma;
// first skip to the 1st key char
while (keyPtr != NULL && !isalpha(*keyPtr) && *keyPtr != 0) keyPtr++;
comma = strchr(keyPtr, ','); // find the end
*comma = 0; // NULL-out the end
comma++;
strncpy(key, keyPtr, sizeof(key));
key[sizeof(key)-1] = 0; // keeps people from entering crazy-long keys that could crash us
// the sequence
char seq[1024];
sscanf(comma, "%s", seq);
seq[sizeof(seq)-1] = 0;
if (strlen(seq) > 0) {
// allocate space for the new entry
if (tfInfo == NULL)
tfInfo = (TFInfo *)malloc(sizeof(TFInfo));
else
tfInfo = (TFInfo *)realloc(tfInfo, (numTFs+1)*sizeof(TFInfo));
// save entry into our struct
strcpy(tfInfo[numTFs].name, name);
strcpy(tfInfo[numTFs].key, key);
strcpy(tfInfo[numTFs].seq, seq);
tfInfo[numTFs].len = strlen(seq);
tfInfo[numTFs].count = 0;
memset(tfInfo[numTFs].Ionogram, 0, sizeof(tfInfo[numTFs].Ionogram));
tfInfo[numTFs].flows = 0;
numTFs++;
}
}
}
}
fclose(fp);
}
if (numTFs == 0) {
//THIS IS THE NEW ERROR CONDITION TO HANDLE: NO TFs TO MATCH AGAINST
//Should never get here. Only if file exists but is empty.
fprintf (stderr, "NO TFs TO USE!!!\n");
return false;
}
UpdateIonograms();
return true;
}
void TFs::UpdateIonograms()
{
// convert each TF into array of flow-space hits
char keyAndSeq[2048];
int tf;
for(tf=0;tf<numTFs;tf++) {
snprintf(keyAndSeq, 512, "%s%s", tfInfo[tf].key, tfInfo[tf].seq);
int keyAndSeqLen = strlen(keyAndSeq);
tfInfo[tf].flows = GenerateIonogram(keyAndSeq, keyAndSeqLen, tfInfo[tf].Ionogram);
}
}
int TFs::GenerateIonogram(const char *seq, int len, int *ionogram)
{
return(seqToFlow(seq,len,ionogram,800,flowOrder,numFlowsPerCycle));
}
char *TFs::GetTFConfigFile ()
{
return (GetIonConfigFile ("DefaultTFs.conf"));
}
/*******************************************************************************
**
** TFTracker Class
**
*******************************************************************************/
TFTracker::TFTracker(char *experimentName)
{
char file[] = {"TFTracking.txt"};
fileName = (char *) malloc (strlen(experimentName) + strlen(file) + 2);
sprintf (fileName, "%s/%s", experimentName, file);
fd = NULL;
fd = fopen_s(&fd, fileName, "wb");
if (!fd)
fprintf (stderr, "error creating TFTracking.txt (errno: #%d)\n", errno);
fprintf (fd, "#Format: row,column,label\n");
}
TFTracker::~TFTracker()
{
free(fileName);
}
bool TFTracker::Add(int row, int col, char *label)
{
if (fd == NULL) {
// Cannot access file to write to
return (true);
}
if (label)
fprintf (fd, "%d,%d,%s\n",row,col,label);
else
fprintf (fd, "%d,%d,\n",row,col);
return (false);
}
bool TFTracker::Close()
{
if (fd)
fclose (fd);
return (false);
}