Class: Bio::BaseSpace::File

Inherits:
Model
  • Object
show all
Defined in:
lib/basespace/model/file.rb

Overview

Represents a BaseSpace file object.

Instance Attribute Summary

Attributes inherited from Model

#attributes, #swagger_types

Instance Method Summary collapse

Methods inherited from Model

#get_attr, #method_missing, #set_attr, #to_str

Constructor Details

#initializeFile

Create a new File instance.



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
# File 'lib/basespace/model/file.rb', line 24

def initialize
  @swagger_types = {
    'Name'          => 'str',
    'HrefCoverage'  => 'str',
    'HrefParts'     => 'str',
    'DateCreated'   => 'datetime',
    'UploadStatus'  => 'str',
    'Id'            => 'str',
    'Href'          => 'str',
    'HrefContent'   => 'str',
    'HrefVariants'  => 'str',
    'ContentType'   => 'str',
    'Path'          => 'str',
    'Size'          => 'int',
  }
  @attributes = {
    'Name'          => nil, # str
    # If set, provides the relative Uri to fetch the mean coverage statistics for data stored in the file
    'HrefCoverage'  => nil, # str
    # If set, provides the relative Uri to fetch a list of completed file parts for multi-part file uploads in progress
    'HrefParts'     => nil, # str
    'DateCreated'   => nil, # datetime
    'UploadStatus'  => nil, # str
    'Id'            => nil, # str
    'Href'          => nil, # str
    'HrefContent'   => nil, # str
    # If set, provides the relative Uri to fetch the variants stored in the file
    'HrefVariants'  => nil, # str
    'ContentType'   => nil, # str
    'Path'          => nil, # str
    'Size'          => nil, # int
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bio::BaseSpace::Model

Instance Method Details

#delete_file(api) ⇒ Object

Delete the file; not implemented yet.

api

A BaseSpaceAPI instance with delete permissions on the file object.



116
117
118
# File 'lib/basespace/model/file.rb', line 116

def delete_file(api)
  raise 'Not yet implemented'
end

#download_file(api, local_dir, range = []) ⇒ Object

Download the file object to the specified local directory or a byte range of the file, by specifying the start and stop byte in the range.

api

A BaseSpaceAPI instance with read access to the file object.

local_dir

The local directory to place the file in.

:range+

Two-item array with start and stop byte of the file chunk that needs retrieved.



98
99
100
101
102
103
104
# File 'lib/basespace/model/file.rb', line 98

def download_file(api, local_dir, range = [])
  if range.empty?
    return api.file_download(get_attr('Id'),local_dir, get_attr('Name'))
  else
    return api.file_download(get_attr('Id'),local_dir, get_attr('Name'), range)
  end
end

#filter_variant(api, chrom, start_pos, end_pos, qp = nil) ⇒ Object

Returns a list of Variant objects available in the specified region.

api

BaseSpaceAPI instance.

chrom

Chromosome as a string - for example ‘chr2’.

start_pos

The start position of region of interest as a string.

end_pos

The end position of region of interest as a string.

qp: TODO queryParameters object for custom filtering.



140
141
142
143
144
145
# File 'lib/basespace/model/file.rb', line 140

def filter_variant(api, chrom, start_pos, end_pos, qp = nil)
  is_init
  is_valid_file_option('vcf')
  set_attr('Id', get_attr('HrefVariants').split('/').last)
  return api.filter_variant_set(get_attr('Id'), chrom, start_pos, end_pos, 'txt')
end

#get_coverage_meta(api, chrom) ⇒ Object

Return an object of CoverageMetadata for the selected region.

api

BaseSpaceAPI instance.

chrom

Chromosome as a string - for example ‘chr2’.



151
152
153
154
155
156
# File 'lib/basespace/model/file.rb', line 151

def get_coverage_meta(api, chrom)
  is_init
  is_valid_file_option('bam')
  set_attr('Id', get_attr('HrefCoverage').split('/').last)
  return api.get_coverage_meta_info(get_attr('Id'), chrom)
end

#get_file_url(api) ⇒ Object

Return the S3 URL of the file.

api

A BaseSpaceAPI instance with read access to the file object.



109
110
111
# File 'lib/basespace/model/file.rb', line 109

def get_file_url(api)
  return api.file_url(get_attr('Id'))
end

#get_interval_coverage(api, chrom, start_pos, end_pos) ⇒ Object

Return a coverage object for the specified region and chromosome.

api

BaseSpaceAPI instance.

chrom

Chromosome as a string - for example ‘chr2’.

start_pos

The start position of region of interest as a string.

end_pos

The end position of region of interest as a string.



126
127
128
129
130
131
# File 'lib/basespace/model/file.rb', line 126

def get_interval_coverage(api, chrom, start_pos, end_pos)
  is_init
  is_valid_file_option('bam')
  set_attr('Id', get_attr('HrefCoverage').split('/').last)
  return api.get_interval_coverage(get_attr('Id'), chrom, start_pos, end_pos)
end

#get_variant_meta(api) ⇒ Object

Return the the meta info for a VCF file as a VariantInfo object.

api

BaseSpaceAPI instance.



161
162
163
164
165
166
# File 'lib/basespace/model/file.rb', line 161

def get_variant_meta(api)
  is_init
  is_valid_file_option('vcf')
  set_attr('Id', get_attr('HrefVariants').split('/').last)
  return api.(get_attr('Id'), 'txt')
end

#is_initObject

Tests if the File instance has been initialized.

Throws ModelNotInitializedError, if the instance has not been populated yet.



76
77
78
# File 'lib/basespace/model/file.rb', line 76

def is_init
  raise ModelNotInitializedError.new('The File model has not been initialized yet') unless get_attr('Id')
end

#is_valid_file_option(filetype) ⇒ Object

Tests if the File instance matches the filetype parameter.

filetype

The filetype for coverage or variant requests (bam|vcf).



83
84
85
86
87
88
89
90
# File 'lib/basespace/model/file.rb', line 83

def is_valid_file_option(filetype)
  if filetype == 'bam'
    raise WrongFiletypeError.new(get_attr('Name')) unless get_attr('HrefCoverage')
  end
  if filetype == 'vcf'
    raise WrongFiletypeError.new(get_attr('Name')) unless get_attr('HrefVariants')
  end
end

#to_sObject

Returns the name of the file, its ID, size and upload status.



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/basespace/model/file.rb', line 59

def to_s
  str = get_attr('Name')
  begin
    str += " - id: '#{get_attr('Id')}', size: '#{get_attr('Size')}'"
    str += ", status: '#{get_attr('UploadStatus')}'" if get_attr('UploadStatus')
  rescue => err
    # [TODO] What to do with this 'err'?
    $stderr.puts "    # ----- File#to_s ----- "
    $stderr.puts "    # Error: #{err}"
    $stderr.puts "    # "
  end
  return str
end