ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/msatfinder/Msatminer.pm
Revision: 1.1.1.1 (vendor branch)
Committed: Mon Mar 7 15:34:42 2005 UTC (11 years, 6 months ago) by knirirr
Branch: MAIN
CVS Tags: HEAD, HEAD
Changes since 1.1: +0 -0 lines
Log Message:
First import

Line File contents
1 ########################################################################
2 # NAME: msatminer.pm #
3 # FUNCTION: use to add together regions of a sequence (e.g. msats plus #
4 # flanks, or CDS regions, and to find out if other positions #
5 # are within those regions #
6 ########################################################################
7
8 # Beware: a global variable - @nreg - must be reset by using the new
9 # method before each new sequece for which positions are to be saved,
10 # as it was easier than keeping @nreg in an proper object - large hashes with
11 # unacceptable overheads were needed. If you have a more elegant solution,
12 # please submit it to mith@ceh.ac.uk for inclusion in msatminer. Thanks!
13
14 package Msatminer;
15 use strict;
16 use warnings;
17 use File::Copy;
18 use Cwd;
19
20 my $cwd = getcwd;
21
22 my (@nreg,$k);
23
24 sub zap
25 {
26 @nreg = ();
27 }
28
29 # add msat + flanking regions together
30 sub addregion
31 {
32 my ($start,$end) = @_;
33 if ($start > $end)
34 {
35 ($start,$end) = ($end,$start);
36 }
37 my $done = 0;
38 my $k;
39 my $tempcoding = 0;
40
41 for ($k=0;$k<@nreg;$k++)
42 {
43 # calculate sum of bases
44 #$tempcoding += $nreg[$k][1] - $nreg[$k][0] + 1;
45
46 # new region starts after old region ends
47 next if ($nreg[$k][1] < $start);
48
49 # new region ends before old region starts
50 if ($end < $nreg[$k][0])
51 {
52 splice(@nreg,$k,0,[$start,$end]);
53 $done = 1;
54 last;
55 }
56
57 # regions overlap
58 $nreg[$k][0] = $start < $nreg[$k][0] ? $start : $nreg[$k][0];
59 $nreg[$k][1] = $end > $nreg[$k][1] ? $end : $nreg[$k][1];
60 $done = 1;
61
62 # Combine two regions if the new section has brough about an overlap
63 while ($k < $#nreg and $nreg[$k][1] > $nreg[$k+1][0])
64 {
65 $nreg[$k][1] = $nreg[$k+1][1];
66 splice(@nreg,$k+1,1);
67 }
68 last;
69 }
70 unless ($done)
71 {
72 push @nreg,[$start,$end];
73 }
74 #return $tempcoding;
75 }
76
77 # determine if region is within the defined area
78 sub isitin
79 {
80 my ($what) = @_;
81 my $within = 0;
82 my $k;
83 for ($k=0; $k<@nreg; $k++)
84 {
85 if ($what >= $nreg[$k][0] and $what <= $nreg[$k][1])
86 {
87 return 1;
88 }
89 }
90 return 0;
91 }
92
93 # return length of coding regions
94 sub howlong
95 {
96 my $length;
97 for ($k=0; $k<@nreg; $k++)
98 {
99 $length += ($nreg[$k][1] - $nreg[$k][0]);
100 }
101 return $length;
102 }
103
104 # read config file
105 sub getconfig
106 {
107 my ($install,$cwd,$screendump) = @_;
108 my $config_file="msatminer.rc";
109 my %config;
110 if (-R "$cwd/$config_file")
111 {
112 Config::Simple->import_from("$cwd/$config_file", \%config);
113 print "Using config file in $cwd\n" unless ($0 =~ /viewer/ or $> == 48); # or 81...
114 }
115 elsif ($install and -R "$install/etc/$config_file")
116 {
117 Config::Simple->import_from("$install/etc/$config_file", \%config);
118 print "Using system default config file in $install/etc/\n" unless ($0 =~ /viewer/);
119 copy("$install/etc/$config_file","$cwd") or warn "Can't copy $config_file $!";
120 print <<COPY;
121 A copy of the Msatminer configuration file (msatminer.rc) has been
122 copied to your current directory ($cwd)
123 If you would like to customize the software please edit the variables
124 set in this file. If you wish to set up a new Msatminer database, you
125 will need to put a valid username and password into this configuration
126 file. This user must be permission to create new databases.
127 COPY
128 }
129 else
130 {
131 print "No configuration file was found. Please make sure you have a copy of msatminer.rc in $cwd, and try again.\n";
132 die;
133 }
134 return \%config;
135 }
136
137 # check dependencies
138 sub depcheck
139 {
140 my $find = shift;
141 my $screendump = shift;
142 my @missing;
143 foreach my $dep (@_)
144 {
145 if (-x $find->{$dep})
146 {
147 print "Dependency $dep found.\n" if ($screendump == 1);
148 }
149 else # at least try to find it even if the confit is wrong
150 {
151 my $search = `which $dep 2> /dev/null`;
152 chomp($search);
153 if ($search =~ /aliased to (\.+)/)
154 {
155 $find->{$dep} = $1;
156 print "$dep found, but not where your config file says it should be. Continuing...\n" if ($screendump == 1);
157 }
158 elsif (-x $search)
159 {
160 $find->{$dep} = $search;
161 print "$dep found, but not where your config file says it should be. Continuing...\n" if ($screendump == 1);
162 }
163 else
164 {
165 push(@missing, $dep);
166 }
167 }
168 }
169 if (@missing)
170 {
171 print "The following dependencies are not found: @missing\n";
172 print "Please add the full path to each dependency to the configuration file and try again.\n";
173 exit;
174 }
175 }
176
177 # ask user if they are sure
178 sub areyousure
179 {
180 my $dbname = shift;
181 my $dbtype = shift;
182 my $dbusername1 = shift;
183 my $dbusername2 = shift;
184 my $cgiloc = shift;
185 my $msatview = shift;
186 print "\nYou have selected the following settings:\n\n";
187 print "Database name: $dbname\n";
188 print "Database type: $dbtype\n";
189 print "Database user (privileged): $dbusername1\n";
190 print "Database user (unprivileged): $dbusername2\n";
191 print "CGI script will be installed in $cgiloc\n";
192 print "Data will be installed in $msatview\n\n";
193 print "Press <return> to continue if these are correct.\n";
194 print "If they are not, press <x><return> to exit and then edit msatminer.rc.\n";
195 chomp(my $choices = <STDIN>);
196 exit if ($choices =~ /^[Xx]/);
197 return 0;
198 }
199
200 1;