我有一个包含文件名的哈希。这些文件名中的每一个都有不同类型的扩展名。即(.txt,.bam,.bam.bai,.fastq等)我试图找出一种迭代这些键并将列表排序为(3)的方法基于HTML / erb中的文件名的三个部分......就像这样。
第1节 所有FastQ文件(.fastq)
第2节 所有.bam和bam.bai文件(.bam,.bam.bai)
第3节 所有文本文件(.txt)
基本上我到目前为止就是这个......
<div class="content_box_title">Sequencing Results</div>
<div class="content_box">
<% seqfiles = @vmsr_project.seqfiles %>
<% seqfiles.keys.sort.each do |file|
fpath = file.to_s
fsize = File.size(file)
fdate = File.mtime(file)
`enter code here`fsize = fsize / 1000000
fsize = fsize.to_int
fsize = fsize.to_s
if fsize == "0" then fsize = "EMPTY or <1" end
%>
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => @vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
<div style="float: left">
<!-- <%= fpath %> -->
</div>
<% end %>
<% if seqfiles.empty? %>
<ul><li>None</li></ul>
<% end %>
</div>
<div class="content_box_title">Aligned</div>
<div class="content_box">
<p>.bam, .bam.bai files display here</p>
</div>
<div class="content_box_title">QC</div>
<div class="content_box">
<p>.txt files display here</p>
</div>
作为一个更新大家......我得到以下工作(请不要笑,我是新手)但问题现在当然是标题: 测序结果 每次在所有三个部分中解析时都会打印。绝对需要一个重构。
<% seqfiles.keys.sort.each do |file|
fpath = file.to_s
fsize = File.size(file)
fdate = File.mtime(file)
fsize = fsize / 1000000
fsize = fsize.to_int
fsize = fsize.to_s
if fsize == "0" then fsize = "EMPTY or <1" end
case fpath
when /\.fastq.gz$/ %>
<div class="content_box_title">Sequencing Results</div>
<div class="content_box">
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => @vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
</div>
<% when /\.bam(\.bai)?$/ %>
<div class="content_box_title">Aligned</div>
<div class="content_box">
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => @vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
</div>
<% else %>
<div class="content_box_title">QC</div>
<div class="content_box">
<div style="float: left">
<%= link_to File.basename(file), {:action => "send_analysis_file", :project => @vmsr_project[:proj_id], :file => file }, { :onmouseover => "Tip('<div class=\\'tooltip\\'>Click to download. <p style=\\'color: #e26666;\\'>" + fsize + " MB</p></div>', TITLE, 'Download File')", :onmouseout => "UnTip()" } %>
</div>
<div style="float: left">
(<%= fdate.strftime("%m/%d/%Y") %>):
</div>
<div>
<%= seqfiles[file].join(", ") %>
</div>
<div style="clear: both; border-top: 1px solid #ddd;"> </div>
</div>
<% end %>
<% end %>
<% if seqfiles.empty? %>
<ul><li>None</li></ul>
<% end %>
答案 0 :(得分:2)
只需将文件名分成3个数组,然后对每个数组进行迭代。
<% separated = seqfiles.keys.sort.inject({}){|h,key|
case key.to_s
when /\.fastq$/
h[:fastq] ||= []
when /\.bam(\.bai)?$/
h[:bam] ||= []
else
h[:other] ||= []
end.push(key)
h
} %>
<% separated[:fastq].each do |file| %>
...
<% end %>
<% separated[:bam].each do |file| %>
...
<% end %>
<% separated[:other].each do |file| %>
...
<% end %>