source: branches/port5/PERL_SCRIPTS/GENOME/GI.pm

Last change on this file was 5858, checked in by westram, 16 years ago
  • fixed errors and warnings in perl scripts
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1#  ==================================================================== #
2#                                                                       #
3#    File      : GI.pm                                                  #
4#    Purpose   : Genome import library                                  #
5#                                                                       #
6#                                                                       #
7#  Coded by Ralf Westram (coder@reallysoft.de) in December 2003         #
8#  Copyright Department of Microbiology (Technical University Munich)   #
9#                                                                       #
10#  Visit our web site at: http://www.arb-home.de/                       #
11#                                                                       #
12#                                                                       #
13#  ==================================================================== #
14
15package GI;
16
17use strict;
18use warnings;
19use ARB;
20
21my $gb_main;
22my $columns;
23
24sub connectDB() {
25  print "Connecting to running ARB database\n";
26  $gb_main = ARB::open(":","r");
27  if (! $gb_main ) {
28    my $error = ARB::await_error();
29    print $error."\n";
30    exit 0;
31  }
32  ARB::begin_transaction($gb_main);
33}
34
35sub disconnectDB() {
36  ARB::commit_transaction($gb_main);
37  ARB::close($gb_main);
38}
39
40sub findCurrentGenome() {
41  my $organism = BIO::read_string($gb_main, 'tmp/focus/organism_name'); # aka AWAR_ORGANISM_NAME
42  if ($organism eq '') {
43    error("You have to select the target organism in ARB!");
44  }
45
46  my $gb_orga = BIO::find_organism($gb_main,$organism);
47  if (!$gb_orga) { error("'$organism' is not a organism"); }
48
49  return ($gb_orga,$organism);
50}
51
52sub unmarkGenesOfGenome($) {
53  my ($gb_genome) = @_;
54  my $gb_gene = BIO::first_marked_gene($gb_genome);
55  while ($gb_gene) {
56    ARB::write_flag($gb_gene, 0); # unmark
57    $gb_gene = BIO::next_marked_gene($gb_gene);
58  }
59}
60
61sub findORF($$$$$) {
62  my ($gb_gene_data,$genome_name,$orf,$create,$verbose) = @_;
63  my $error;
64  my $gb_orf;
65  my $gb_locus_tag = ARB::find_string($gb_gene_data, "locus_tag", $orf, 1, "down_2");
66  if (!$gb_locus_tag) {
67    if ($create==0) {
68      $error = "no gene with locus_tag '$orf' found for organism '$genome_name'";
69    }
70    else {
71      my $gb_genome = ARB::get_father($gb_gene_data);
72      $gb_orf = BIO::create_nonexisting_gene($gb_genome, $orf);
73      if (!$gb_orf) {
74        my $reason = ARB::get_error();
75        $error = "cannot create gene '$orf' ($reason)";
76      }
77      else {
78        my $gb_locus_tag = ARB::search($gb_orf, "locus_tag", "STRING");
79        if (!$gb_locus_tag) {
80          my $reason = ARB::get_error();
81          $error = "cannot create field 'locus_tag' ($reason)";
82        }
83        else {
84          $error = ARB::write_string($gb_locus_tag, $orf);
85          if ($error) {
86            $error = "Couldn't write to 'locus_tag' ($error)";
87          }
88        }
89      }
90      if (!$error and $verbose==1) { print "Created new gene '$orf'\n"; }
91    }
92  }
93  else {
94    $gb_orf = ARB::get_father($gb_locus_tag);
95  }
96
97  if (!$gb_orf) { if (!$error) { die "Internal error"; }}
98  return ($gb_orf,$error);
99}
100
101sub write_entry($$$$$$) {
102  my ($gb_container, $field_name, $field_type, $field_content, $overwrite, $verbose) = @_;
103  my $gb_field = ARB::search($gb_container, $field_name, "NONE");
104  my $error;
105  if (!$gb_field) {
106    $gb_field = ARB::search($gb_container, $field_name, $field_type);
107    if (!$gb_field) {
108      my $reason = ARB::get_error();
109      $error = "Can't create '$field_name' ($reason)";
110    }
111  }
112  else {
113    if ($overwrite==0) {
114      $error = "Field '$field_name' already exists";
115    }
116  }
117
118  if (!$error) {
119    if (!$gb_field) { die "internal error"; }
120    $error = ARB::write_as_string($gb_field, $field_content);
121    if ($error) { $error = "Cannot write to '$field_name' ($error)"; }
122  }
123
124  return $error;
125}
126
127# --------------------------------------------------------------------------------
128
129sub show_csv_info() {
130    print "  CSV may be saved with Excel and StarCalc. It simply is a\n".
131          "  comma separated list with strings quoted in \"\". The first line\n".
132          "  contains the column titles.\n";
133}
134
135sub message($) {
136  my ($msg) = @_;
137  BIO::message($gb_main, $msg);
138  print "$msg\n";
139}
140
141sub error($) {
142  my ($msg) = @_;
143  $msg = "Error: ".$msg;
144  ARB::commit_transaction($gb_main); # this undoes all changes made by this script
145#   ARB::abort_transaction($gb_main); # this undoes all changes made by this script
146
147  ARB::begin_transaction($gb_main);
148  BIO::message($gb_main, $msg);
149  BIO::message($gb_main, "Script aborted!");
150  ARB::commit_transaction($gb_main);
151  die $msg."\n";
152}
153
154# --------------------------------------------------------------------------------
155
156sub define_tokenizer_columns($) {
157  ($columns) = @_;
158}
159
160sub tokenize_columns($$) {
161  my ($line,$errline) = @_;
162  chomp $line;
163  $line .= ',';
164
165  my @array = ();
166
167  while (not $line =~ /^[ ]*$/ig) {
168    if ($line =~ /^[ ]*\"([^\"]*)\"[ ]*,/ig) {
169      my $content = $1;
170      $line = $';
171      $content =~ s/^[ ]*//ig;
172      $content =~ s/[ ]*$//ig;
173      push @array, $content;
174    }
175    elsif ($line =~ /[ ]*([0-9]+)[ ]*,/ig) {
176      push @array, $1;
177      $line = $';
178    }
179    else {
180      error("cannot parse line $errline (at '$line')");
181    }
182  }
183
184  my $cols = @array;
185  if ($cols != $columns) {
186    error("expected $columns columns (found $cols) in line $errline");
187  }
188
189  return @array;
190}
191
192# --------------------------------------------------------------------------------
193
1941; # result of module initialization
195
Note: See TracBrowser for help on using the repository browser.