This is a test version of Biostars. For the public version, visit https://www.biostars.org.
problem with retrieving multiple records from Entrez using while read loop

Hello, I have a list of 60 NCBI biosamp entries, each in a new line. The file name is biosmp. I need to retrieve the bio sample attributes. I run the following command but I get only the first record.

cat biosmp | while read lines; do esearch -db biosample -query "${lines}" | efetch -format xml | xtract -pattern BioSampleSet -element Attribute; done;
entrez esearch

3 answers

Example solution below.

$ more id
SAMN22787239
SAMN22787238
SAMN22787237
SAMN22787236
SAMN22787235
SAMN22787234

$ for i in `cat id`; do efetch -db biosample -id ${i} -format xml | xtract -pattern BioSampleSet -element Attribute ; done
not collected   not collected   submitter lab   not collected   Spleen  PvSRA treated Splenic Fibroblasts   biological replicate 3
not collected   not collected   submitter lab   not collected   Spleen  PvSRA treated Splenic Fibroblasts   biological replicate 2
not collected   not collected   submitter lab   not collected   Spleen  PvSRA treated Splenic Fibroblasts   biological replicate 1
not collected   not collected   ScienCell Research Laboratories not collected   Spleen  untreated Splenic Fibroblasts Catalog No.: 5530 biological replicate 3
not collected   not collected   ScienCell Research Laboratories not collected   Spleen  untreated Splenic Fibroblasts Catalog No.: 5530 biological replicate 2
not collected   not collected   ScienCell Research Laboratories not collected   Spleen  untreated Splenic Fibroblasts Catalog No.: 5530 biological replicate 1

epost equivalent:

$ epost -db biosample -format acc -input id | efetch -format xml | xtract -pattern BioSampleSet -element Attribute

Cleaner view (only one sample shown):

$ for i in `cat id`; do efetch -db biosample -id ${i}  ; done
1: Human sample from Homo sapiens
Identifiers: BioSample: SAMN22787239; Sample name: PvSRA-3; SRA: SRS10786264
Organism: Homo sapiens
Attributes:
    /isolate="not collected"
    /age="not collected"
    /biomaterial provider="submitter lab"
    /sex="not collected"
    /tissue="Spleen"
    /cell line="PvSRA treated Splenic Fibroblasts"
    /replicate="biological replicate 3"
Accession: SAMN22787239 ID: 22787239

Thank you. The efetch loop worked well . epost was 10 time faster, but it concatenated all records to one line. The xargs solution retrieved only the first record.

Using xargs:

cat biosmp | xargs -i esearch -db biosample -query "{}" | efetch -format xml | xtract -pattern BioSampleSet -element Attribute

For your command to work, you need to add </dev/null to the esearch portion of it as described here. The following should fetch data for all 60 entries. Note, I modified the xtract command to use -pattern BioSample to address the issue of all output being on the same line.

cat biosmp | while read lines; do 
    esearch -db biosample -query "${lines}"  </dev/null \
    | efetch -format xml \
    | xtract -pattern BioSample -element Attribute; 
done

Log in to answer this question.