Add static text in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add static text in perl
# 1  
Old 02-03-2016
Add static text in perl

I am trying to add a static value to a field in perl. Basically, what happens is a file is created and "null" results in the fields then after some manipulation a field (AB) is split and the text from that is parsed into the desired fields. All that works great what doesn't is the line in bold where I am trying to add a static value of "VUS" to field 46. As of now it is still "Null". Thank you Smilie.

Code:
my @colsleft = map "Null",(1..$#left);	  
	   my @colsright = map "Null",(0..$#right);	 
	  	  
	  while(<FH>)  {  # puts row of input file into $_ 
	  chomp;
	  my @vals = split/\t/; # this splits the line at tabs
	  my @mutations=split/,/,$vals[9]; # splits on comma to create an array of mutations
	  my ($gene,$transcript,$exon,$coding,$aa);
	  for (@mutations)
	  {	
			($gene,$transcript,$exon,$coding,$aa) = split/\:/; # this takes col AB and splits it at colons
			grep {$transcript eq $_} keys %nms or next;
		}
		# warn join ("\t",$gene,$transcript,$exon,$coding,$aa);
		my @out=($.,@colsleft,$_,@colsright);
		$out[2]=$gene;
		$out[3]=$nms{$transcript};
		$out[4]=$transcript;
		$out[15]=$coding;
		$out[17]=$aa;
		$out[45]="VUS";

# 2  
Old 02-03-2016
Please post your complete program. We can't tell from this why its not being printed.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-03-2016
The complete program is below with the only thing not working is the static text. Thank you Smilie.

Code:
 #!/bin/perl
   use strict;
   my %nms=("NM_004004.5"=>"AR","NM_004992.3"=>"XLD","NM_003924.3"=>"AD");  # match NM and inheritence with gene
    
 # Accept the input and output files as parameters
       my $input_file = $ARGV[0];
       my $output_file = $ARGV[1];
     
       # Set the header columns to be added to the left
       # and to the right of the header in the input file
      my @left =  (
                       "Index",
                       "Chromosome Position",
                       "Gene",
                       "Inheritance",
                       "RNA Accession",
                       "Chr",
                       "Coverage",
                       "Score",
                       "A(#F,#R)",
                       "C(#F,#R)",
                       "G(#F,#R)",
                       "T(#F,#R)",
                       "Ins(#F,#R)",
                       "Del(#F,#R)",
                       "SNP db_xref",
                       "Mutation Call",
                       "Mutant Allele Frequency",
                       "Amino Acid Change"
                  );
      my @right = (
                      "HP",
                      "SPLICE",
                      "Pseudogene",
                      "Classification",
                      "HGMD",
                      "Disease",
                      "Sanger",
                      "References"
                  );
    
      # open the input file, read the header line and sandwich it
      # between @left and @right arrays
      my $final_header;
      open (FH, "<", $input_file) or die "Can't open $input_file: $!";
   chomp(my $hdr=<FH>);
       $final_header = sprintf("%s\t%s\t%s\n", join("\t", @left), $hdr, join("\t",@right));
       # final header is set, print it to the output file
      open (OF, ">", $output_file) or die "Can't open $output_file: $!";
      print OF "$final_header";
     # close (FH) or die "Can't close $output_file: $!";
   
    my @colsleft = map "Null",(1..$#left);   
    my @colsright = map "Null",(0..$#right);  
      
   while(<FH>)  {  # puts row of input file into $_ 
   chomp;
   my @vals = split/\t/; # this splits the line at tabs
   my @mutations=split/,/,$vals[9]; # splits on comma to create an array of mutations
   my ($gene,$transcript,$exon,$coding,$aa);
   for (@mutations)
   { 
   ($gene,$transcript,$exon,$coding,$aa) = split/\:/; # this takes col AB and splits it at colons
    grep {$transcript eq $_} keys %nms or next;
  }
  # warn join ("\t",$gene,$transcript,$exon,$coding,$aa);
  my $classification = "VUS";
  my @out=($.,@colsleft,$_,@colsright);
  $out[2]=$gene;
  $out[3]=$nms{$transcript};
  $out[4]=$transcript;
  $out[15]=$coding;
  $out[17]=$aa;
  $out[45]=$classification;
  
  #print OF join("\t",$.,@colsleft,$_,@colsright),"\n";   # row data is set, print it to the output file  
  print OF join("\t",@out),"\n";   # row data is set, print it to the output file   
     }

# 4  
Old 02-03-2016
In general, with push you can add an element to the end of an array.
Code:
push @out, "VUS";

Because you assemble @out anyway, you can add it just there:
Code:
my @out=($., @colsleft, $_, @colsright, "VUS");

# 5  
Old 02-03-2016
How does perl know that VUS goes in [45]? Thank you Smilie.
# 6  
Old 02-03-2016
Quote:
Originally Posted by cmccabe
How does perl know that VUS goes in [45]? Thank you Smilie.
You are doing correctly:
$out[45] = "VUS";
if you want to guarantee that the 46th element of @out has the string "VUS". push will only add to the end of the array, whatever that might be next as element is concerned.

$out[45] = "VUS"; pretty much assigns it and there is not way you have "NULL" after that unless it gets changed or your understanding of what you are looking at is not correct.
Do, you want to test it?
Code:
$out[45] = "VUS";
print "$out[45]\n";


Anything I might say now, it is not a criticism of your posted code, but rather trying to understand why you do it.
Code:
my @colsleft = map "null",(1..$#left);
my @colsright = map "null",(0..$#right);

I think you are trying to create two arrays of certain size with some empty value. But what you are creating is two arrays with each element holding the string "null" which it has no meaning in Perl as empty. In Perl, the equivalent would be undef
Nevertheless, you do not need to worry about that. If you create an array and manually add two elements but not in order, the remaining elements are created with the undef value assigned to it.

Example:
I am going to create an array named @a and populate only the third element $a[2] and the eleventh element $a[10]

Code:
perl -MData::Dumper -e '@a[2] = "Third element of a"; @a[10]="Eleventh element of a"; END{print Dumper \@a}'

Take a look at the representation of that array courtesy of the Data::Dumper module:

Code:
$VAR1 = [
          undef,
          undef,
          'Third element of a',
          undef,
          undef,
          undef,
          undef,
          undef,
          undef,
          undef,
          'Eleventh element of a'
        ];

Let me point to a few issues with your posted code.
Code:
    for (@mutations) {
            ($gene,$transcript,$exon,$coding,$aa) = split/\:/; # this takes col AB and splits it at colons
            grep {$transcript eq $_} keys %nms or next;
    }

I do not know how may times the for loop is being executed because it depends of the size of @mutations set previously by my @mutations=split/,/,$vals[9], however I know that the work it does is in vain, since $gene,$transcript,$exon,$coding,$aa will only keep the last iteration of it. The rest of them are overwritten through the loop.
Code:
grep {$transcript eq $_} keys %nms or next;

Is not producing much since the result is not saved anywhere.

Code:
   $out[2]=$gene;
    $out[3]=$nms{$transcript};
    $out[4]=$transcript;
    $out[15]=$coding;
    $out[17]=$aa;
    $out[45]=$classification;

Remember, many of these will only contain the last iteration from the for loop.

Hopefully, I have given you something to consider.

Last edited by Aia; 02-04-2016 at 01:43 AM.. Reason: correct wrong word.
# 7  
Old 02-03-2016
@Aia, please feel free to make any improvements/suggestions to any code posted by me. I am a scientist learning programming and so this is still new to me. I learn from each post and try to improve each time. Thank you very much Smilie. I will try again tomorrow and post back.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add line breaks to perl command with large text in single quotes?

Below code extracts multiple field values from XML into array and prints all in one line. perl -nle '@r=/(?: jndiName| authDataAlias| value| minConnections| maxConnections| connectionTimeout| name)="(+)/g and print join ",",$ENV{tIPnSCOPE},$ENV{pr ovider},$ENV{impClassName},@r' server.xml ... (4 Replies)
Discussion started by: kchinnam
4 Replies

2. Shell Programming and Scripting

awk to skip lines find text and add text based on number

I am trying to use awk skip each line with a ## or # and check each line after for STB= and if that value in greater than or = to 0.8, then at the end of line the text "STRAND BIAS" is written in else "GOOD". So in the file of 4 entries attached. awk tried: awk NR > "##"' "#" -F"STB="... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Programming

Perl find text and add line

Hi All I need to add a line to a file but after a certain block of text is found The block of text looks like this <RDF:Description RDF:about="urn:mimetype:video/quicktime" NC:value="video/quicktime" and i need to add this in the next line down ( note there is... (4 Replies)
Discussion started by: ab52
4 Replies

4. Programming

Even the Static cURL Library Isn't Static

I'm writing a program which uses curl to be run on Linux PCs which will be used by a number of different users. I cannot make the users all install curl on their individual machines, so I have tried to link curl in statically, rather than using libcurl.so. I downloaded the source and created a... (8 Replies)
Discussion started by: BrandonShw
8 Replies

5. UNIX for Advanced & Expert Users

Static code analysis for Perl

As an addition to our ongoing investigation into static code analysis tools for a Perl programming we are maintaining, can anyone recommend a certain tool that he/she is experienced with? We are already actively using perl::critic (Perl::Critic) and rats... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

Removing text between two static strings

Hi everyone, I need to replace the text between two strings (html tags) and I'm having trouble figuring out how to do so. I can display the text with sed but I'm not having any luck deleting the text between the two strings. My file looks like this: <oths>test</oths><div class="text">1928... (2 Replies)
Discussion started by: cg2
2 Replies

7. IP Networking

I need HELP to Set up Coyote Linux router with 1 static IP & 64 internal static IP

hello, i need help on setting my coyote linux, i've working on this for last 5 days, can't get it to work. I've been posting this message to coyote forum, and other linux forum, but haven't get any answer yet. Hope someone here can help me...... please see my attached picture first. ... (0 Replies)
Discussion started by: dlwoaud
0 Replies

8. Shell Programming and Scripting

How to add static lines to short file?

I've got a simple log file that looks something like this: And I need to append it to look like this: So I just want to add a timestamp and a static (non-variable) word to each line in the file. Is there an easy scripted way to cat the file and append that data to each line....?? (4 Replies)
Discussion started by: kevinmccallum
4 Replies

9. Red Hat

permanently add static route

I have a machine with an interface that has two different addresses on CentOS 5 eth0: 10.20.21.77 eth0:1 141.218.1.221 If I issue this command I get the result I'm looking for. /sbin/route add -net 141.218.1.0 netmask 255.255.255.0 gw 10.20.21.77 ip route show dev eth0 141.218.1.0/24... (1 Reply)
Discussion started by: beaker457
1 Replies

10. Solaris

Add Static Routes to new physical address

Hi, I need help to add new route: 10.252.0.138, GW 10.252.0.129 to e1000g1 and 10.252.0.10, GW 10.252.0.1 to e1000g2 tnx (4 Replies)
Discussion started by: mehrdad68
4 Replies
Login or Register to Ask a Question