File: osfinder2anchors.pl

package info (click to toggle)
murasaki 1.68.6-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,928 kB
  • ctags: 3,100
  • sloc: cpp: 16,010; perl: 8,365; makefile: 186
file content (134 lines) | stat: -rwxr-xr-x 4,090 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/perl -w

#Copyright (C) 2006-2008 Keio University
#(Kris Popendorf) <comp@bio.keio.ac.jp> (2006)
#
#This file is part of Murasaki.
#
#Murasaki is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation, either version 3 of the License, or
#(at your option) any later version.
#
#Murasaki is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with Murasaki.  If not, see <http://www.gnu.org/licenses/>.

use strict;

use Getopt::Long;
use Pod::Usage;
use File::Basename;
#use Data::Dump qw {dump};

use strict;

BEGIN {
  unshift(@INC,(fileparse($0))[1].'perlmodules');
}
use Murasaki;

my ($man,$help,$makecds,$seqfile);
my (@seqnames,$output_opt);
my $opterr=
  GetOptions('help|?' => \$help, man => \$man, 'seq=s'=>\$seqfile,
	    'output=s'=>\$output_opt);
pod2usage(1) if $help;
pod2usage(-exitstatus => 0, -verbose => 2) if $man;

pod2usage({-verbose=>1,-exitval=>2,-message=>'Need some input file...'}) if $#ARGV<0;

my $round;
foreach my $inf (@ARGV){
  my ($iprefix,$dir,$ext)=fileparse($inf,qr{\..*});
  $seqfile="$dir/$iprefix.seqs" unless $seqfile;
  die "Need $seqfile (or specify manually)" unless -f $seqfile;
  open(my $infh,$inf) or die "Couldn't open $inf";
  my @seqs=map { {file=>$_} } (split(/\n/,slurp($seqfile)));
  foreach my $seq (@seqs){
    print "Loading regions for $seq->{file}\n";
    $seq->{regions}=[getRegions($seq->{file})];
  }

  my $outf=($output_opt and $round) ? "$output_opt.$round":$output_opt;
  $outf="$inf.anchors" unless $outf;
  my ($oprefix,$odir,$oext)=fileparse($outf,qr/\.anchors/);
  my $oseq="$dir$oprefix.seqs";
  print STDERR "Warning: Overwriting $oseq\n" if -f $outf;
  open(my $oseqfh,">$oseq");
  foreach my $seq (@seqs){
    print $oseqfh "$seq->{file}\n";
  }
  print STDERR "Warning: Overwriting $outf\n" if -f $outf;
  open(my $ofh,">$outf");
  while(my $line=<$infh>){
    chomp $line;
    my @anchors=getAnchors($line);
    die "Illegal number of anchors (".scalar(@anchors).")" unless scalar(@anchors)==scalar(@seqs);
    print $ofh join("\t",
		    map {fixInRegion($anchors[$_],$seqs[$_]->{regions})} 0..$#anchors)."\n";
  }
}

sub fixInRegion {
  my ($anchor,$regions)=@_;
  return ($anchor->{start},$anchor->{stop},$anchor->{sign}) unless $regions;
  die "programming error ftl!" unless ref($regions) eq 'ARRAY';
  my $region=$$regions[$anchor->{chrom}-1];
  die "Invalid region: $anchor->{chrom}" unless $region;
  return ($anchor->{start}+$region->{start}-1,$anchor->{stop}+$region->{start}-1,$anchor->{sign});
}

sub anchorToString {
  my ($anchor)=@_;
  return join("\t",$anchor->{start},$anchor->{stop},$anchor->{sign});
}

sub getAnchors {
  my ($line)=@_;
  my @d=split(/\s+/,$line);
  my @anchors;
  while(@d){
    my ($chrom,$start,$stop,$sign)=map {shift(@d)} (1..4);
    ($start,$stop)=(-$start,-$stop) if($start<0);
    ($start,$stop)=($stop,$start) if $stop<$start; #be flexible
    push(@anchors,{chrom=>$chrom,start=>$start,stop=>$stop,sign=>$sign});
  }
  return @anchors
}

sub getRegions {
  my ($file)=@_;
  die "No file specified?" unless $file;
  my @res;
  if($file=~m/\.stitch$/){
    open(my $fh,$file) or die "Couldn't open stitch sub-file $file";
    while(<$fh>){
      my @dat=split(/\t/);
      push(@res,{file=>$dat[0],length=>$dat[1],start=>$dat[2],stop=>$dat[3]});
    }
  }else{
    unless(-f "$file.len"){
      system("$root/getsegments $file");
    }
    open(my $fh,"$file.len") or die "Couldn't open segment data for $file";
    my $length=<$fh>; #first line is length only
    my $at=1;
    while(<$fh>){
      my ($length,$meta)=m/^(\d+)\t(.*)/;
      push(@res,{length=>$length,start=>$at,stop=>$at+$length});
      $at+=10;
    }
  }
  return @res;
}

sub slurp {
  local $/;
  open(my $fh,"@_") or return;
  return <$fh>;
}