0
|
1 /*
|
|
2 * Copyright (c) <2008 - 2012>, University of Washington, Simon Fraser University
|
|
3 * All rights reserved.
|
|
4 *
|
|
5 * Redistribution and use in source and binary forms, with or without modification,
|
|
6 * are permitted provided that the following conditions are met:
|
|
7 *
|
|
8 * Redistributions of source code must retain the above copyright notice, this list
|
|
9 * of conditions and the following disclaimer.
|
|
10 * - Redistributions in binary form must reproduce the above copyright notice, this
|
|
11 * list of conditions and the following disclaimer in the documentation and/or other
|
|
12 * materials provided with the distribution.
|
|
13 * - Neither the names of the University of Washington, Simon Fraser University,
|
|
14 * nor the names of its contributors may be
|
|
15 * used to endorse or promote products derived from this software without specific
|
|
16 * prior written permission.
|
|
17 *
|
|
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29 */
|
|
30
|
|
31 /*
|
|
32 Authors:
|
|
33 Farhad Hormozdiari
|
|
34 Faraz Hach
|
|
35 Can Alkan
|
|
36 Emails:
|
|
37 farhadh AT uw DOT edu
|
|
38 fhach AT cs DOT sfu DOT ca
|
|
39 calkan AT uw DOT edu
|
|
40 */
|
|
41
|
|
42
|
|
43
|
|
44 #include <stdio.h>
|
|
45 #include <stdlib.h>
|
|
46 #include <sys/time.h>
|
|
47 #include <zlib.h>
|
|
48 #include <string.h>
|
|
49 #include "Common.h"
|
|
50
|
|
51
|
|
52 unsigned short SEQ_LENGTH = 0;
|
|
53 long long memUsage = 0;
|
|
54 /**********************************************/
|
|
55 FILE *fileOpen(char *fileName, char *mode)
|
|
56 {
|
|
57 FILE *fp;
|
|
58 fp = fopen (fileName, mode);
|
|
59 if (fp == NULL)
|
|
60 {
|
|
61 fprintf(stdout, "Error: Cannot Open the file %s\n", fileName);
|
|
62 fflush(stdout);
|
|
63 exit(0);
|
|
64 }
|
|
65 return fp;
|
|
66 }
|
|
67 /**********************************************/
|
|
68 gzFile fileOpenGZ(char *fileName, char *mode)
|
|
69 {
|
|
70 gzFile gzfp;
|
|
71 gzfp = gzopen (fileName, mode);
|
|
72 if (gzfp == Z_NULL)
|
|
73 {
|
|
74 fprintf(stdout, "Error: Cannot Open the file %s\n", fileName);
|
|
75 fflush(stdout);
|
|
76 exit(0);
|
|
77 }
|
|
78 return gzfp;
|
|
79 }
|
|
80 /**********************************************/
|
|
81 double getTime(void)
|
|
82 {
|
|
83 struct timeval t;
|
|
84 gettimeofday(&t, NULL);
|
|
85 return t.tv_sec+t.tv_usec/1000000.0;
|
|
86 }
|
|
87
|
|
88 /**********************************************/
|
|
89 char reverseCompleteChar(char c)
|
|
90 {
|
|
91 char ret;
|
|
92 switch (c)
|
|
93 {
|
|
94 case 'A':
|
|
95 ret = 'T';
|
|
96 break;
|
|
97 case 'T':
|
|
98 ret = 'A';
|
|
99 break;
|
|
100 case 'C':
|
|
101 ret = 'G';
|
|
102 break;
|
|
103 case 'G':
|
|
104 ret = 'C';
|
|
105 break;
|
|
106 default:
|
|
107 ret = 'N';
|
|
108 break;
|
|
109 }
|
|
110 return ret;
|
|
111 }
|
|
112 /**********************************************/
|
|
113 void reverseComplete (char *seq, char *rcSeq , int length)
|
|
114 {
|
|
115 int i;
|
|
116 for (i=0; i<length; i++)
|
|
117 {
|
|
118 rcSeq[i]=reverseCompleteChar (seq[length-1-i]) ;
|
|
119 }
|
|
120 }
|
|
121 /**********************************************/
|
|
122 void * getMem(size_t size)
|
|
123 {
|
|
124 void *ret;
|
|
125 ret = malloc(size);
|
|
126 if (ret == NULL){
|
|
127 fprintf(stderr, "Cannot allocate memory. Currently addressed memory = %0.2f MB, requested memory = %0.2f MB.\nCheck the available main memory, and if you have user limits (ulimit -v).\n", getMemUsage(), (double)size);
|
|
128 exit(0);
|
|
129 }
|
|
130 memUsage+=size;
|
|
131 return ret;
|
|
132 }
|
|
133 /**********************************************/
|
|
134 void freeMem(void *ptr, size_t size)
|
|
135 {
|
|
136 memUsage-=size;
|
|
137 free(ptr);
|
|
138 }
|
|
139 /**********************************************/
|
|
140 double getMemUsage()
|
|
141 {
|
|
142 return memUsage/1048576.0;
|
|
143 }
|
|
144 /**********************************************/
|
|
145 void reverse (char *seq, char *rcSeq , int length)
|
|
146 {
|
|
147 int i;
|
|
148 int l = length;
|
|
149 if(l != strlen(seq))
|
|
150 l = strlen(seq);
|
|
151 for (i=0; i<l; i++)
|
|
152 {
|
|
153 rcSeq[i]=seq[l-1-i] ;
|
|
154 }
|
|
155 rcSeq[l] = '\0';
|
|
156 }
|
|
157 /**********************************************/
|
|
158 void stripPath(char *full, char **path, char **fileName)
|
|
159 {
|
|
160 int i;
|
|
161 int pos = -1;
|
|
162
|
|
163 for (i=strlen(full)-1; i>=0; i--)
|
|
164 {
|
|
165 if (full[i]=='/')
|
|
166 {
|
|
167 pos = i;
|
|
168 break;
|
|
169 }
|
|
170
|
|
171 }
|
|
172
|
|
173 if (pos != -1)
|
|
174 {
|
|
175 sprintf(*fileName, "%s%c", (full+pos+1), '\0');
|
|
176 full[pos+1]='\0';
|
|
177 sprintf(*path,"%s%c", full, '\0');
|
|
178 }
|
|
179 else
|
|
180 {
|
|
181 sprintf(*fileName, "%s%c", full, '\0');
|
|
182 sprintf(*path,"%c", '\0');
|
|
183 }
|
|
184 }
|