summaryrefslogtreecommitdiff
path: root/meme/log2mark.pl
blob: b47436f6085d17843d54d72e0b4fa72ae149edc9 (plain)
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
#!/usr/bin/perl
#
# log2mark.pl - Convert meme log output to markers
#
# Written 2016 by Werner Almesberger
# Copyright 2016 by Werner Almesberger
#
# This program 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 2 of the License, or
# (at your option) any later version.
#

#
# Typical use:
#
# meme -l log -m foo.mark ...
# log2mark.pl -f Z log
#
# Then copy entires output by log2mark.pl manually to foo.mark and add them to
# the display by reloading with R.
#


sub usage
{
	print STDERR <<"EOF";
usage: $0 [-f] tag [file ...]

  -f  follow file (like tail -f)
EOF
	exit(1);
}


while ($ARGV[0] =~ /^-./) {
	if ($ARGV[0] eq "-f") {
		$follow = 1;
	} else {
		&usage;
	}
	shift @ARGV;
}

$field = shift @ARGV;
&usage unless defined $field;

if ($follow) {
	$in = shift @ARGV;
	open(IN, $in) || die "$in: $!" if defined $in;
}

$d = 0;
while (1) {
	if (!defined $in) {
		$_ = <>;
		exit unless defined $_;
	} else {
		$_ = <IN>;
		if (!defined $_) {
			exit unless $follow;
			close IN;
			open(IN, $in) || die "$in: $!" if defined $in;
		}
	}

	chop;

	$d = /^D:/ if /^\S/;
	if (/^([AB]):\s+X\s+(\S+)\s+Y\s+(\S+)/) {
		if ($1 eq "A") {
			@a = ($2, $3);
		} else {
			@b = ($2, $3);
		}
		next;
	}
	next unless $d;
	/\S*\s+/;
	$tag = undef;
	for (split(/\s+/, $')) {
		if (defined $tag) {
			print "$a[0]\t$a[1]\t$b[0]\t$b[1]\t$_\n"
			    if $tag eq $field;
			undef $tag;
		} else {
			$tag = $_;
		}
	}
}