annotate lib/csv.pm @ 0:023c380900ef draft default tip

Init repository with last massbank_ws_searchspectrum master version
author fgiacomoni
date Wed, 19 Apr 2017 11:31:58 -0400
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
1 package lib::csv ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
2
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
3 use strict;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
4 use warnings ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
5 use Exporter ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
6 use Carp ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
7
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
8 use Text::CSV ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
9
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
10 use Data::Dumper ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
11
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
12 use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS);
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
13
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
14 our $VERSION = "1.0";
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
15 our @ISA = qw(Exporter);
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
16 our @EXPORT = qw( get_csv_object get_value_from_csv get_value_from_csv_multi_header );
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
17 our %EXPORT_TAGS = ( ALL => [qw( get_csv_object get_value_from_csv get_value_from_csv_multi_header )] );
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
18
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
19 =head1 NAME
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
20
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
21 My::Module - An example module
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
22
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
23 =head1 SYNOPSIS
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
24
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
25 use My::Module;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
26 my $object = My::Module->new();
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
27 print $object->as_string;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
28
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
29 =head1 DESCRIPTION
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
30
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
31 This module does not really exist, it
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
32 was made for the sole purpose of
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
33 demonstrating how POD works.
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
34
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
35 =head1 METHODS
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
36
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
37 Methods are :
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
38
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
39 =head2 METHOD new
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
40
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
41 ## Description : new
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
42 ## Input : $self
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
43 ## Ouput : bless $self ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
44 ## Usage : new() ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
45
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
46 =cut
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
47
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
48 sub new {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
49 ## Variables
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
50 my $self={};
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
51 bless($self) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
52 return $self ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
53 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
54 ### END of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
55
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
56 =head2 METHOD get_csv_object
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
57
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
58 ## Description : builds a csv object and etablishes format
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
59 ## Input : $separator
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
60 ## Output : $csv
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
61 ## Usage : my ( $csv ) = get_csv_object( $separator ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
62
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
63 =cut
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
64 ## START of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
65 sub get_csv_object {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
66 ## Retrieve Values
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
67 my $self = shift ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
68 my ( $separator ) = @_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
69
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
70 # my $csv = Text::CSV->new({'sep_char' => "$separator"});
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
71 my $csv = Text::CSV->new ( {'sep_char' => "$separator", binary => 1, } ) # should set binary attribute.
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
72 or die "Cannot use CSV: ".Text::CSV->error_diag ();
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
73
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
74 return($csv) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
75 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
76 ## END of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
77
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
78 =head2 METHOD get_value_from_csv
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
79
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
80 ## Description : extract a targeted column in a csv file
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
81 ## Input : $csv, $file, $column, $is_header
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
82 ## Output : $value
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
83 ## Usage : my ( $value ) = get_value_from_csv( $csv, $file, $column, $is_header ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
84
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
85 =cut
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
86 ## START of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
87 sub get_value_from_csv {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
88 ## Retrieve Values
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
89 my $self = shift ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
90 my ( $csv, $file, $column, $is_header ) = @_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
91
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
92 my @value = () ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
93
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
94 ## Adapte the number of the colunm : (nb of column to position in array)
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
95 $column = $column - 1 ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
96
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
97 open (CSV, "<", $file) or die $! ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
98
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
99 my $line = 0 ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
100
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
101 while (<CSV>) {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
102 $line++ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
103 chomp $_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
104 # file has a header
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
105 if ( defined $is_header ) { if ($line == 1) { next ; } }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
106 # parsing the targeted column
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
107 if ( $csv->parse($_) ) {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
108 my @columns = $csv->fields();
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
109 push ( @value, $columns[$column] ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
110 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
111 else {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
112 my $err = $csv->error_input;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
113 die "Failed to parse line: $err";
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
114 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
115 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
116 close CSV;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
117 return(\@value) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
118 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
119 ## END of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
120
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
121 =head2 METHOD get_value_from_csv_multi_header
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
122
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
123 ## Description : extract a targeted column in a csv file
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
124 ## Input : $csv, $file, $column, $is_header, $nb_header
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
125 ## Output : $value
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
126 ## Usage : my ( $value ) = get_value_from_csv_multi_header( $csv, $file, $column, $is_header, $nb_header ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
127
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
128 =cut
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
129 ## START of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
130 sub get_value_from_csv_multi_header {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
131 ## Retrieve Values
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
132 my $self = shift ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
133 my ( $csv, $file, $column, $is_header, $nb_header ) = @_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
134
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
135 my @value = () ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
136
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
137 ## Adapte the number of the colunm : (nb of column to position in array)
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
138 $column = $column - 1 ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
139
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
140 open (CSV, "<", $file) or die $! ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
141
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
142 my $line = 0 ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
143
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
144 while (<CSV>) {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
145 $line++ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
146 chomp $_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
147 # file has a header
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
148 if ( defined $is_header and $is_header eq 'yes') { if ($line <= $nb_header) { next ; } }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
149 # parsing the targeted column
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
150 if ( $csv->parse($_) ) {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
151 my @columns = $csv->fields();
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
152 my $value = $columns[$column] ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
153 $value =~s/\r|\n// ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
154 push ( @value, $value ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
155 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
156 else {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
157 my $err = $csv->error_input;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
158 die "Failed to parse line: $err";
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
159 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
160 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
161 close CSV;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
162 return(\@value) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
163 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
164 ## END of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
165
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
166 =head2 METHOD parse_csv_object
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
167
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
168 ## Description : parse_all csv object and return a array of rows
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
169 ## Input : $csv, $file
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
170 ## Output : $csv_matrix
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
171 ## Usage : my ( $csv_matrix ) = parse_csv_object( $csv, $file ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
172
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
173 =cut
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
174 ## START of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
175 sub parse_csv_object {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
176 ## Retrieve Values
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
177 my $self = shift ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
178 my ( $csv, $file ) = @_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
179
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
180 my @csv_matrix = () ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
181
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
182 open my $fh, "<:encoding(utf8)", $$file or die "Can't open csv file $$file: $!";
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
183
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
184 while ( my $row = $csv->getline( $fh ) ) {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
185 push @csv_matrix, $row;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
186 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
187 $csv->eof or $csv->error_diag();
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
188 close $fh;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
189
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
190 return(\@csv_matrix) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
191 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
192 ## END of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
193
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
194 =head2 METHOD parse_allcsv_object
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
195
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
196 ## Description : parse_all csv object and return a array of rows with or without header
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
197 ## Input : $csv, $file, $keep_header
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
198 ## Output : $csv_matrix
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
199 ## Usage : my ( $csv_matrix ) = parse_csv_object( $csv, $file, $keep_header ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
200
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
201 =cut
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
202 ## START of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
203 sub parse_allcsv_object {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
204 ## Retrieve Values
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
205 my $self = shift ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
206 my ( $csv, $file, $keep_header ) = @_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
207
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
208 my @csv_matrix = () ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
209 my $line = 1 ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
210
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
211 open my $fh, "<:encoding(utf8)", $$file or die "Can't open csv file $$file: $!";
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
212
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
213 while ( my $row = $csv->getline( $fh ) ) {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
214 if ( ( $keep_header eq 'n' ) and ($line == 1) ) { }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
215 else { push @csv_matrix, $row; }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
216 $line ++ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
217 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
218 my $status = $csv->eof or $csv->error_diag();
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
219 close $fh;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
220
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
221 return(\@csv_matrix, $status) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
222 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
223 ## END of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
224
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
225
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
226 =head2 METHOD write_csv_from_arrays
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
227
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
228 ## Description : write a csv file from list of rows
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
229 ## Input : $csv, $file_name, $rows
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
230 ## Output : $csv_file
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
231 ## Usage : my ( $csv_file ) = write_csv_from_arrays( $csv, $file_name, $rows ) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
232
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
233 =cut
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
234 ## START of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
235 sub write_csv_from_arrays {
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
236 ## Retrieve Values
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
237 my $self = shift ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
238 my ( $csv, $file_name, $rows ) = @_ ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
239
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
240 my $fh = undef ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
241 $csv->eol ("\n"); ## end-of-line string to add to rows
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
242 $csv->quote_char(undef) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
243 open $fh, ">:encoding(utf8)", "$file_name" or die "$file_name: $!";
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
244
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
245 my $status = $csv->print ($fh, $_) for @{$rows};
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
246 close $fh or die "$file_name: $!";
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
247
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
248 return(\$file_name) ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
249 }
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
250 ## END of SUB
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
251
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
252 1 ;
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
253
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
254
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
255 __END__
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
256
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
257 =head1 SUPPORT
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
258
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
259 You can find documentation for this module with the perldoc command.
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
260
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
261 perldoc csv.pm
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
262
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
263 =head1 Exports
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
264
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
265 =over 4
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
266
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
267 =item :ALL is get_csv_object, get_value_from_csv
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
268
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
269 =back
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
270
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
271 =head1 AUTHOR
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
272
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
273 Franck Giacomoni E<lt>franck.giacomoni@clermont.inra.frE<gt>
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
274
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
275 =head1 LICENSE
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
276
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
277 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
278
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
279 =head1 VERSION
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
280
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
281 version 1 : 23 / 10 / 2013
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
282
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
283 version 2 : ??
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
284
023c380900ef Init repository with last massbank_ws_searchspectrum master version
fgiacomoni
parents:
diff changeset
285 =cut