-
Notifications
You must be signed in to change notification settings - Fork 0
/
makestar.pl
executable file
·179 lines (158 loc) · 4.87 KB
/
makestar.pl
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#! /usr/bin/perl -w
# script to make a Relion selection setup from Appion particle picks and downloads
# select according to Thon ring max (best of Appion 50% or package)
#
# three files needed:
# 1 ctf star file downloaded from Appion
# 2 best ctf dat file downloaded from Appion
# 3 downloaded coordinate file
# input star file and dat file should both refer to enn-a
# particle picks should have been done on enn-a-DW files
#outputs: ctfdata_allgood.star
# --replaces enn-a with enn-a-DW in output star file
# -- also works for esn-a, enn-b, etc.
# Appionpicks direcory, with star formatted coordinate files
#
# To start in Relion, select ctfdata_allgood as the micrograph star file
# and Appionpick/coords_suffix_manualpick.star as the Input Coordinates file
# wjr 06-28-19
print "star files:\n";
@files = glob ("micrographs*.star");
foreach $file (@files) {print "$file\n";}
print "Enter current ctf star file (default $files[0]): ";
chomp ($starfile = <STDIN>);
unless ($starfile) {$starfile = $files[0];}
print "dat files:\n";
@files = glob ("*.dat");
foreach $file (@files) {print "$file\n";}
print "input ctf data file (default $files[0]): ";
chomp ($datfile=<STDIN>);
unless ($datfile) {$datfile = $files[0];}
print "csv files:\n";
@files = glob ("*.csv");
foreach $file (@files) {print "$file\n";}
print "input particle data file (default $files[0]): ";
chomp ($ptclfile=<STDIN>);
unless ($ptclfile) {$ptclfile = $files[0];}
print "Enter Thon ring limit in A (default 4.5): ";
chomp ($ctflimit = <STDIN>);
unless ($ctflimit) {$ctflimit = 4.5;}
my $pickdir = 'Appionpick';
my $micdir = 'micrographs';
#print "Enter name of micrographs directory (default $micdir): ";
#chomp ($line = <STDIN>);
#if ($line) {$micdir=$line;}
open (IN,$datfile) or die "cannot read $datfile\n";
<IN>; #skip first line of .dat file
while (<IN>) {
@data=split(" ",$_);
$imagename=$data[$#data];
$imagename .= '.mrc' ; #need to add extension
$ctf_pack = $data[9];
$ctf_app = $data[8];
$ctf_avg = $ctf_pack <= $ctf_app ? $ctf_pack : $ctf_app ; #chooses lowest value
if ($ctf_app == 0) {$ctf_avg = $ctf_pack;}
$ctfdata{$imagename} = $ctf_avg
}
close (IN);
open (IN,$starfile) or die "cannot read $starfile\n";
while (<IN>) {
if ($_ =~ m/rlnPhaseShift/) {
last;
}
}
my %stardata;
while (<IN>) {
chomp $_;
@data = split(" ",$_);
@data1 = split(/\//,$data[0]);
my $filename = pop @data1;
$stardata{$filename} =$_;
# print "$filename\n";
}
close (IN);
my $outfile = 'ctfdata_allgood.star';
open (OUT,">$outfile") or die "cannot write\n";
my $header = <<"END_HEADER";
data_
loop_
_rlnMicrographName #1
_rlnCtfImage #2
_rlnDefocusU #3
_rlnDefocusV #4
_rlnDefocusAngle #5
_rlnVoltage #6
_rlnSphericalAberration #7
_rlnAmplitudeContrast #8
_rlnMagnification #9
_rlnDetectorPixelSize #10
_rlnCtfFigureOfMerit #11
_rlnPhaseShift #12
_rlnCtfMaxResolution #13
END_HEADER
print OUT $header;
#now keep the good ctf-only no-thickness data
@ctfkeys = sort keys %ctfdata;
my $ctfmean=0;
print "searching through $#ctfkeys micrographs with no thickness data available\n";
foreach $key (@ctfkeys) {
if ($ctfdata{$key} <$ctflimit ) {
$stardata{$key} =~ s/e([ns])n-([a-z])/e$1n-$2-DW/g; # add DW flag to line
print OUT "$stardata{$key} $ctfdata{$key}\n";
$good++;
$ctfmean += $ctfdata{$key};
}
}
print "found total of $good good micrographs\n";
$ctfmean /= $good;
printf ("Mean Thon ring extent = %4.2f \n",$ctfmean);
#
# write particles
my ($xc,$yc,$fc);
open (IN,$ptclfile) or die "no read!";
print "mkdir ./$pickdir\/$micdir\n";
`mkdir -p $pickdir/$micdir`;
while (<IN>) {
my @data=split /\s+/,$_ ;
if (m/xcoord/) { #get the indices
for (my $i=0; $i <=$#data; $i++) {
if ($data[$i] eq 'xcoord') {$xc=$i;}
elsif ($data[$i] eq 'ycoord') {$yc = $i;}
elsif ($data[$i] eq 'filename') {$fc = $i;}
}
}
else { #store the data
if ($#data < $fc) {$fc=$#data;}
my $file = $data[$fc];
$file = $micdir . '/' . $file . '.mrc'; #add path and extension
my $xcoord=$data[$xc];
my $ycoord = $data[$yc];
@data=($xcoord,$ycoord);
push (@{$info{$file}},\@data);
}
}
close (IN);
my @keys = sort keys %info;
foreach $file (@keys) {
my $filename = $file;
$filename =~ s/\.mrc/_manualpick.star/;
open (OUT,">$pickdir/$filename") or die "cannot write $pickdir/$filename\n";
print OUT <<EOF
data_
loop_
_rlnCoordinateX #1
_rlnCoordinateY #2
EOF
;
my @points = @{$info{$file}};
for my $i (0 .. $#points) {
print OUT join(" ",@{$points[$i]}) . "\n";
}
close (OUT);
# print "finished $filename\n";
}
# final setup of Relion directory for extraction
open (OUT,">$pickdir/coords_suffix_manualpick.star") or die "cannot write $pickdir/coords_suffix_manualpick.star\n";
print OUT "ctfdata_allgood.star\n";
close OUT;
`cp ctfdata_allgood.star $pickdir/micrographs_selected.star\n`;