I am trying do it the script to put out data after compare two file.
I make the script to do that but the result was not enough.

in put 1:

Num    posi	base
      1	14183	ATC
      2	26657	GGG
      3	49403	GCA
      4	57244	GCA
      5	58068	GCT
      6	58320	GTG
      7	87959	GGC
      8	93889	GGG
      9	101239	GCC
      10	119158	GGC
      etc......

in put 2:

> num	posi	Ref	Mut

    1	14185	T	C
    2	21066	A	G
    3	26659	A	G
    4	26968	A	G
    5	46277	A	G
    6	47756	T	C
    7	49403	A	G
    8	57244	A	G
    9	58069	T	C
    10	58320	A	G
    etc..........................

out put :

Posi	 base  Reft mut  
         14183	ATC    T	C
         49403	GCA    A	G
    etc.............

This my script :

use strict; 
    use warnings; 
    use autodie;  
    my (%data1,%data2);  
    open my $in, '<', 'baselist.txt'; 
    while (<$in>) { 
    next unless /^\s*\d/; 
    my ($num, $posi) = split; 
    $data1{$num} = $posi; 
    } 
     open $in, '<', 'lao1.txt'; 
    while (<$in>) { 
    next unless /^\s*\d/; 
     my ($num, $SNP_posi, $ref, $mut) = split;  
     $data2{$num}{'SNP_posi'} = $SNP_posi;   
     $data2{$num}{'ref'}  = $ref;
     $data2{$num}{'mut'} = $mut;  
     } 
     close $in; 
     open (SNP,">SNP.txt"); 
     for my $num (keys %data1) { 
      my $val = $data1{$num}; 
     for my $num2 (keys %data2) { 
         my $min = $data2{$num2}{'SNP_posi'}; 
        my $max = $data2{$num2}{'ref'}; 
        my $tengen = $data2{$num2}{'mut'};   	
     if ( $val eq $min) {  
	    print SNP $val.  "\t";  
	    print SNP $tengen.  "\t";          
            print SNP $max.  "\n";   
            last;  
            }
      } 
     } 
     close(SNP);

my out put:

Posi	Reft	mul
    58320  	G	A
    49403	          G	A
    57244	         G	A

But with that script the result is not enough with data of in put 1 and I do not know how to add the base colum of in put 1 data into out put. Coul you please show me how to solve that problems? Thank you very much.

Recommended Answers

All 2 Replies

You need to save your base information into a variable as you read it, and save this into one of your hashes, along with position.

#!/usr/bin/perl;
use strict;
use warnings;
use autodie;
my ( %data1, %data2 );
open my $in, '<', 'baselist.txt';
while (<$in>) {
    next unless /^\s*\d/;
    my ( $num, $posi, $base ) = split;#Read base into a variable
    $data1{$num}{'base_posi'} = $posi;#Save position into your hash
    $data1{$num}{'base'}      = $base;#Save base into your hash
}
open $in, '<', 'lao1.txt';
while (<$in>) {
    next unless /^\s*\d/;
    my ( $num, $SNP_posi, $ref, $mut ) = split;
    $data2{$num}{'SNP_posi'} = $SNP_posi;
    $data2{$num}{'ref'}      = $ref;
    $data2{$num}{'mut'}      = $mut;
}
close $in;
open( SNP, ">SNP.txt" );
for my $num ( keys %data1 ) {
    my $val  = $data1{$num}{'base_posi'};###
    my $base = $data1{$num}{'base'};###
    for my $num2 ( keys %data2 ) {
        my $min    = $data2{$num2}{'SNP_posi'};
        my $max    = $data2{$num2}{'ref'};
        my $tengen = $data2{$num2}{'mut'};
        if ( $val eq $min ) {
            print SNP $val . "\t";
            print SNP $base . "\t";
            print SNP $tengen . "\t";
            print SNP $max . "\n";
            last;
        }
    }
}
close(SNP);

thank you very much. It worked well!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.