-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_seqs_from_fastq.cpp
158 lines (131 loc) · 4.22 KB
/
get_seqs_from_fastq.cpp
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
#include "get_seqs_from_fastq.h"
using namespace std;
int main()
{
// Open fastq file
char* infile_filepath = new char[1024];
memset(infile_filepath,0,1024);
// Set the following string to the full path to the FASTQ format file from which you want to extract valid reads
strcpy(infile_filepath,"C:\\Users\\hpatterton\\Documents\\Students\\Mzwanele Ngubo\\PhD\\Sequence data\\Sample_H1\\H1_ATCACG_L001_R1_001.fastq");
char* filetype_point = strrchr(infile_filepath,'.');
int number_of_characters = filetype_point-infile_filepath;
char* outfile_filepath = new char[1024];
memset(outfile_filepath,0,1024);
strncpy(outfile_filepath,infile_filepath,number_of_characters);
strcat(outfile_filepath,"_barcodes.txt");
cout << "Fastq file: " << infile_filepath << endl;
cout << "Retrieving sequences..." << endl;
// Read all the sequences to an array
HP_DynamicStringArray* string_array = new HP_DynamicStringArray();
HP_ReadTextFile* readfile = new HP_ReadTextFile;
int entries = readfile->ReadFastqFile(infile_filepath, string_array);
cout << entries << " sequences retrieved from fastq file" << endl;
char** primers = new char*[8];
for(int x = 0; x < 8; x++)
{
primers[x] = new char[18];
memset(primers[x], 0, 18);
}
/*
* The primer sequences were copied from Dai et al. (2008) Probing nucleosome function: A highly versatile library of synthetic histone H3 and H4 mutants
* and correspond to the following:
*
* primers[0]: U1h
* primers[4]: U2h complement
* primers[1]: U2h
* primers[5]: U1h complement
* primers[2]: D1h
* primers[6]: D2h complement
* primers[3]: D2h
* primers[7]: D1h complement
*
*/
strcpy(primers[0],"ATGTCCACGAGGTCTCT");
strcpy(primers[4], "TACGCTGCAGGTCGAGG");
strcpy(primers[1], "CCTCGACCTGCAGCGTA");
strcpy(primers[5], "AGAGACCTCGTGGACAT");
strcpy(primers[2], "CGGTGTCGGTCTCGTAG");
strcpy(primers[6], "GATGAATTCGAGCTGGG");
strcpy(primers[3], "CCCAGCTCGAATTCATC");
strcpy(primers[7], "CTACGAGACCGACACCG");
int two_primers = 0;
int correct_length = 0;
int one_primer = 0;
int incorrect_length = 0;
int no_primers = 0;
// See if we can match a primer
int primer_length = 17;
int insert_length = 20;
char* pointer_1;
char* pointer_2;
int primer_1;
int primer_2;
HP_DynamicStringArray* barcodes = new HP_DynamicStringArray();
HPDynamicIntArray* up_down = new HPDynamicIntArray();
char* temp = new char[21];
for(int x = 0; x < string_array->GetNumberOfStrings(); x++)
{
memset(temp, 0, 21);
int index = 0;
while(index < 4 && !strstr(string_array->GetStringPointer(x), primers[index]))
index++;
if(index < 4) // we found a match
{
pointer_1 = strstr(string_array->GetStringPointer(x), primers[index]);
// See if we can find the match for the primer pair
pointer_2 = strstr(string_array->GetStringPointer(x), primers[index+4]);
primer_1 = pointer_1 - string_array->GetStringPointer(x);
primer_2 = pointer_2 - string_array->GetStringPointer(x);
if(pointer_2 == 0) // could not find a match
{
one_primer++;
}
else
{
if(primer_2 > primer_1) // the order of primers is correct
{
two_primers++;
if((primer_2 - primer_1 - primer_length) == insert_length)
{
correct_length++;
strncpy(temp, (pointer_1+primer_length), insert_length);
if(index < 2)
up_down->AddInt(1);
else
up_down->AddInt(0);
if((index == 1) || (index == 3))
barcodes->AddString(readfile->ReverseComplement(temp));
else
barcodes->AddString(temp);
}
else
incorrect_length++;
}
}
}
else
{
no_primers++;
}
}
cout << "two_primers: " << two_primers << endl;
cout << "correct_length: " << correct_length << endl;
cout << "one_primer: " << one_primer << endl;
cout << "incorrect_length: " << incorrect_length << endl;
cout << "no_primers: " << no_primers << endl;
// Save the strings from the array
char* outfilename = new char[1024];
// Change the string below to a filename and path of your choice
fstream outfile;
outfile.open(outfile_filepath, fstream::out | fstream::binary);
char* temp_2 = new char[30];
for(int x = 0; x < barcodes->GetNumberOfStrings(); x++)
{
memset(temp_2, 0, 30);
sprintf(temp_2, "%s%c%d", barcodes->GetStringPointer(x), '\t', up_down->GetEntry(x));
outfile << temp_2 << endl;
}
outfile.close();
cout << "done" << endl;
return 0;
}