From 0b209dd1e9af84a816d40ed27f8a26cdfc248bd3 Mon Sep 17 00:00:00 2001 From: Haoyu Cheng Date: Fri, 10 May 2019 00:11:19 -0400 Subject: [PATCH] Input reads --- .vscode/ipch/491621f0e7654f5/mmap_address.bin | Bin 0 -> 8 bytes Assembly.cpp | 32 ++++ Assembly.h | 8 + CommandLines.cpp | 63 +++++++ CommandLines.h | 10 ++ Makefile | 18 ++ Process_Read.cpp | 164 ++++++++++++++++++ Process_Read.h | 76 ++++++++ main.cpp | 28 +++ 9 files changed, 399 insertions(+) create mode 100644 .vscode/ipch/491621f0e7654f5/mmap_address.bin create mode 100644 Assembly.cpp create mode 100644 Assembly.h create mode 100644 CommandLines.cpp create mode 100644 CommandLines.h create mode 100644 Makefile create mode 100644 Process_Read.cpp create mode 100644 Process_Read.h diff --git a/.vscode/ipch/491621f0e7654f5/mmap_address.bin b/.vscode/ipch/491621f0e7654f5/mmap_address.bin new file mode 100644 index 0000000000000000000000000000000000000000..862b8428b9e068428b1a4e8a38f94019008d8940 GIT binary patch literal 8 NcmZQzU~Ksh1ON<}1cCqn literal 0 HcmV?d00001 diff --git a/Assembly.cpp b/Assembly.cpp new file mode 100644 index 0000000..d15be16 --- /dev/null +++ b/Assembly.cpp @@ -0,0 +1,32 @@ +#include "Assembly.h" +#include +#include +#include "Process_Read.h" + +void Counting() +{ + Read r; + init_Read(&r); + + long long read_number = 0; + + while (inputRead(&r)) + { + + fprintf(stderr,"%s\n",r.name); + + fprintf(stderr,"%s\n",r.seq); + fprintf(stderr,"+\n"); + fprintf(stderr,"%s\n",r.qual); + + + read_number++; + + clear_read(&r); + } + + fprintf(stdout, "read_number: %lld\n",read_number); + + + +} diff --git a/Assembly.h b/Assembly.h new file mode 100644 index 0000000..348da2d --- /dev/null +++ b/Assembly.h @@ -0,0 +1,8 @@ +#ifndef __ASSEMBLY__ +#define __ASSEMBLY__ + + + +void Counting(); + +#endif diff --git a/CommandLines.cpp b/CommandLines.cpp new file mode 100644 index 0000000..6c31b3a --- /dev/null +++ b/CommandLines.cpp @@ -0,0 +1,63 @@ +#include "CommandLines.h" +#include +#include +#include + +char* read_file_name = NULL; +char* output_file_name = NULL; +int thread_num = 1; + +void Print_H() +{ + fprintf(stderr, "Incorrect options.\n"); +} + + + + +int CommandLine_process (int argc, char *argv[]) +{ + int o; + int index; + + static struct option longOptions[] = + { + {"seq", required_argument, 0, 'q'}, + {"threads", required_argument, 0, 't'}, + {"threads", required_argument, 0, 'o'}, + {0, 0, 0, 0}, + }; + + + while ( (o = getopt_long ( argc, argv, "q:t:o:", longOptions, &index)) != -1 ) + { + switch (o) + { + case 'q': + read_file_name = optarg; + break; + case 'o': + output_file_name = optarg; + break; + case 't': + thread_num = atoi(optarg); + break; + case '?': + fprintf(stderr, "Unrecognized option!\n" ); + return 0; + break; + default: + Print_H(); + return 0; + } + } + + if (argc == 1) + { + Print_H(); + return 0; + } + + + return 1; +} \ No newline at end of file diff --git a/CommandLines.h b/CommandLines.h new file mode 100644 index 0000000..1ecdfcb --- /dev/null +++ b/CommandLines.h @@ -0,0 +1,10 @@ +#ifndef __COMMAND_LINE_PARSER__ +#define __COMMAND_LINE_PARSER__ + +extern char* read_file_name; +extern char* output_file_name; +extern int thread_num; + +int CommandLine_process (int argc, char *argv[]); + +#endif \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..dd44402 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +CC=g++ + +CFLAGS = -w -c -msse4.2 -mpopcnt -fomit-frame-pointer -Winline -O3 -lz +LDFLAGS = -lm -lz -lpthread -O3 -mpopcnt -msse4.2 -lz -w + +SOURCES = main.cpp CommandLines.cpp Process_Read.cpp Assembly.cpp +OBJECTS = $(SOURCES:.c=.o) +EXECUTABLE = ccs_assembly + + +all: $(SOURCES) $(EXECUTABLE) +$(EXECUTABLE): $(OBJECTS) + $(CC) $(OBJECTS) -o $@ $(LDFLAGS) + +.c.o: + $(CC) $(CFLAGS) $< -o $@ +clean: + rm -f *.o *~ \#* ccs_assembly \ No newline at end of file diff --git a/Process_Read.cpp b/Process_Read.cpp new file mode 100644 index 0000000..09b09ac --- /dev/null +++ b/Process_Read.cpp @@ -0,0 +1,164 @@ +#include "Process_Read.h" +#include +#include +#include +#include +#include + + +int format = -1; +FILE *_r_fp = NULL; +gzFile g_r_fp; + + +int check_file_format(char *fileName) +{ + int i = 0; + + char* file_extention; + i = strlen(fileName) - 1; + while (i >= 0 && fileName[i] != '.') + { + i--; + } + + if (i < 0) + { + fprintf(stderr, + "ERROR: the input read files must be FASTQ format (file extension: .fq or .fastq) or compressed FASTQ format (file extension: .fq.gz or .fastq.gz)!\n"); + exit(0); + } + + if ((strcmp(".fq", fileName+i) == 0) + || + strcmp(".fastq", fileName + i) == 0) + { + return FASTQ; + } + else if (strcmp(".gz", fileName + i) == 0) + { + i--; + while (i >= 0 && fileName[i] != '.') + { + i--; + } + + + if (i < 0) + { + fprintf(stderr, + "ERROR: the input read files must be FASTQ format (file extension: .fq or .fastq) or compressed FASTQ format (file extension: .fq.gz or .fastq.gz)!\n"); + exit(0); + } + + if ((strcmp(".fq.gz", fileName + i) == 0) + || + strcmp(".fastq.gz", fileName + i) == 0) + { + return FASTQGZ; + } + else + { + fprintf(stderr, + "ERROR: the input read files must be FASTQ format (file extension: .fq or .fastq) or compressed FASTQ format (file extension: .fq.gz or .fastq.gz)!\n"); + exit(0); + } + + } + + + fprintf(stderr, + "ERROR: the input read files must be FASTQ format (file extension: .fq or .fastq) or compressed FASTQ format (file extension: .fq.gz or .fastq.gz)!\n"); + exit(0); +} + + +int initiReadAllReads(char *fileName) +{ + + format = check_file_format(fileName); + + if (format == FASTQ) + { + _r_fp = fopen(fileName, "r"); + + if (_r_fp == NULL) + { + return 0; + } + + fprintf(stdout, + " Read files are in FASTQ format...\n"); + } + else if (format == FASTQGZ) + { + int fd = open(fileName, O_CREAT | O_RDONLY, 0666); + g_r_fp = gzdopen(fd, "rb"); + + fprintf(stdout, + " Read files are in compressed FASTQ format...\n"); + } + else + { + fprintf(stderr, + "ERROR: the input read files must be FASTQ format (file extension: .fq or .fastq) or compressed FASTQ format (file extension: .fq.gz or .fastq.gz)!\n"); + exit(0); + } + +} + +#define BUFFER_SIZE 20000 +char Read_Buffer[BUFFER_SIZE]; +char true_flag[2]; + + +inline char *Input_line_from_file(char *seq) +{ + if (format == FASTQ) + { + return fgets(Read_Buffer, BUFFER_SIZE, _r_fp); + } + else + { + seq = gzgets(g_r_fp, seq, BUFFER_SIZE); + + return (!gzeof(g_r_fp)) ? true_flag : NULL; + + } + +} + +inline int get_whole_line(str_v* line) +{ + while(Input_line_from_file(Read_Buffer)) + { + append_str(line, Read_Buffer); + + if (line->centext[line->length - 1] == '\n') + { + line->length--; + line->centext[line->length] = '\0'; + return 1; + } + + } + + return 0; +} + + +int inputRead(Read *read) +{ + int buffer_length; + + if (get_whole_line(&read->name)) + { + get_whole_line(&read->seq); + Input_line_from_file(Read_Buffer); + get_whole_line(&read->qual); + return 1; + } + + return 0; + +} \ No newline at end of file diff --git a/Process_Read.h b/Process_Read.h new file mode 100644 index 0000000..7fd3283 --- /dev/null +++ b/Process_Read.h @@ -0,0 +1,76 @@ +#ifndef __READ__ +#define __READ__ + +#include +#include +#include + + +#define FASTQ 1 +#define FASTQGZ 2 + +#define NAME_SZIE 25 +#define SEQ_SZIE 20000 + + +typedef struct +{ + char* centext; + uint32_t length; + uint32_t size; + +}str_v; + + +inline void init_str_v(str_v* buf, uint32_t length) +{ + buf->size = length + 1; + buf->length = 0; + buf->centext = (char*)malloc(sizeof(char)*buf->size); +} + + +inline void append_str(str_v* buf, char* input) +{ + int length = strlen(input); + buf->length = buf->length + length; + if (buf->length > buf->size) + { + buf->centext = (char*)realloc(buf->centext, buf->length + 1); + } + memcpy(buf->centext + buf->length - length, input, length); + buf->centext[buf->length] = '\0'; +} + +inline void clear_str(str_v* buf) +{ + buf->length = 0; +} + +typedef struct +{ + str_v name; + str_v seq; + str_v qual; + +} Read; + +inline void init_Read(Read* r) +{ + init_str_v(&r->name, NAME_SZIE); + init_str_v(&r->seq, SEQ_SZIE); + init_str_v(&r->qual, SEQ_SZIE); +} + +inline void clear_read(Read* r) +{ + clear_str(&r->name); + clear_str(&r->seq); + clear_str(&r->qual); +} + + +int initiReadAllReads(char *fileName); +int inputRead(Read *read); + +#endif diff --git a/main.cpp b/main.cpp index e69de29..fc25faf 100644 --- a/main.cpp +++ b/main.cpp @@ -0,0 +1,28 @@ +#include +#include +#include "CommandLines.h" +#include "Process_Read.h" +#include "Assembly.h" + + +int main(int argc, char *argv[]) +{ + + if (!CommandLine_process(argc, argv)) + return 1; + + + if (!initiReadAllReads(read_file_name)) + { + fprintf(stdout, "Cannot open read files. \n"); + return 1; + } + + + Counting(); + + + + + return 1; +}