package PubMed::BaselineChange; use strict; use warnings; use Carp qw(confess); use File::Compare; use File::Copy; use File::Temp; use Data::Dumper; our $raw = sub { my $fufi=shift // "I need a file here."; my $blix=shift // "I need a blix here."; ## for real? my $real=shift // ''; if(not $blix=~m|^\d{2}n$|) { confess "I don't like your blix '$blix'."; } my $tmp = File::Temp->new(UNLINK => 0); open(I,"< $fufi") or confess "I can't open $fufi."; my $line; while($line=) { chomp $line; if($line=~m|^$blix\d+ \d+ \d+ \d+$|) { ## old line, to be skipped next; } print $tmp "$line\n"; } $tmp->close; close I; my $tmp_filename=$tmp->filename(); if(compare($fufi,$tmp_filename)==0) { print "$tmp_filename and $fufi are the same.\n"; unlink $tmp_filename; return; } if(not $real) { print "I need to copy $tmp to $fufi but I will not do it.\n"; exit; #unlink $tmp; return; } print "I copy $tmp_filename to $fufi\n"; copy($tmp_filename,$fufi); unlink $tmp_filename; }; our $sox = sub { my $fufi=shift // "I need a file here."; my $blix=shift // "I need a blix here."; ## for real? my $real=shift // ''; if(not $blix=~m|^\d{2}n$|) { confess "I don't like your blix '$blix'."; } my $tmp = File::Temp->new(UNLINK => 0); my $tmp_filename=$tmp->filename(); open(I,"< $fufi") or confess "I can't open $fufi."; my $line; while($line=) { chomp $line; while($line=~m|( \d+ \d+ $blix\d+)|) { my $to_go=$1; $line=~s|$to_go||; } ## what remains? if($line=~m|^\s*$|) { ## old line, to be skipped next; } ## added after the first run in 2015 if($line=~m|^\d+$|) { ## old line, to be skipped next; } print $tmp "$line\n"; } $tmp->close; close I; if(compare($fufi,$tmp_filename)==0) { print "$tmp_filename and $fufi are the same.\n"; unlink $tmp; return; } if(not $real) { print "I need to copy $tmp to $fufi but I will not do it.\n"; exit; #unlink $tmp; return; } print "I copy $tmp to $fufi.\n"; copy($tmp_filename,$fufi); unlink $tmp; }; ## removes bad pmids from dain our $dain = sub { my $fufi=shift // confess "I need a file here."; my $gone_pmids=shift // confess "I need a gone_pmids hashref here."; my $ref_gone_pmids=ref $gone_pmids; if(not $ref_gone_pmids='HASH') { confess "I need a hashref here, not $ref_gone_pmids."; } ## for real? my $real=shift // ''; my $tmp = File::Temp->new(UNLINK => 0); my $tmp_filename=$tmp->filename(); open(I,"< $fufi") or confess "I can't open $fufi."; my $line; while($line=) { chomp $line; if(not $line=~m|^(\d+)$|) { confess "bad line '$line'." #print $tmp "$line\n"; #next; } my $pmid=$1; if(not defined($gone_pmids->{$pmid})) { print $tmp "$line\n"; next; } ## if defined, we don't write } $tmp->close; close I; if(compare($fufi,$tmp_filename)==0) { print "$tmp_filename and $fufi are the same.\n"; unlink $tmp; return; } if(not $real) { print "I need to copy $tmp to $fufi but I will not do it.\n"; exit; #unlink $tmp; return; } print "I copy $tmp_filename to $fufi.\n"; copy($tmp_filename,$fufi); unlink $tmp; }; 1;