comparison mrsfast-2.3.0.2/Common.c @ 0:ec628ba33878 default tip

Uploaded source code for mrsFAST
author calkan
date Tue, 21 Feb 2012 10:39:28 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:ec628ba33878
1 /*
2 * Copyright (c) <2008 - 2009>, 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 name of the <ORGANIZATION> nor the names of its contributors may be
14 * used to endorse or promote products derived from this software without specific
15 * prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 /*
31 * Author : Faraz Hach
32 * Email : fhach AT cs DOT sfu
33 * Last Update : 2009-02-01
34 */
35
36
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <sys/time.h>
40 #include <zlib.h>
41 #include <string.h>
42 #include "Common.h"
43
44
45 unsigned short SEQ_LENGTH = 0;
46 unsigned short QUAL_LENGTH = 0;
47 long long memUsage = 0;
48 /**********************************************/
49 FILE *fileOpen(char *fileName, char *mode)
50 {
51 FILE *fp;
52 fp = fopen (fileName, mode);
53 if (fp == NULL)
54 {
55 fprintf(stdout, "Error: Cannot Open the file %s\n", fileName);
56 fflush(stdout);
57 exit(0);
58 }
59 return fp;
60 }
61 /**********************************************/
62 gzFile fileOpenGZ(char *fileName, char *mode)
63 {
64 gzFile gzfp;
65 gzfp = gzopen (fileName, mode);
66 if (gzfp == Z_NULL)
67 {
68 fprintf(stdout, "Error: Cannot Open the file %s\n", fileName);
69 fflush(stdout);
70 exit(0);
71 }
72 return gzfp;
73 }
74 /**********************************************/
75 double getTime(void)
76 {
77 struct timeval t;
78 gettimeofday(&t, NULL);
79 return t.tv_sec+t.tv_usec/1000000.0;
80 }
81
82 /**********************************************/
83 char reverseCompleteChar(char c)
84 {
85 char ret;
86 switch (c)
87 {
88 case 'A':
89 ret = 'T';
90 break;
91 case 'T':
92 ret = 'A';
93 break;
94 case 'C':
95 ret = 'G';
96 break;
97 case 'G':
98 ret = 'C';
99 break;
100 default:
101 ret = 'N';
102 break;
103 }
104 return ret;
105 }
106 /**********************************************/
107 void reverseComplete (char *seq, char *rcSeq , int length)
108 {
109 int i;
110 for (i=0; i<length; i++)
111 {
112 rcSeq[i]=reverseCompleteChar (seq[length-1-i]) ;
113 }
114 }
115 /**********************************************/
116 void * getMem(size_t size)
117 {
118 memUsage+=size;
119 return malloc(size);
120 }
121 /**********************************************/
122 void freeMem(void *ptr, size_t size)
123 {
124 memUsage-=size;
125 free(ptr);
126 }
127 /**********************************************/
128 double getMemUsage()
129 {
130 return memUsage/1048576.0;
131 }
132 /**********************************************/
133 void reverse (char *seq, char *rcSeq , int length)
134 {
135 int i;
136 for (i=0; i<length; i++)
137 {
138 rcSeq[i]=seq[length-1-i];
139 }
140 }
141 /**********************************************/
142 void stripPath(char *full, char **path, char **fileName)
143 {
144 int i;
145 int pos = -1;
146
147 for (i=strlen(full)-1; i>=0; i--)
148 {
149 if (full[i]=='/')
150 {
151 pos = i;
152 break;
153 }
154
155 }
156
157 if (pos != -1)
158 {
159 sprintf(*fileName, "%s%c", (full+pos+1), '\0');
160 full[pos+1]='\0';
161 sprintf(*path,"%s%c", full, '\0');
162 }
163 else
164 {
165 sprintf(*fileName, "%s%c", full, '\0');
166 sprintf(*path,"%c", '\0');
167 }
168 }