I am trying to read in a template config (template.config) file like ( which is little more complex then then the one posted earlier -

[module1] ;;path to speedseq package binary directory $;SPEEDSEQ_BIN_DIR$; = /usr/local/packages/ ;;Sequence file 1 $;Seq1File$; = ;;Sequence file 2 $;Seq2File$; = ;;Read Group $;Read_Group$;='@RG\tID:NA12878\tSM:NA12878\tPL:ILLUMINA\tLB:NA12878\t +PU:NA12878' ;;Reference $;Reference$; = [module2] ;;Output Chromosome $;Chromosome$; = ;;use --v for verbose summary $;OTHER_ARGS$; = --v
whose fields will get populated by user input on command line eg

 perl script.pl template.config --seq1 USER_INPUT.txt --Seq2 USER_INPT2.txt --Ref USER_INPUT_REF.txt --chr_list USER_INPUT_CHR.txt

and it will output a populated a new config file like -

[module1] ;;path to speedseq package binary directory $;SPEEDSEQ_BIN_DIR$; = /usr/local/packages/ ;;Sequence file 1 $;Seq1File$; = "USER_INPUT.txt" ;;Sequence file 2 $;Seq2File$; = "USER_INPT2.txt" ;;Read Group $;Read_Group$;='@RG\tID:NA12878\tSM:NA12878\tPL:ILLUMINA\tLB:NA12878\t +PU:NA12878' ;;Reference $;Reference$; = "USER_INPUT_REF.txt" [module2] ;;Output Chromosome $;Chromosome$; = "USER_INPUT_CHR.txt" ;;use --v for verbose summary $;OTHER_ARGS$; = --v

I tried with hash also, but that is not keeping the output order same as in the input file. I would really appreciate if anyone can provide suggestions on how to approach this.

Here is my Code -

my ($template_name, $Prefix, $Seq1File,$Seq2File,$Align_Reference,$Chr +omosome_list,$Reference,$KnownSites ) = @ARGV; GetOptions ('template_config=s' => \$template_name, 'Project=s' => \$P +refix, 'Fasta_seq1=s' => \$Seq1File, 'Fasta_seq2=s' => \$Seq2File, 'R +ef_align=s' => \$Align_Reference, 'Chr_list=s' => \$Chromosome_list, +'Ref=s' => \$Reference, 'Sites=s' => \$KnownSites); open my $in_fh, '<', $template_name; my %field = map { s/^\s+|\s*\n$//g; $_; } grep { !/^\s*#|^\s*$/ } <$i +n_fh>; close $in_fh; foreach my $spec (sort keys %field) { if($spec =~ m/Output\s+Prefix/) { $field{$spec} = $field{$spec} .= $Prefix; } elsif ($spec =~ m/Sequence\s+file\s+1/) { $field{$spec} = $field{$spec} .= $Seq1File; } elsif ($spec =~ m/Sequence\s+file\s+2/) { $field{$spec} = $field{$spec} .= $Seq2File; } elsif ($spec =~ m/Align_Reference/) { $field{$spec} = $field{$spec} .= $Align_Reference; } elsif ($spec =~ m/Chromsome\s+List\s+file/) { $field{$spec} = $field{$spec} .= $Chromosome_list; } elsif ($spec =~ m/Reference/) { $field{$spec} = $field{$spec} .= $Reference; } elsif ($spec =~ m/Known\s+sites/) { $field{$spec} = $field{$spec} .= $KnownSites; } } # Print it to an output file { #open my $output_file = './test_processing.txt'; open my $out_fh, '>', './complete_nicu.txt'; foreach my $key (keys %field) { print $out_fh "$key\n"; print $out_fh "$field{$key}\n"; print "$field{$key}\n"; } close $out_fh; }
Thanks!

In reply to Read in a template file, populate using user input and and generate a new file by Bioinfocoder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.