Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

As I am biologist and new to perl, I would like to get help of perl expert

cat input.txt

##gff-version 2
##source-version geneious 5.6.3
gi371443188gbJH5566721_extraction_reversed  Geneious    CDS 1043    1132    .    +   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed  Geneious    CDS 2063    2260    .    +   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed  Geneious    CDS 2336    2593    .    +   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed  Geneious    CDS 3474    3633    .    +   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed  Geneious    extracted region    1   13933   .   +   .   Name=Extracted region from gi|371443188|gb|JH556672.1|;Extracted interval="2010140 <- 2024072"

My output.txt

gi371443188gbJH5566721_extraction_reversed  CDS 2023029 2022940 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed  CDS 2022009 2021812 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed  CDS 2021736 2021479 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi371443188gbJH5566721_extraction_reversed  CDS 2020598 2020439 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
###

My expected ouput

gi_371443188_gb_JH5566721  gene    2020598 2023029 .     -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.13412469767431
gi_371443188_gb_JH5566721   CDS 2020598 2020439 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi_371443188_gb_JH5566721   CDS 2021736 2021479 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi_371443188_gb_JH5566721   CDS 2022009 2021812 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
gi_371443188_gb_JH5566721   CDS 2023029 2022940 .    -   .   Name=Xm ITGB3;created by=User;modified by=User;ID=Pa0FVoXpt/GgL1I/VO7LY0UlFAc.1341246976743.1
###

I would like help of perl expert in reformating my output in perl code given below.

1.I would like add under score to array[0] in output (i.e gi371443188gbJH5566721_extraction_reversed as gi_371443188_gb_JH5566721)

2.sort rows of CDS in ascending order based on values of column3 & 4 of the output (see expected output)

3.Add new row at the top of file as gi_371443188_gb_JH556672.1 gene with minimum & maximum value of CDS rows (see expected output)

My Perl code is given below.

#usr/bin/perl;

open(FH,"$ARGV[0]");

my %num="";
my %all="";
while(<FH>){
 chomp $_; 
   my @array=split("\t"); #print "$array[2]\n";
   if($array[2] eq "extracted region"){ 
   $array[8]=~/.*\w+=\"\d+ <- (\d+)"/gm; 
   $num{$array[0]}="$1";
   }
   if($array[2] eq "CDS"){
   $all{$array[0]}.="$_\n";
    }
    }

   foreach $i (keys %all){

    my @line=split "\n",$all{$i};
    for ($j=0;$j<=$#line;$j++){
    my @new_line=split "\t",$line[$j];
    my $pos1=$num{$i}-$new_line[3];
    my $pos2=$num{$i}-$new_line[4]; #print $num{$i}; exit;
    $new_line[6] =~ s/\+/-/g;
    print "$new_line[0]\t$new_line[2]\t$pos1\t$pos2\t$new_line[5]\t$new_line[6]\t$new_line[7]\t$new_line[8]\n";
    }
    }
    print "###\n";
share|improve this question
    
I don't fully understand. Do you run the perl code with input.txt and get output.txt? Would you like to get the second output instead? Your perl code doesn't compile in my test, so I couldn't try that at least. –  Birei Jul 13 '12 at 17:57
    
Yes, I have tested with input.txt and I want second ouput. Try compling the input file with tab separated columns. –  jack Jul 13 '12 at 18:37
    
I commented the line use strict and run it like perl script.pl input.txt assuming script.pl has the code of your question, and the output was only ###. What is your whole code and how do you run it to get the output.txt of your question? –  Birei Jul 13 '12 at 19:00
    
I didnt use the line use strict. Try compliling with out using strict. –  jack Jul 13 '12 at 19:22
add comment

1 Answer

up vote 2 down vote accepted

This will do it (i.e., my output matches yours), although it's not quite the cleanest.

The regexp's for adding the underscores can probably be combined. But for the sorting, you need to push all of your output lines to a list and then sort it (it could also be done on the incoming side, but you still need all of it in one place first).

--- test.pl~    2012-07-13 12:04:36.000000000 -0700
+++ test.pl 2012-07-13 12:17:58.000000000 -0700
@@ -1,4 +1,4 @@
-#usr/bin/perl
+#!/usr/bin/perl
 use strict;

 open(FH,"$ARGV[0]");
@@ -18,6 +18,7 @@
     }

    my $i;
+   my @output;
    foreach $i (keys %all){

     my @line=split "\n",$all{$i};
@@ -27,8 +28,15 @@
     my $pos1=$num{$i}-$new_line[3];
     my $pos2=$num{$i}-$new_line[4]; #print $num{$i}; exit;
     $new_line[6] =~ s/\+/-/g;
-    print "$new_line[0]\t$new_line[2]\t$pos1\t$pos2\t$new_line[5]\t$new_line[6]\t$new_line[7]\t$new_line[8]\n";
+    $new_line[0] =~ s/gi/gi_/;
+    $new_line[0] =~ s/gb/_gb_/;
+    $new_line[0] =~ s/_extraction_reversed//;
+    push @output, "$new_line[0]\t$new_line[2]\t$pos1\t$pos2\t$new_line[5]\t$new_line[6]\t$new_line[7]\t$new_line[8]\n";
     }
     }
+    @output = sort (@output);
+    foreach my $out (@output) {
+    print $out;
+    }
     print "###\n";
share|improve this answer
    
thanks bahamat. I would like to add row 1 (see expected ouput) with $new_line[0], "gene" in column 2 and minimum value of $pos1 and maximum value of $pos2 and $new_line[5]\t$new_line[6]\t$new_line[7]\t$new_line[8]\n –  jack Jul 13 '12 at 19:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.