#!/usr/bin/perl #################################### # # SeparateElandReads.pl -f Eland-file -o output-path # # reads Eland-files line by line # writes line to file contig_name.eland # if file exits: appends # if file does not exits: creates # known bugs: does not delete files in output folder before writing! DO IT MANUALLY! ##################################### use diagnostics; use strict; use Carp; use FileHandle; use File::Path; use File::Basename; use Getopt::Std; my @elandline = (); my @temp = (); my $FILE = FileHandle->new(); my $seq = ""; my $num = 0; my $i; my $outfile = ""; my $contigname = ""; my $j; my $seqname = ""; my $elandlinenumber = 0; my $linesread = 0; $| = 1; #print immediately use vars qw( $opt_f $opt_o); # Command line parsing # getopts('f:o:'); # # Handle missing / at path end: # if ($opt_o=~/\/$/) {$opt_o=$opt_o} else {$opt_o=$opt_o."/"} print "Reading file: ".$opt_f."\n"; print "Writing to output directory: ".$opt_o."\n"; #replace Mac and PC CRLF by UNIX LF system ("perl -pi -e 's/\r/\n/g' $opt_f"); # # Read input file and write each line in an array cell # open($FILE,$opt_f) || croak(sprintf("Cannot open file \"%s\"",$opt_f)); while ( <$FILE> ) { chomp; s/\ /_/g; if ( /^>(\S+)/) { push(@elandline,$_); $linesread++; } else { #do nothing } } close($FILE) || croak(sprintf("Cannot close open file \"%s\"",$opt_f)); print "File read. ".$linesread." lines processed."."\n"; # # Loop through array and write into individual files # for $i (0 .. $#elandline) { $elandlinenumber=$elandlinenumber+1; @temp = (); @temp=split('\t',$elandline[$i]); $contigname=$temp[6]; $outfile = $opt_o . $contigname . ".eland"; open($FILE,">>".$outfile) || croak(sprintf("Cannot open file \"%s\"",$outfile)); print $FILE (($elandline[$i])."\n"); } close ($FILE); print "Finished. ".$elandlinenumber." sequences processed."."\n";